So I am trying to create a navbar for a school project, however I cannot get all the buttons to be evenly spaced across the whole navbar. I have been trying to do this for a while now, and the navbar code is all messy which doesn't help my situation. Any solutons?
The HTML code is:
<ul>
<li> <img src="Images/homepage/logo.png" width="20" height="20" href="#home"> </li>
<div class="other";>
<li>Films</li>
<li>Contact</li>
<li>About</li>
<li>FAQ</li>
</div>
</ul>
The CSS code is:
ul {
list-style-type: none;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
z-index: 1;
top: 0;
left: 0;
width: 100%;
text-align: center;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
z-index: 1;
width: 100px;
}
li a:hover:not(.active) {
background-color: #111;
}
li a:hover{
text-decoration: none;
color: white;
}
.active {
background-color: #222;
color:white;
}
Please help, this has been confusing me for a while now and I would rather not write up a new navbar code unless absolutely necessary!
Use Flex
ul{ display:flex;}
li{flex:1;}
* {
margin: 0;
}
ul {
display: flex;
flex-direction: row;
list-style-type: none;
padding: 0;
background-color: #333;
position: fixed;
z-index: 1;
height: 50px;
line-height: 50px;
width: 100%;
}
li {
flex: 1;
}
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
z-index: 1;
}
li a:hover:not(.active) {
background-color: #111;
}
li a:hover {
text-decoration: none;
color: white;
}
.active {
background-color: #222;
color: white;
}
img {
margin: -15px;
}
<ul>
<li>
<img src="https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/68dd54ca-60cf-4ef7-898b-26d7cbe48ec7/10-dithering-opt.jpg" width="40" height="40px" href="#home">
</li>
<li>Films</li>
<li>Contact</li>
<li>About</li>
<li>FAQ</li>
</div>
</ul>
You can only nest li elements within the ul element. Nesting div inside the ul is not semantically correct. Also, the incorrectly nested div element within ul has a semicolon after its class name: this is not legal.
The flex box module is the best option to use in order to achieve what you are after. You can read more about it here.
One way to get the results you are after is as follows:
ul,
li,
a,
img {
box-sizing: border-box;
padding: 0px;
margin: 0px;
border: 0px;
height: 50px; /* Adjust this value to whatever height you want your nav to be */
}
ul {
position: fixed;
top: 0px;
left: 0px;
right: 0px;
list-style-type: none;
background-color: #333;
text-align: center;
display: flex;
justify-content: space-around;
align-items: center;
}
ul li {
flex: 0 1 100%;
}
ul li a{
display: block;
color: white;
text-decoration: none;
line-height: 50px; /* This value should be the same as your nav's height is in order for text to stay centered. */
padding: 0px 1em;
}
ul li a img {
height: 100%;
width: auto;
padding: 5px; /*You can adjust this value to whatever you want. */
}
li a:hover:not(.active) {
background-color: #111;
}
li a:hover{
text-decoration: none;
color: white;
}
.active {
background-color: #222;
color:white;
}
<ul>
<li> <img src="https://www.w3.org/html/logo/downloads/HTML5_1Color_White.png" href="#home"> </li>
<li>Films</li>
<li>Contact</li>
<li>About</li>
<li>FAQ</li>
</ul>
I hope this is helpful.
Related
I want the blue of the navbar to extend across the screen, while the About, Updates, and Who am I? to stay in the middle? I want the background to remain a background so that it will change if I resize anything. I am fine with a different centering method, if that works better.
.centered {
display: flex;
justify-content: center;
}
#navbar {
background: #0099CC;
color: #FFF;
height: 51px;
padding: 0;
margin: 0;
border-radius: 0px;
}
*{
padding:0;
margin:0;
}
#navbar ul, #navbar li {
margin: 0 auto;
padding: 0;
list-style: none
}
#navbar ul {
width: 100%;
}
#navbar li {
float: left;
display: inline;
position: relative;
}
#navbar a {
display: inline-block;
display:flex;
line-height: 51px;
padding: 0 14px;
text-decoration: none;
color: #FFFFFF;
font-size: 16px;
text-align: center;
}
#navbar li a:hover {
color: #0099CC;
background: #F2F2F2;
}
#navbar label {
display: none;
line-height: 51px;
text-align: center;
position: absolute;
left: 35px
}
<div class="centered">
<nav id='navbar'>
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Updates</a></li>
<li><a href='#'>Who am I?</a></li>
</ul>
</nav>
</div>
You can add 100% width to #navbar to extend across the screen and change display & width properties for #navbar ul, like this
#navbar {
width: 100%;
}
#navbar ul {
display: flex;
width: fit-content;
}
I am trying to create a horizontal navbar with a logo on the left and the menu items on the right of the navbar. I can find the basic setup for this, but what I cannot find is how to create sub menus off of some of the parent links :( here is what I have so far, I am kinda new - so please, if you can, be gentle k :)
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: sans-serif;
}
body {
background-color: green;
}
header {
height: 100px;
background-color: white;
padding: 10px 0;
}
.menu-wrap {
display: flex;
justify-content: space-between;
padding: 0 15px;
}
.logo-img {
height: 79px;
}
.menu-icon {
font-size: 2.4em;
color: #ffffff;
line-height: 50px;
}
nav {
position: absolute;
background-color: #3D4852;
top: 70px;
left: 0;
width: 100%;
}
nav ul {
list-style-type: none;
}
nav ul li {
padding: 0 15px;
}
nav ul li a {
display: inline-block;
padding: 12px;
/* Add your custom styles below to change appearance of links */
color: black;
text-decoration: none;
letter-spacing: 0.05em;
}
nav ul li a:hover,
nav ul li a:focus {
color: #eb6f4a;
}
nav ul li a:hover,
nav ul li a:focus {
color: #eb6f4a;
}
#checkbox {
display: none;
}
#checkbox:checked~nav ul {
max-height: 200px;
padding: 15px 0;
transition: all 0.5s;
}
#media (min-width: 768px) {
.menu-icon {
display: none;
}
nav {
position: relative;
top: -10px;
background-color: transparent;
}
nav ul {
max-height: 70px;
padding: 15px 0;
text-align: right;
}
nav ul li {
display: inline-flex;
padding-left: 20px;
}
<header class="menu">
<div class="menu-wrap">
<img src="logoHOLD.gif" class="logo-img" alt="Logo">
<input type="checkbox" id="checkbox">
<label for="checkbox"><i class="fa fa-bars menu-icon"></i></label>
<nav>
<ul>
<li>Home</li>
<li>Topics
<ul>
<li>Item One
<li>Item Two
<li>Item Three
</ul>
</li>
<li>Commentaries</li>
<li>Donate</li>
<li>Something</li>
</ul>
</nav>
</div>
</header>
What you'll need to do is assign a class or id to the parent ul that has the other ul you want to appear as a dropdown and give it a relative position. Then, give the child ul (the dropdown element) absolute positioning and play around with transform / top / opacity values. That's one way to do it.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: sans-serif;
}
body {
background-color: green;
}
header {
height: 100px;
background-color: white;
padding: 10px 0;
}
.menu-wrap {
display: flex;
justify-content: space-between;
padding: 0 15px;
}
.logo-img {
height: 79px;
}
.menu-icon {
font-size: 2.4em;
color: #ffffff;
line-height: 50px;
}
nav {
position: absolute;
background-color: #3D4852;
top: 70px;
left: 0;
width: 100%;
}
nav ul {
list-style-type: none;
}
nav ul li {
padding: 0 15px;
}
nav ul li a {
display: inline-block;
padding: 12px;
/* Add your custom styles below to change appearance of links */
color: black;
text-decoration: none;
letter-spacing: 0.05em;
}
nav ul li a:hover,
nav ul li a:focus {
color: #eb6f4a;
}
nav ul li a:hover,
nav ul li a:focus {
color: #eb6f4a;
}
#checkbox {
display: none;
}
#checkbox:checked~nav ul {
max-height: 200px;
padding: 15px 0;
transition: all 0.5s;
}
#media (min-width: 768px) {
.menu-icon {
display: none;
}
nav {
position: relative;
top: -10px;
background-color: transparent;
}
nav ul {
max-height: 70px;
padding: 15px 0;
text-align: right;
}
nav ul li {
display: inline-flex;
padding-left: 20px;
}
.dd-parent {
position: relative;
}
.dd-list {
position: absolute;
top: 25px;
left: 0;
width: 100%;
transform: scaleY(0);
opacity: 0;
transition: .3s all ease;
transform-origin: top;
}
.dd-list li {
text-align: left;
background: DarkOrchid;
color: white;
}
.dd-list li:not(:first-of-type) {
border-top: 2px solid black;
}
.dd-parent:hover > .dd-list {
transform: none;
opacity: 1;
}
<header class="menu">
<div class="menu-wrap">
<img src="logoHOLD.gif" class="logo-img" alt="Logo">
<input type="checkbox" id="checkbox">
<label for="checkbox"><i class="fa fa-bars menu-icon"></i></label>
<nav>
<ul>
<li>Home</li>
<li class="dd-parent">Topics
<ul class="dd-list">
<li>Item One
<li>Item Two
<li>Item Three
</ul>
</li>
<li>Commentaries</li>
<li>Donate</li>
<li>Something</li>
</ul>
</nav>
</div>
</header>
ul {
padding: 0;
margin: 0;
text-decoration: 0;
list-style: none;
background: #343434;
height: 20 px;
}
ul li a.home {
float: left;
color: white;
font-size: 24px;
line-height: 0px;
padding: 20px 100px;
margin: 0px 30px;
}
ul li a.home:hover {
background-color: white;
color: black;
transition: .5s;
}
ul {
list-style: none;
color: white;
}
ul li {
display: inline-block;
line-height: 80px;
}
.main ul {
display: none;
}
ul li a {
font-size: 18px;
color: white;
padding: 12px 100px;
border-radius: 2px;
}
a:hover {
text-decoration: none;
background: #A179C9;
color: black;
transition: .7s;
}
.main li:hover>ul {
display: block;
position: absolute;
}
<ul class="main">
<li><a class="home" href="index.html">blank</a></li>
<li>listen
<ul>
<li><a id="spotify" href="https://open.spotify.com/">spotify</a></li>
<li><a id="apple" href="https://open.spotify.com/">apple music</a></li>
</ul>
</li>
<li>gallery</li>
<li>download</li>
<li>store</li>
</ul>
The "spotify" and "apple music" block elements display in one line next to each other as opposed to below each other.
I also use bootstrap on the page. Not sure if it's got anything to do with the issue because when I remove the script it still works the same.
Sorry if my code is hard to see through.
It's the inline-block on ul li in your CSS. inline-block won't break the line. See MDN Inline Elements.
in css I marked my edits.
was it necessary?
ul {
padding: 0;
margin: 0;
text-decoration: 0;
list-style: none;
background: #343434;
height: 20 px;
}
ul li a.home {
float: left;
color: white;
font-size: 24px;
line-height: 0px;
padding: 20px 100px;
margin: 0px 30px;
}
ul li a.home:hover {
background-color: white;
color: black;
transition: .5s;
}
ul {
list-style: none;
color: white;
}
ul li {
display: inline-block;
line-height: 80px;
}
.main ul {
display: none;
flex-direction: column; /*add this it*/
position: absolute; /*add this it*/
}
ul li a {
font-size: 18px;
color: white;
padding: 12px 100px;
border-radius: 2px;
}
a:hover {
text-decoration: none;
background: #A179C9;
color: black;
transition: .7s;
}
.main li:hover>ul {
display: flex; /*add this it*/
position: absolute; /*add this it*/
}
<ul class="main">
<li><a class="home" href="index.html">blank</a></li>
<li>listen
<ul class="sub_main">
<li><a id="spotify" href="https://open.spotify.com/">spotify</a></li>
<li><a id="apple" href="https://open.spotify.com/">apple music</a></li>
</ul>
</li>
<li>gallery</li>
<li>download</li>
<li>store</li>
</ul>
I'm trying to mimic this website (but with my own images): https://www.w3schools.com/w3css/tryw3css_templates_band.htm#
Every time I click on more to try to go down to "merchandise", "Extras" or "media" it closes (unless I do it very quickly). How can I fix this?
I know there's probably a better way of doing the drop-down menu in HTML/CSS but this is the only way I know how.
nav {
background-color: black;
height: 3rem;
widows: 100%;
}
.navbar {
height: 100%;
display: flex;
flex-flow: row wrap;
border: 1px solid hotpink;
justify-content: flex-start;
align-items: center;
}
.navbar li {
border: 2px solid green;
list-style-type: none;
height: 100%;
}
.navbar li a {
border: 1px solid orange;
color: white;
text-decoration: none;
padding: 0 20px;
text-transform: uppercase;
display: block;
height: 100%;
line-height: 2rem;
}
.navbar li a:hover {
background-color: #CCCCCC;
color: black;
display: block;
height: 100%;
}
section#header-image {
background-image: url('images/sky.jpg');
background-repeat: no-repeat;
background-size: cover;
background-position: center;
width: 100%;
height: 60vh;
display: flex;
flex-flow: column nowrap;
justify-content: flex-end;
align-items: center;
color: white;
}
.moreArrow{
height: 6px;
width: 8px;
margin-left: 5px;
}
ul li ul li {
display: none;
}
ul li:hover ul li {
display: block;
}
ul li:hover ul li a {
color: white;
background-color: black;
text-decoration: none;
padding: 0 20px;
height: 100%;
}
<header>
<nav>
<ul class="navbar">
<li>home</li>
<li>band</li>
<li>tour</li>
<li>contact</li>
<li>more<img src="images/arrow.png" alt="x" class="moreArrow">
<ul>
<li>Merchandise</li>
<li>Extras</li>
<li>Media</li>
</ul>
</li>
</ul>
</nav> <!--end of nav-->
<section id="header-image">
<div class="header-text">
<h2>London</h2>
<p>We killing it up in 'ere </p>
</div>
</section>
</header>
So, i made a lil change on your HTML, in order to persist ':hover' state you need to put anything that it has to stay visible inside the hovered item, in this case, a 'span' tag (i did changed from an 'a' tag because you cant nest 'a' tags)
The css is pretty simple from this point, you hide the sub menu and display on hover, anything that its inside the span tag belongs to ':hover' state as well
body {
margin: 0px;
}
nav {
width: 100vw;
height: auto;
border: 1px solid red;
}
nav ul.navbar {
display: flex;
margin: 0;
padding: 0;
}
nav ul.navbar > li {
width: auto;
display: inline-block;
vertical-align: top;
}
nav ul.navbar > li a {
display: inline-block;
text-decoration: none;
color: black;
border: 1px solid red;
padding: 10px 30px;
box-sizing: border-box;
width: auto;
}
nav ul.navbar > li span {
display: inline-block;
text-decoration: none;
color: black;
border: 1px solid red;
position: relative;
padding: 10px 30px;
}
nav ul.navbar > li span:hover ul {
display: inline-block;
}
nav ul.navbar > li span ul {
display: none;
margin: 0;
padding: 0;
position: absolute;
top: 38px;
left: 0px;
width: auto;
border: 1px solid blue;
}
nav ul.navbar > li span ul li {
display: inline-block;
list-style: none;
width: 100%;
}
nav ul.navbar > li span ul li a {
width: 100%;
display: inline-block;
}
<header>
<nav>
<ul class="navbar">
<li>home</li>
<li>band</li>
<li>tour</li>
<li>contact</li>
<li>
<span class="more">more
<ul rel=":D">
<li>Merchandise</li>
<li>Extras</li>
<li>Media</li>
</ul>
</span>
</li>
</ul>
</nav> <!--end of nav-->
</header>
Hope this helps
I'm trying to do a nav bar with a contact button stuck to the right of the page. I want contact to be aligned with the rest of the elements of the nav bar, however when I add float: right; it just disaligns my nav bar and it doesn't move contact to the right.
Here you can see my code: http://jsfiddle.net/LG2vR/19/
Can someone please tell me the proper way to accomplish this please?
Thanks!
Am not sure if this is what you need exactly, see the updated fiddle
http://jsfiddle.net/ov74xcyg/1/
Basically, i have used position absolute to move your last child of the navigation to the right side and increased width of the navigation till the end of the header.
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100px;
padding: 10px 100px;
z-index: 500;
box-sizing: content-box;
transition: .3s;
background: red;
}
nav.white {
background: white;
height: 35px;
padding: 10px 100px;
transition: .5s;
}
nav ul {
list-style: none;
float: left;
margin: 0;
padding: 0;
display: flex;
width: 90%;
position: relative;
}
nav ul li {
list-style: none;
}
nav ul li:last-child {
display: inline-block;
right: 5%;
position: absolute;
top: 0;
}
nav ul li a {
line-height: 80px;
color: #FFFFFF;
margin: 12px 30px;
text-decoration: none;
text-transform: capitalize;
transition: .5s;
padding: 10px 5px;
font-size: 16px;
}
nav ul li a:hover {
background-color: rgba(255,255,255,0.2);
}
nav.white ul li a {
color: #000;
line-height: 40px;
transition: .5s;
}
nav ul li a:focus {
outline: none;
}
<div class="wrapper">
<nav>
<ul>
<li>LOGO</li>
<li>Home</li>
<li>Page 1</li>
<li>Page 2</li>
<li><a class="contact" href="#">Contact</a></li>
</ul>
</nav>
Just add:
nav ul li a {
float: left;
}
right before:
nav ul li a.contact {
float: right;
}
or use flexbox!
.wrapper li {list-style-type: none;}
.wrapper {
overflow: hidden;
background-color: red;
}
.wrapper .logo{
margin-right : 30px;
}
.wrapper a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.wrapper a:hover {
background-color: #ddd;
color: black;
}
.wrapper a.active {
background-color: #4CAF50;
color: white;
}
.topnav-right {
float: right;
}
<div class="wrapper">
<a class="active logo" href="#">Logo</a>
Home
Page 1
Page 2
<div class="topnav-right">
<li><a class="contact" href="#">Contact</a></li>
</div>
</div>