<img> not centered horizontally in <ul> - html

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>

Related

How to center unordered list of images? [duplicate]

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>

How can I properly move my navbar to the right of my logo when using Skeleton grid

I'm using the skeleton grids and have vertically aligned my two columns using flexbox. However, this pushed my navigation closer to the logo image, and won't move to the right even using text-align or align.
The HTML:
<nav class="nav">
<div class="row">
<div class="one-third column">
<img class="brand" src="img/logo.png" alt="Logo" />
</div>
<div class="two-thirds column" align="right">
<span class="bars"><i class="material-icons">menu</i></span>
<ul>
<li>LOL</li>
<li>LOL</li>
<li>LOL</li>
<li>LOL</li>
<li>LOL</li>
</ul>
</div>
</div>
</nav>
The SCSS:
nav.nav {
display: flex;
align-items: center;
.brand {
width: 200px;
}
ul {
width: 100%;
margin: 20px;
list-style: none;
text-align: right;
li {
display: inline-block;
margin: 0;
a {
display: block;
text-decoration: none;
color: inherit;
text-transform: uppercase;
letter-spacing: 2px;
padding: 12px 16px;
border-bottom: 2px solid lighten($foreground-colour, 30);;
margin-right: -5px;
}
a:hover {
color: lighten($foreground-colour, 30);
border-bottom-color: red;
}
}
}
.bars {
display: none;
}
}
The image of the result I have now is this (logo scribbled out):
That's probably because you use the flex display on the nav, which contains only the row in which the logo and menu are. By default using a flex display without defining the flex property in the child, will result in flex: 0 1 auto for the child. This cuts the width of the child back to the combined width of its children(which are the logo and the menu), which is why they end up so close to each other.
So, in order to fix this, you either want to use the flex display in the row div, and then specify widths with the flexbox properties, removing the skeleton-css classes (they don't work well together).
Or, you could work with skeleton-css and play with line-heights and vertical-align properties in the row div. It's useful to temporarily give your divs and other elements a different color, so you can see what happens.

Column-count Sort Left to Right for a <ul> List

I have the following html and css structure. The problem I am having is that the <li> items are sorted Top to Bottom, instead of Left to Right.
I have tried to follow the solutions on other similar posts but it didn't work out for my specific structure. Any ideas please?
#mylist {
list-style: none;
text-align: left;
margin: 10px 0 2% 5%;
column-count: 3;
-webkit-column-count: 3;
-moz-column-count: 3;
width: 100%;
}
#mylist li {
border-bottom: 1px dashed #eaeaea;
padding: 3px 0;
margin: 5% 0.5% 0% 0.5%;
display: inline-block;
min-width: 80%;
max-width: 80%;
}
<ul id="mylist">
<li>
<a id="floimg" href="/">
<img width="180" height="180" src="img.jpg" class="attachment-mini-me size-mini-me wp-post-image"><span class="flotnm">My Label</span>
<div class="emic">
<div class="emif">
<i id="flocut" class="x-icon x-icon-bookmark-o" data-x-icon="" aria-hidden="true"></i><span class="feme">Designer</span>
</div>
<div class="emis">
<span class="flag"></span><span class="feme">French</span>
</div>
</div>
</a>
</li>
<li>
...same structure
</li>
</ul>
With a width of 80% no two li elements can ever be in one line....
I agree with Johannes. Further to this,
min-width: 20%;
max-width: 80%;
If you want them to fit in the same line if their widths will vary, then why not make the min-width a lower number? At most 50% if you want its neighbor to fit next to it.
Set the height to the same min-height as well if you want each li block to display well next to each other. For example, your code is here but I changed the min/max widths and simplified it. You can see the problem of how this doesn't present well: https://jsfiddle.net/yu2w14hp/2/ (the second li sort of goes under the image. You can fix this by fixing setting a minimum height/width or generally understanding how you want to display your li block).

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 :)

Social Media icons, not centering inside of div

not sure why, but I can't get the icons centered on the page without using padding-left:130px;
Which isn't ideal of course because the icons don't center properly when you re-size the browser window. Maybe I need more coffee, but I could use some stacker help this morning!
http://towerdive.net/
HTML
<div id="center2">
<div id="social_icons">
<p>
Thanks for your interest in our blog!
You can also find us here, as well as subscribe to our newsletter:
</p>
<ul>
<li id="facebook">
<img src="img/icon_facebook.png" alt="Facebook"/>
</li>
<li id="twitter">
<img src="img/icon_twitter.png" alt="Twitter"/>
</li>
<li id="newsletter">
<img src="img/icon_newsletter.png" alt="Newsletter"/>
</li>
</ul>
</div>
</div>
CSS
#center2 {
width: 100%;
}
#social_icons {
margin: 0 auto;
width: 400px;
height: 250px;
text-align: center;
}
#social_icons p {
color: #e3cda4;
}
#social_icons ul {
margin: 0 auto;
list-style: none;
text-align: center;
}
#social_icons ul li {
float: left;
padding-right: 10px;
}
Let me know if you guys need the full HTML or CSS!
You can use display:inline-block for this. Write Like this:
#social_icons ul li {
display: inline-block;
*display:inline;/* For IE7*/
*zoom:1;/* For IE7*/
vertical-align:top;
padding-right: 10px;
}
You currently use floats to display your navigational list horizontally. You can't align the list-items in the middle of the unordered lists because of the floats.
Another technique is instead of float: left; using display: inline-block;. The list-items will be displayed horizontally but also all extra white-space will taken into account. You'll get extra margins between the items (like 4px). Now you can use text-align: center; on the UL to center the list-items.
There are several (easy) workouts described in this article by Chris Coyier:
http://css-tricks.com/fighting-the-space-between-inline-block-elements/