Css: how to fix li's in one line - html

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>&copy 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/

Related

I am having trouble positioning my list element in my nav element

I am having troubles making my list go to the left of my nav element. I have tried to make the nav element relative and list absolute, but that just makes the words overlap each other.
nav {
height: 50px;
width: 400px;
background-color: red;
}
li {
display: inline;
justify-content: space-evenly;
margin: 10px;
font-size: 20px;
}
<nav>
<ul style="list-style: none">
<li>#stayhome</li>
<li>anime</li>
<li>queue</li>
<li>discord</li>
</ul>
</nav>
You can achieve the same result by setting display property of ul to flex
nav {
height: 50px;
width: 400px;
background-color: red;
line-height: 48px;
}
ul{
margin: 0;
padding: 0;
display:flex;
justify-content: space-around;
}
<nav>
<ul style="list-style: none">
<li>#stayhome</li>
<li>anime</li>
<li>queue</li>
<li>discord</li>
</ul>
</nav>
The justify-content property is irrelevant here - it only applies to elements in a Flex or Grid layout.
The issue here is the default margin/padding on the <ul> and <li> elements.
You might also want to set the line-height on the <nav> so that the list items are more centred.
nav {
height: 50px;
width: 400px;
background-color: red;
line-height: 48px;
}
nav > ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
display: inline;
margin: 10px;
font-size: 20px;
}
<nav>
<ul style="list-style: none">
<li>#stayhome</li>
<li>anime</li>
<li>queue</li>
<li>discord</li>
</ul>
</nav>

I want the nav bar in top right corner [duplicate]

I have a horizontal navbar like the following:
<ul id = "Navigation">
<li>About</li>
<li>Contact</li>
<!-- ... -->
</ul>
I use CSS to remove the bullet points and make it horizontal.
#Navigation li
{
list-style-type: none;
display: inline;
}
I'm trying to justify the text so each link is spread out evenly to fill up the entirety of the ul's space. I tried adding text: justify to both the li and ul selectors, but they're still left-aligned.
#Navigation
{
text-align: justify;
}
#Navigation li
{
list-style-type: none;
display: inline;
text-align: justify;
}
This is strange, because if I use text-align: right, it behaves as expected.
How do I spread out the links evenly?
Modern Approach - CSS3 Flexboxes
Now that we have CSS3 flexboxes, you no longer need to resort to tricks and workarounds in order to make this work. Fortunately, browser support has come a long way, and most of us can start using flexboxes.
Just set the parent element's display to flex and then change the justify-content property to either space-between or space-around in order to add space between/around the children flexbox items. Then add additional vendor prefixes for more browser support.
Using justify-content: space-between - (example here):
ul {
list-style: none;
padding: 0;
margin: 0;
}
.menu {
display: flex;
justify-content: space-between;
}
<ul class="menu">
<li>About</li>
<li>Contact</li>
<li>Contact Longer Link</li>
<li>Contact</li>
</ul>
Using justify-content: space-around - (example here):
ul {
list-style: none;
padding: 0;
margin: 0;
}
.menu {
display: flex;
justify-content: space-around;
}
<ul class="menu">
<li>About</li>
<li>Contact</li>
<li>Contact Longer Link</li>
<li>Contact</li>
</ul>
You need to use a "trick" to make this work.
See: http://jsfiddle.net/2kRJv/
HTML:
<ul id="Navigation">
<li>About</li>
<li>Contact</li>
<!-- ... -->
<li class="stretch"></li>
</ul>
CSS:
#Navigation
{
list-style-type: none;
text-align: justify;
height: 21px;
background: #ccc
}
#Navigation li
{
display: inline
}
#Navigation .stretch {
display: inline-block;
width: 100%;
/* if you need IE6/7 support */
*display: inline;
zoom: 1
}
Details on IE6/7 trickery: Inline block doesn't work in internet explorer 7, 6
This can also be achieved using a pseudo element on the ul element:
ul {
margin: 0;
padding: 0;
list-style-type: none;
text-align: justify;
}
ul:after {
content: "";
width: 100%;
display: inline-block;
}
li {
display: inline;
}
Just do:
ul { width:100%; }
ul li {
display:table-cell;
width:1%;
}
This might suit your needs:
#Navigation{
}
#Navigation li{
list-style-type: none;
text-align: center;
float: left;
width: 50%; /*if 2 <li> elements, 25% if 4...*/
}
demo : http://jsfiddle.net/KmqzQ/
HTML
<ul id="Navigation">
<li>The Missing Link</li>
<li>About</li>
<li>Riluri</li>
<li>Contact us</li>
<!-- ... -->
<li class="stretch"></li>
</ul>
CSS
#Navigation {
list-style-type: none;
text-align: justify;
height: 21px;
background: #ccc
}
#Navigation li{
display: inline
}
#Navigation li a {
text-align: left;
display:inline-block;
}
#Navigation .stretch {
display: inline-block;
width: 100%;
/* if you need IE6/7 support */
*display: inline;
zoom: 1
}
View demo: http://jsfiddle.net/2kRJv/392/
You need to have the <li> elements separated, otherwise the justify won't work.
For example, this:
<ul><li>test</li><li>test</li></ul>
needs to be like this:
<ul>
<li>test</li>
<li>test</li>
</ul>
or at least have spaces between the opening and closing <li> tags.
This blog has a satisfyingly robust solution. It needs some slight changes to accommodate a ul/li navigation, though:
#Navigation {
width: 100%;
padding: 0;
text-align: justify;
font-size: 0;
font-size: 12px\9; /* IE 6-9 */
}
#Navigation>li {
font-size: 12px;
text-align: center;
display: inline-block;
zoom: 1;
*display: inline; /* IE only */
}
#Navigation:after {
content:"";
width: 100%;
display: inline-block;
zoom: 1;
*display: inline;
}
http://jsfiddle.net/mblase75/9vNBs/
The marked answer does not work if has a white space in it.
The top answer here works with white spaces
How do I *really* justify a horizontal menu in HTML+CSS?
Using CSS Flexboxes
#Navigation {
width: 100%;
padding: 0;
margin: .5rem 0;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
list-style-type: none;
color: #ffffff;
}

How to fix reversed header?

I know that when you put float: right to a li element it displays in a reversed order, but how can I fix the order so it displays "correctly" and on the right side of the website? Now it displays in the left side of the website. I've tried to read some old questions but didn't find anything that could help me, and also, how can I make the header display in the middle of the #333333 colored header without padding? Will auto element work?
.header {
background-color: #333333;
height: 50px;
width: 100%;
}
.upper_header ul {
margin: 0px;
float: right;
}
.upper_header li {
display: table-cell;
vertical-align: middle;
float: left;
}
.upper_header a {
text-decoration: none;
color: #fff;
}
<div class="header">
<div class="headerContainer">
<ul class="upper_header">
<li>Home
</li>
<li>About Us
</li>
<li>Team
</li>
<li>News
</li>
<li>Contact Us
</li>
</ul>
</div>
</div>
I've tried putting float: right to the ul element and float: left to the li element, then the order is correct but the position of it is in the left. (Sorry for putting two questions in one thread, didn't want to wait another 30 minutes to submit another question.)
You can set your LI's to display: inline-block then you dont need to use floats.
Inline-block elements then can be aligned using text-align
Note:
Inline-block can cause a space between elements, for more info about then please read this https://css-tricks.com/fighting-the-space-between-inline-block-elements/
.header {
background-color: #333333;
width: 100%;
text-align: right;
}
.upper_header {
width: 100%;
}
.upper_header li {
display: inline-block
}
.upper_header a {
padding: 5px 10px;
display: block;
text-decoration: none;
color: #fff;
}
<div class="header">
<div class="headerContainer">
<ul class="upper_header">
<li>Home
</li>
<li>About Us
</li>
<li>Team
</li>
<li>News
</li>
<li>Contact Us
</li>
</ul>
</div>
</div>
First - you have some mistakes in your code, in CSS you use .upper_header ul, but this is not correct syntax in your context. Right is ul.upper_header (your ul list is not under class upper_header, but on the same level), so it does not have effect for you.
If you don't need so much nested div and not so much classes, prevent using it. Example is below (this is solution with centered menu):
.header ul {
text-align: center;
list-style: none;
}
.header li {
display: inline-block;
}
http://jsfiddle.net/aqhesrjn/4/
Then you can easily play with text-align: right in ul element
.header ul {
text-align: right;
list-style: none;
}
.header li {
display: inline-block;
}
http://jsfiddle.net/aqhesrjn/3/
slightly modified CSS & you are using wrong selector(.upper_header ul)instead of ul.upper_header.
ul.upper_header ==> center to align center
ul.upper_header ==> right to align right
.header {
background-color: #333333;
height: 50px;
width: 100%;
}
ul.upper_header {
margin: 0px;
text-align: center;
}
.upper_header li {
display: inline-block;
}
.upper_header a {
text-decoration: none;
color: #fff;
}
<div class="header">
<div class="headerContainer">
<ul class="upper_header">
<li>Home
</li>
<li>About Us
</li>
<li>Team
</li>
<li>News
</li>
<li>Contact Us
</li>
</ul>
</div>
</div>
you can do this as well ,one of the many options available depending upon ofcourse what you are trying to get as an end result.and you were using that upper_header class in a wrong way,you dont even need that.
.header {
background-color: #333333;
height: 50px;
width: 100%;
}
.headerContainer{width:30%;
float:right;}
.headerContainer ul li {
display:inline;
}
.headerContainer ul li a {
text-decoration: none;
color: #fff;
}

Center list in div (for navigation bar)

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

How do I center list items inside a UL element?

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;
}