CSS Horizontal Navigation Spacing

css horizontal navigation spacing

I've had this happen to me. Unfortunately it is caused by the browser taking the line breaks between the list items and rendering them as spaces. To fix, change your HTML to:

<div id="footer">
<ul>
<li><a href="one.html">One</a></li><li><a href="two.html">Two</a></li><li><a href="three.html">Three</a></li><li><a href="four.html">Four</a></li><li><a href="five.html">Five</a></li>
</ul>
</div>

Having trouble increasing spacing between horizontal menu items with CSS

try

a { 
display: block;
padding: 10px 30px;
}

edit

Do you want something like this ? http://jsfiddle.net/Y8Ng7/

Just remove that ridiculous margin you have for the nav and increase the li padding

li {
display:inline;
padding: 10px 40px;
}

To center a div element, don't do margin: 21px 646px 21px 646px;

just do margin: 21px auto;

Full width horizontal nav bar with evenly spaced items

With a static number of elements it's easy - http://jsfiddle.net/X56cJ/

However, if you want to have uniform spacing between the text, not the elements themselves - it becomes tricky. Again, if the number of elements doesn't change, you do it with css classes and static widths. Otherwise it'll have to be javascript

EDIT: Here's the JavaScript way of getting same space between elements.

HTML:

<ul class="menu">
<li>About Us</li>
<li>Our Products</li>
<li>FAQs</li>
<li>Contact</li>
<li>Login</li>
</ul>

JS:

function alignMenuItems(){
var totEltWidth = 0;
var menuWidth = $('ul.menu')[0].offsetWidth;
var availableWidth = 0;
var space = 0;

var elts = $('.menu li');
elts.each(function(inx, elt) {
// reset paddding to 0 to get correct offsetwidth
$(elt).css('padding-left', '0px');
$(elt).css('padding-right', '0px');

totEltWidth += elt.offsetWidth;
});

availableWidth = menuWidth - totEltWidth;

space = availableWidth/(elts.length);

elts.each(function(inx, elt) {
$(elt).css('padding-left', (space/2) + 'px');
$(elt).css('padding-right', (space/2) + 'px');
});
}

Full example here

Horizontal Nav Bar Spacing

<ul id="navlist">
<li id="active"><a id="current" href="#">Item one</a></li><li><a href="#">Item two</a></li><li><a href="#">Item three</a></li><li><a href="#">Item four</a></li><li><a href="#">Item five</a></li>
</ul>

Remove all new lines between <li> elements. The new line in the code is interpreted as a space, that's the problem, it's not styling.

also you can add style:

#navcontainer ul li a {
margin-left:-3px;
}

How to stretch a fixed number of horizontal navigation items evenly and fully across a specified container

The modern way to distribute items evenly is to set the following two declarations on the container element:

.container {
display: flex; /* (1) */
justify-content: space-between; /* (2) or space-around or space-evenly */
}

The value to use for justify-content depends on which kind of even distribution is needed.

Sample Image

See MDN

ul {
list-style: none;
padding: 0;
width: 90vw;
border: 3px solid gold;
display: flex;
}
a {
background: gold;
}
ul {
justify-content: space-between;
}
ul ~ ul {
justify-content: space-around;
}
ul ~ ul ~ ul {
justify-content: space-evenly;
}
<h3>justify-content: space-between; </h3>

<ul id="nav">
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
<li><a href="#">BASIC SERVICES</a></li>
<li><a href="#">OUR STAFF</a></li>
<li><a href="#">CONTACT US</a></li>
</ul>
<div>Distributes items evenly. The first item is flush with the start, the last is flush with the end </div>
<hr>
<h3>justify-content: space-around;</h3>
<ul id="nav">
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
<li><a href="#">BASIC SERVICES</a></li>
<li><a href="#">OUR STAFF</a></li>
<li><a href="#">CONTACT US</a></li>
</ul>
<div>Distribute items evenly. Items have a half-size space on either end</div>
<hr>
<h3>justify-content: space-evenly;</h3>
<ul id="nav">
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
<li><a href="#">BASIC SERVICES</a></li>
<li><a href="#">OUR STAFF</a></li>
<li><a href="#">CONTACT US</a></li>
</ul>
<div>Distribute items evenly. Items have equal space around them</div>
<hr>

navigation bar entire width spaced items evenly

I'd rework your CSS like this jsFiddle example.

CSS

ul.nav {
width:100%;
margin:0 auto;
padding:0;
list-style:none;
background-color:#62564A;
text-align:center;
font-family: sans-serif;
}
.nav li {
display:inline-block;
width:33%;
margin:0;
padding:0;
}
.nav a {
text-align:center;
padding:12px 0 13px 0;
margin:0;
border-left: 1px solid #fff;
border-right: 1px solid #fff;
display:block;
}
.nav a:hover {
background:#A26A42;
border:none;
}

ul.nav li:first-child a{
border:none;
}
ul.nav li:last-child a {
border:none;
}​

HTML - Formatting Horizontal Navigation Bar

I added a display:flex to ul.topnav , but make sure you use a media query, so you can give the display at the resolution you want. Reason why i added the display:flex is so i can align them in a column.

@media (max-width:760px) {
ul.topnav {
display: flex;
flex-direction: column;
}

}

You can also add a width and another padding to ul.topnav li a , so that every a have the same width.

@media (max-width:760px) {
ul.topnav li a {
width: 46%;
padding:20px 20px 20px 103px;
text-align: left;
}
}

Example: ( change the resolution to 760px )

https://jsfiddle.net/fzy7cysf/1/



Related Topics



Leave a reply



Submit