Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Laying out list as grid

Featured Replies

Hi all,

 

I haven't been around here for a while! I wonder if there's any CSS experts out there that would be able to help me with the following:

 

I've got a bunch of images which, semantically, should be marked up as a list:

 

<ul>
<li><a href=""><img src="image-1.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-2.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-3.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-4.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-5.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-6.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-7.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-8.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-9.gif" width="100" height="100"/></a></li>
</ul>

 

They are to be displayed within a column with a width of 330px:

 

<div id="main-content" style="width:330px;">
<ul>
<li><a href=""><img src="image-1.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-2.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-3.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-4.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-5.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-6.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-7.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-8.gif" width="100" height="100"/></a></li>
<li><a href=""><img src="image-9.gif" width="100" height="100"/></a></li>
</ul>
</div>

 

I want to display these images in a 3x3 grid with 15px spacing between each image but flush to each side of the containing div.

 

I initially went with the following:

 

#main-content li {
float:left;
margin:0 15px 15px 0;
}

 

This of course gives every list element a right margin of 15px which means the items do not fit within the containing div -- totalImageWidths (100*3) + totalMarginWidths (15*3) == 345px which is too wide, so basically I don't want the right margin on every third element.

 

As these are being dynamically generated and I don't want to litter my markup with classes I can't remove the margin from every third element.

 

I then tried the following:

 

#main-content li {
display:inline;
word-spacing:15px;
}

#main-content img {
margin:0 0 15px;
}

 

This works nicely in Firefox but fundamentally Webkit doesn't appear to support word-spacing on elements other than text!

 

It would be good to hear how you guys would achieve this?

 

Cheers

 

Rich

  • Author

Also I just noticed a mistake: the word-spacing property would need to be 11px -- presumably it adds on those pixels to the default spacing?

  • Author

Just wanted to bump this up to see if anyone knows of the best way to do this?

you could try adding

#main-content ul{width:345px:}
#main-content{width:330px;overflow:hidden;background-color:#09c;}
#main-content li{float:left;margin:0 15px 15px 0;display:block;}

 

You would need to remove the borders from your image

img{border:none;}

 

I think that should work

  • Author

Thanks mteam, that works a treat, in fact it doesn't even require overflow:hidden; as the margin is obviously transparent so doesn't need to be clipped.

 

Cheers

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.