I am trying to equally place my 8 nav links across my nav bar.
Here is my nav HTML:
<nav>
<ul class="nav">
<li>Home</li>
<li>Destinations</li>
<li>Culture</li>
<li>Attractions</li>
<li>History</li>
<li>Media</li>
<li>Contact</li>
<li>Links</li>
</ul>
</nav>
I am using a basic grid and am trying to place each of the 8 links equally into 1/8 of the nav.
My css for the grid :
.col-1-8 {
width:12.5% }
I am applying this class to each of the 8 li, but can't seem to get them all centered equally.
If someone could help me out here, i'd be very happy!
thanks
Tim
This code does exactly what you want to obtain, I think that you didn't apply box-sizing border-box or text-align center.
http://codepen.io/anon/pen/WQVQGr
HTML:
<nav>
<ul>
<li>Home</li>
<li>Destinations</li>
<li>Culture</li>
<li>Attractions</li>
<li>History</li>
<li>Media</li>
<li>Contact</li>
<li>Links</li>
</ul>
</nav>
CSS:
*{
box-sizing: border-box;
}
nav ul {
background-color:yellow;
overflow:auto;
list-style:none;
padding: 0;
margin: 0;
width: 800px;
}
nav ul>li {
float:left;
width: 12.5%;
text-align: center
}
You can use flex for this.
.nav {
display: flex;
justify-content: space-around;
/* justify-content: space-between; */
background: yellow;
margin: 0;
padding: 0;
list-style: none;
}
<nav>
<ul class="nav">
<li>Home</li>
<li>Destinations</li>
<li>Culture</li>
<li>Attractions</li>
<li>History</li>
<li>Media</li>
<li>Contact</li>
<li>Links</li>
</ul>
</nav>
Related
This question already has answers here:
Does UL have default margin or padding [duplicate]
(2 answers)
Closed 3 years ago.
I'm having issues with excess space to the left of my header.
I had the same problem with my footer as well using <nav> and <ul>, but edited this to only using <a> elements instead, and this seemed to do the trick. I would, however, like to keep the <nav> in the header, and preferably without using negative margin-left.
https://jsfiddle.net/thereseel/d75jurzy/6/
<header>
<nav>
<ul class="mainnav">
<li>Home</li>
<li>Menu</li>
<li>Logo</li>
<li>Contact</li>
<li>Location</li>
</ul>
</nav>
</header>
you are facing excess space to the left of your header because of ul
browsers have default CSS values for many elements and ul is one of them.
from your current code just remove the padding
example:
.mainnav {
/* add this to override default padding left of ul*/
padding-left: 0;
}
Here is Reference of default CSS values used by browser : w3schools link
Try with this code.
HTML code:
<header>
<nav>
<ul class="mainnav">
<li>Home</li>
<li>Menu</li>
<li>Logo</li>
<li>Contact</li>
<li>Location</li>
</ul>
</nav>
</header>
<div class="main-body">
</div>
<footer >
<section class="footnav">
Home
About
Order
Jobs
Contact
</section>
</footer>
CSS code:
.main-body{
min-height:500px;
}
.mainnav {
max-width: 800px;
margin-left: 0;
margin-bottom: 5px;
text-align: center;
display: flex;
justify-content: space-evenly;
padding-left:0px;
}
.mainnav li {
display: inline-block;
}
.footnav {
width: 100%;
text-align: center;
display: flex;
justify-content: space-evenly;
}
footer{
position: fixed;
left:0;
right:0;
bottom:0;
}
.footnav a {
display: inline-block;
padding: 10px;
}
Hope it working.
I am using Apple's navigation toolbar as my design aesthetic/goal as everything is center justified and the logo included as part of that justification.
Here's how I have it setup currently...
<body>
<header>
<div class="logo"><img src="Images/Logo.png" alt="DIVINITAL"width="35" height="35"></div>
<nav>
<ul class="menu">
<li>Home</li>
<li>Shop</li>
<li>Services</li>
<li>About</li>
<li>Contact</li>
<li>User</li>
</ul>
</nav>
<div class="menu-toggle"><i class="fa fa-bars" aria-hidden="true"></i></div>
</header>
</body>
This, however, proves difficult for me to properly edit the logo alongside the ul items on the navbar. For some reason when I begin to style them accordingly, both widths have to be set to 100% for the alignment to workout but that obviously isn't allowed as they just move to two separate lines (the logo and the ul items on the nav bar...
So is there a better way to handle this? Thank you!
Welcome to StackOverflow.
You can easily do it using flex. There's a good tutorial that explains how to use flex..
Look at the following example, and see for yourself how flex can easily achieve what you're looking for:
header {
background-color: rgba(0,0,0,0.8);
display: flex;
justify-content: center;
align-items: center;
height: 44px;
}
.logo > img {
width: 35px;
height: 35px;
margin: auto 20px;
}
nav {
flex: 1
}
nav > ul {
color: white;
display: flex;
padding: 0;
list-style: none;
justify-content: space-between;
margin: auto 20px;
}
nav > ul > li {
}
nav a {
color: inherit
}
<header>
<div class="logo">
<img src="https://cdn4.iconfinder.com/data/icons/black-icon-social-media/512/099310-feedburner-logo.png" alt="DIVINITAL">
</div>
<nav>
<ul class="menu">
<li>Home</li>
<li>Shop</li>
<li>Services</li>
<li>About</li>
<li>Contact</li>
<li>User</li>
</ul>
</nav>
<div class="menu-toggle"><i class="fa fa-bars" aria-hidden="true"></i></div>
</header>
The most important rules I used here were (flex rules):
display: flex
justify-content
align-items
I would like this code to resemble the bottom of Google's homepage. It is still listing vertically, even with "inline". I want each three "lists" to be next to each other horizontally on the left and right side of the screen.
#HTML
<footer>
<div class="footer">
<ul style="width:10%; float:left;list-style-type:none;">
<li>Advertising</li>
<li>Business</li>
<li>About</li>
</ul>
<ul style="width:10%; float:right;list-style-type:none;">
<li>Privacy</li>
<li>Terms</li>
<li>Settings</li>
</ul>
</div>
</footer>
#CSS
.footer li{
display:inline;
}
You should remove the width on your uls. See this codepen. Floating elements will take the width of all their children. By wrapping them in an element that take 10% of the width, they don't have enough space to stay on the same row.
You could use display: flex; on the .footer DIV, with settings as below
.footer ul {
list-style-type: none;
padding: 0;
}
.footer li {
display: inline-block;
}
.footer {
display: flex;
justify-content: space-between;
}
<footer>
<div class="footer">
<ul>
<li>Advertising</li>
<li>Business</li>
<li>About</li>
</ul>
<ul>
<li>Privacy</li>
<li>Terms</li>
<li>Settings</li>
</ul>
</div>
</footer>
On my website, I want a horizontal menu, which is centered on the page. So, the whole menu should be centred.
At this moment, I can create a horizontal list, but the list still stays at the left side. I want it centered.
Can someone please tell me what to change in my code to center it?
My HTML:
<div class=menu>
<ul>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
</ul>
</div>
My CSS:
ul {
text-align: center;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
div.menu{
display: table;
}
div.menu a {
display: block;
margin-left: auto;
margin-right: auto;
width: 60px;
color: navy;
background-color: #FF0000
}
li{
float: left;
}
Add margin:auto to your div.menu to accomplish this
div.menu{
display: table;
margin:auto;
}
JSFiddle: http://jsfiddle.net/0xb7j9zc/
Check out this fiddle http://jsfiddle.net/ByShine/33sz6nrt/4/
HTML
<div class="menu">
<ul>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
</ul>
</div>
CSS
ul {
text-align: center;
}
ul li {
display: inline-block;
}
I'm assuming you wish to center the menu (align it to the middle of the page). One approach, and I'm sure there's a few out there, is to wrap the 'menu' div into another div tag and set the align attribute to center, like so:
<div align="center">
<div class=menu>
<ul>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
</ul>
</div>
</div>
Here's an example: http://jsfiddle.net/q9ae01qe/
The <nav> should be aligned to the left, below the logo and slogan. However, despite attempting to do so with both margin and padding it remains above the logo and centered on the page. Any help with how to rectify this is greatly appreciated.
HTML
<header>
<div id="logo">
<img src="img/logo.png" alt="GEC logo" />
</div>
<div id="slogan">
<h1>Revolutionizing Potential</h1>
</div>
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Coaching Services</li>
<li>Resources</li>
<li>Events</li>
<li>Contact Us</li>
</ul>
</nav>
</header>
CSS
/* Header */
header {
margin-top: 95px;
}
#logo {
float: left;
}
#slogan {
float: right;
margin-top: 90px;
}
nav ul li {
display: inline;
float: left;
}
nav ul li a {
padding-right: 20px;
}
nav ul li:last-child {
padding-right: 0;
}
To your CSS add:
nav {
clear:both;
}
jsFiddle example
First off what I always do is to give all elements default widths and hights.
The nav is an inline element and not a block.
to refrain from using clear fixes you should put a div element around the logo and the slogan.
like this:
HTML
<header>
<div id="header_top">
<div id="logo">
<img src="img/logo.png" alt="GEC logo" />
</div>
<div id="slogan">
<h1>Revolutionizing Potential</h1>
</div>
</div>
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Coaching Services</li>
<li>Resources</li>
<li>Events</li>
<li>Contact Us</li>
</ul>
</nav>
</header>
Now you can just give the header_top div a display block and a overlfow:hidden.
That will give make the header_top div grow with its content.
Now giv you nav a display:block style and it is done
I tested it local on my notebook and it worked like a charm :)
add:
CSS
#header_top{
display: block;
overflow: hidden;
}
nav{
display: block;
}