Centering Menu Bar CSS/HTML - html

I am designing a website, but having issues getting the menu bar to center correctly. I have done a lot of research on the web, but can't seem to figure it out. Any suggestions?
Thanks!
The html code that deals with links.
<nav>
<ul>
<li>Home</li>
<li>Shorthorn Herdsires</li>
<li>Shorthorn Cows</li>
<li>Hereford Herdsires</li>
<li>Bull Sale</li>
<li>Cow Sale</li>
<li>History</li>
<li>Contact</li>
</ul>
</nav>
This is my css style sheet.
nav{
display: block;
width: 100%;
overflow: hidden;
}
nav ul {
padding: .7em;
float: left;
list-style: none;
background: #a4d25d;
box-shadow: 0 1px 0 rgba(255,255,255,.5), 0 2px 1px rgba(0,0,0,.3) inset;
border: 3px solid black;
/* added*/
}
nav li {
float:left;
}
nav a {
float:left;
padding: .8em .7em;
text-decoration: none;
color: black;
text-shadow: 0 1px 0 rgba(255,255,255,.5);
font: bold 1.1em/1 'trebuchet MS', Arial, Helvetica;
letter-spacing: 1px;
text-transform: uppercase;
border-width: 1px;
border-style: solid;
border-color: #black #BF7530;
background: #FF8700;
}
nav a:hover, nav a:focus {
outline: 0;
color: #black;
text-shadow: 0 1px 0 rgba(0,0,0,.2);
background: #FFDB73;
}
nav a:active {
box-shadow: 0 0 2px 2px rgba(0,0,0,.3) inset;
}
nav li:first-child a {
border-left: 0;
border-radius: 4px 0 0 4px;
}
nav li:last-child a {
border-right: 0;
border-radius: 0 4px 4px 0;
}

Here one solution to center your menu :
http://codepen.io/anon/pen/BuqlA/
nav{
display: block;
width: 100%;
overflow: hidden;
text-align: center;
}
nav ul {
padding: .7em;
list-style: none;
background: #a4d25d;
box-shadow: 0 1px 0 rgba(255,255,255,.5), 0 2px 1px rgba(0,0,0,.3) inset;
border: 3px solid black;
display: inline-block;
}

Here's another possibility, depending on exactly what you are looking for.
This uses display:inline-block for the a and li elements.
http://jsfiddle.net/jdWgE/

You can use aplying display: inline-block; to the <ul> with text-align: center; to the parent, like this:
nav{
text-align: center;
}
nav ul {
display: inline-block;
}
Hope it is that what you need
DEMO here: http://jsfiddle.net/verber/fENS4/4/

If you don't mind supporting only modern browsers that recognize and support the flexbox specification, you can use that to your advantage. You only need to change the styles for nav and nav ul:
nav{
width: 100%;
display: flex;
justify-content: center;
}
nav ul {
padding: .7em;
display: block;
overflow: hidden;
list-style: none;
background: #a4d25d;
box-shadow: 0 1px 0 rgba(255,255,255,.5), 0 2px 1px rgba(0,0,0,.3) inset;
border: 3px solid black
}
The trick is to set <nav> to use flexbox display, and instruct it to justify its content to the middle/center. I have removed the float: left declaration for the list because that is not needed.
http://jsfiddle.net/teddyrised/z6KVk/

Related

Tabs at the top of the page not aligned properly

I'm trying build a news website but im having a problem with centering the tabs at the top of th page. Can anyone give me some tips on how to fix this and also explain(I'm new to web development).
Thanks in advance.
ul{list-style-type: none;
text-align: center;
margin: 0 auto;
padding: 0;
overflow: hidden;
background-color: #333333;}
li{float: right;
margin: 0 auto;
border-right: 1px solid black;
border-bottom: 1px solid black}
li a{display: inline-block;
background-color: #333333;
color: white;
text-align: center;
padding: 10px;
text-decoration: none;}
li a:hover {background-color: black;}
<ul role="menubar">
<li></li>
<li>News</li>
<li>Sports</li>
<li>Business</li>
<li>TV</li>
<li>Automobiles</li>
<li>Culture</li>
<li>Fashion</li>
<li>Food</li>
<li>Healthcare</li>
<li>Tourism</li>
<li>TECH</li>
</ul>
change your li's to display: inline-block and remove float: right. That will allow text-align: center on the parent ul to center to work.
ul{
list-style-type: none;
text-align: center;
margin: 0 auto;
padding: 0;
overflow: hidden;
background-color: #333333;
}
li{
display: inline-block;
margin: 0 auto;
border-right: 1px solid black;
border-bottom: 1px solid black
}
li a{
display: inline-block;
background-color: #333333;
color: white;
text-align: center;
padding: 10px;
text-decoration: none;
}
li a:hover{background-color: black;}
<ul role="menubar">
<li></li><li>News</li><li>Sports</li><li>Business</li><li>TV</li><li>Automobiles</li><li>Culture</li><li>Fashion</li><li>Food</li><li>Healthcare</li><li>Tourism</li><li>TECH</li>
</ul>

How can i center this menu?

I have this code for my menu
I got some list items i wanna center in the black box.
The 4 Li-items are floating left to get them inside the black box
nav {
display: block;
width: 900px;
overflow: hidden;
}
nav ul {
width: 900px;
padding: .7em;
float: left;
margin: 0px;
list-style: none;
background: #444;
}
nav li {
float: left;
}
nav a {
float: left;
padding: .8em 1.5em;
text-decoration: none;
color: #555;
text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
font: bold 1.1em/1'trebuchet MS', Arial, Helvetica;
letter-spacing: 1px;
text-transform: uppercase;
border-width: 1px;
border-style: solid;
border-color: #fff #ccc #999 #eee;
background: #c1c1c1;
background: linear-gradient(#f5f5f5, #c1c1c1);
}
nav a:hover,
nav a:focus {
outline: 0;
color: #fff;
text-shadow: 0 1px 0 rgba(0, 0, 0, .2);
background: #fac754;
background: linear-gradient(#fac754, #f8ac00);
}
nav a:active {
box-shadow: 0 0 2px 2px rgba(0, 0, 0, .3) inset;
}
nav li:first-child a {
border-left: 0;
border-radius: 4px 0 0 4px;
}
<nav>
<ul>
<div>
<li><a href='storingsoverzicht.php'>Storingen</a>
</li>
<li><a href='Contacten.php'>Contacten</a>
</li>
<li><a href='nieuwestoring.php'>Nieuwe Storing</a>
</li>
<li><a href='uitloggen.php'>Uitloggen</a>
</li>
</div>
</ul>
</nav>
I like to have the Li-items centered in the middle of the black box.
How do i do this?
Remove float from ul and li and add display: inline-block; and for parent nav add text-align: center; I just corrected css, have a look:
nav {
display: block;
width: 900px;
overflow: hidden;
text-align: center;
}
nav ul {
width: 900px;
padding: .7em;
margin: 0px;
list-style: none;
background: #444;
font-size: 0;
}
nav li {
float: none;
display: inline-block;
font-size: 18px;
}
nav a {
float: left;
padding: .8em 1.5em;
text-decoration: none;
color: #555;
text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
font: bold 1.1em/1'trebuchet MS', Arial, Helvetica;
letter-spacing: 1px;
text-transform: uppercase;
border-width: 1px;
border-style: solid;
border-color: #fff #ccc #999 #eee;
background: #c1c1c1;
background: linear-gradient(#f5f5f5, #c1c1c1);
}
nav a:hover,
nav a:focus {
outline: 0;
color: #fff;
text-shadow: 0 1px 0 rgba(0, 0, 0, .2);
background: #fac754;
background: linear-gradient(#fac754, #f8ac00);
}
nav a:active {
box-shadow: 0 0 2px 2px rgba(0, 0, 0, .3) inset;
}
nav li:first-child a {
border-left: 0;
border-radius: 4px 0 0 4px;
}
<nav>
<ul>
<li><a href='storingsoverzicht.php'>Storingen</a>
</li>
<li><a href='Contacten.php'>Contacten</a>
</li>
<li><a href='nieuwestoring.php'>Nieuwe Storing</a>
</li>
<li><a href='uitloggen.php'>Uitloggen</a>
</li>
</ul>
</nav>
Though your HTML is invalid, since you should not have a div inside you r `ul, Just make the following changes/addition to your css.
Demo : http://jsbin.com/quluhevibe/edit?output
nav ul {
width: 900px;
padding: .7em;
float: left;
margin: 0px;
list-style: none;
background: #444;
text-align: center;
box-sizing: border-box;
}
nav ul div {
display: inline-block;
}

How to center a horizontal navigation bar

I am having trouble centering my navigation bar.
This is the example nav bar I am working with
<style>
/* Begin Navigation Bar Styling */
#nav {
width: 100%;
float: left;
margin: 0 0 3em 0;
padding: 0;
list-style: none;
background-color: #f2f2f2;
border-bottom: 1px solid #ccc;
border-top: 1px solid #ccc; }
#nav li {
float: left; }
#nav li a {
display: block;
padding: 8px 15px;
text-decoration: none;
font-weight: bold;
color: #069;
border-right: 1px solid #ccc; }
#nav li a:hover {
color: #c00;
background-color: #fff; }
/* End navigation bar styling. */
</style>
but as seen, the navigation bar is not centered.
I have tried using auto margins but it doesn't work. How do I go about fixing this?
Add text-align:center; to #nav
Remove float:left; and add display: inline-block; to #nav li
Change to this:
#nav {
width: 100%;
float: left;
margin: 0 0 3em 0;
padding: 0;
list-style: none;
text-align:center;
background-color: #f2f2f2;
border-bottom: 1px solid #ccc;
border-top: 1px solid #ccc;
}
#nav li {
display: inline-block;
}
JSFiddle Demo
display: inline-block;
This should center the navigation bar
try this new css:
#nav {
margin: 0 0 3em 0;
padding: 0;
list-style: none;
background-color: #f2f2f2;
border-bottom: 1px solid #ccc;
position: relative;
border-top: 1px solid #ccc; }
#nav li {
display: inline-block;}
#wrap {
width: 750px;
margin: 0 auto;
background-color: #fff;
text-align: center;
width:100%;}

Navigation incompatible for IE 8

I don't know why this not working properly on ie8.
<div id="bluemenu" class="bluetabs">
<ul>
<li class="">
<a rel="dropmenu3_c" href="company/">Company
<span><img width="11"height="7" src="images/arrow_btn.png"></span>
</a>
</li>
</ul>
</div>
This is the css for the navigation ul
.bluetabs ul {
color: #FFFFFF;
float: right;
font: 13px 'Open Sans','Lucida Grande',Arial,"Verdana sans-serif";
list-style-type: none;
margin: 0;
padding: 0;
text-align: left;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.75);
text-transform: uppercase;
}
This is the css for the navigation li
.bluetabs li {
display: inline;
float: left;
height: 36px;
margin: 0;
padding: 0;
}
This is the css for the navigation li a
.bluetabs li a {
background: url("../images/menu_nav_hover_bg.png") repeat-x scroll 0 0 rgba(0, 0, 0, 0);
color: #FFFFFF;
margin: 0;
padding: 5px 15px;
text-decoration: none;
}
Any CSS hacks that I can make this work on IE 8?
I can kind of reproduce this locally, but not entirely.
Gonna take a shot in the dark...
Try:
.bluetabs li a {
background: #ccc;
color: #FFFFFF;
margin: 0;
padding: 5px 15px;
text-decoration: none;
display: inline-block; /* Add this */
}
/* Add this */
.bluetabs li a > span {
display: inline-block;
}

how to align text to the top in unordered list?

quick html question:
how can I align the text in this unordered list to the top of the div?
<div id="menucontainer">
<ul id="menu">
<li style="vertical-align:top">Home</li>
<li>About</li>
</ul>
</div>
/* TAB MENU
----------------------------------------------------------*/
ul#menu {
border-bottom: 1px #5C87B2 solid;
padding: 0 0 2px;
position: relative;
margin: 0;
text-align: right;
}
ul#menu li {
display: inline-block;
list-style: none;
vertical-align:top;
}
ul#menu li#greeting {
padding: 10px 20px;
font-weight: bold;
text-decoration: none;
line-height: 2.8em;
color: #fff;
}
ul#menu li a {
padding: 10px 20px;
font-weight: bold;
text-decoration: none;
line-height: 2.8em;
background-color: #e8eef4;
color: #034af3;
-webkit-border-radius: 4px 4px 0 0;
-moz-border-radius: 4px 4px 0 0;
border-radius: 4px 4px 0 0;
}
Seems like it would be by default, but this should fix any styles that are getting in the way.
ul#menu li {display: inline-block; vertical-align: top;}
http://jsfiddle.net/uyhHh/