I'm having issues with centering the text in my navigation bar. I've tried everything I can think of. Any help would be greatly appreciated.
What I have here is my navigation bars html and css code. I have tried centering it and using other means but am still stuck.
hTML
<div class="content-outer" id="top_nav">
<div class="content-inner">
<ul>
<li>Home<li>
<li>Digital</li>
<li>Film</li>
<li>Timeline</li>
<li>Contact</li>
</ul>
</div>
</div>
css
.container {
width: 1060px;
background: #FFF;
padding: 50px;
margin: 0 auto;
}
#top_nav {
position:fixed;
background:#343642;
font-family: Impact, Charcoal, sans-serif;
}
#top_nav ul {
margin:0;
padding:0px 0;
list-style:none;
width:990px;
text-align: center;
overflow:hidden;
}
#top_nav ul li {
margin:0 56px 0 0;
padding:0;
font-size:1.7em;
text-transform:uppercase;
float:left;
font-weight:bold;
}
#top_nav ul li a {
color:#fef6e9;
text-decoration:none;
display: block;
}
#top_nav ul li a:hover {
color:#ed7163;
text-decoration:none;
}
.content { padding: 10px 0;}
.content-outer { width:100%; float:left; overflow:visible; }
.content-outer .content-inner { width:978px; margin:0 auto; overflow:visible; position:relative; }
Is this what you are looking for?
I removed the extra divs; created a nav element; put margin: 0 auto; on the ul; replaced the float: left on the li with display: inline-block; and there you have it!
HTML
<nav id="top_nav">
<ul>
<li>Home<li>
<li>Digital</li>
<li>Film</li>
<li>Timeline</li>
<li>Contact</li>
</ul>
</nav>
CSS
#top_nav {
position:fixed;
background:#343642;
font-family: Impact, Charcoal, sans-serif;
width: 100%;
}
#top_nav ul {
margin: 0 auto;
padding:0px 0;
list-style:none;
width:990px;
text-align: center;
}
#top_nav ul li {
margin:0 56px 0 0;
padding:0;
font-size:1.7em;
text-transform:uppercase;
display: inline-block;
font-weight:bold;
}
#top_nav ul li a {
color:#fef6e9;
text-decoration:none;
display: block;
}
#top_nav ul li a:hover {
color:#ed7163;
text-decoration:none;
}
Instead of giving just a margin-right for each li element like I can see in your code margin: 0 56px 0 0;, why you don't give equal margin left and right to each li tag?
For example: margin:0 34px 0 34px;
It could be a quick solution.
Check this DEMO:
http://jsfiddle.net/4n9hq/1/
Here is the answer in simplest form
<nav>
<ul>
<li>Home<li>
<li>Digital</li>
</ul>
</nav>
CSS
ul {
display: block;
text-align: center;
}
li {
display: inline-block;
}
In conclusion: set the "ul" tag to display:block and then center text
Related
This question already has answers here:
Does UL have default margin or padding [duplicate]
(2 answers)
Closed 3 years ago.
I'm building my first website with HTML5, and have run into a problem that is driving me insane. I'm trying to perfectly center the items within a horizontal navigation bar at the top of my page. The items are within an unordered list.
I have display:inline-block applied to the list items with text-align:center on the parent. It seems to want to work, but it appears just to the right of the center. I tried taking everything out of a list and simply putting it into a div, and it immediately worked and centered perfectly, but I could not figure out how to efficiently format the individual elements without having them in a list. As soon as I put them back into a list, they shifted back over to the right. I have put a white background on the header to make it easier to see the alignment.
#menu {
width: 960px;
max-height: 90px;
background: #ffffff;
}
#menu ul {
text-align: center;
list-style: none;
padding-top: 18px;
width: 960px;
}
#menu li {
display: inline-block;
vertical-align: middle;
}
#menu li a,
menu li a:visited {
color: #333347;
text-decoration: none;
font-size: 20px;
font-weight: bold;
padding: 0px 13px 0px 13px;
}
<nav id="menu">
<ul>
<li class="menuitem">Home</li>
<li class="menuitem">Gallery</li>
<li class="menuitem">Shop</li>
<li class="menuitem">Contact</li>
</ul>
</nav>
I expected it to center the list, but it appears slightly to the right of the center.
You Try
<nav id="menu">
<ul>
<li class="menuitem">Home</li>
<li class="menuitem">Gallery</li>
<li class="menuitem">Shop</li>
<li class="menuitem">Contact</li>
</ul>
</nav>
CSS:
#menu{
width:960px;
max-height:90px;
background:#ffffff;
}
#menu ul{
text-align:center;
list-style:none;
padding: 18px 0 0 0;
width:960px;
}
#menu li{
display:inline-block;
vertical-align:middle;
}
#menu li a,menu li a:visited{
color:#333347;
text-decoration:none;
font-size:20px;
font-weight:bold;
padding:0px 13px 0px 13px;
}
It will make list at center
I have removed the default padding of ul set by the user agent stylesheet. Also I have centered the nav container using auto margin. hope this helps.
html,body{
margin: 0;
padding: 0;
}
#menu{
width:960px;
max-height:90px;
background:#ffffff;
margin: 0 auto;
}
#menu ul{
text-align:center;
list-style:none;
padding: 18px 0 0;
width:960px;
margin: 0 auto;
}
#menu li{
display:inline-block;
vertical-align:middle;
}
#menu li a,menu li a:visited{
color:#333347;
text-decoration:none;
font-size:20px;
font-weight:bold;
padding:0px 13px 0px 13px;
}
<nav id="menu">
<ul>
<li class="menuitem">Home</li>
<li class="menuitem">Gallery</li>
<li class="menuitem">Shop</li>
<li class="menuitem">Contact</li>
</ul>
</nav>
Try to add these styles:
#menu{
....
margin: 0 auto;
}
#menu ul{
....
padding-left: 0;
}
you need to remove default margin and padding from html elements like this:
*{
margin:0;
padding:0
}
Please use this css.
#menu {
width: 960px;
max-height: 90px;
background: #ffffff;
margin-left:auto;
margin-right:auto;
}
#menu ul {
text-align: center;
list-style: none;
padding-top: 18px;
width: 960px;
padding-left: 0;
}
#menu li{
display:inline-block;
vertical-align:middle;
}
#menu li a,menu li a:visited{
color:#333347;
text-decoration:none;
font-size:20px;
font-weight:bold;
padding:0px 13px 0px 13px;
}
This is due to ul have by default 40px left padding. So you need to remove that left padding from ul.
#menu ul{
...
padding-left:0;
}
/*or*/
#menu ul{
...
padding: 18px 0 0;
}
Put 0 padding on your #menu ul instead of not defining it at all. Like this:
padding: 18px 0px;
I've edited the widths in order to fit it better into the snippet window, and I also added background color to the #menu.
#menu {
width: 100%;
max-height: 90px;
background: #dd0;
}
#menu ul {
text-align: center;
list-style: none;
padding: 18px 0px;
width: 100%;
}
#menu li {
display: inline-block;
vertical-align: middle;
}
#menu li a,
menu li a:visited {
color: #333347;
text-decoration: none;
font-size: 20px;
font-weight: bold;
padding: 0px 13px;
}
<nav id="menu">
<ul>
<li class="menuitem">Home</li>
<li class="menuitem">Gallery</li>
<li class="menuitem">Shop</li>
<li class="menuitem">Contact</li>
</ul>
</nav>
I've change a little your code to make it a little more simplier (Try not to use ID's on CSS, always prefer to use classes instead):
<nav class="menu">
<ul class="menu-list">
<li class="menu-item">Home</li>
<li class="menu-item">Gallery</li>
<li class="menu-item">Shop</li>
<li class="menu-item">Contact</li>
</ul>
</nav>
And for the CSS (The trick is to use the 100% of the width where you put the menu, also prefer to use Flexbox that made a lot more simpler to align items in the DOM):
.menu {
display: flex;
flex-direction: row;
width: 100%;
max-height: 90px;
background: #ffffff;
margin: 0 auto;
}
.menu-list {
display: flex;
flex-direction: row;
justify-content: center;
list-style:none;
padding-top:18px;
width:100%;
padding-left: 0;
}
.menu-item a, .menu-item a:visited{
color: #333347;
text-decoration: none;
font-size: 20px;
font-weight: bold;
padding-left: 1em;
}
Hope this help.
Just trying to figure out why this div won't center. If I can get this working, I'll be set.
JSFiddle
body{
margin:0;
overflow: hidden;
}
ul{
list-style-type:none;
overflow:hidden;
padding:0px;
margin: 0 auto;
width:60%;
text-align:center;
}
ul li{
text-transform:uppercase;
float:left;
}
ul li a{
display: block;
width: 120px;
text-decoration:none;
background-color:#98bf21;
text-align:center;
margin:0px;
padding:10px;
font-weight:Bold;
color:#fff;
font-family:Arial;
letter-spacing: 1px;
}
ul li a:hover{
background-color:#7A991A;
}
#container{
width:100%;
}
<div id = "container">
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
Every time I try to display it in the browser, it looks like this:
Also how do I stretch the navigation bar to 100%? Like from the very left of the page to the right?
You want to center a div without specified width. For that, first remove ul{width:60%;}, then add text-align:center to the Parent and display:inline-block to the Child :
#container{
text-align:center;
}
ul{
display:inline-block;
}
body{
margin:0;
overflow: hidden;
}
ul{
list-style-type:none;
overflow:hidden;
padding:0px;
margin: 0 auto;
text-align:center;
display:inline-block;
}
ul li{
text-transform:uppercase;
float:left;
}
ul li a{
display: block;
width: 120px;
text-decoration:none;
background-color:#98bf21;
text-align:center;
margin:0px;
padding:10px;
font-weight:Bold;
color:#fff;
font-family:Arial;
letter-spacing: 1px;
}
ul li a:hover{
background-color:#7A991A;
}
#container{
width:100%;
text-align:center;
}
<body>
<div id = "container">
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
</body>
1、delete margin: 0 auto;width:60%;from ul and add display: table;margin: 0 auto;like this:
ul{
display:inline-block;
list-style-type:none;
overflow:hidden;
padding:0px;
/* margin: 0 auto;
width:60%; */
display: table;
margin: 0 auto;
}
2、delete margin: 0 auto;width:60%;from ul and add ‘text-align: center’ in #container,‘display: inline-block’ in ul like this:
#container{
width:100%;
text-align: center;
}
ul{
display:inline-block;
list-style-type:none;
overflow:hidden;
padding:0px;
display: inline-block;
}
3、
#container{
width:100%;
position: relative;
}
ul{
display:inline-block;
list-style-type:none;
overflow:hidden;
padding:0px;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
4、
#container{
width:100%;
display: flex;
justify-content: center;
}
ul{
display:inline-block;
list-style-type:none;
overflow:hidden;
padding:0px;
}
5、
#container{
width:100%;
display: flex;
}
ul{
display:inline-block;
list-style-type:none;
overflow:hidden;
padding:0px;
margin: 0 auto;
}
may help you。
choose the best one for you project。
Is this what you are looking for?
All I did was moving the background color to the #container, removed the 60% width, and make the LI elements display: inline-block
ul li{
text-transform:uppercase;
display: inline-block;
}
https://jsfiddle.net/391sz22s/1/
Here is one way of doing it.
Set display: table to the ul element, which will give you a shrink-to-fit block level element that will then center using margin: 0 auto.
Set display: table-cell on the li elements.
The advantage of this approach is that the links will remain on a single line no matter how small you make the page width, which may be the desired effect for some designs.
You could set the with of the ul to 100%, but then your link elements would increase in width, which may not be what you want.
ul {
list-style-type:none;
overflow:hidden;
padding:0px;
margin: 0 auto;
width: auto;
display: table;
}
ul li {
text-transform:uppercase;
display: table-cell;
}
ul li a {
display: block;
width: 120px;
text-decoration:none;
background-color:#98bf21;
text-align:center;
margin:0px;
padding:10px;
font-weight:Bold;
color:#fff;
font-family:Arial;
letter-spacing: 1px;
}
ul li a:hover {
background-color:#7A991A;
}
#container {
width:100%;
}
<div id="container">
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
My issue is that the first element in my nav bar which is "home" has more spacing than the rest of the elements. I can't seem for the life of me to figure out what it is. All I wanted was to have a centered evenly spaced out navigation bar but it's seeming harder than I thought.
CSS
.container {
width: 1060px;
background: #FFF;
padding: 50px;
margin: 0 auto;
}
#top_nav {
position:fixed;
background:#343642;
font-family: Impact, Charcoal, sans-serif;
}
#top_nav ul {
list-style:none;
width:100%;
margin:0 auto;
padding:0;
text-align:center;
overflow:hidden;
}
#top_nav ul li {
margin:0 56px 0 0;
padding: 0;
font-size:1.7em;
text-transform:uppercase;
display: inline-block;
font-weight:bold;
}
#top_nav ul li a {
color:#fef6e9;
text-decoration:none;
display: block;
}
#top_nav ul li a:hover {
color:#ed7163;
text-decoration:none;
}
.content { padding: 10px 0;}
.content-outer { width:100%; overflow:visible; }
.content-outer .content-inner { width:100%; margin:0 auto; overflow:visible; position:relative; }
HTML
<div class="content-outer" id="top_nav">
<ul>
<li>Home<li>
<li>Digital</li>
<li>Film</li>
<li>Timeline</li>
<li>Contact</li>
</ul>
</div>
</div>
That is because you are not properly closing the first li in your list.
<li>Home<li>
should be
<li>Home</li>
Consider running your code through a mark up validator like W3C mark up validator when you are having formatting problems. Many times its something small like a mismatched tag or not properly closing a tag such as in this case.
I've tried making horizontal drop down navigation bars following tutorials, however they are never centered and I can't figure out how to center them. I tried going in the opposite direction and centering my navigation bar first, and then attempting to make it a drop down menu, though this seems to throw everything off. This is the code I have.
EDIT: The problem I am having is that the submenu is displayed when the page is loaded, along with a bullet point, which I'm sure can be fixed by setting the list-style to none, however I'm not sure where in the CSS this should be.
I'm trying to create a menu similar to THIS. I understand this uses joomla and I am not.
#header {
height: 100px;
margin-left: auto;
margin-right: auto;
text-align: center;
}
#content {
max-width: 700px;
margin-left: auto;
margin-right: auto;
padding: 20px;
}
#footer {
height: 85px;
padding-top: 40px;
margin-left: auto;
margin-right: auto;
text-align: center;
}
#menu {
margin: 0 auto;
display: inline-block;
list-style: none;
padding: 0;
border-top: 1 solid #ccc;
border-left: 1 solid #ccc;
border-bottom: 1 solid #ccc;
}
#menu li {
float: left;
}
#menu li a {
display: block;
padding: 10px 10px;
text-decoration: none;
font-weight: bold;
}
#menu li a:hover {
color: #c00;
}
<ul id="menu">
<li>Home
</li>
<li>Kandi
<ul>
<li>Claim Kandi
</li>
</li>
<li>Events
</li>
<li>Artists
</li>
<li>Community
</li>
<li>Merchandise
</li>
</ul>
Add this CSS:
#menu, #menu ul {
margin:0 auto;
padding:0;
}
#menu li {
float: left;
position: relative;
list-style: none;
}
#menu > li:hover > ul {
display: block;
}
#menu > li > ul {
display: none;
position: absolute;
}
#menu li a {
white-space: nowrap;
}
http://jsfiddle.net/tcKvH/1/
use this css
#menu{
position:absolute;
top:150px;
left:8%;
padding:0;
margin:0;
}
#menu ul{
padding:0;
margin:0;
line-height:30px;
}
#menu li{
position:relative;
float:left;
list-style:none;
background:rgba(0,0,0,1);
border-radius:5px;
}
#menu ul ul{
position:absolute;
visibility:hidden;
padding:0;
margin:0;
top:30px;
}
#menu ul li a{
text-align:center;
font:"Arial Black", Arial;
font-size:24px;
color:rgba(255,255,255,9);
width:150px;
height:30px;
display:block;
text-decoration:none;
}
#menu ul li:hover{
background-color:rgba(128,128,128,1);
text-decoration:none;
}
#menu ul li:hover ul{
visibility:visible;
z-index:1;
}
#menu ul li:hover ul li a{
background:rgba(0,0,0,9);
z-index:1;
border-bottom:1px solid rgba(160,160,164,1);
opacity:0.9;
text-decoration:none;
border-radius:5px;
}
#menu ul li ul li:hover{
background:rgba(128,128,128,1);
opacity:0.8;
text-decoration:underline;
}
with this html code
<div id="menu">
<ul>
<li>Home</li></ul>
<ul>
<li>Video <!--This is in main menu-->
<ul>
<li>Technology</li> <!--This is in drop downmenu-->
<li>Tutorial</li> <!--This is in drop downmenu-->
</ul>
</li>
</ul>
I'm having a total brain fart and can't figure out how to display my title/logo and navigation on the same line in my html code. I want the title/logo to be on the left and the nav to be on the right.
Here's my HTML:
<div class="header">
<div class="title">
<h1>Homegrown</h1>
</div><!--/.title-->
<nav class="nav">
<ul>
<li>Home</li>
<li>Services</li>
<li>Contact</li>
</ul>
</nav><!--/.nav-->
</div><!--/.header-->
Here's my CSS:
.title {display:inline; float:left; margin:0 auto;}
.nav {
display:inline;
float:left;
width:100%;
margin:0 1.7%;
padding:25px 2%;
margin-bottom:0;
}
ul {display:inline; float:right; list-style:none;}
li {display:inline;}
For the .nav selector remove width: 100%; , change float: left to float: right and (if you want) change padding:25px 2%; into padding:0 2%;
.nav {
display: inline;
float: right;
/*width:100%;*/
margin: 0 1.7%;
/*padding:25px 2%;*/
padding: 0 2%;
margin-bottom: 0;
}
Demo: http://jsfiddle.net/4svrN/
Remove the padding will work
Demo http://jsfiddle.net/vFgke/
.title {display:inline; float:left; margin:0 auto;}
.nav {
display:inline;
float:right;
margin:0 1.7%;
margin-bottom:0;
}
ul {display:inline; float:right; list-style:none;}
li {display:inline;}
Try
body {
width: 100%;
}
.title {
display:inline;
float:left;
margin:0 auto;
}
.nav {
display: inline;
float: right;
padding: 25px 2%;
}
}
ul {display:inline; float:right; list-style:none;}
li {display:inline;}