How to center unordered list of images? [duplicate] - html

This question already has answers here:
How to center an unordered list?
(8 answers)
Closed 3 years ago.
I am trying to center an unordered list of imagines on a website.
I have tried using 'text-align: center;', margins(which works, kinda, but looks super sloppy)
#nav{
margin-left:44%;
margin-bottom: 10px;
width: 100%;
text-align: center;
background-color: transparent;
height: 40px;
margin-bottom: 100px;
}
#nav li a {
padding: 10px;
text-decoration: none;
font-weight: bold;
color: #FFFFFF;
background-color: transparent;
float: left;
text-align: center;
display: inline;
}
<div id="nav">
<ul>
<li>
<img src='./public/images/discord.png' style="height:40px;border:0;">
</li>
<li>
<img src='./public/images/youtube.png' style="height:40px;">
</li>
<li>
<img src='./public/images/twitch.png' style="height:40px;border:0;">
</li>
</ul>
</div>
The list just doesn't center properly, as expected with manually set margins.

I recommend using flex box to handle problems like these. The first example in the snippet shows centering using flex box. The second example shows centering an unordered list.
To center an unordered list, I've set the margin left and right on the containing element (#nav) to auto. For this to center the list, you will need to know the exact width of the list. I've calculated to be 3 x 40px for each image, plus 20px for spacing for each image, making 180px in total.
A common problem some run into when dealing with undordered is that lists come with default padding on the left. For perfect centering, I've removed the list style bullets, and removed the padding too.
.flex {
display: flex;
justify-content: center;
}
.flex>div {
margin-right: 20px;
}
#nav {
margin: 0 auto;
width: 180px;
}
#nav>ul {
padding-left: 0px;
}
#nav li {
list-style-type: none;
text-decoration: none;
float: left;
margin-right: 20px;
}
<h2>Display flex</h2>
<div class="flex">
<div>
<img src='https://picsum.photos/40' style="height:40px;border:0;">
</div>
<div>
<img src='https://picsum.photos/40' style="height:40px;">
</div>
<div>
<img src='https://picsum.photos/40' style="height:40px;border:0;">
</div>
</div>
<h2>Centered UL</h2>
<div id="nav">
<ul>
<li>
<img src='https://picsum.photos/40' style="height:40px;border:0;">
</li>
<li>
<img src='https://picsum.photos/40' style="height:40px;">
</li>
<li>
<img src='https://picsum.photos/40' style="height:40px;border:0;">
</li>
</ul>
</div>

Related

<img> not centered horizontally in <ul>

I want to make a list of images that are displayed horizontally. The problem is that paragraphs are displayed correctly but images aren't.
My Code:
ul, li {
list-style-type: none;
display: inline-block;
}
img {
margin-bottom: 20px;
border: 5px solid black;
border-radius: 3px;
}
<ul>
<li>
<img src="a.png" style="max-width: 20%;">
</li>
<li>
<img src="b.png" style="max-width: 20%;">
</li>
</ul>
The problem is with the inline style you have added to the images. When you apply max-width: 20%, there will be a 80% empty space next to them, so that in a large screen, the images will have a large space at their right side, and in smaller screens, they will be displayed vertically because of space.
Simply add a fixed width to your images or use vw measures (example: max-width: 20vw) in order to the images get a width that is relative to the viewport width.
this can easily be done with flexbox by adding display: flex and justify-content: center to the ul. You can also add a flex-wrap to collapse the images on small screens if needed. You'll also need to set padding-left to 0 due to default padding on ul elements I would look into adding a reset css to help with that
ul {
justify-content: center;
display: flex;
padding-left: 0;
}
ul, li {
list-style-type: none;
}
img {
margin-bottom: 20px;
border: 5px solid black;
border-radius: 3px;
}
<ul>
<li>
<img src="a.png" style="max-width: 20%;">
</li>
<li>
<img src="b.png" style="max-width: 20%;">
</li>
</ul>

How to middle align text in a list item if the previous list item is heigher than it

How do you middle align text in a list item if the previous list item is heigher than it?
I have the following HTML code:
<ul class="list-inline">
<li style="background-color: aqua;">
<div style="height:50px;width:50px;background-color:green;display:block;"> </div>
</li>
<li style="background-color: lime;">
<div style="height:50px;background-color:green;display:block;">Brendan</div>
</li>
<li style="background-color: salmon;">Vogt</li>
</ul>
I am using Bootstrap. The first list item has to have a height and width of 50px. It is meant to be used for a colour. The second list item is plain text. The text is 10px in height.
I tried adding padding in the <li></li> but it doesn't work. I tried adding margin but this also doesn't work. I've tried adding the same to the <div></div> surrounding the text, also nothing happens. Seems like the padding and margin is added but overlaps the other elements so it looks like nothing happens.
How do I middle align the text?
PS: The styling is inline at the moment as I am playing around with it, will move it to an external style sheet later when everything works.
you can try this ..
.list-inline > li div {
margin: 10px;
padding: 14px;
}
here is the FIDDLE.
If you need vertical alignment you can use display: table-cell property. Since you already have ul and li they can be table and table-rows accordingly. In this case ul li > div would be table-cells which allow to use vertical-align: middle. You can additionally set text-align: center If needed.
.list-inline {
display: table;
width: 100%;
margin: 0;
padding: 0;
}
.list-inline li {
display: table-row;
}
.list-inline li > div {
display: table-cell;
vertical-align: middle;
height: 50px;
}
.list-inline li:nth-child(1) {
background-color: aqua;
}
.list-inline li:nth-child(2) {
background-color: lime;
}
.list-inline li:nth-child(3) {
background-color: salmon;
}
.myColor {
background-color: green;
width: 50px;
height: 50px;
}
<ul class="list-inline">
<li>
<div>
<div class="myColor"></div>
</div>
</li>
<li>
<div>Brendan</div>
</li>
<li>
<div>Vogt</div>
</li>
</ul>
Do you want all list items to appear in one row and to be vertically align center to each other?
If I understood your question correctly then this should help you -
<ul class="list-inline">
<li style="background-color: aqua; height:50px; width:50px; display:inline-block; vertical-align: middle;"> </li>
<li style="background-color: lime; display:inline-block; vertical-align: middle;"> Brendan </li>
<li style="background-color: salmon; vertical-align: middle; display: inline-block;"> Vogt </li>
</ul>
Use the css property inline-css for li it will work for you
<li style="text-align: center !Important;"
</li>
Or use line-height property
I ended up doing it like this:
<ul class="list-inline">
<li style="background-color: green;">
<div style="padding: 15px 23px;background-color: aqua;"> </div>
</li>
<li style="background-color: green;">
<div style="padding: 15px 0 15px 0;background-color: lime;">Brendan</div>
</li>
<li style="background-color: green;">
<div style="padding: 15px 0 15px 0;background-color: yellow;">Vogt</div>
</li>
</ul>
I needed the first colour block 50px high and wide. The is 4px wide and 20px high, hence the padding: 15px 23px;. The text in the other list items I'm not too worried about the length because the text can be varying length. I just set the hight. The text is 20px high hence the padding: 15px 0 15px 0;.
I'm not a CSS expert but this works for me :)

How to center image in <li> elements?

I have a ul whose cells consist of an image and a text:
<ul class="special_ul">
<li>
<img src="img/solution_icon_optimize-efficnt.png">
OTT
</li>
<li>
<img src="img/solution_icon_personal.png">
Cloud Computing
</li>
<li>
<img src="img/solution_icon_diti-cx.png">
Managed Services
</li>
<li>
<img src="img/solution_icon_biz-analitics.png">
Social Media
</li>
</ul>
css:
.special_ul li{
padding-right: 10px;
list-style-type: none;
display: inline-block;
text-align: center;
}
.special_ul li img , .special_ul li a{
display:block;
}
The problem is that the text is centered but the images are not:
(i highlighted the list with the mouse so you can see the images are not in the center)
How do I make the images centered (horizontally) as well?
You can just remove img {display: block;}, by default it is inline level, they will get centered with text-align: center; set on the container, which you already did.
Or, add margin: 0 auto; if you need to keep display: block; on the img:
.special_ul li img {
display: block;
margin: 0 auto;
}

I'm having trouble making a navbar with a logo in the center, and two links on either side with equal spacing

I want to create a navbar with a logo in the center, and two navbar items (links) on either side, width equal spacing. I also need the logo to be lined up center with the navbar items.
Here's the code i have thus far. I'm using bootstrap.
HTML:
<div class="nav">
<div class="container">
<ul>
<li> Navbaritem1 </li>
<li> Navbaritem2 </li>
</ul>
<div class="logo">
<img src="Img/logo.png" class="img-responsive">
</div>
<ul>
<li> Navbaritem3 </li>
<li> Navbaritem4 </li>
</ul>
</div>
</div>
CSS:
.nav li {
display: inline;
margin-left:5%;
margin-right:5%;
}
.logo {
width: 100px;
display: inline-block;
height: auto;
}
.logo img {
float: none;
padding-top:10%;
}
ul {
display: inline;
text-align:center;
width:30%;
}
Did you try centering a div inside the container?
<div class="container">
<div class="center">
Centered content here
</div>
</div>
And the css:
.center {
display: table;
margin: 0 auto;
}
Also this post might've helped you with a quick search.

How do I center my content inside the menu?

I have been trying to figure this little bugger out for a little while, and now I seek some expertise. What am I trying to do? I want to create a 100% width menubar, but keep the logo, and the content centered so it can fit in with the rest of the content. The container is on 1000px. Right now I have placed my menu outside of the container tag, and used "width: 100%" to get it to cover the screen. However with that the content moves to the left. I don't really know how to fix this, I have been working on it for a while.
Here is my HTML code:
<body>
<!-- navigation / header -->
<div class="grid_12" id="menu">
<div class="grid_3" id="logo">
<img src="img/logo.png" alt="logo" />
</div>
<div class="grid_9 omega">
<ul class="nav">
<li>
<label>
Features
<br />Check our features
</label>
</li>
<li>
<label>
Pricing
<br />Starts at $X/month
</label>
</li>
<li>
<label>
30-day free trial
<br />Start using us right away
</label>
</li>
<li class="sign-btn">
Log in
</li>
</ul>
</div>
</div>
<div class="container clearfix">
and here is my css file:
#menu {
width:100%;
margin: 0 0 0 0;
padding: 0;
background: #1d3853;
color: white;
font-size: 13px;
}
.nav li {
padding-top: 15px;
list-style:none;
float: left;
text-align: center;
margin-right: 20px;
}
.nav li a {
color: #a7c5e3;
font-family: helvetica;
font-weight: bold;
letter-spacing: 1px;
}
.sign-btn a {
color: #FFF;
display: block;
background: #105296;
padding: 10px 15px;
text-align: center;
border-radius: 5px;
border: 1px solid white;
}
So I ask again, dear StackOverflow... how do I get my links in the center with the rest of my content. Like this picture: http://www.cssnewbie.com/wp-content/uploads/2009/12/centered-navbar-sketch.png
write your navigation code inside another div with class=container.
That is
<body>
<div class="container">
<!-- your navigation code here-->
</div>
</body>
To center something with a set width, add the style margin:auto. To be more precise, you only need margin-left:auto and margin-right:auto to give equal padding to an element inside a container.