I am trying to implement a hamburger menu, I have a basic header container that houses two div elements the branding of my site and the navigation links / hamburger icon, I am trying to implement the navigation links as an unordered list, however, I can't seem to put it below the header since it is inside a flex child.
Here's my code:
html,
body {
height: 100%;
}
body {
font-family: sans-serif;
margin: 0;
}
/*
* HEADER: The Web Page's Header
*/
.site-header { /* The Header's Main Container */
height: 54px;
background-color: aquamarine;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 15px 0 15px;
font-size: 12px;
}
.navigation ul { /* The Unordered List Container */
list-style: none;
}
<header class="site-header">
<div class="kokar-branding">
<img src="https://placehold.it/143x34" alt="Kokar's Logo">
</div>
<nav class="navigation">
<img src="https://placehold.it/24x24" alt="Navigation Hamburger Icon">
<ul>
<li>HOME</li>
<li>EVENTS</li>
<li>VOLUNTEERS</li>
<li>SDGs</li>
<li>JOIN US</li>
<li>ABOUT US</li>
<li class="nav-btn-donate">DONATE</li>
<li>LANGUAGE</li>
</ul>
</nav>
</header>
I am trying to achieve the following effect:
So I would try something like this:
HTML:
<header class="site-header">
<div class="kokar-branding">
<img src="https://placehold.it/143x34" alt="Kokar's Logo">
</div>
<nav class="">
<input type='Checkbox' id="nav" />
<label class="navbar-toggle" for="nav">
<img src="https://placehold.it/24x24" alt="Navigation Hamburger Icon"></label>
<ul class="navigation">
<li>HOME</li>
<li>EVENTS</li>
<li>VOLUNTEERS</li>
<li>SDGs</li>
<li>JOIN US</li>
<li>ABOUT US</li>
<li class="nav-btn-donate">DONATE</li>
<li>LANGUAGE</li>
</ul>
</nav>
</header>
CSS:
html,
body {
height: 100%;
}
body {
font-family: sans-serif;
margin: 0;
}
/*
* HEADER: The Web Page's Header
*/
.site-header {
/* The Header's Main Container */
height: 54px;
background-color: aquamarine;
display: block;
align-items: center;
justify-content: space-between;
padding: 0 15px 0 15px;
font-size: 12px;
}
.navigation {
/* The Unordered List Container */
list-style: none;
display: none;
}
[type="checkbox"] {
display: none;
}
label {
cursor: pointer;
right:0;
}
input[type="checkbox"]:checked ~ * .site-header {
background-color: white;
}
input[type="checkbox"]:checked ~ .navigation {
display: block;
}
Now the Hamburger is below the icon, because of display:block but still the functionality is there. I'm sure you will get the rest.
Related
Made a Website with a header menu which changes to a hamburger menu when I go to mobile version.
The hamburger menu works well, but the logo is not in the same line with the nav-links when I am in the header menu (desktop view) and it does not look appealing.
I could solve with putting the "logo-container" class outside the "navigation" but then the hamburger menu wouldn't work well (because putting the logo-container to the navigation div solved another problem).
.logo-container,.nav-links,.calendar {
display: flex;
}
.logo-container {
flex: 1;
position: relative;
left: 5%;
}
.logo {
font-weight: 400;
margin: 5px;
}
.logo-container img{
max-width: 120px;
max-height: 120px;
}
/* Logo container JS*/
.logo-container { display: 'none' }
.logo-container.open { display: 'block' }
nav {
flex: 2;
}
.nav-links {
justify-content: space-around;
list-style: none;
}
.nav-link {
color: var(--clr-dark);
font-size:20px;
text-decoration: none;
font-weight: 600;
}
<header class="header" id="myHeader">
<nav role="navigation">
<div class="logo-container" id="myLogo">
<img src="./img/logo.png" alt="logo"/>
</div>
<div class="hamburger" id="hamburgerID">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>
<ul class="nav-links">
<li><a class="nav-link" href="#details">DETAILS</a></li>
<li><a class="nav-link" href="#description">DESCRIPTION</a></li>
<li><a class="nav-link" href="#aboutus">ABOUT US</a></li>
</ul>
</nav>
</header>
You can make use of the nested flexbox. The edited code is explained in the comments.
.logo-container,
.nav-links,
.calendar {
display: flex;
}
.logo-container {
position: relative;
left: 5%;
}
.logo {
font-weight: 400;
margin: 5px;
}
.logo-container img {
max-width: 120px;
max-height: 120px;
}
/* Logo container JS*/
.logo-container {
display: 'none'
}
.logo-container.open {
display: 'block'
}
nav {
flex: 2;
display: flex; /* Make nav a flexbox container */
}
.nav-links {
justify-content: space-around;
list-style: none;
flex: 1; /* Let it occupy rest of the container */
align-items: center; /* Align to the vertical center because logo is bigger. */
}
.nav-link {
color: var(--clr-dark);
font-size: 20px;
text-decoration: none;
font-weight: 600;
}
<header class="header" id="myHeader">
<nav role="navigation">
<div class="logo-container" id="myLogo">
<img src="http://placehold.it/120x120" alt="logo" />
</div>
<div class="hamburger" id="hamburgerID">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>
<ul class="nav-links">
<li><a class="nav-link" href="#details">DETAILS</a></li>
<li><a class="nav-link" href="#description">DESCRIPTION</a></li>
<li><a class="nav-link" href="#aboutus">ABOUT US</a></li>
</ul>
</nav>
</header>
As you can see, the background color of <aside> has not taken the full height of the page (empty white space above the nav bar), How do I fix this, so it takes the full height of the screen?
body {
margin: auto;
}
.container {
background-color: black;
color: white;
width: 7rem;
height: 100vh;
display: flex;
flex-direction: column;
text-align: center;
}
ul {
list-style-type: none;
padding: 1rem 0;
}
li {
padding-bottom: 2rem;
}
.header {
display: flex;
justify-content: center;
}
<header>
<div class="header">
<h1>Welcome</h1>
</div>
</header>
<aside>
<section>
<div class="container">
<nav>
<ul class="center">
<a>
<li>Home</li>
</a>
<a>
<li>Projects</li>
</a>
<a>
<li>Contact</li>
</a>
</ul>
</nav>
</div>
</section>
</aside>
You can simply set the position of the container to fixed and thereafter make its height to 100% and set top to 0
body {
margin: 0;
min-height: 100vh;
}
.container {
position: fixed;
top: 0;
height: 100vh;
background-color: black;
color: white;
width: 7rem;
display: flex;
flex-direction: column;
text-align: center;
}
ul {
list-style-type: none;
padding: 1rem 0;
}
li {
padding-bottom: 2rem;
}
.header {
display: flex;
justify-content: center;
}
<header>
<div class="header">
<h1>Welcome</h1>
</div>
</header>
<aside>
<section>
<div class="container">
<nav>
<ul class="center">
<a>
<li>Home</li>
</a>
<a>
<li>Projects</li>
</a>
<a>
<li>Contact</li>
</a>
</ul>
</nav>
</div>
</section>
</aside>
Try setting the background-color to the body tag, or wrap the whole thing in a div, and set the background color on the div.
I am trying to center a logo so that it is constantly in the center of my navigation bar, regardless of how many navigation links there are.
This would be easy enough for me if the items at the sides were both even, for example 2 links on the left and 2 on the right, but I'm trying to get 3 on the left and 2 on the right with the centered logo.
Is it possible to center the image using flexbox properties or would I have to go about it a different way, like involving absolute/relative positioning?
header.main-header {
background: url("../images/slides/slide-1.jpg") no-repeat 75%/cover;
height: 100vh;
}
nav.nav-bar {
background-color: tomato;
}
ul.container {
display: flex;
align-items: center;
justify-content: space-around;
padding: 0;
}
ul.container li {
list-style-type: none;
}
li.logo img {
max-width: 125px;
}
<header class="main-header">
<nav class="nav-bar">
<ul class="container">
<li>My Story</li>
<li>Wines</li>
<li>How to Order</li>
<li class="logo"><img src="http://placeimg.com/640/480/any" alt="Wines"></li>
<li>Personal Sommelier</li>
<li>Contact</li>
</ul>
</nav>
<p style="text-align: center;">|<br>(Center)</p>
</header>
Thank you in advance.
Have a great day. :)
An idea is to use the logo as background and centred then you rely on some margin to adjust the menu elements:
header.main-header {
height: 100vh;
}
nav.nav-bar {
background-color: tomato;
}
ul.container {
display: flex;
align-items: center;
padding: 0;
background:url(http://placeimg.com/640/480/any) center/contain no-repeat;
min-height:100px;
}
ul.container li {
list-style-type: none;
margin:0 2%;
}
ul.container li:nth-child(3) {
margin-right:auto;
}
<header class="main-header">
<nav class="nav-bar">
<ul class="container">
<li>My Story</li>
<li>Wines</li>
<li>How to Order</li>
<li>Personal Sommelier</li>
<li>Contact</li>
</ul>
</nav>
<p style="text-align: center;">|<br>(Center)</p>
</header>
But in case you can adjust your HTML you may simply split the menu into 2 parts:
header.main-header {
height: 100vh;
}
nav.nav-bar {
background-color: tomato;
display:flex;
align-items:center;
}
ul.container {
display: flex;
align-items: center;
justify-content:space-around;
flex:1;
padding: 0;
}
img {
max-width:100px;
}
ul.container li {
list-style-type: none;
margin:0 2%;
}
<header class="main-header">
<nav class="nav-bar">
<ul class="container">
<li>My Story</li>
<li>Wines</li>
<li>How to Order</li>
</ul>
<img src="http://placeimg.com/640/480/any" >
<ul class="container">
<li>Personal Sommelier</li>
<li>Contact</li>
</ul>
</nav>
<p style="text-align: center;">|<br>(Center)</p>
</header>
I want to create a navigation panel like this
but I have a problem with img I can't put it in the middle
here is my code:
.leftNav {
text-align: right;
position: fixed;
width: 49%;
top: 0;
left: 0;
}
.rightNav {
position: fixed;
width: 49%;
top: 0;
right: 0;
left: auto;
}
.nav {
margin-top: 10px;
}
.nav li {
display: inline-block;
margin-left: 35px;
}
.nav li:first-child {
margin-left: 0;
}
.nav li a {
text-decoration: none;
color: white;
padding: 5px 0px;
text-transform: uppercase;
}
.wrapLogo {}
.logo {
height: 100px;
width: auto;
}
<header>
<div class="wrapNav">
<nav class="leftNav">
<ul class="nav">
<li>Menu</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</nav>
<div class="wrapLogo">
<img src="./style/img/xxx.png" alt="" class="logo">
</div>
<nav class="rightNav">
<ul class="nav">
<li>Menu</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</nav>
</div>
</header>
Thanks for all your help 😄
The best and simplest solution is to use flexbox.
You should put flex on the root div (.wrapNav) then to vertically align add align-items: center;.
.wrapNav {
display: flex;
align-items: center;
justify-content: center;
}
.nav {
display: flex;
/* RESET THE LIST */
list-style: none;
padding: 0;
margin: 0;
}
.nav li a{
display: block; /* MUST BECAUSE OF THE PADDING */
text-decoration: none;
color: red;
padding:0px 5px;
text-transform: uppercase;
}
.wrapLogo {
margin: 0 10px;
}
.logo {
height: 100px;
width: auto;
}
<header>
<div class="wrapNav">
<nav class="leftNav">
<ul class="nav">
<li>Menu</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</nav>
<div class="wrapLogo">
<img src="http://via.placeholder.com/350x150" class="logo">
</div>
<nav class="rightNav">
<ul class="nav">
<li>Menu</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</nav>
</div>
</header>
You can do it with the Flexbox:
body {margin: 0}
.wrapNav {
display: flex; /* displays flex-items (children) inline */
justify-content: center; /* centers them horizontally */
align-items: center; /* and vertically */
background: Aquamarine;
}
img {
display: block; /* removes bottom margin/whitespace */
width: 100%; /* responsiveness */
}
.nav {
display: flex;
justify-content: space-between;
list-style: none;
}
.nav > li {
margin: 0 15px;
}
.nav li a {
text-decoration: none;
color: #000;
padding: 5px 0px;
text-transform: uppercase;
}
/* addition */
#media (max-width: 568px) { /* adjust to your needs */
.wrapNav {flex-direction: column} /* stacks flex-items vertically */
}
<header>
<div class="wrapNav">
<nav class="leftNav">
<ul class="nav">
<li>Menu</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</nav>
<img src="http://placehold.it/200x100" alt="" class="logo">
<nav class="rightNav">
<ul class="nav">
<li>Menu</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</nav>
</div>
</header>
Something like this?
.leftNav {
width:40%;
top: 0;
left: 0;
}
.rightNav {
width:40%;
top: 0;
right: 0;
left: auto;
}
.nav {
margin-top: 10px;
}
.nav li{
display: inline-block;
margin-left: 35px;
}
.nav li:first-child {
margin-left: 0;
}
.nav li a{
text-decoration: none;
color: white;
padding: 5px 0px;
text-transform: uppercase;
}
.wrapLogo {
width:20%;
}
.wrapNav{
display: flex;
}
.logo {
height: 100px;
width: auto;
}
<header style="background-color: black;">
<div class="wrapNav">
<nav class="leftNav">
<ul class="nav">
<li>Menu</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</nav>
<div class="wrapLogo">
<img src="http://via.placeholder.com/150x150" alt="" class="logo">
</div>
<nav class="rightNav">
<ul class="nav">
<li>Menu</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</nav>
</div>
</header>
I've been trying to create a navigation bar which consists of three pieces, a list to the left of the centered logo, the logo itself and a list to the right of the logo. I've tried absolutely positioning the logo and floating the lists however this leads to the logo overlaying the lists when the width of the browser is altered.
Any suggestions would be much appreciated, JSFiddle included below :-).
JSFiddle
HTML
<div class="navigation">
<div class="container-1020">
<ul>
<li>Home</li>
<li>Work</li>
<li>Contact</li>
<li>Blog</li>
</ul>
<div class="nav-logo">
<img src="http://placehold.it/200x60"/>
</div>
<ul>
<li>01234 123456</li>
</ul>
</div>
</div>
CSS
.navigation {
background: #222222;
}
.container-1020 {
max-width: 1020px;
min-width: 500px;
margin: 0 auto;
}
ul {
color: #fff;
margin: 0;
list-style-type: none;
}
li {
display: inline;
margin-right: 10px;
}
li:last-child {
margin-right: 0 !important;
}
.logo-container {
width: 200px;
height: 60px;
}
This might work for youFIDDLE
css:
* {
margin: 0;
}
a {
color: #ffffff;
text-decoration: none;
}
.navigation {
background: #222222;
}
.container-1020 {
max-width: 1020px;
min-width: 500px;
margin: 0 auto;
}
ul {
color: #fff;
list-style-type: none;
display: inline-block;
width: 30%;
}
ul:last-child {
text-align: right;
}
.nav-logo {
display: inline-block;
width: 30%;
}
html:
<div class="navigation">
<div class="container-1020">
<ul class="left">
<li>Home
</li>
<li>Work
</li>
<li>Contact
</li>
<li>Blog
</li>
</ul>
<div class="nav-logo">
<img src="http://placehold.it/200x60" />
</div>
<ul class="right">
<li>01234 123456</li>
</ul>
</div>
</div>
well for one, correct your class name for the logo, is it .nav-logo or .logo-container
then set your ul's and whichever logo container class you decide on to display:inline-block