Display Logo inline with menu items in HTML and CSS - html

I am trying to display logo at top end in HTML and CSS.
However, the logo image size is not reduced to the specified width and height and I do not see it being overridden anywhere else.
Code is as follows:
/*********************************************************************************/
/* Banner */
/*********************************************************************************/
#banner
{
overflow: hidden;
height: 300px;
position: relative;
background: url(images/pic01.jpg) no-repeat center;
background-size: cover;
}
#banner .image
{
}
#banner p{
position: absolute;
top:30%;
left: 40%;
width: 100px;
padding: 5px;
margin: 5px;
font-family: 'Lobster', cursive;
font-weight: bold;
font-size: 55px;
color: #fff;
}
/** MENU */
#menu-wrapper
{
background: #16a085;
overflow:auto;
}
#menu {
overflow: hidden;
height: 100px;
float:left;
}
#menu ul {
margin: 0;
padding: 0px 0px 0px 0px;
list-style: none;
line-height: normal;
text-align: center;
}
#menu li {
display: inline-block;
}
#menu a {
display: block;
letter-spacing: 1px;
padding: 0px 40px;
text-decoration: none;
text-align: center;
text-transform: uppercase;
font-size: 0.80em;
line-height: 100px;
border: none;
color: #FFF;
}
#menu a:hover, #menu .current_page_item a {
text-decoration: none;
}
#menu .current_page_item a {
background: #1abc9c;
}
#Logo{
width:10px;
height:30px;
float: left;
margin-top: 5px;
}
<div id="wrapper">
<div id="menu-wrapper">
<div id = "Logo" >
<img src="images/Logo.jpg" ></img>
</div>
<div id="menu" class="container">
<ul>
<li class="current_page_item">Homepage</li>
<li>About</li>
<li>Blog</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</div>
/*and so on */
</div>
It looks like this:
White part in the banner is image that is taking up the width despite of having set width attribute to 10 px.

In your CSS. change the style for #Logo to #Logo img{}. you have to set the width to img tag. Not its parent container.
#Logo img{
width:100px;
height:30px;
float: left;
margin-top: 5px;
}
JS Fiddle

Related

Div disappears removing height constraint

I need to build a small website and I'm having problems with the page resizing.
I want that the png image that I put as header is resizable with the whole page (zooming in or out).
I use this css
div#container {
overflow: hidden;
min-width:900px;
width:50%;
margin:0px auto;
background-color:#ffffff;
}
div#header {
background: url('header.png') no-repeat center center;
height:20%;
min-width:900px;
width:inherit;
margin:0px auto;
position:relative;
}
div#upper_nav {
position: absolute;
bottom:0%;
right:0%;
text-align:center;
z-index:9999;
background-color:#e6e6e6;
}
and here the html part
<div id="upper_nav">
<ul id="menu_header">
<li class="active">Home</li>
<li>Link11</li>
<li>Link22</li>
<li>Link33</li>
<li>Link44</li>
</ul>
</div>
Here a link with the test code: https://jsfiddle.net/809ofmvr/1/ I don't know how to add image in the test. Let's say that in the upper part there is the header image and in the bottom part the footer.
I use for the header, a div header and inside the upper_nav div with some links. I think the problem is inside the header div but I don't know where. I also tried to replace position:relative; with display:block; but gives me the same problem.
What can I do to allow a complete resize of the entire page when changes the monitor resolution, zoom, using a smartphone or whatever? With the content it resizes in the correct way.
You can use 20vw to force the header to have a height:
html {
margin:0;
padding:0;
background-color:#ffffff;
}
body {
font-family: Tahoma, Geneva, sans-serif;
color:#000;
text-align:justify;
background-image: url('http://placehold.it/800x200');
background-repeat: no-repeat;
background-attachment:fixed;
background-size: cover;
}
div#container {
overflow: hidden;
min-width:900px;
/*max-width:2000px;*/
width:50%;
margin:0px auto;
background-color:#ffffff;
}
div#header {
background: url('http://placehold.it/800x200') no-repeat center center;
background-size: cover;
height:20vw;
min-width:900px;
width:inherit;
margin:0px auto;
position:relative;
}
div#upper_nav {
position: absolute;
bottom:0%;
right:0%;
text-align:center;
z-index:9999;
background-color:#e6e6e6;
}
div#navigation_left {
padding:5px 5px;
float: left;
width:22%;
background-color:#e6e6e6;
box-shadow: 5px 5px 2px #888888;
padding-bottom: 99999px; margin-bottom: -99999px;
}
div#content {
overflow:auto;
margin-left:25%;
background-color:#e6e6e6;
box-shadow: 5px 5px 2px #888888;
padding-bottom: 99999px; margin-bottom: -99999px;
}
div#footer {
clear:both;
background: url('footer.png') no-repeat center center;
height:105px;
position:relative;
}
div#footer_content {
position: relative;
bottom:-50%;
text-align:center;
z-index:9999;
background-color:#fff;
}
P {
font-size: 17px;
color:#000;
font-family:Tahoma;
}
a {
text-decoration: none;
color:#0066FF;
font-size: 17px;
}
a:hover {
color: #0066FF;
text-decoration: underline
}
h1 {
background-color: #737373;
color: #fff;
font-family: verdana;
font-size: 200%;
}
h2,h3,h4,h5,h6,h7,h8 {
background-color: #737373;
color: #fff;
font-family: verdana;
font-size: 150%;
}
ul#menu_left{
font-size: 11px;
list-style: none;
}
ul#menu_left li {
display: block;
width: 150px;
height: 30px;
margin: 2px;
}
ul#menu_left li a {
color:#000;
display: block;
line-height: 30px;
text-decoration: none;
text-align: left;
}
ul#menu_left li.active, ul#menu_left li:hover {
background-color: #c9c9c9;
}
ul#menu_header {
font-size: 11px;
margin: 0;
padding: 0;
list-style: none;
}
ul#menu_header li {
background-color: #737373;
display: block;
width: 150px;
height: 30px;
margin: 2px;
float: left; /* elementi su singola riga */
}
ul#menu_header li a {
color: #fff;
display: block;
line-height: 30px;
text-decoration: none;
width: 150px;
height: 30px;
text-align: center;
}
ul#menu_header li.active, ul#menu_header li:hover {
background-color: #b1b1b1;
}
<div id="container">
<div id="header">
<div id="upper_nav">
<ul id="menu_header">
<li class="active">Home</li>
<li>Link11</li>
<li>Link22</li>
<li>Link33</li>
<li>Link44</li>
</ul>
</div>
</div>
<div id="navigation_left">
<ul id="menu_left">
<a hfre="#">links</a>
</ul>
</div>
<div id="content"><p>text</p></div>
<div id="footer"><div id="footer_content">text</div></div>
</div>
Also, I added background-size: cover; to make sure the background image always fills the container

how to reduce the height of the navigation bar in css

I'm trying to reduce the height of my navigation bar. when I use min-height property, it adds a scrollbar to the navigation bar which makes it not so appealing. I would like to align the logo to the extreme left and the rest of the contents to the extreme right.I have attached the code below. Please help me to fix it.
.navigation {
background-color: rgba(15,58,114,0.9);
overflow-y: auto;
position: fixed;
left: 0;
top: 0;
text-align: left;
width: 100%;
z-index: 10000;
box-sizing: border-box;
}
.log {
text-align: right;
top: 0;
margin: 0 auto;
right: 0;
}
ul {
padding: 0;
}
li {
color: White;
display: inline-block;
font-family: 'Raleway', sans-serif;
font-size: 18px;
font-weight: 300;
margin: 16px 20px;
text-transform: uppercase;
}
li a {
display: inline-block;
color: White;
text-align: Center;
padding: 14px 16px;
text-decoraton: none;
}
li a:hover {
background-color: rgba(0,0,0,0);
}
#logo {
color: #FFFFFF;
font-family: 'Vollkorn';
font-size: 34px;
padding: 0px 50px;
text-align: center;
}
<div class="navigation">
<ul>
<li id="logo">LOGO</li>
<div class="log">
<li>LOGIN</li>
<li>JOIN WITH US</li>
</div>
</ul>
</div>
your logo and below that your menu list - that's the problem
make its side by side
.navigation ul {
display: flex;
}
.navigation {
background-color: rgba(15,58,114,0.9);
overflow-y: auto;
position: fixed;
left: 0;
top: 0;
/*text-align: left;*/
width: 100%;
z-index: 10000;
box-sizing: border-box;
}
.navigation ul {
display: flex; /* new line added */
}
#logo {
flex: 2; /* new line added */
}
.log {
/*text-align: right;*/
top: 0;
margin: 0 auto;
right: 0;
display: flex; /* new line added */
flex: 8; /* new line added */
justify-content: flex-end; /* new line added */
}
ul {
padding: 0;
}
li {
color: White;
display: inline-block;
font-family: 'Raleway', sans-serif;
font-size: 18px;
font-weight: 300;
margin: 16px 20px;
text-transform: uppercase;
}
li a {
display: inline-block;
color: White;
text-align: Center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: rgba(0,0,0,0);
}
#logo {
color: #FFFFFF;
font-family: 'Vollkorn';
font-size: 34px;
padding: 0px 50px;
text-align: center;
}
<div class="navigation">
<ul>
<li id="logo">LOGO</li>
<div class="log">
<li>LOGIN</li>
<li>JOIN WITH US</li>
</div>
</ul>
</div>
You had the scroll bar because the contents were too big and you had overflow-y: auto; Overflow controls the behavior when the contents are too big for the parent div size.
The float property can be used to move elements to different sides of the viewer in a consistent manner.
Your code is still not responsive and will not display correctly on screens smaller that 365px wide. The support range will get smaller if you add more menu items.
There are a lot of resources you can check out for making a responsive menu
Is this what you wanted?
body {
margin: 0 auto;
padding: 0
}
.navigation {
background-color: rgba(15, 58, 114, 0.9);
position: fixed;
height: 60px;
line-height: 60px; /* needs to be the same height as your navigation div */
top: 0;
width: 100%;
z-index: 10000;
}
.log {
float: right; /* pulls the div to the right side */
}
ul {
padding: 0;
margin: 0 auto;
}
li {
color: White;
display: inline-block;
font-family: 'Raleway', sans-serif;
font-size: 18px;
font-weight: 300;
margin: 0 auto;
text-transform: uppercase;
}
li a {
color: White;
text-align: Center;
padding: 0 10px;
text-decoration: none;
}
li a:hover {
color:wheat;
}
#logo {
color: #FFFFFF;
font-family: 'Vollkorn';
font-size: 34px;
padding: 0px 10px;
float: left; /* pulls the div to the left side */
}
<div class="navigation">
<ul>
<li id="logo">LOGO</li>
<div class="log">
<li>LOGIN</li>
<li>JOIN WITH US</li>
</div>
</ul>
made Changes to your HTML and CSS
div with class log is a block level element, means it occupies 100% width of the page, make them display:inline-block and float it to right, make the logo wrap in a div and float it to left. since we are having two float elements navigation bar height gets collapsed to avoid it we need to add clearfix
HTML
<div class="navigation clearfix">
<div class="logo">LOGO</div>
<div class="menu">
<ul>
<li>LOGIN</li>
<li>JOIN WITH US</li>
</ul>
</div>
</div>
CSS
.navigation {
background-color: rgba(15,58,114,0.9);
overflow-y: auto;
position: fixed;
left: 0;
top: 0;
text-align: left;
width: 100%;
z-index: 10000;
box-sizing: border-box;
}
.logo{
float:left;
display:inline-block;
}
.logo a{
padding:8px 2px;
color:#fff;
display:block;
}
.menu{
float:right;
}
.clearfix::after{
content:"";
display:table;
width:100%;
}
.menu ul{
padding:0px;
list-style-type:none;
margin:0;
}
.menu ul li{
display:inline-block;
}
.menu ul li a{
color:#fff;
padding:8px 2px;
display:block;
}
Link For reference
style accordingly
hope this helps..
I've done a little change in the style:
.navigation {
background-color: rgba(15, 58, 114, 0.9);
overflow-y: auto;
position: fixed;
left: 0;
top: 0;
right: 0;
text-align: left;
width: 100%;
z-index: 10000;
box-sizing: border-box;
}
.log {
margin: 0 auto;
float: right;
}
ul {
padding: 0;
}
li {
color: White;
list-style: none;
font-family: 'Raleway', sans-serif;
font-size: 18px;
font-weight: 300;
text-transform: uppercase;
float: right;
}
li a {
display: inline-block;
color: White;
text-align: Center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: rgba(0, 0, 0, 0);
}
#logo {
color: #FFFFFF;
font-family: 'Vollkorn';
font-size: 34px;
padding: 0px 50px;
text-align: center;
float: left !important;
margin-top: -10px;
}
<div class="navigation">
<ul>
<li id="logo">LOGO</li>
<div class="log">
<li>LOGIN</li>
<li>JOIN WITH US</li>
</div>
</ul>
</div>

Center photo and text in height <ul>

How am I able to center the photo with the text, without minimizing the photo itself?
I've tried; max-height: xx, but that wasn't it.
The photo is centered in height, but not the text. How's this possible?
http://jsfiddle.net/gLpqoamn/
.hoyre{
background-color:#f19f00;
}
.hoyre:hover{
background-color:#d98500;
}
header{
background-color: white;
}
.logo{
width: 57px;
height: 34px;
padding: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #fff;
text-transform: uppercase;
width: 100%;
color: black;
}
li {
float: left;
}
li a {
display: block;
text-align: center;
padding: 20px 20px;
text-decoration: none;
color: black;
}
li a:hover {
color: #f19f00;
}
body{
margin:0;
font-family: 'Roboto', sans-serif;
background-color: #333;
}
.container{
max-width:1300px;
margin-left:auto;
margin-right:auto;
}
.active {
position: relative;
}
</style>
<body>
<header>
<div class="container">
<ul>
<li><img src="http://i.imgur.com/QAEzQxp.png" class="logo"></li>
<li>Home</li>
<li>Contact</li>
<li>About</li>
<li class="hoyre" style="float:right;">Donate</li>
</ul>
</div>
</header>
</body>
I would use flex on the ul, then you can use align-items to vertically center or position the children. A margin-left: auto on the last link will push it to the right, then you can move the background color to the link to keep it from stretching the height of the parent
.hoyre {
margin-left: auto;
background-color: #f19f00;
min-height: 100%;
align-self: stretch;
display: flex;
align-items: center;
}
.hoyre:hover {
background-color: #d98500;
}
header {
background-color: white;
}
.logo {
width: 57px;
height: 34px;
padding: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #fff;
text-transform: uppercase;
width: 100%;
color: black;
display: flex;
align-items: center;
}
li {}
li a {
display: block;
text-align: center;
padding: 20px 20px;
text-decoration: none;
color: black;
}
li a:hover {
color: #f19f00;
}
body {
margin: 0;
font-family: 'Roboto', sans-serif;
background-color: #333;
}
.container {
max-width: 1300px;
margin-left: auto;
margin-right: auto;
}
.active {
position: relative;
}
</style>
<body>
<header>
<div class="container">
<ul>
<li>
<img src="http://i.imgur.com/QAEzQxp.png" class="logo">
</li>
<li>Home</li>
<li>Contact</li>
<li>About</li>
<li class="hoyre">Donate</li>
</ul>
</div>
</header>
</body>
For using height:100% in your css you should define parent element height first. then you change li element to behave like table and fill 100% height.
Update your styles like this(new styles have comment in front of them):
li {
float: left;
height: 100%; /*fill height */
display: table; /* behave as table*/
}
li a {
display: table-cell; /* behave as table-cell then you can use vertical alignment*/
vertical-align: middle;
text-align: center;
padding: 20px 20px;
text-decoration: none;
color: black;
height: 100% ; /*fill height */
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #fff;
text-transform: uppercase;
width: 100%;
color: black;
height: 80px; /*define height for using height:100% for child elements */
}

HTML/CSS Margin-top for text moving entire background?

I'm trying to move certain text down from the top of it's container, but it's moving the entire container down.
#import url("https://fonts.googleapis.com/css?family=Open+Sans:400,300");
*
{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body
{
font-family: 'Open Sans';
background-color: lightgray;
}
a
{
text-decoration: none;
color: #fff;
}
div#header
{
background-image: url("header_img.png");
background-size: cover;
background-repeat: no-repeat;
width: 80%;
height: 220px;
margin: 0 auto;
margin-top: 50;
color: #fff;
font-size: 22px;
}
div#header span
{
font-weight: 300;
}
div#header h3
{
text-align: center;
padding-top: 85px;
}
div#header h4
{
text-align: center;
}
/*
#222222 - top nav
#F5F5F5 - sidebar background color
#FFF - sidebar link hover background color
#63B7E8 - sidebar nav link hover text color
#D8D8D8 - sidebar nav text color
50px top margin
50px bottom margin
*/
/* Navigation */
/* Nav Title */
.nav
{
background-color: dimgray;
width: 80%;
height: 40px;
margin: 0 auto;
}
/* Remove bullet points on list */
.nav ul
{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: dimgray;
}
.nav ul li
{
float: left;
}
.nav ul li a
{
display: block;
color: #fff;
text-align: center;
padding: 14px; 16px;
text-decoration: none;
}
.nav ul li a:hover
{
color: #099;
background-color: #fff;
}
.home-content
{
width: 80%;
height: 350px;
margin: 0 auto;
margin-top: 30px;
text-align: center;
font-size: 22px;
background-color: #fff;
}
.home-content span
{
font-weight: 300;
}
/* This is the Text I want to push down from the top of it's container.*/
.title
{
margin-top: 15px;
}
/* I also tried this:
.home-content h3
{
margin-top: 15px;
}
*/
<html>
<head>
<title>Dashboard</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="container">
<div id="header">
<h3>Sample<span> Text</span></h3>
<h4>Sample. Text. Here.</h4>
</div>
</div>
<div class="container">
<!-- Navigation -->
<div class="nav">
<ul>
<li><a class="active" href="#">Home</a></li>
<li>Menu1</li>
<li>Menu2</li>
<li>Menu3</li>
</ul>
</div>
<div class="home-content">
<div class="title">
<h3>Default<span> Text</span></h3>
</div>
</div>
</div>
</body>
</html>
Any tips to push just the text down? I'd appreciate it. Thanks. I have also tried padding, but it gives me the same result.
Set the position of .title to relative and use the top property instead of margin-top.
Like so:
https://jsfiddle.net/kbgb0qvr/
I was able to do it with padding on your parent container, and no margin on the child. I don't know that this is the best way to do it though.
#import url("https://fonts.googleapis.com/css?family=Open+Sans:400,300");
*
{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body
{
font-family: 'Open Sans';
background-color: lightgray;
}
a
{
text-decoration: none;
color: #fff;
}
div#header
{
background-image: url("header_img.png");
background-size: cover;
background-repeat: no-repeat;
width: 80%;
height: 220px;
margin: 0 auto;
margin-top: 50;
color: #fff;
font-size: 22px;
}
div#header span
{
font-weight: 300;
}
div#header h3
{
text-align: center;
padding-top: 85px;
}
div#header h4
{
text-align: center;
}
/*
#222222 - top nav
#F5F5F5 - sidebar background color
#FFF - sidebar link hover background color
#63B7E8 - sidebar nav link hover text color
#D8D8D8 - sidebar nav text color
50px top margin
50px bottom margin
*/
/* Navigation */
/* Nav Title */
.nav
{
background-color: dimgray;
width: 80%;
height: 40px;
margin: 0 auto;
}
/* Remove bullet points on list */
.nav ul
{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: dimgray;
}
.nav ul li
{
float: left;
}
.nav ul li a
{
display: block;
color: #fff;
text-align: center;
padding: 14px; 16px;
text-decoration: none;
}
.nav ul li a:hover
{
color: #099;
background-color: #fff;
}
.home-content
{
width: 80%;
height: 350px;
margin: 0 auto;
margin-top: 30px;
text-align: center;
font-size: 22px;
background-color: #fff;
padding-top: 15px;
}
.home-content span
{
font-weight: 300;
}
/* This is the Text I want to push down from the top of it's container.*/
.title
{
/*margin-top: 15px;*/
}
/* I also tried this:
.home-content h3
{
margin-top: 15px;
}
<div class="container">
<div id="header">
<h3>Sample<span> Text</span></h3>
<h4>Sample. Text. Here.</h4>
</div>
</div>
<div class="container">
<!-- Navigation -->
<div class="nav">
<ul>
<li><a class="active" href="#">Home</a></li>
<li>Menu1</li>
<li>Menu2</li>
<li>Menu3</li>
</ul>
</div>
<div class="home-content">
<div class="title">
<h3>Default<span> Text</span></h3>
</div>
</div>
</div>
Try this-
#import url("https://fonts.googleapis.com/css?family=Open+Sans:400,300");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Open Sans';
background-color: lightgray;
}
a {
text-decoration: none;
color: #fff;
}
div#header {
background-image: url("header_img.png");
background-size: cover;
background-repeat: no-repeat;
width: 80%;
height: 220px;
margin: 0 auto;
margin-top: 50;
color: #fff;
font-size: 22px;
}
div#header span {
font-weight: 300;
}
div#header h3 {
text-align: center;
padding-top: 85px;
}
div#header h4 {
text-align: center;
}
/*
#222222 - top nav
#F5F5F5 - sidebar background color
#FFF - sidebar link hover background color
#63B7E8 - sidebar nav link hover text color
#D8D8D8 - sidebar nav text color
50px top margin
50px bottom margin
*/
/* Navigation */
/* Nav Title */
.nav {
background-color: dimgray;
width: 80%;
height: 40px;
margin: 0 auto;
}
/* Remove bullet points on list */
.nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: dimgray;
}
.nav ul li {
float: left;
}
.nav ul li a {
display: block;
color: #fff;
text-align: center;
padding: 14px;
16px;
text-decoration: none;
}
.nav ul li a:hover {
color: #099;
background-color: #fff;
}
.home-content {
width: 80%;
height: 350px;
margin: 0 auto;
margin-top: 30px;
text-align: center;
font-size: 22px;
background-color: #fff;
}
.home-content span {
font-weight: 300;
}
/* This is the Text I want to push down from the top of it's container.*/
.title {
position: relative;
top: 15px;
}
/* I also tried this:
.home-content h3
{
margin-top: 15px;
}
*/
<body>
<div class="container">
<div id="header">
<h3>Sample<span> Text</span></h3>
<h4>Sample. Text. Here.</h4>
</div>
</div>
<div class="container">
<!-- Navigation -->
<div class="nav">
<ul>
<li><a class="active" href="#">Home</a>
</li>
<li>Menu1
</li>
<li>Menu2
</li>
<li>Menu3
</li>
</ul>
</div>
<div class="home-content">
<div class="title">
<h3>Default<span> Text</span></h3>
</div>
</div>
</div>
</body>
Instead of using padding or margin, try using position and top attributes on .title as done in the snippet above.

How do i center my navigation bar?

I have a script here for my navigation bar:
<style type="text/css">
/* Navigation Bar */
#nav_bar {
display:inline-block;
height:50px;
}
#nav_bar ul {
display:inline-block;
list-style-type: none;
border: 1px solid red;
width: 565px;
height: 100%;
text-align: center;
padding: 0;
margin: 0;
}
#nav_bar li {
display:inline;
height:100%;
padding: 0;
margin: 0;
}
#nav_bar a:hover {
background-color: #000000;
}
#nav_bar a {
display:inline-block;
height: 100%;
color:white;
text-decoration:none;
line-height: 50px;
padding: 0 1em 0 1em;
background-color: #900000;
}
</style>
</font>
I'm having trouble trying to get it displayed in the centre of the page, how can I do it?
I've looked into "display: inline-block;" and "position: relative" and couldn't find a code that worked
the html part of my nav bar is as follows (in regards to your comments) I hope it helps! :)
<div id="nav_bar">
<ul>
<li> Home </li>
<li> Forums </li>
<li> Shipping Info </li>
<li> Contact us </li>
<li> About us </li>
</ul>
</div>
Give it a width and auto margins and make sure its a block level element.
By default a 'div' is a block level element so you can remove this rule.
You must set a width or the menu with expand to the width of its container.
#nav_bar {
display:block;
height:50px;
margin: 0 auto;
width: 567px; /* or whatever you require */
}
Example:
http://jsfiddle.net/29FRa/
#nav_bar {
height:50px;
width:800px;
margin:0 auto;
}
HTML:
<html>
<body>
<div id="#nav_bar"></div>
</body>
</html>
Use text-align: center; on your #nav_bar and be sure it is a block-level element.
http://jsfiddle.net/TKMeU/
A total of six kinds of methods:
1、Margin and width to achieve horizontal center
#nav_bar {
height:50px;
}
#nav_bar ul {
list-style-type: none;
border: 1px solid red;
width: 565px;
height: 100%;
text-align: center;
padding: 0;
margin-left: auto;
margin-right: auto;
}
please view the demo.
2、use the inline-block, like this:
#nav_bar {
height:50px;
text-align: center;
}
#nav_bar ul {
list-style-type: none;
display: inline-block;
border: 1px solid red;
width: 565px;
height: 100%;
text-align: center;
padding: 0;
text-align: center;
font-size: 0;
letter-spacing: -4px;
word-spacing: -4px;
}
#nav_bar li {
margin: 0 5px;
display: inline-block;
*display: inline;
zoom:1;
letter-spacing: normal;
word-spacing: normal;
font-size: 12px;
}
please view the demo.
3、use the float,like this:
#nav_bar {
float: left;
width: 100%;
overflow: hidden;
position: relative;
}
#nav_bar ul {
list-style-type: none;
width: 565px;
height: 50px;
height: 100%;
padding: 0;
clear: left;
float: left;
position: relative;
left: 50%;/*整个分页向右边移动宽度的50%*/
text-align: center;
}
#nav_bar li {
margin: 0 5px;
display: block;
height: 50px;
float: left;
position: relative;
right: 50%;/*将每个分页项向左边移动宽度的50%*/
}
#nav_bar a:hover {
background-color: #000000;
}
#nav_bar a {
display:block;
height: 100%;
color:white;
text-decoration:none;
line-height: 50px;
padding: 0 1em 0 1em;
background-color: #900000;
}
Please view the demo.
Other methods, you can click here.