How to structure HTML navbar for both mobile and desktop screens - html

I have made two HTML navbars (one for desktop screens and another for mobile screens) and I want to have only one HTML navbar and use CSS to style for both screen sizes since I believe it's not a good idea to repeat your code.
The thing is I have never done this thing before and most sample examples I found are using CSS frameworks. How should I approach this issue?
Here's a simplified version of the code:
function openNav() {
document.getElementById("sidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("sidenav").style.width = "0";
}
.navbar {
background-color: white;
padding-bottom: 10px;
width: 100%;
}
.main-nav {
display: none;
}
.navbar-toggle {
position: absolute;
top: 8px;
right: 20px;
font-size: 19px;
}
.nav-mobile {
height: 100%;
width: 0;
position: fixed;
z-index: 2;
top: 0;
right: 0;
background-color: white;
overflow-x: hidden;
padding-top: 80px;
}
.nav-mobile a {
display: block;
}
.nav-mobile .closebutton {
position: absolute;
top: 0;
right: 5px;
font-size: 56px;
}
.bar1,
.bar2,
.bar3 {
width: 25px;
height: 2px;
background-color: black;
margin: 6px 0;
}
#media (min-width: 768px) {
.main-nav {
display: flex;
margin-right: 30px;
flex-direction: row;
justify-content: flex-end;
}
.navbar-toggle {
display: none;
}
.navbar {
display: flex;
justify-content: space-between;
}
}
<nav class="navbar">
<div class="logo-position">
LOGO
</div>
<ul class="main-nav">
<li>Home</li>
<li>About</li>
<li>
Portfolio
</li>
<li>Contact</li>
</ul>
</nav>
<!-- Navbar for mobile -->
<div id="sidenav" class="nav-mobile">
<a href="javascript:void(0)" class="closebutton" onclick="closeNav()"
>×</a
>
Home
About
Portfolio
Contact
</div>
<div class="navbar-toggle" onclick="openNav()">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>

You should use media queries to make anything responsive. You are right about repetitive code, it's not good in any way. You can use #media screen and (max-width: 600px) {} to target mobile devices and show/hide elements in desktop/mobile.
<nav class="navbar">
<div class="logo-position">
LOGO
</div>
<div class="navbar-toggle" onclick="openNav()"> <!--HIDE IN DESKTOP-->
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<ul class="main-nav"> <!--HIDE IN MOBILE-->
<li>Home</li>
<li>About</li>
<li>
Portfolio
</li>
<li>Contact</li>
</ul>
</nav>
Following a tutorial is a good start. https://www.w3schools.com/howto/howto_js_topnav_responsive.asp

Related

Failing When Implementing A Hamburger Menu Using Flexbox

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.

Logo is not in the same line as the navigation links

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>

Two navigation and one logo in header

I have a problem, I need to do, two navigation and one logo on left side. What I need you can see on image below
I need to do effectively and responsively, when the browser shrinks, gaps will gradually shrink until it jumps to the hamburger menu.
Below you see how I have it now, the picture I need but I do not know how to do it
.site-nav {
width: 100%;
height: 100px;
display: flex;
}
.site-nav .nav-logo {
width: 200px;
height: 100px;
float: left;
display: flex;
}
.site-nav .nav-logo img {
width: 97px;
height: 47px;
margin: auto 0;
}
.site-nav .nav-links {
height: 100px;
float: right;
flex-grow: 1;
}
.site-nav .nav-links ul {
list-style: none;
margin: 0;
padding: 0;
height: 100px;
display: flex;
}
.site-nav .nav-links ul li {
float: left;
margin: auto;
padding-left: 16px;
}
.site-nav .nav-links ul li:not(:last-child) {
padding-right: 16px;
}
<nav class="site-nav">
<div class="container">
<div class="nav-logo">
<img src="logo.png" alt="">
</div>
<div class="nav-links">
<ul class="nav-list">
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
</ul>
</div>
</div>
</nav>
You can use nested flexboxes and media queries to get the desired result. I used dummy images for the logo and the hamburger.
ul {
list-style: none;
margin: 0;
}
.site-nav {
width: 100%;
}
.container {
display: flex;
justify-content: space-between;
align-items: center;
width: 80%;
margin: 0 auto;
}
.nav-links {
display: none;
}
.nav-links .nav-list {
display: flex;
flex-wrap: no-wrap;
}
.mobile-menu {
position: relative;
}
.mobile-menu .nav-list {
display: none;
position: absolute;
bottom: -90px;
left: -40px;
}
.mobile-menu:hover>.nav-list {
display: inline-block;
}
.nav-links .nav-list>li:not(:last-child) {
margin-right: 1rem;
}
#media screen and (min-width: 500px) {
.mobile-menu {
display: none;
}
.nav-links {
display: inline-block;
}
}
<nav class="site-nav">
<div class="container">
<div class="nav-logo">
<img src="https://via.placeholder.com/50x50" alt="">
</div>
<div class="mobile-menu">
<img src="https://via.placeholder.com/30x30" alt="">
<ul class="nav-list">
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
</ul>
</div>
<div class="nav-links">
<ul class="nav-list">
<li>Home</li>
<li>Home</li>
<li>Home</li>
</ul>
</div>
<div class="nav-links">
<ul class="nav-list">
<li>Home</li>
<li>Home</li>
</ul>
</div>
</div>
</nav>
How about the following example. With css grid it's easy to specify columns. With extra properties like justify-content you can specify how the columns are spaced.
.site-nav {
width: 100%;
height: 100px;
}
.site-nav .container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
}
.site-nav .nav-logo {
grid-column: 1;
width: 200px;
height: 100px;
float: left;
display: flex;
}
.site-nav .nav-logo img {
width: 97px;
height: 47px;
margin: auto 0;
}
.site-nav .nav-links {
grid-column: auto;
}
.site-nav {
list-style-type: none;
}
.site-nav li {
display: inline-block;
}
<nav class="site-nav">
<div class="container">
<div class="nav-logo">
<img src="https://cdn.pixabay.com/photo/2018/08/19/19/56/peacock-feathers-3617474_960_720.jpg" alt="">
</div>
<div class="nav-links">
<ul class="nav-list">
<li>Home</li>
<li>Home</li>
<li>Home</li>
</ul>
</div>
<div class="nav-links">
<ul class="nav-list">
<li>Home</li>
<li>Home</li>
</ul>
</div>
</div>
</nav>

Can't get two divs to align horizontally

I'm pulling my hair out trying to get two div tags to align. I've read page after page of solutions on here but I've not been able to get any of them to work. I'm not sure if this is related to this being a Visual Studio project using MVC. It seems unlikely but I thought I'd mention it.
So this is for a header bar on a company website. Logo should be on the left and the menu should be on the right. It must be responsive. Here's what I've got so far:
header {
width: 100%;
position: absolute;
top: 0;
left: 0;
background-color: #ffffff;
}
logo {
float: none;
width: 215px;
}
nav {
width: 100%;
height: 100%;
float: left;
}
nav ul {
height: auto;
padding: 8px 0px;
margin: 0px;
}
nav li {
display: inline;
padding: 20px;
}
nav a {
text-decoration: none;
color: #171581;
padding: 8px 8px 8px 8px;
}
nav a:hover {
color: #D60053;
}
And here is the HTML
<div style="opacity: 1;" class="wrapper">
<header class="">
<div class="container">
<div class="logo">
<a href="/" class="glyphicon-log-out top-menu">
<img src="~/assets/images/sunwavelogo.png" alt="Sunwave Logo" />
</a>
</div>
<div class="hamburger"></div>
<nav>
<ul>
<li>About</li>
<li>Residential & Business</li>
<li>My Accounts Details</li>
<li>FAQ</li>
<li>Contact us</li>
</ul>
</nav>
</div>
</header>
By changing your CSS like this (note the added dot in .logo)
.logo {
float: left;
width: 215px;
}
nav {
margin-left: 215px;
}
header {
width: 100%;
position: absolute;
top: 0;
left: 0;
background-color: #ffffff;
}
.logo {
float: left;
width: 215px;
}
nav {
margin-left: 215px;
}
nav ul {
height: auto;
padding: 8px 0px;
margin: 0px;
}
nav li {
display: inline;
padding: 20px;
}
nav a {
text-decoration: none;
color: #171581;
padding: 8px 8px 8px 8px;
}
nav a:hover {
color: #D60053;
}
<div style="opacity: 1;" class="wrapper">
<header class="">
<div class="container">
<div class="logo">
<a href="/" class="glyphicon-log-out top-menu">
<img src="~/assets/images/sunwavelogo.png" alt="Sunwave Logo" />
</a>
</div>
<div class="hamburger"></div>
<nav>
<ul>
<li>About</li>
<li>Residential & Business</li>
<li>My Accounts Details</li>
<li>FAQ</li>
<li>Contact us</li>
</ul>
</nav>
</div>
</header>
You have many problems in your code:
logo in your css should be .logo to refer to the class of the logo.
The property float:none should be set to float:left; so it should be correctly floated.
And for the nav you shouldn't specify a width:100% because it will be forced to take the whole width of the header, you need to set it to auto for example.
This is a working snippet:
header {
width: 100%;
position: absolute;
top: 0;
left: 0;
background-color: #ffffff;
}
.logo {
float: left;
width: 215px;
}
nav {
width: auto;
height: 100%;
float: left;
}
nav ul {
height: auto;
padding: 8px 0px;
margin: 0px;
}
nav li {
display: inline;
padding: 20px;
}
nav a {
text-decoration: none;
color: #171581;
padding: 8px 8px 8px 8px;
}
nav a:hover {
color: #D60053;
}
<div style="opacity: 1;" class="wrapper">
<header class="">
<div class="container">
<div class="logo">
<a href="/" class="glyphicon-log-out top-menu">
<img src="~/assets/images/sunwavelogo.png" alt="Sunwave Logo" />
</a>
</div>
<div class="hamburger"></div>
<nav>
<ul>
<li>About
</li>
<li>Residential & Business
</li>
<li>My Accounts Details
</li>
<li>FAQ
</li>
<li>Contact us
</li>
</ul>
</nav>
</div>
</header>
1.Your code was badly formatted.I have formatted it.
2..logo should be set to "float:left".
3..container should have"overflow:hidden"
I have also made Your li straight.(I have made it in one line )
This contains your html formatted code,Css which You may need to change as well as add
<div style="opacity: 1;" class="wrapper">
<header class="">
<div class="container">
<div class="logo">
<a href="/" class="glyphicon-log-out top-menu">
<img src="~/assets/images/sunwavelogo.png" alt="Sunwave Logo" />
</a>
</div>
<div class="hamburger">
<nav>
<ul>
<li>About</li>
<li>Residential & Business</li>
<li>My Accounts Details</li>
<li>FAQ</li>
<li>Contact us</li>
</ul>
</nav>
</div>
</div>
</header>
</div>
Your css code:
* {
margin: 0px;
padding: 0px;
}
header{
width:700px;
margin:0 auto;
}
.container {
overflow: hidden;
}
.logo {
float: left;
margin-right:100px;
}
.hamburger {
/* float: left; */
overflow: hidden;
}
li {
float: left;
padding: 5px;
list-style-type: none;
}
Hope This Is what You had expected

Centered logo with varied widths and multiple lists

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