I am trying to animate slider up and down on toggle on navbar using javascript but the css transition property is not working on the ul element
Following is my updated codepen https://codepen.io/divya95/pen/vYNdKrz
Code:
<div class="container row">
<nav class="nav">
<a class="logo"><img src="../flexbox/vanderbilt-commodores-1.svg"></a>
<ul class="nav__list nav__list--primary">
<li class="nav__item">
Home
</li>
<li class="nav__item">
About
</li>
<li class="nav__item">
Contact Us
</li>
<li class="nav__item">
Sign In
</li>
<li class="nav__item">
Sign Up
</li>
</ul>
<button class="nav-toggle">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</button>
<!-- <ul class="nav__list nav__list--secondary">
</ul> -->
</nav>
</div>
Css:
.nav-visible {
width: 100%;
text-align: center;
position: fixed;
display: flex;
flex-direction: column;
justify-content: center;
top: 40px;
display: block;
background: #2E323F;
list-style-type: none;
padding: 1em;
/* this is not working */
transition: height 5s ease;
}
Here I used jQuery because it's easier for me but you can make it without it.
notice how i also changed .nav-visible and .nav__list
$(document).ready(function(){
$('.nav-toggle').click(function () {
$(this).toggleClass('is-active');
toggleMenu($(this).hasClass('is-active'));
});
});
function toggleMenu(openMenu) {
let menu = $('.nav__list');
if(openMenu) {
menu.css('height', 'auto');
let nextHeight = menu.height();
menu.css('height', 0);
menu.animate({
'height': nextHeight,
'padding': '1em'
}, 200, function() {
menu.css({
'height': 'auto',
});
});
} else {
menu.css('overflow', 'hidden');
menu.animate({
'height': 0,
'padding': 0
}, 200);
}
}
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Montserrat', sans-serif;
font-size: 1.3125rem;
line-height: 1.6;
color: #222C2A;
}
.container {
max-width: 1200px;
}
.row {
display: flex;
justify-content: space-between;
}
.nav {
display: flex;
justify-content: space-between;
width: 100%;
background: #2E323F;
}
.nav__list {
height: 0;
overflow: hidden;
}
.nav__link {
text-decoration: none;
text-transform: uppercase;
font-size: 1rem;
font-weight: 600;
color: white;
}
.nav__list {
width: 100%;
text-align: center;
position: fixed;
display: flex;
flex-direction: column;
justify-content: center;
top: 40px;
display: block;
background: #2E323F;
list-style-type: none;
padding: 0em;
}
.nav-visible {
padding: 1em;
}
.nav-visible .nav__item {
padding-bottom: 1em;
}
.nav__link {
padding: 1em;
}
.logo {
margin-left: 2.5em;
}
.logo img {
width: 45px;
height: 45px;
margin-left: 1em;
margin-top: 0.5em;
margin-right: 0.5em;
}
.nav-toggle {
width: 2.5rem;
height: 2.5rem;
border-radius: 50%;
margin-top: 0.85em;
margin-right: 0.95em;
}
.bar {
cursor: pointer;
display: block;
height: 2px;
background: #222C2A;
transition: all 0.3s ease-in-out;
}
.bar:nth-child(1) {
-webkit-transform: translateY(-6px);
transform: translateY(-6px);
}
.bar:nth-child(3) {
-webkit-transform: translateY(6px);
transform: translateY(6px);
}
.hamburger-active .bar:nth-child(1) {
-webkit-transform: translateY(2px) rotate(45deg);
transform: translateY(2px) rotate(45deg);
}
.hamburger-active .bar:nth-child(2) {
opacity: 0;
}
.hamburger-active .bar:nth-child(3) {
-webkit-transform: translateY(-2px) rotate(-45deg);
transform: translateY(-2px) rotate(-45deg);
}
#media(min-width: 700px) {
header {
background: #2E323F;
}
.row {
display: flex;
align-items: center;
justify-content: space-between;
}
.nav {
display: flex;
justify-content: space-between;
}
.nav__list {
list-style-type: none;
display: flex;
/* visibility: visible; */
}
.nav__link {
text-decoration: none;
text-transform: uppercase;
font-size: 0.85rem;
margin-right: 0.8em;
}
.nav-toggle {
display: none;
}
.nav__item {
margin-right: 1.5em;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<div class="container row">
<nav class="nav">
<a class="logo"><img src="../flexbox/vanderbilt-commodores-1.svg"></a>
<ul class="nav__list nav__list--primary">
<li class="nav__item">
Home
</li>
<li class="nav__item">
About
</li>
<li class="nav__item">
Contact Us
</li>
<li class="nav__item">
Sign In
</li>
<li class="nav__item">
Sign Up
</li>
</ul>
<button class="nav-toggle">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</button>
</nav>
</div>
</header>
Related
<body>
<header class="main-header">
<div class="container">
<img src="images/logo.svg" alt="Manage">
<button class="toggle-button">
<span class="toggle-button__bar"></span>
<span class="toggle-button__bar"></span>
<span class="toggle-button__bar"></span>
</button>
<nav class="main-nav">
<ul role="list" class="main-nav__list">
<li class="main-nav__list--item">Pricing</li>
<li class="main-nav__list--item">Product</li>
<li class="main-nav__list--item">About Us</li>
<li class="main-nav__list--item">Careers</li>
<li class="main-nav__list--item">Community</li>
</ul>
</nav>
<button class="button button-header">Get Started</button>
</div>
</header>
<script type="module" src="/script/script.js"></script>
/* CSS/SASS code
I do not know why my logo/image is being placed in the middle of the header, instead of
the top left corner of the header. I do not know how to fix this.
If anybody has some nice suggestions, feel free to tell */
#mixin display-flex() {
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
align-items: center;
}
#mixin media-min-width($width) {
#media (min-width: $width) {
#content;
}
}
.main-header {
position: relative;
padding: $pad-header 0;
.container {
#include display-flex();
justify-content: space-around;
a {
display: inline-block;
text-decoration: none;
font-size: $fs-nav;
font-weight: $fw-semi-bold;
}
.main-nav {
display: none;
#include media-min-width(50rem) {
display: block;
}
}
.button {
color: hsl(0deg, 0%, 98%);
font-weight: 700;
background: hsl(12deg, 88%, 59%);
padding: 0.7rem 1rem;
border: 0;
border-radius: 100vmax;
&:hover {
cursor: pointer;
}
:focus {
outline: none;
}
}
.button-header {
display: none;
#include media-min-width(50rem) {
display: block;
}
}
.toggle-button {
position: absolute;
top: 50%;
right: 2%;
transform: translateY(-50%);
background: transparent;
border: 0;
cursor: pointer;
#include media-min-width(50rem) {
display: none;
}
.toggle-button__bar {
display: block;
background: $clr-secondary;
width: 2rem;
height: 0.3rem;
margin: 0.3rem 0;
&:last-of-type {
margin: 0;
}
}
}
}
}
.main-nav__list {
#include display-flex();
justify-content: center;
gap: clamp(1.7rem, 1.9rem, 2.5rem);
.main-nav__list--item a {
cursor: pointer;
}
}
I have a working code for flex in css. Although the buttons are realigning but the blue bars is not moving. Due to this changing the screen size make it seem that all the buttons have disappeared.
*
{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body
{
background-image: url(background.jpg);
background-size: cover;
background-position : center;
font-family: sans-serif;
}
.menu-bar
{
background: #273044;
text-align: center;
height:30px;
}
.menu-bar ul
{
display: inline-flex;
list-style: none;
color: #fff;
}
.menu-bar ul li
{
width: 120px;
place-items:center;
display: grid;
padding: 5px;
}
.menu-bar ul li a
{
text-decoration: none;
color: white;
}
.active, .menu-bar ul li:hover
{
background-color: #0b56ab;
height:auto;
height:30px;
}
.toggle-button{
position: absolute;
top: 0.75rem;
right: 1rem;
display: flex;
flex-direction: column;
justify-content: space-between;
width: 31px;
height: 21px;
}
.toggle-button .bar{
height: 3px;
width: 100%;
background-color: white;
border-radius: 10px;
}
#media(max-width: 700px){
.toggle-button{
display:flex;
}
.menu-bar-links{
/* display:none; */
width:100%;
}
.menu-bar{
flex-direction: column;
align-items: flexstart;
}
.menu-bar-links ul{
width:100%;
flex-direction: column;
}
.menu-bar-links li{
text-align: center;
}
}
/* body{margin:0; padding:0; font-size:13px; font-family:Georgia, "Times New Roman", Times, serif; color:#919191; background-color:#232323;} */
This is my HTML Code:
<html>
<head>
<title> Title of page </title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet1" href="https://cdn.jsdelivr.net/npm/#fortawesome/fontawesome-free#6.1.1/css/fontawesome.min.css">
</head>
<!----header---->
<body>
<div class="menu-bar">
<a href = "#" class="toggle-button">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
<div class="menu-bar-links">
<ul>
<li class="active">Home</li>
<li>About us</li>
<li>Services</li>
<li>Investors</li>
<li>Pricing</li>
<li>Training </li>
<li>Contact</li>
</ul>
</div>
</div>
<!-- <div class= "grid">
</div> -->
</body>
</html>
This is how it looks when in full screen:
When i rearrange the screen it looks like this:
As you can see all the buttons seems to have disappeared except the one which is hovered over
It is also not centering even with
.menu-bar-links li{
text-align: center;
The problem you have is that you have a fixed height on your .menu-bar, namely height: 30px. You're better off removing that, adding some padding for presentation and making the .menu-bar element a flex container.
Link to CodePen:
https://codepen.io/thechewy/pen/QWQQMjg
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.menu-bar {
background: #273044;
text-align: center;
/* height: 30px; */
padding: 1rem;
display: flex;
justify-content: center;
align-items: center;
}
.menu-bar ul {
display: inline-flex;
list-style: none;
color: #fff;
}
.menu-bar ul li {
width: 120px;
place-items: center;
display: grid;
padding: 5px;
}
.menu-bar ul li a {
text-decoration: none;
color: white;
}
.active,
.menu-bar ul li:hover {
background-color: #0b56ab;
height: auto;
height: 30px;
}
.toggle-button {
position: absolute;
right: 1rem;
display: flex;
flex-direction: column;
justify-content: space-between;
width: 31px;
height: 21px;
}
.toggle-button .bar {
height: 3px;
width: 100%;
background-color: white;
border-radius: 10px;
}
#media (max-width: 700px) {
.toggle-button {
display: flex;
}
.menu-bar-links {
/* display:none; */
width: 100%;
}
.menu-bar {
flex-direction: column;
align-items: flexstart;
}
.menu-bar-links ul {
width: 100%;
flex-direction: column;
}
.menu-bar-links li {
text-align: center;
}
}
<html>
<head>
<title> Title of page </title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet1" href="https://cdn.jsdelivr.net/npm/#fortawesome/fontawesome-free#6.1.1/css/fontawesome.min.css">
</head>
<body>
<div class="menu-bar">
<a href="#" class="toggle-button">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
<div class="menu-bar-links">
<ul>
<li class="active">Home</li>
<li>About us</li>
<li>Services</li>
<li>Investors</li>
<li>Pricing</li>
<li>Training </li>
<li>Contact</li>
</ul>
</div>
</div>
</body>
</html>
I'm trying to create a mobile layout with a logo on the left nav in the middle and a chevron to display and hide the items of a navbar(I'm not interested in doing this functionality right now).
But right now I'm trying to achieve something like this:
+------+---------+---------+
| logo | lorem | chevron |
+------+---------+---------+
| ipsum |
+---------+
| dolorem |
+---------+
I've been trying to align the first item of the ul with the icons.
Any ideas on how can I achieve this while still maintaining the html syntax?
* {
box-sizing: border-box;
}
body {
font-family: "IBM Plex Mono", monospace;
font-size: 18px;
}
.main-nav {
font-size: 24px;
display: flex;
justify-content: space-around;
align-items: center;
border: 1px solid red;
height: 50px;
}
.main-nav__list {
display: flex;
flex: 1;
padding: 0;
list-style-type: none;
border: 1px solid greenyellow;
flex-direction: column;
align-self: flex-start;
position: relative;
}
.main-nav__icon {
height: 24px;
width: auto;
margin: auto;
}
.main-nav__links {
text-decoration: none;
color: white;
}
.main-nav__list-item {
width: 100%;
text-align: center;
}
/*.main-nav__list-item:not(:first-child){
display: none;
}*/
.main-nav__list-item--blue {
background-color: #1c77c3;
}
.main-nav__list-item--green {
background-color: #00aa00;
}
.main-nav__list-item--orange {
background: #dd2e02;
}
.main-nav__logo {
flex: initial;
width: 50px;
background-color: black;
display: flex;
height: 100%;
color: white;
}
<script src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"></script>
<header class="">
<nav class="main-nav">
<a class="main-nav__logo" href="#"><i class="far fa-user main-nav__icon" alt="logo"></i></a>
<ul class="main-nav__list">
<li class="main-nav__list-item main-nav__list-item--blue"><a class="main-nav__links" href="#">Lorem</a></li>
<li class="main-nav__list-item main-nav__list-item--green"><a class="main-nav__links" href="#">Ipsum</a></li>
<li class="main-nav__list-item main-nav__list-item--orange"><a class="main-nav__links" href="#">dolorem</a></li>
</ul>
<a class="main-nav__logo" href="#"><i class="fas fa-chevron-down main-nav__icon" alt="chevron down"></i></a>
</nav>
</header>
To your .main-nav__list, add:
/* Add margin: 0; and height: 300%; */
margin: 0;
height: 300%;
To your .main-nav__list-item. add:
/* Add flex-grow: 1; and center items */
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
* {
box-sizing: border-box;
}
body {
font-family: "IBM Plex Mono", monospace;
font-size: 18px;
}
.main-nav {
font-size: 24px;
display: flex;
justify-content: space-around;
align-items: center;
border: 1px solid red;
height: 50px;
}
.main-nav__list {
display: flex;
flex: 1;
padding: 0;
list-style-type: none;
border: 1px solid greenyellow;
flex-direction: column;
align-self: flex-start;
position: relative;
/* Add margin: 0; and height: 300%; */
margin: 0;
height: 300%;
}
.main-nav__icon {
height: 24px;
width: auto;
margin: auto;
}
.main-nav__links {
text-decoration: none;
color: white;
}
.main-nav__list-item {
width: 100%;
text-align: center;
/* Add flex-grow: 1; and center items */
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
}
/*.main-nav__list-item:not(:first-child){
display: none;
}*/
.main-nav__list-item--blue {
background-color: #1c77c3;
}
.main-nav__list-item--green {
background-color: #00aa00;
}
.main-nav__list-item--orange {
background: #dd2e02;
}
.main-nav__logo {
flex: initial;
width: 50px;
background-color: black;
display: flex;
height: 100%;
color: white;
}
<script src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"></script>
<header class="">
<nav class="main-nav">
<a class="main-nav__logo" href="#"><i class="far fa-user main-nav__icon" alt="logo"></i></a>
<ul class="main-nav__list">
<li class="main-nav__list-item main-nav__list-item--blue"><a class="main-nav__links" href="#">Lorem</a></li>
<li class="main-nav__list-item main-nav__list-item--green"><a class="main-nav__links" href="#">Ipsum</a></li>
<li class="main-nav__list-item main-nav__list-item--orange"><a class="main-nav__links" href="#">dolorem</a></li>
</ul>
<a class="main-nav__logo" href="#"><i class="fas fa-chevron-down main-nav__icon" alt="chevron down"></i></a>
</nav>
</header>
i just found an issue on my web project and i couldnt find a way around it.
i saw something about sprites and a blog that mentions this issue.
everything looks fine on 100% but when i zoom in with my browser the icons get bigger and bigger and break the layout.
this is what my css look like
.Sidebar {
position: fixed;
background-color: #262626;
width: 5%;
height: 100%;
z-index: 1;
}
.Sidebar-Icon {
align-items: center;
font-size: 30px;
}
.Sidebar-Icon .SDIcon {
text-decoration-line: none;
}
.Sidebar-Icon li {
list-style-type: none;
}
ul.Sidebar-Icon {
cursor: pointer;
display: flex;
padding: 0;
flex-direction: column;
align-items: center;
flex: 1;
margin: 0;
height: 100%;
position: relative;
}
.Sidebar-Icon li:first-child{
padding: 20px 0 0 0;
}
.Sidebar-Icon li:first-child,
.Sidebar-Icon li:last-child{
margin-bottom:auto;
}
.sb-i{
color: #999AAA !important; // shadow AAABBB
}
Html (Angular) Code
<div class="Sidebar" [#collapse]="collapse">
<div class="Sidebar-Inner">
<div class="profileimg"></div>
</div>
<ul class="Sidebar-Icon ">
<li><a class="sb-i" (click)="toggleCollapse()">
<i class="fas fa-ellipsis-v" ></i></a></li>
<li><a class="sb-i"href="javascript:void(0)"><i class="fas fa-home"></i></a>
</li>
<li><a class="sb-i"href="javascript:void(0)"><i class="fas fa-child"></i>
</a></li>
<li><a class="sb-i"href="javascript:void(0)"><i class="fab fa-accessible-
icon"></i></a></li>
<li><a class="sb-i"href="javascript:void(0)"><i class="fas fa-cog"></i></a>
</li>
</ul>
</div>
example of the issue (Zoomlevel on 500%):
HTML:
<div class="Sidebar" [#collapse]="collapse">
<div class="Sidebar-Inner">
<div class="profileimg"></div>
</div>
<ul class="Sidebar-Icon ">
<li><a class="sb-i" (click)="toggleCollapse()">
<i class="fas fa-ellipsis-v" ></i></a></li>
<li><a class="sb-i" href="javascript:void(0)"><i class="fas fa-home"></i></a>
</li>
<li><a class="sb-i" href="javascript:void(0)"><i class="fas fa-child"></i>
</a></li>
<li><a class="sb-i" href="javascript:void(0)"><i class="fab fa-accessible-
icon"></i></a></li>
<li><a class="sb-i" href="javascript:void(0)"><i class="fas fa-cog"></i></a>
</li>
</ul>
</div>
CSS:
.Sidebar {
position: fixed;
background-color: #262626;
width: 5%;
height: 100%;
z-index: 1;
}
.Sidebar-Icon {
align-items: center;
font-size: 30px;
}
.Sidebar-Icon .SDIcon {
text-decoration-line: none;
}
.Sidebar-Icon li {
list-style-type: none;
}
ul.Sidebar-Icon {
cursor: pointer;
display: flex;
padding: 0;
flex-direction: column;
align-items: center;
flex: 1;
margin: 0;
height: 100%;
position: relative;
}
.Sidebar-Icon li:first-child{
padding: 20px 0 0 0;
}
.Sidebar-Icon li:first-child,
.Sidebar-Icon li:last-child{
margin-bottom:auto;
}
.sb-i{
color: #999AAA !important; // shadow AAABBB
}
.sb-i i{
width: 100%;
height: auto;
}
.sb-i svg {
width: 100% !important;
height: auto !important;
}
I have the following navigation which I have made responsive by using display:flex:
#header-nav { display: flex; flex-grow: 1; background: #707276; background: rgba(112,114,118,0.75); }
#header-nav .nav-item { display: flex; flex-grow: 1; flex-basis: 0; }
#header-nav .nav-link { display: block; color: #ffffff; display: flex; flex-grow: 1; justify-content: center; align-items: center; white-space: nowrap; }
#header-nav > .nav-item > .nav-link { box-sizing: border-box; text-transform: uppercase; text-decoration: none; padding: 15px 0; position: relative; transition: color 0.5s ease-in-out; font-size: 115.385%; font-family: VegurRegular; }
#header-nav > .nav-item > .nav-link:hover { color: #000000; }
#header-nav > .nav-item > .nav-link:after { content: ''; display: block; position: absolute; bottom: 0; left: 0; right: 0; height: 5px; opacity: 0; transition: opacity 0.5s ease-in-out; background-color: #36383a; }
#header-nav > .nav-item > .nav-link:hover:after { opacity: 0.8; }
#header-nav .home { flex-grow: 0; flex-basis: auto; padding: 0 50px; }
#header-nav .nav-item.home > .nav-link { display: block; }
#header-nav .nav-item.home > .nav-link:after { display: none; }
<ul id="header-nav">
<li class="nav-item home">
<a href="/" class="nav-link">
<span id="logo-text" class="sprite">Logo</span>
</a>
</li>
<li class="nav-item">
Shop
</li>
<li class="nav-item">
Tiling Ideas
</li>
<li class="nav-item">
Advice
</li>
<li class="nav-item">
Find a Retailer
</li>
</ul>
As you can see, the logo stays the same width, whilst the rest is responsive. But if you use the fullpage link and then make the width of the screen smaller, you will see the links start to overlap each other. Is there any way to stop this from happening using flex?
I know I can find a route with using display table that would achieve the same thing but I would prefer to use display flex.
Try removing the white-space: nowrap; as it is is inherently not responsive.
#header-nav {
display: flex;
flex-grow: 1;
background: #707276;
background: rgba(112, 114, 118, 0.75);
}
#header-nav .nav-item {
display: flex;
flex: 1;
}
#header-nav .nav-link {
color: #ffffff;
display: flex;
flex-grow: 1;
text-align: center;
justify-content: center;
align-items: center;
align-content: center;
}
#header-nav > .nav-item > .nav-link {
box-sizing: border-box;
text-transform: uppercase;
text-decoration: none;
padding: 15px 0;
position: relative;
transition: color 0.5s ease-in-out;
font-size: 115.385%;
font-family: VegurRegular;
}
#header-nav > .nav-item > .nav-link:hover {
color: #000000;
}
#header-nav > .nav-item > .nav-link:after {
content: '';
display: block;
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 5px;
opacity: 0;
transition: opacity 0.5s ease-in-out;
background-color: #36383a;
}
#header-nav > .nav-item > .nav-link:hover:after {
opacity: 0.8;
}
#header-nav .home {
flex-grow: 0;
flex-basis: auto;
padding: 0 50px;
}
#header-nav .nav-item.home > .nav-link {
display: block;
}
#header-nav .nav-item.home > .nav-link:after {
display: none;
}
<ul id="header-nav">
<li class="nav-item home">
<a href="/" class="nav-link">
<span id="logo-text" class="sprite">Logo</span>
</a>
</li>
<li class="nav-item">
Shop
</li>
<li class="nav-item">
Tiling Ideas
</li>
<li class="nav-item">
Advice
</li>
<li class="nav-item">
Find a Retailer
</li>
</ul>
Alternatively, consider media queries to reduce the font size at lower screen sizes.