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.

CSS photo gallery display

Featured Replies

Hi!

 

The landing page on my website displays a collection of small photos in a TABLE http://torontorabbits.org. I'm wondering if there's a better way to do this? This display will be expanded to include lots more photos.

 

My latest attempt just puts the images loose in a DIV http://nicoleonline.tumblr.com/testing, which is cleaner than using a table but it still has a fixed width.

 

The reason I made it fixed is because I may be putting some text beside it. If I don't, I'd like it to fill the entire area (the blue box), regardless of screen size -- but then maybe I need to center it, so that it doesn't look awkward when the screen isn't quite big enough to admit another photo on the right...

 

I don't really know what to do about it. Ideally I'd like something that's more responsive to screen size. Any suggestions?? Thanks!!

Why don't you use list?

You can make something like this:

 

<ul>
<li><img src="your-image"/> Any text you need</li>
<li><img src="your-image"/> Any text you need</li>
...
</ul>

Then you can use % for the widths to make it responsive.

 

Here is a different CSS gallery example using divs http://www.w3schools.com/css/css_image_gallery.asp

  • Author

Thanks for the idea!

 

Where do you suggest putting the width definitions?

 

I'm running into this problem: it looks fine at certain screen sizes (see figure1), but not so good at others (figure2, a slightly smaller screen -- there's not quite enough room there to accommodate the bunny in the Santa hat, so I'm left with a big empty space).

 

Also, do you know how I could remove the spaces between the pictures? I can remove them somewhat by removing the line breaks in my code (I had been starting each LI on a new line), but I can't remove them completely.

 

They seem to be caused by the LIs themselves (no padding or margin -- see figure3, a screenshot from firebug; I added the red rectangle to point out the space identified as belonging to the LI).

 

Thanks!

post-55889-0-73047500-1369423715_thumb.png

post-55889-0-71357900-1369423721_thumb.png

post-55889-0-51322500-1369423726_thumb.png

Nice rabbits :)

I am on a mobile, so I can't really see it well atm.

 

But you can define the widths of "li" in %, in your css file.

You can combine % with media queries to make the gallery look good at any resolution.

I will have a look again when I am on the computer.

OK, here is a list gallery, which has 5 pictures in a row. Each li is 20% wide, each picture takes 100% of the li.

The gallery container div takes 90% of the screen, but maximum width of 960px.

 

So the images resize down, when you resize your browser.

I have used media queries, and made it so each li takes 100% of the parent container (and therefore of the screen), when the resolution is below 600px.

You can change that or you can add more media queries to adjust it to your liking.

You can change the % to make more than 5 columns or less.

 

However your images (and text) need to be the same height, otherwise the gallery will break.

You can either set a height of the li elements and overflow:hidden; (this is not a very good solution, though) or there is a javascript (I can post it here if needed) that makes the li elements the same height - it takes the tallest element and adjust the rest according to that. Hope that makes sense :)

 

Here is the code:

 

HTML

 

<div id="gallery">
<ul>
<li><img src="#" alt="image goes here"/> <span>Text goes here</span></li>
<li><img src="#" alt="image goes here"/> <span>Text goes here</span></li>
<li><img src="#" alt="image goes here"/> <span>Text goes here</span></li>
<li><img src="#" alt="image goes here"/> <span>Text goes here</span></li>
<li><img src="#" alt="image goes here"/> <span>Text goes here</span></li>
</div><!--/gallery-->

 

and CSS

 

#gallery{
	width:90%;
	max-width:960px;
	margin:0 auto;
}
#gallery ul{
	margin:0;
	padding:0;
	list-style:none;
}
#gallery ul li{
	width:20%;
	float:left;
	background:#ccc;
	text-align:center;
}
#gallery ul li img{
	width:100%;
	height:auto;
	background:grey;
}

@media only screen and (max-width: 600px) {
	#gallery ul li{
		width:100%;
	}
}

Ignore the background colours, I have added them just to see what is what.

 

Hope that helps, but if you are not sure about something, ask :)

Edited by teodora

OK, here is a list gallery, which has 5 pictures in a row. Each li is 20% wide, each picture takes 100% of the li.

The gallery container div takes 90% of the screen, but maximum width of 960px.

 

So the images resize down, when you resize your browser.

I have used media queries, and made it so each li takes 100% of the parent container (and therefore of the screen), when the resolution is below 600px.

You can change that or you can add more media queries to adjust it to your liking.

You can change the % to make more than 5 columns or less.

 

However your images (and text) need to be the same height, otherwise the gallery will break.

You can either set a height of the li elements and overflow:hidden; (this is not a very good solution, though) or there is a javascript that makes the li elements the same height - it takes the tallest element and adjust the rest according to that. Hope that makes sense :)

 

Here is the code:

 

HTML

 

<div id="gallery">
<ul>
<li><img src="#" alt="image goes here"/> <span>Text goes here</span></li>
<li><img src="#" alt="image goes here"/> <span>Text goes here</span></li>
<li><img src="#" alt="image goes here"/> <span>Text goes here</span></li>
<li><img src="#" alt="image goes here"/> <span>Text goes here</span></li>
<li><img src="#" alt="image goes here"/> <span>Text goes here</span></li>
</div><!--/gallery-->

 

and CSS

 

#gallery{
	width:90%;
	max-width:960px;
	margin:0 auto;
}
#gallery ul{
	margin:0;
	padding:0;
	list-style:none;
}
#gallery ul li{
	width:20%;
	float:left;
	background:#ccc;
	text-align:center;
}
#gallery ul li img{
	width:100%;
	height:auto;
	background:grey;
}

@media only screen and (max-width: 600px) {
	#gallery ul li{
		width:100%;
	}
}

Ignore the background colours, I have added them just to see what is what.

 

Hope that helps, but if you are not sure about something, ask :)

 

This reminds me, i've tried to use @media-screen in my CSS, and it worked on all (compatible) browsers except Firefox, i disabled all plugins etcetera, but with no avail. Instead, i assigned it through HTML, which worked fine. But still, anyone an idea why it wouldn't work?

This reminds me, i've tried to use @media-screen in my CSS, and it worked on all (compatible) browsers except Firefox, i disabled all plugins etcetera, but with no avail. Instead, i assigned it through HTML, which worked fine. But still, anyone an idea why it wouldn't work?

 

Can you post the exact code you used?

^^ It was quite simple really:

 

I used this: (not the specific styling, just an example.)

@media screen and (min-width:960px){
  body{
    background-color:#000; /* Or any code for that matter */
  }
}

in the CSS, and i called is like this: (Again, it might've been called different)

<link href="/css/style.css" rel="stylesheet" type="text/css" />

 

It might've been my browser, because it DID work on all other browsers

Hmm, maybe you need to add "only" - @media only screen and (min-width:960px){....}

Here is a link to some info about the difference between screen and only screen. Mozilla sometimes could be sensitive, I guess.

http://stackoverflow.com/questions/8549529/what-is-the-difference-between-screen-and-only-screen-in-media-queries

Just tried it, and still does not seem to work..

 

It's not some sort of gamebreaker for me, but it does raise questions..

Not sure, it was just a thought :) I just saw this http://css-tricks.com/forums/discussion/15251/firefox-ignoring-max-width320px-media-query/p1

and there were some comments about Mozilla and media queries.

 

Thanks for the link (and help for that matter)

 

I've been looking around those places as well, but cannot seem to find a proper solution, i'm gonna try it on a different computer with Firefox just to see if it's just this one that ignores it. And i'll share it here

 

Thanks again, much appreciated :)

Great, glad to help :) I have tested it in IE for you and it looks fine in IE9 and 8. In IE 7 it shows 4 columns instead of 5, your site is not centered in IE7, too.

Bu yo may not need to worry about IE7, it's about time we stop supporting it :)

  • Author

However your images (and text) need to be the same height, otherwise the gallery will break.

You can either set a height of the li elements and overflow:hidden; (this is not a very good solution, though) or there is a javascript (I can post it here if needed) that makes the li elements the same height - it takes the tallest element and adjust the rest according to that. Hope that makes sense :)

 

It didn't matter earlier because my images are all the same size and I wasn't using text, but I've started using your gallery elsewhere and this time it's just a text list. Could you post that Javascript? Thank you so much!!!!!

<script type="text/JavaScript">
$(document).ready(function (){
	$listItem = $('#gallery ul li');
	maxValue = findMaxValue($listItem);
	$listItemImg = $('img',$listItem);
	maxImgHeight = findMaxValue($listItemImg);
	$listItem.each(function(){
		$(this).css("height",maxValue);
		var imageHeight = $('img',this).height();
		var imageMargin = maxImgHeight - imageHeight;
		$('img',this).css("margin-bottom",imageMargin);
	});
});
function findMaxValue(element){
		var maxValue = undefined;
		$(element).each(function(){
			var val = parseInt($(this).height());
			if (maxValue === undefined || maxValue < val) {
				maxValue = val;
			}
		});
		return maxValue;
	}
</script>

This is the code, put it in your footer, just replace $listItem = $('#gallery ul li'); with your div id. Hope it helps :)

 

Edit: I can't remember where I saw it first, I would have posted a link, as there was a good explanation to go with the code. But basically how it works is: it finds the tallest <li> and then calculates the height the other <li> items to make them all equal.

Edited by teodora

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.