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>
Related
I am creating a navbar with a logo on the left side on the navbar and the links on the right side of the navbar. Although I have been successful, there are some unwanted line breaks in the text on the right side of the navbar. How do I get rid of the line breaks in the texts "How it works" and "Available Products"? I am not using Bootstrap and I do not want to use it.
* {
padding: 0;
margin: 0;
font-family: "Roboto", sans-serif;
}
ul {
list-style-type: none;
overflow: hidden;
}
li {
float: left;
}
.container {
align-items: center;
justify-content: center;
display: flex;
}
img {
max-width: 100%;
width: 72.5%;
height: auto;
}
.logo {
flex-basis: 75%;
margin-top: 10px;
margin-left: 10px;
}
.nav-link {
display: block;
text-align: center;
text-decoration: none;
font-size: 20px;
padding-right: 20px;
}
<header id="header">
<nav id="nav-bar">
<div>
<ul>
<div class="container">
<div class="logo">
<li>
<img
id="header-img"
src="https://i.ibb.co/5Mcnrcm/N-logo.png"
style="vertical-align: middle"
/>
</li>
</div>
<li><a class="nav-link" href="#f">Features</a></li>
<li><a class="nav-link" href="#h">How It Works</a></li>
<li><a class="nav-link" href="#a">Available Products</a></li>
</div>
</ul>
</div>
</nav>
</header>
It can be done by applying CSS white-space: nowrap; to e.g. .nav-link as shown below:
* {
padding: 0;
margin: 0;
font-family: "Roboto", sans-serif;
}
ul {
list-style-type: none;
overflow: hidden;
}
li {
float: left;
}
.container {
align-items: center;
justify-content: center;
display: flex;
}
img {
max-width: 100%;
width: 72.5%;
height: auto;
}
.logo {
flex-basis: 75%;
margin-top: 10px;
margin-left: 10px;
}
.nav-link {
display: block;
text-align: center;
text-decoration: none;
font-size: 20px;
padding-right: 20px;
white-space: nowrap;
}
<header id="header">
<nav id="nav-bar">
<div>
<ul>
<div class="container">
<div class="logo">
<li>
<img
id="header-img"
src="https://i.ibb.co/5Mcnrcm/N-logo.png"
style="vertical-align: middle"
/>
</li>
</div>
<li><a class="nav-link" href="#f">Features</a></li>
<li><a class="nav-link" href="#h">How It Works</a></li>
<li><a class="nav-link" href="#a">Available Products</a></li>
</div>
</ul>
</div>
</nav>
</header>
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
I cant see my alignment in one line. logo, menu and right menu... Please help to achieve the final output.
body {
background-color: #ffffff;
}
#wrapper {
width: 1200px;
margin-left: auto;
margin-right: auto;
/* background-color: #CDCDCD; */
}
.top-menu {
margin-top: 40px;
/* background-color: #95E659; */
}
header {
/* width: 100%; */
}
.logo {
width: 40px;
height: 40px;
}
.menu {
float: left;
}
.menu ul li {
display: inline;
list-style: none;
}
.menu ul li a {
margin-right: 55px;
text-decoration: none;
color: #303030;
font: 14px lato;
}
.menu ul li:first-child {}
.menu ul li:nth-child(6) {
margin-right: 300px;
}
.search-container {
float: right;
}
<div id="wrapper">
<header>
<div class="top-menu clearfix">
<div class="menu">
<div class="logo">
<a href=""><img src="assets/images/logo.jpg" alt="">
</div>
<ul>
<li>Women</li>
<li>Men</li>
<li>Kids</li>
<li>Coming Soon</li>
<li>About</li>
<li>
<img src="assets/images/user.png" alt=""> Login
</li>
<li>
<img src="assets/images/bucket.png" alt=""> Basket
</li>
</ul>
<div class="search-container">
<form action="/action_page.php">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</div>
</div>
</div>
</div>
</header>
<hr>
</div>
Use flex box for inline block elements. This will also give you a lot more control over the children elements and their layout.
I altered your HTML a little, adding the center menu buttons in a parent div and separating unordered list into two unordered lists. This way we can use flex to justify their content with space-between placing them on separate ends of the parent elements block.
On the menu display: flex, we also add a flex value for each child element, the logo, the menu buttons parent div between and the login button. By placing these elements into their own parent divs, we can control the width using flex. To center the elements vertically, if our default axis for display flex is set to row, we can use the align-items property. This will align the items in that parent elements border vertically. align-items: center
The logo and login buttons parents both having a flex value of 0, which means they will take up only the space they need to show their content. The middle element, .between having a flex of 1 will mean it will take up the rest of the parent menus width. This will allow us to place a flex display on the UL elements and we can then set them on separate sides of the remaining sections width using justify-content on the UL elements parent element .between.
Snippit best if viewed in full page view as you define the width of your wrapper to 1200px.
body {
background-color: #ffffff;
margin: 0 auto;
}
#wrapper {
width: 1200px;
margin: 0 auto;
background-color: #CDCDCD;
}
.top-menu {
margin-top: 40px;
background-color: #95E659;
}
.logo {
flex: 0;
}
.logo img {
width: 40px;
height: 40px;
}
.search-container {
flex: 0;
display: inline-flex;
align-items: center;
justify-content: center;
}
.menu {
display: flex;
justify-content: space-between;
align-items: center;
padding-right: 2rem;
}
.between {
display: flex;
justify-content: space-between;
flex: 1;
}
.menu div ul.left {
display: flex;
justify-content: flex-start;
}
.menu div ul.right {
display: flex;
justify-content: flex-end;
}
.menu ul li {
flex: auto;
display: inline;
list-style: none;
padding: 0 1rem;
}
.menu ul li a {
text-decoration: none;
color: #303030;
font: 14px lato;
}
<header>
<div id="wrapper">
<div class="top-menu clearfix">
<div class="menu">
<div class="logo">
<a href="">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a" alt="">
</a>
</div>
<div class="between">
<ul class="left">
<li>Women</li>
<li>Men</li>
<li>Kids</li>
<li>Coming Soon</li>
<li>About</li>
</ul>
<ul class="right">
<li>
<img src="assets/images/user.png" alt="">Login
</li>
<li>
<img src="assets/images/bucket.png" alt="">Basket
</li>
</ul>
</div>
<div class="search-container">
<form action="/action_page.php">
<button type="submit"><i class="fa fa-search">search</i></button>
</form>
</div>
</div>
</div>
</div>
</header>
Here's a start with flexbox.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
body {
background-color: #ffffff;
}
#wrapper {
max-width: 1200px;
margin-left: auto;
margin-right: auto;
}
.top-menu {
margin-top: 40px;
display: flex;
align-items: center;
justify-content: space-between;
}
.menu {
display: flex;
align-items: center;
}
.logo {
width: 40px;
height: 40px;
}
.menu ul li {
display: inline;
list-style: none;
}
.menu ul li a {
text-decoration: none;
color: #303030;
font: 14px lato;
}
<div id="wrapper">
<header>
<div class="top-menu clearfix">
<div class="menu">
<div class="logo">
<img src="https://via.placeholder.com/50" alt="">
</div>
<ul>
<li>Women</li>
<li>Men</li>
<li>Kids</li>
<li>Coming Soon</li>
<li>About</li>
</ul>
</div>
<div class="menu">
<ul>
<li>
<img src="https://via.placeholder.com/10" alt=""> Login
</li>
<li>
<img src="https://via.placeholder.com/10" alt=""> Basket
</li>
</ul>
<div class="search-container">
<form action="/action_page.php">
<button type="submit"><i class="fa fa-search">s</i></button>
</form>
</div>
</div>
</div>
</header>
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.
I am trying to understand how to get this work. I have a navbar with logo on left and menu links on the right. When the screen reaches 768px, menu links should disappear and menu icon should show up instead. And when menu icon is pressed, menu overlay should appear with those links and some button and links.
My example below:
<nav class="navbar navbar-expand-md fixed-top navbar-dark" id="section1">
<div class="container">
<a class="navbar-brand" href="#"><img class="company-logo" src="img/company-logo.svg"></a>
<button class="navbar-toggler" type="button" data-target="#collapsingNavbarMd">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse" id="collapsingNavbarMd">
<ul class="nav navbar-right">
<li class="nav-item">
<a class="nav-link" href="#works">Works</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#services">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#contact-us">Contact Us</a>
</li>
<section class="contact-buttons">
<div class="ask-button">
<button class="btn-outlined">Ask me</button>
</div>
<div class="social-icons">
<div class="whatsapp-icon social-icon">
<img src="img/whatsapp-icon.svg" alt="WhatsApp">
</div>
<div class="linkedin-icon social-icon">
<img src="img/linkedin-icon.svg" alt="LinkedIn">
</div>
<div class="facebook-icon social-icon">
<img src="img/facebook-icon.svg" alt="Facebook">
</div>
<div class="twitter-icon social-icon">
<img src="img/twitter-icon.svg" alt="Twitter">
</div>
</div>
</section>
</ul>
</div>
</div>
</nav>
CSS:
.navbar {
padding: 0;
.company-logo {
width: 140px;
height: auto;
}
.company-logo:hover {
opacity: 0.7;
}
}
.navbar .container {
margin-top: 30px;
}
.navbar .contact-buttons {
display: none;
}
.navbar button {
display: block;
}
.navbar-right .nav-item .nav-link {
padding: 0;
padding-left: 2.5rem;
color: #fff;
font-size: 1rem;
font-weight: 400;
}
.navbar-dark .navbar-toggler {
border-color: transparent;
z-index: 2;
}
.navbar-toggler {
padding: 0;
}
.navbar {
button:focus {
outline: none;
}
}
.navbar-expand-md .container {
padding-right: 30px;
padding-left: 30px;
}
.navbar-expand-md .navbar-collapse {
justify-content: flex-end;
}
Overlay style should be like this:
.navbar-overlay {
display: block;
background-color: #585858;
position: relative;
width: 100%;
text-align: center;
display: flex;
.container {
flex-direction: column;
padding: 12vh;
ul {
flex-direction: column;
}
}
.navbar-right .nav-item .nav-link {
padding: 0;
padding-left: 0;
color: #fff;
font-size: 3rem;
margin-bottom: 30px;
font-weight: 500;
}
.company-logo {
display: none;
}
.contact-buttons {
display: block;
}
}