Horizontal list element evenly spaced and responsive - html

I am creating an unordered list that contains images and text, and would like each item to be 1/3rd the width with a margin, evenly spaced. I can't figure out how to ensure, in a responsive website, the items are always exactly 1/3rd the width, including margins. Percentages seem to not remain consistent across browsers.
HTML
<ul>
<li><img>Text</li>
<li><img>Text</li>
<li><img>Text</li>
<li><img>Text</li>
<li><img>Text</li>
<li><img>Text</li>
</ul>
CSS
ul {list-style:none; text-align:center; margin:0;}
ul li {display:inline-block; zoom:1; *display:inline; vertical-align:top; width:32%; margin:10px 1%;}
If I want two rows of three columns here, it will sometimes look fine and other times push the third item down to the next row--almost as if some browsers are adding a pixel or two somewhere. Is there CSS that I can use to just space it evenly across the entire width of the parent element?
Note that the site is responsive so calculating exact pixels isn't going to work. I'd also rather avoid writing separate CSS for different browsers, if possible.

There's a couple of ways to tackle this, but the easiest CSS solution is to use inline-blocks for the li elements. Now the issue with inline-block is, they will take whitespace into account, breaking your layout, but there are two easy solutions for this in the markup:
HTML comments between the li elements;
Compressed HTML code.
Example with HTML comments:
<ul>
<li><img>Text</li><!--
--><li><img>Text</li><!--
--><li><img>Text</li><!--
--><li><img>Text</li><!--
--><li><img>Text</li><!--
--><li><img>Text</li>
</ul>
Example with compressed HTML:
<ul><li><img>Text</li><li><img>Text</li><li><img>Text</li><li><img>Text</li><li><img>Text</li><li><img>Text</li></ul>
For the CSS, this should do the trick (you can modify the width of the 'columns' in media queries).
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
display: inline-block;
width: 50%;
}
#media screen and (min-width: 20em) {
li {
width: 33.33333%;
}
}
Another thing to note is that you can't use margins on the left and right of these items, as they will not be part of the original width, but you can use padding on the inside to "space" elements apart. This is also how some of the repsonsive grid systems are built (a little more advanced, but same principle).
Here's the live code: http://codepen.io/TheDutchCoder/pen/zxxwom

Here is a cross-browser solution. I have added an html5 structure if you need to include images in your list.
/* the two following properties modify the box-model so that padding is removed from the width instead of being added */
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
ul:after { /* to clearfix your ul list */
content: "";
display: table;
clear: both;
}
ul li {
display: block; /* inline-block have an extra 4px margin wich makes it harder to use in this situation */
width: 33.3333333%; /* You get it (100/3) but use exact number */
padding: 10px 5px;
float: left; /* if you use block elements you have to float them */
text-align: center;
}
figure {
margin: 0; /* to normalize figure element */
}
figure img {
max-width: 100%;
max-height: 100%;
}
<ul>
<li>
<figure>
<img src="http://placehold.it/500x500" />
<figcaption>Text</figcaption>
</figure>
</li>
<li>
<figure>
<img src="http://placehold.it/500x500" />
<figcaption>Text</figcaption>
</figure>
</li>
<li>
<figure>
<img src="http://placehold.it/500x500" />
<figcaption>Text</figcaption>
</figure>
</li>
<li>
<figure>
<img src="http://placehold.it/500x500" />
<figcaption>Text</figcaption>
</figure>
</li>
<li>
<figure>
<img src="http://placehold.it/500x500" />
<figcaption>Text</figcaption>
</figure>
</li>
<li>
<figure>
<img src="http://placehold.it/500x500" />
<figcaption>Text</figcaption>
</figure>
</li>
</ul>

Try this: http://jsfiddle.net/j13889cx/
I've added a div element wrapping everything inside each li element called .ul-wrap.
This allows you to add any margin/padding you'd like because .ul-wrap is a block element.
Try changing .ul-wrap's margin or padding.

Related

img inside <li> with property "display: inline" goes out of li tag

I have an unordered list, listaAuto, which is filled with <li> items, which contain an img for each.
The problem is that if I use the <li> property display: inline;, they won't expand to contain the images. Instead, if I display them horizontally, they are no mistakes.
I already tried using overflow: auto, height: auto, clear: both: and other solutions, but I still can't find the way.
HTML:
<body>
<div>
<ul id="listaAuto">
<li><img src="example.png" width="121.33" height="75.92"/></li>
<li><img src="example.png" width="121.33" height="75.92"/></li>
</ul>
</div>
</body>
CSS:
#listaAuto, li{
list-style: none;
padding: 5px;
}
li{
clear: both;
display: inline;
border: 1px solid black;
}
Current output:
Use inline-block for the li items:
https://jsfiddle.net/y86263mq/
Based on another Stackoverflow answer:
Inline elements:
respect left & right margins and padding, but not top & bottom
cannot have a width and height set
allow other elements to sit to their left and right.
see very important side notes on this [here][1].
Block elements:
respect all of those
force a line break after the block element
Inline-block elements:
allow other elements to sit to their left and right
respect top & bottom margins and padding
respect height and width
Simple explaination
inline-block elements can have the heights defined. In this case, you're not explicitly setting the heights but the contents are.
Add display:inline-block for li and remove the unnecessary clear:both.
Refer this fiddle https://jsfiddle.net/wsmzhn1h/
Adding float: left; and removing clear:both; on the li should do the trick. https://jsfiddle.net/y8sxhv1z/2/
You may try this.
li{
display: inline-block;
}
li img{
max-width: 100%;
}
#listaAuto, li{
list-style: none;
padding: 5px;
}
li{
width: auto;
float: left;
display: block;
border: 1px solid black;
}
<body>
<div>
<ul id="listaAuto">
<li><img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=2bb144720a66" width="121.33" height="75.92"/></li>
<li><img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=2bb144720a66" width="121.33" height="75.92"/></li>
</ul>
</div>
</body>

Align Images without spacing horizontally and veritcally

I have a group of 4 images that I'm trying to align vertically and horizontally.
The problem:
I cant get ride of a a small vertical spacing between them.
Please check out the issue reproduced in Fiddle
html:
<div>
<ul>
<li> <img src="http://placekitten.com/100/100" alt="">
</li>
<li> <img src="http://placekitten.com/100/100" alt="">
</li>
<li> <img src="http://placekitten.com/100/100" alt="">
</li>
<li> <img src="http://placekitten.com/100/100" alt="">
</li>
</ul>
</div>
css:
* {margin:0; padding: 0;}
div {width: 200px; height: 200px;}
ul {
list-style:none;
}
ul li {
display: inline-block;
float:left;
}
It seems pretty simple, but I haven't been able to get ride of spacing other than manually specify the height to 100px, which isn't responsive and so not viable.
Adding vertical-align:top on the img elements will remove the gap. The default is baseline.
As a side note, bottom and middle work too.
jsFiddle example
img {
vertical-align:top;
}
Adding display:block to the img elements works too. (example)
img {
display:block;
}
Try
margin: 0 auto;
border: 0px;
If you don't have text in this you can just say
ul {
font-size: 0;
}
This eliminates the space, see modified JSFiddle

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/

Creating lists in IE 8 with CSS

I am new to CSS and I am running into this problem with lists in IE8. I am creating an unordered list with several li elements with two divs called left and right.
HTML code -
<ul>
<li>
<div class="left">
<p>ONE</p>
</div>
<div class="right">
<p>TWO</p>
</div>
</li>
<li>
<div class="left">
<p>THREE</p>
</div>
<div class="right">
<p>FOUR</p>
</div>
</li>
</ul>
I essentially want to create a list with two columns. My CSS -
ul {
list-style-type: none;
padding: 0px;
margin: 0px;
}
ul li {
height: 40px;
width: 800px;
border: 1px solid;
}
ul li div.left, div.right {
display: inline-block;
*display: inline;
zoom: 1;
}
Using the above CSS and html, I get a list with two columns with a height a 40px. In firefox and chrome, it shows up just as I wanted, but in IE it is a different story.
In IE, I see the same list but the only problem is spacing. In IE, the divs left and right sit at the top of its parent li whereas in Chrome and Firefox they elements sit in the middle which I is what I want.
How do I get this to happen in IE? In IE, my divs cling to the top of the li element, with no padding in between. Do I need to add something else in my CSS? If anyone can help, it would be great.
Thanks.
You could also float you boxes, and give them a height matching the li.
Inline block doesn't work as expected in all browsers/versions.
ul li div.left,
ul li div.right {
float: left;
height: 40px;
width: 400px;
}

Center inline block images

I need some help centering some images
HTML:
<div id="thumbs" class="navigation">
<ul class="thumbs noscript">
<li>
<a class="thumb" href="#">
<img src="http://farm4.static.flickr.com/3261/2538183196_8baf9a8015_s.jpg" />
</a>
</li>
<li>
<a class="thumb" href="#">
<img src="http://farm4.static.flickr.com/3261/2538183196_8baf9a8015_s.jpg" />
</a>
</li>
<li>
<a class="thumb" href="#">
<img src="http://farm4.static.flickr.com/3261/2538183196_8baf9a8015_s.jpg" />
</a>
</li>
<br>
<li>
<a class="thumb" href="#">
<img src="http://farm4.static.flickr.com/3261/2538183196_8baf9a8015_s.jpg" />
</a>
</li>
<li>
<a class="thumb" href="#">
<img src="http://farm4.static.flickr.com/3261/2538183196_8baf9a8015_s.jpg" />
</a>
</li>
CSS:
ul.thumbs {
clear: both;
padding-left: 0px;
}
ul.thumbs li {
display: inline-block;
/*float: left;*/
padding: 0;
margin: 5px 10px 5px 0;
list-style: none;
}
a.thumb {
padding: 2px;
display: block;
border: 1px solid #ccc;
}
ul.thumbs li.selected a.thumb {
background: #DAA12F;
}
a.thumb:focus {
outline: none;
}
ul.thumbs img {
border: none;
display: block;
height: 120px;
width: 120px;
margin: 0px auto;
}
I need the images be centered together so that they remained lined up. Ive tried using:
ul.thumbs {
text-align:center;
}
but since there are a different number of images on each row, they move out of alignment
Thanks for the help, this is driving me crazy
EDIT: I thought I found a solution using this as a guide: http://www.tightcss.com/centering/center_variable_width.htm but if the images go over more than row, they no longer center. Originally I put a br tag to break up the rows and that fixes the problem, but a JS library Im using doesnt play nice with br tags between list items. Any suggestions (you can see the issue here http://jsfiddle.net/HvZva/26/)
There is two solutions to this, one is if you want your images to be centered and the another is if your images should be aligned left, but centered on the page.
Center block, left align images
To do this you need to add the following to your css (or see: http://jsfiddle.net/HvZva/20/)
ul.thumbs{
margin:0 auto;
width:416px;
}
what I do is basicly to tell the browser that this un ordered list should have an equal margin to both sides. But since this object has an width of 100% as standard, we need to specifiy that aswell, and in this case it is 416px.
Center align images
If this is static content, one easy way to do this is to add a div with the class of center that wraps each line, and then adding "text-align:center;" to those divs, that will do the trick.
note: there is one new way to center the block, but aligning the images to the left. but that involves the box flex model and css 3, and it has not yet been implemented by browsers yet
Applying text-align: center; to .thumbs.noscript ul fixes it in the jsfiddle
You can give the parent element a width and apply margin-left: auto; margin-right: auto;
http://jsfiddle.net/HvZva/13/
ul.thumbs {
display:inline-block;
text-align:center;
}
Something like this? http://jsfiddle.net/HvZva/11/
I may have mis interpreded your issue but is it something like this you want?
http://jsfiddle.net/HvZva/12/