How do I center list items inside a ul without using extra divs or elements. I have the following. I thought text-align:center would do the trick. I can't seem to figure it out.
<style>
ul {
width:100%;
background:red;
height:20px;
text-align:center;
}
li {
display:block;
float:left;
background:blue;
color:white;
margin-right:10px;
}
</style>
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
Check jsfiddle http://jsfiddle.net/3Ezx2/1/
write display:inline-block instead of float:left.
li {
display:inline-block;
*display:inline; /*IE7*/
*zoom:1; /*IE7*/
background:blue;
color:white;
margin-right:10px;
}
http://jsfiddle.net/3Ezx2/3/
A more modern way is to use flexbox:
ul{
list-style-type:none;
display:flex;
justify-content: center;
}
ul li{
display: list-item;
background: black;
padding: 5px 10px;
color:white;
margin: 0 3px;
}
div{
background: wheat;
}
<div>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</div>
li{
display:table;
margin:0px auto 0px auto;
}
This should work.
Looks like all you need is text-align: center; in ul
I have run into this issue before and found that sometimes padding is the issue.
By removing padding from the ul, any li's set to inline-block will be nicely centred:
* {
box-sizing: border-box;
}
ul {
width: 120px;
margin: auto;
text-align: center;
border: 1px solid black;
}
li {
display: inline-block;
}
ul.no_pad {
padding: 0;
}
p {
margin: auto;
text-align: center;
}
.break {
margin: 50px 10px;
}
<div>
<p>With Padding (Default Style)</p>
<ul class="with_pad">
<li>x</li>
<li>x</li>
<li>x</li>
<li>x</li>
</ul>
<div class="break"></div>
<p>No Padding (Padding: 0)</p>
<ul class="no_pad">
<li>x</li>
<li>x</li>
<li>x</li>
<li>x</li>
</ul>
<div>
Hope that helps anyone running into this same issue :)
Cheers,
Jake
Another way to do this:
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
ul {
width: auto;
display: table;
margin-left: auto;
margin-right: auto;
}
ul li {
float: left;
list-style: none;
margin-right: 1rem;
}
http://jsfiddle.net/f6qgf24m/
ul {
width: 100%;
background: red;
height: 20px;
display: flex;
justify-content: center;
align-items: center;
}
li {
background: blue;
color: white;
margin-right: 10px;
}
I added the div line and it did the trick:
<div style="text-align:center">
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
</div>
I had a problem slimier to yours I this quick and its the best solution I have found so far.
What the output looks like
Shows what the output of the code looks like
The borders are just to show the spacing and are not needed.
Html:
<div class="center">
<ul class="dots">
<span>
<li></li>
<li></li>
<li></li>
</span>
</ul>
</div>
CSS:
ul {list-style-type: none;}
ul li{
display: inline-block;
padding: 2px;
border: 2px solid black;
border-radius: 5px;}
.center{
width: 100%;
border: 3px solid black;}
.dots{
padding: 0px;
border: 5px solid red;
text-align: center;}
span{
width: 100%;
border: 5px solid blue;}
Not everything here is needed to center the list items.
You can cut the css down to this to get the same effect:
ul {list-style-type: none;}
ul li{display: inline-block;}
.center{width: 100%;}
.dots{
text-align: center;
padding: 0px;}
span{width: 100%;}
The thing is ul tag comes with default margin and padding, therefore all you need to do is:
ul {
margin: 0px;
padding: 0px;
}
Neither text-align:center nor display:inline-block worked for me on their own, but combining both did:
ul {
text-align: center;
}
li {
display: inline-block;
}
In Bootstrap (4) use display: inline-flex, like so:
li {
display: inline-flex;
/* ... */
}
// Ul css
ul{
width: 100%;
justify-content: center;
list-style-type: none;
display: flex;
}
// ul li css
ul li{
display: inline;
}
I overcame the problem with this solution.
HTML:
<div class="list-wrapper">
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
</ul>
</div>
CSS:
.list-wrapper {
text-align: -webkit-center;
}
.list-wrapper ul {
display:block;
}
Related
Currently I have my website with the logo in the middle and the navigation on the right as shown below. However I want it to have social icons on the left, logo in the middle, and navigation on the right, therefore how do I do this ? I keep trying and failing.
Any help is appreciated, thanks.
HTML
<div id="nav-div">
<ul>
<h1>JOSH TAYLOR</h1>
<li>Contact</li>
<li>CV</li>
<li>Portfolio</li>
<li>Home</li>
</ul>
</div>
CSS
#nav-div {
opacity: 1;
font-size: 15px;
}
#nav-div h1{
color: lightskyblue;
cursor: pointer;
margin-top: 0px;
margin-bottom: 0px;
font-size: 30px;
float: left;
margin-left: 460px;
width: 200px;
}
#nav-div h1:hover{
color: white;
transition:all 0.40s;
}
#nav-div ul{
margin: 0px;
padding: 0px;
width: 100%;
height: 80px;
background: ;
line-height: 80px;
border-bottom: px solid black;
}
#nav-div ul a{
text-decoration: none;
color: lightskyblue;
padding: 25px;
}
#nav-div ul a:hover{
color:white;
transition:all 0.40s;
font-style:italic;
}
#nav-div ul li {
list-style-type: none;
display: inline-block;
float: right;
font-style:normal;
font-size: 13px;
margin-top: 1px;
}
Using flexbox you can have a flex div or header and enclose the three elements within and give it a justify-content:space-around Fiddle the rest is for presentation purpose. Hope this helps.
.flex{
display:flex;
justify-content: space-around;
height: 100px;
align-items: center;
border: 1px solid black;
}
.flex .social{
list-style: none;
padding: 0;
}
.flex .social li{
display: inline-block;
}
.flex nav ul{
list-style: none;
align-items: center;
padding: 0;
}
.flex nav ul li{
display: inline-block;
}
.flex nav ul li a{
text-decoration: none;
}
<header class="flex">
<ul class="social">
<li>SOCIAL</li>
<li>SOCIAL</li>
<li>SOCIAL</li>
</ul>
<img src="http://placehold.it/100x100" alt="">
<nav>
<ul>
<li>Menu 1</li>
<li>Menu 2</li>
<li>Menu 3</li>
<li>Menu 4</li>
</ul>
</nav>
</header>
You could use Bootstrap and enclose all three in a div with class row and add the class col-md-4 to all sub-divs(if you want a better result for mobile users replace md with xs).
My li's aren't in one line. How do I fix this?
I need to have 3 columns and on the same line.
How do I do this?
Thanks!
example
My HTML:
<main>
<header>
<nav>
<ul>
<li>Movies</li>
<li>Quote per category</li>
<li>Home</li>
</ul>
</nav>
<img src="assets/img/banner.png" alt="banner" title="banner">
</header>
<section>
<h1>Movie db search</h1>
<article>
<ul>
</ul>
</article>
</section>
<footer>
<p>© copyright Robin Hennebel. Data fetched from <a href="https://www.themoviedb.org/" title="themoviedb"
target="_blank">Tmdb</a> and Chucknorris.io</p>
</footer>
</main>
So you can see my li's where i put a picture and a paragraph via fetch.
I need them to be in one line in 3 columns.
My CSS:
article ul li {
display:inline-block;
width: 30%;
}
article li:nth-child(even){
background-color: #A0C0FF;
border-radius: 25px;
}
article li:nth-child(odd){
background-color: #F897CA;
border-radius: 25px;
margin-left: 1%;
}
figure img{
width: 35%;
display:block;
margin-left:auto;
margin-right: auto;
margin-top: 1%;
}
figcaption{
padding: 2%;
}
figure {
display: flex;
flex-direction: column;
align-items: center;
}
article{
display: flex;
flex-direction: column;
justify-content: space-between;
}
Does anyone has a solution?
METHOD 1: Use flexbox, that's the cleanest & most modern answer. You already use flex elsewhere, and you can make use of all of its magical powers...
ul {
display: flex;
flex-wrap: wrap;
}
ul li {
flex: 1 0 33.3%;
}
<ul>
<li>Apples</li>
<li>Oranges</li>
<li>Bananas</li>
<li>Pears</li>
<li>Mangos</li>
<li>Peaches</li>
<li>Plums</li>
</ul>
However support for flexbox in older browsers is iffy. Test it in IE11, does it work?
METHOD 2: You can resort to a more classic way, using display:inline-block (fyi I use width: 30%, for some odd reason 33.3% results in two columns)...
ul li {
width: 30%;
display: inline-block;
}
<ul>
<li>Apples</li>
<li>Oranges</li>
<li>Bananas</li>
<li>Pears</li>
<li>Mangos</li>
<li>Peaches</li>
<li>Plums</li>
</ul>
Method 3: Or using float: left, my personal favourite over the years, before becoming a flex addict ...
ul li {
width: 33.3%;
float: left;
}
<ul>
<li>Apples</li>
<li>Oranges</li>
<li>Bananas</li>
<li>Pears</li>
<li>Mangos</li>
<li>Peaches</li>
<li>Plums</li>
</ul>
replace css code like this
your li in header nav
nav ul li {
display:inline-block;
width: 30%;
}
nav li:nth-child(even){
background-color: #A0C0FF;
border-radius: 25px;
}
nav li:nth-child(odd){
background-color: #F897CA;
border-radius: 25px;
margin-left: 1%;
}
https://jsfiddle.net/9twyyafs/
I have a simple layout like this:
<html>
<body>
<div>
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
</body>
But I can't figure out how to center this menu horizontally and dynamically. The CSS is in here http://codepen.io/syarifphmy/pen/egvsk. I'm thinking it has something to do with the width of the < ul >. But if I state it in a fixed size say for example 500px, then it won't be dynamic. I want it to fit according to the lists.
Use display: table;
ul{
display: table;
list-style: none;
padding:0;
margin:0 auto;
}
Working example: http://codepen.io/anon/pen/zpxyD
Also, unrelated: I would recommend giving your anchors display: block, so they use the entire li space. Right now you have to find the text inside the li to actually click the link.
You can use text-align to center (non-floated) inline-block children:
ul{
text-align: center;
}
li{
display: inline-block;
float: none; /* default value */
}
*{
margin:0;
}
li:first-child{
border-radius:5px 0 0 5px;
}
li:last-child{
border-radius:0 5px 5px 0;
}
ul{
text-align: center;
list-style: none;
padding:0;
}
a{
color:white;
text-decoration:none;
font-size:5vmin;
}
li{
display:inline-block;
background-color: #98bf21;
padding:5px 20px;
}
li:hover{
background-color: #7A991A;
}
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
I can't see the codepen snippit here, so i made my own fiddle. Not sure if you wanted horiz or vertical items, so i made both.
div#one { width: 100%; overflow: hidden; }
div#one ul { list-style: none; position: relative; float: left; display: block; left: 50%; }
div#one ul li { position: relative; float: left; display: block; right: 50%; margin-right: 20px; }
div#two { width: 100%; overflow: hidden; }
div#two ul { list-style: none; position: relative; float: left; display: block; left: 50%; }
div#two ul li { position: relative; display: block; right: 50%; text-align: center; }
This fires on the markup:
<div id="one">
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
<div id="two">
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>A REALLY LONG TITLE</li>
</ul>
</div>
demo: http://jsfiddle.net/x40h8Ldh/1/
Be sure you are using a STRICT DTD as some of these concepts will not work cross-browser with just <html> declared.
I have 5 menu items, that I'm having difficulty positioning in line with the background.
The menu text seems to bunch together in the middle, rather than listening to the "width" property to space them out in the correct places.
JSFIDDLE:
http://jsfiddle.net/nmHSD/
HTML:
<div id="menu">
<div class="table">
<ul>
<li>Home
</li>
<li>What we do</li>
<li>Our clients</li>
<li>Testimonials</li>
<li>Contact us</li>
</ul>
</div>
</div>
CSS:
#menu {
top:13px;
left:0px;
position:relative;
height:60px;
width:523px;
background-image:url('http://www.kitoit.com/new/img/menu-buttons.png');
}
.table {
display: table;
/* Allow the centering to work */
margin: 0 auto;
}
#menu ul {
width: 696px;
list-style: none;
padding-top: 20px;
}
#menu ul li {
display: inline;
}
You mean more like this? I changed somethings in your css..
I used float:left; on the <li> instead of inline and because you have 5 list-items i gave them a width:20%;.
Your new css: (less css but the outcome is what you want.)
#menu {
top:13px;
left:0px;
height:60px;
width:523px;
background-image:url('http://www.kitoit.com/new/img/menu-buttons.png');
}
#menu ul {
width:523px;
list-style: none;
padding-top: 30px;
padding-left:0;
}
#menu ul li {
float:left;
width: 20%;
text-align:center;
}
DEMO
i think following code can help for you:
replace code:
#menu ul {
list-style: none;
float: left;
padding: 20px 0 0 0;
}
and also
#menu ul li {
float: left;
width: 104px;
text-align: center;
}
You need to use display: inline-block; on <li> elements in order to be able to use the width, something like that like that:
#menu ul li {
display: inline-block;
width: 150px;
}
Going off of the note at the top of this tutorial (http://css-tricks.com/centering-list-items-horizontally-slightly-trickier-than-you-might-think/), I tried to center a list of two links in the page, but it is centering only the first list item, not the entire list.
li{
font-family: Futura, Arvo, sans-serif;
display: inline-block;
}
ul{
text-align: center;
}
div#nav-list {
left: 0;
right: 0;
margin-right: auto;
margin-left: auto;
width: auto;
height: auto;
background: #E8E8E8;
}
"nav-list" is the container for the navigation bar, shown in the picture colored grey.
Here is the issue- you can see that "about" is centered, but not the entire list.
Thanks in advance!
Edit: Here's the HTML:
<div id="center_content">
<h1 id="page-heading">Title</h1>
<hr id="first-rule"></hr>
<div id="nav-list">
<ul>
<li>about</li> <li>work</li>
</ul>
</div>
<hr></hr>
<p>Here is a paragraph. </p>
</div>
Remove the list padding. By default, list have a 40px padding-left. Try to use ul{ padding:0; }
Try apply these styles :
ul {
margin: 0 auto;
padding: 0;
text-align: center;
width: 170px; /*as per your need*/
}
Hows this for you: http://jsfiddle.net/theStudent/6UnNs/
CSS
li {
font-family: Futura, Arvo, sans-serif;
display: inline-block;
padding: 10px;
}
ul {
text-align: center;
background:red;
width: 25%;
margin:0 auto;
}
ul li:first-child {
background:#ccc;
display:block;
margin:0 0 0 -40px;
}
HTML
<ul>
<li>TITLE</li>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 2</li>
</ul>
Just play with settings I added some color so you can see
Here you go:
1) Set text-align: center; on the nav-list element
2) Set display: inline-block on the list
FIDDLE
li {
font-family: Futura, Arvo, sans-serif;
display:inline-block;
padding: 0 10px;
}
ul{
list-style: none;
display: inline-block;
padding:0;
}
div#nav-list {
text-align: center;
background: #E8E8E8;
}
try this
<div>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
div ul li{float:left;list-style:none;margin:0 10px}
div ul{display: inline-block;margin: 0 auto;}
div{text-align:center}
JSFiddle