I have been working on centering a menu on a webpage I've been working on. I have tried all the popular answers of display:inline-block, centering the text, margin: 0 auto; however, all attempts have failed. Is there something that I am doing wrong, or putting the css rules under the wrong elements?
Just wanting a horizontal center alignment. i have also used a css reset.
I have included a JS Fiddle at my latest failed attempt. Thank you.
JS Fiddle
<div id="menu">
<ul>
<li>
Apples
</li>
<li>
Oranges
</li>
</ul>
</div>
#menu{
width: 100%;
height: 300px;
}
#menu ul{
width: 100%;
height:100%;
}
#menu li{
display:block;
width:150px;
height:150px;
border-radius:75px;
font-size:20px;
line-height:150px;
text-align:center;
background:#000;
float: left;
}
#menu a{
display:block;
margin: 0 auto;
}
JSfiddle Demo
CSS
#menu {
width: 100%;
height: 300px;
text-align: center;
}
#menu ul {
display: inline-block;
margin:0;
padding:0;
}
#menu li {
width:150px;
height:150px;
border-radius:75px;
font-size:20px;
line-height:150px;
background:#000;
display: inline-block;
}
#menu a {
display:block;
margin: 0 auto;
}
NOTE- Inline block means that spacing is affected by whitespace in the HTML. There are various methods for dealing with this detailed in other SO questions & answers.
Here's a solution, which doesn't depend on your menu size.
Please note <ul> have a default left padding you need to remove (unless you're using a CSS reset).
#menu{
position: absolute;
top: 0;
left: 50%;
transform: translate(-50%, 0);
-webkit-transform: translate(-50%, 0);
}
#menu ul{
width: 100%;
padding: 0;
}
#menu li{
display:block;
width:150px;
height:150px;
border-radius:75px;
font-size:20px;
line-height:150px;
text-align:center;
background:#000;
float: left;
}
#menu a{
display:block;
margin: 0 auto;
}
<div id="menu">
<ul>
<li>Apples
</li>
<li>Oranges
</li>
</ul>
</div>
In #menu li remove float:left; and add margin:auto;.
In order to keep both items in a single line, use display: inline-block; on the #menu li and center the parent ul element with an auto width and margin.
Related
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>
I can't really solve a problem. I have some lines with different options in a list, and I want a background color on them as cursor hovers. But I want it to be the same length in every row, so the background color starts and ends in the same distance horizontally. Sadly, now it lasts only to the end of the text.
Here is the demo
CSS
#leftcol1 ul li{
left: 0px;
top: 30px;
position: relative;
margin: 0px auto;
list-style-type: none;
text-decoration:none;
color:black;
list-style-type:none;
font-family:Arial;
line-height: 20px;
display:block;
white-space:nowrap; }
#leftcol1 ul li a{
list-style-type:none;
color:black;
text-decoration:none; }
#leftcol1Wrapper a:hover{
background-color:darkgray;
width: 200px;
min-width: 200px;
max-width:200px; }
HTML
<div id="leftcol1">
<ul id="leftcol1Wrapper">
<li> </li>
<li> Üzenetek </li>
<li> Értékeléseim</li>
<li> Kérdéseim</li>
<li> Exklúzív értékelések</li>
<li> Válaszok</li>
<li> Értesítések</li>
</ul>
</div>
Thank you.
Add display:block to your #leftcol1 ul li a which is an inline element.
#leftcol1 ul li {
left: 0px;
top: 30px;
position: relative;
margin: 0px auto;
list-style-type: none;
text-decoration:none;
color:black;
list-style-type:none;
font-family:Arial;
line-height: 20px;
display:block;
white-space:nowrap;
}
#leftcol1 ul li a {
display: block;<!--Added-->
list-style-type:none;
color:black;
text-decoration:none;
}
#leftcol1Wrapper a:hover {
background-color:darkgray;
width: 200px;
min-width: 200px;
max-width:200px;
}
DEMO
NOTE: display:block to <a> will help to inherit the width of its parent.
The way I would do this is to add the :hover style to the <li> and set a width on your <ul>.
I have updated your jsFiddle with an example: http://jsfiddle.net/nmvwu4v7/3/
Here is the updated CSS:
#leftcol1 ul li {
left: 0px;
top: 30px;
position: relative;
/* removed margin: 0 auto; so it would not center with the width provided below */
list-style-type: none;
text-decoration:none;
color:black;
list-style-type:none;
font-family:Arial;
line-height: 20px;
display:block;
white-space:nowrap;
width: 200px; /* new */
}
#leftcol1 ul li a{
list-style-type:none;
color:black;
text-decoration:none;
}
#leftcol1Wrapper li:hover { /* was #leftcol1Wrapper a:hover */
background-color:darkgray;
}
How to center this thing ? I've tried couple ways but the nav. bar is not exactly in the middle of the site.
<style>
ul
{
background-color:red;
list-style-type:none;
}
li
{
float:left;
}
a
{
display:block;
text-decoration:none;
width:80px;
padding:20px;
text-align:center;
}
a:link
{
background-color:red;
}
a:hover
{
background-color:white;
}
</style>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
Guys it works, the navbar is now centered correctly, but the text is not this time.
.......................
padding:0 to ul solves the problem.
thank you guys
Try this:
Remove float:left; and add display:inline-block; to li.
Add text-align: center; margin:0 auto; and a width to ul.
Change to this:
ul {
background-color:red;
list-style-type:none;
text-align: center;
margin: 0 auto;
width:80%;
}
li {
display: inline-block;
}
JSFiddle Demo
You can modify you CSS as following:
ul {
background-color:red;
list-style-type:none;
text-align: center;
margin-left: auto;
margin-right: auto;
width:80%;
}
li { display: inline-block;}
a {
display:block;
text-decoration:none;
width:80px;
padding:20px;
text-align:center;
}
a:link {background-color:red;}
a:hover { background-color:white;}
Notice 4 important lines added:
text-align: center;
margin-left: auto;
margin-right: auto;
width:80%; - set the desirable width if not 100%
width can be also specified in absolute units, like e.g. width:800px; alternatively, 2nd and 3rd lines can be replaced with a single statement: margin: 0 auto;
Demo: http://jsfiddle.net/e2TUC/
Hope this will help. Regards,
You can see the image in the middle.
Any idea how to move it down to match please?
My code is included as jsfiddle. There is some space bellow the image also and I couldn't erase it. Is there some way that would move it?
HTML
<div id="menu">
<ul>
<li><span>Link 1</span>
</li>
<li><span>Link 2</span>
</li>
<li class='widthAuto'><img src='https://www.google.co.in/images/icons/product/chrome-48.png' height='80' height="175px" alt="Domu"/>
</li>
<li><span>Link 4</span>
</li>
<li><span>Link 5</span>
</li>
</ul>
</div>
CSS
body {
width: auto;
background-image: url(blue4.jpg);
background-size: cover;
margin: 2px 0 0 0;
}
#menu {
height:210px;
line-height:36px;
margin:0 auto;
text-align:center;
width:800px;
padding:0;
}
#menu ul {
display: inline;
-webkit-padding-start: 0px;
-webkit-margin-before: 2x;
-webkit-margin-after: 0px;
margin:0;
font-size: 0;
}
#menu ul li {
text-algin:center;
display:inline;
font-family:Arial, sans-serif;
font-size:40px;
padding:0 0 10px 0;
margin:0;
text-decoration:none;
background-image:url(../Pics/Buttons/Menu/Menu.gif);
}
#menu a {
text-decoration:none;
color:#000000;
margin:10px 0;
padding:0;
min-width:100px !important;
display:inline-block;
}
#menu a:hover {
font-weight:bolder;
}
span {
width:100%;
font-size:30px;
border-left: 1px solid black;
border-right: 1px solid black;
}
.widthAuto {
width:auto;
}
Try this:
#menu a{
/* other css properties */
height: 80px; /* equal to image height */
line-height: 80px; /* equal to this element height, to get in middle */
vertical-align: middle; /* To keep all the elements in middle of the parent container */
}
Working Fiddle
Try adding the following class
#menu img{
position:relative;
top:20px;
}
Also think about using float: left on the li elements and not display:inline. At least use display:inline-block if you really want to go with display. By setting the display of block elements to inline it really messes the whole picture.
I am trying to center then float:right my top fixed navbar with the content area of my page. Currently the navbar is floated right but is not centered with my content area.
Click to view my subject site.
CSS
#topribbon{
width: 100%;
height: 30px;
background-color: #AA1119 ;
margin: -11px 0px 1em 0px;
position: fixed;
z-index: 9999;
}
#topribbon ul{
padding-top:5px;
text-align:right;
float:right;
position:relative;
margin:0 auto;
width:980px;
}
#topribbon ul li{
float:right;
color:white;
padding:0 10px 0 10px;
list-style:none;
display:block;
line-height:20px;
text-align:center;
cursor:pointer;
width:auto;
}
#topribbon ul li:hover{
color:#5C8FA5;
HTML
<div id="topribbon"> <ul>
<li>Free Ground Shipping on all orders over $99!</li>
<li>Why Us?</li>
<li>Account</li>
<li>Cart</li>
<li>+1-800-555-5555</li>
</ul>
</div>
My first guess is the width of ul element doesn't work.
Try this instead
<div id="topribbon">
<div class="ribbonwrapper" style="width:980px;margin:0 auto;">
<ul>
<li>Free Ground Shipping on all orders over $99!</li>
<li>Why Us?</li>
<li>Account</li>
<li>Cart</li>
<li>+1-800-555-5555</li>
</ul>
</div>
</div>
Put the style of .ribbonwrapper into CSS to make the code tidy :) then you can remove irrelevant width & margin property of #topribbon ul
I'm not sure I'm entirely clear on what you're trying to do, but if what you want is to float:right the UL and then have all the LI's within it centered, you could do this:
#topribbon ul { float:right; text-align: center; }
#topribbon ul li { display: inline-block; }
Be sure to remove the float:right; from the li style block.
If you mean something else by "centered with my content area" can you explain more?
Try with float:none; on the #topribbon ul
Pretty confused what you mean exactly myself... Maybe this is what you want? jsFiddle
#topribbon {
width: 100%;
background-color: #AA1119;
margin: -11px 0px 1em 0px;
position: fixed;
z-index: 9999;
overflow:hidden;
}
#topribbon ul {
padding:10px 0 8px;
position:relative;
margin:0 auto;
width:980px;
background:red;
overflow:hidden;
}
#topribbon ul li {
color:white;
list-style:none;
display:inline-block;
line-height:20px;
cursor:pointer;
width:auto;
padding:0 10px 0 10px;
float:right;
}
#topribbon ul li:hover {
color:#5C8FA5;
}
like others have said use the ul below. I am just as stumped as you however, I am new so if my code is wrong then please feel free to tell me. Thanks
#topribbon ul { float:right; text-align: center; }
#topribbon ul li { display: inline-block; }