Responsive images and lists - css

Tryign to get it so that I have a max width for an image to be 308px and when the browser is scaled larger then the list will have more items per row. Just now it seems stuck at 3 list items per row but if I would like it to be fully reponsive and if the browser is scaled to minum it can be then it will be one image per row.
html
<ul>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
<li><img src="http://distilleryimage8.s3.amazonaws.com/23ddf3860e2911e29c2d22000a1e9e7e_6.jpg"></li>
</ul>
css
ul{
position:relative;
float:left;
width:100%;
height:100%;
margin:45px 0 0;
}
ul li{
width: 33.3333%;
float:left;
}
ul li img{
display: block;
height: auto;
width: 100%;
}
JSFIDDLE

You need to use media queries.
Just set up media queries for each "breakpoint" and change the li width.
For example:
/* Show one image on devices under 540px */
#media screen and (max-width: 540px) {
ul li{
width: 100%;
float:left;
}
}
/* Show three images on devices between 550 and 800px wide */
#media screen and (min-width: 540px) and (max-width: 800px) {
ul li{
width: 33.333%;
float:left;
}
}
/* Show five images on devices between 800px and 1180pxwide */
#media screen and (min-width: 800px) and (max-width: 1180px) {
ul li{
width: 20%;
float:left;
}
}
http://jsfiddle.net/spacebeers/gfjDk/3/

Use media queries (as per SpaceBeers' suggestion) coupled with a min-width property on the list items...
JsFiddle

Another idea would be to use display: inline-block; instead of float: left and set max-width: 308px; and min-width: 100px; for the list items.
demo
HTML:
<ul>
<li><img src="image.jpg"></li><!--
--><li><img src="image.jpg"></li><!-- as many more list items as you need
-->
</ul>
The way I've written it (with comments between list items) does serve a purpose - any kind of whitespace matters when using inline-block.
CSS:
ul {
padding: 0;
margin: 45px 0 0;
}
ul li{
min-width: 100px;
width: 33.3333%;
max-width: 308px;
display: inline-block;
}
ul li img{
display: block;
height: auto;
width: 100%;
}

Related

How do I make images responsive with pure css?

I have a Wordpress menu that I've made the tabs background images. I am trying to get the images to scale down when the .wrapper scales down and keep them in a horizontal row and avoid javascript and/or media queries or navwalker class as I'm using bootstrap.
This is what the menu looks like full scale:
Here is my code for my .wrapper and for a one out of the 9 menu-items and the nav class and logo structure. Please note I'm trying to apply the same concept to the logo.
I am using html5blank and Wordpress menu.
<div class="nav" role="navigation">
<?php html5blank_nav(); ?>
</div>
here is the html it renders of one of the 9 <li>'s ( that I'm styling in the css )
<li id="menu-item-1688" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1688">Blog</li>
.wrapper {
max-width:1280px;
width:95%;
margin:0 auto;
position:relative;
}
.logo {
float:left;
width:145px;
height:157px;
}
.nav {
float:left;
padding-top:11px;
}
.nav ul {
padding:0;
margin:0;
}
.nav ul li {
float:left;
list-style-type:none;
padding:0;
margin:0;
}
li#menu-item-1688 a {
display:block;
background:url('img/ksl_news.png');
background-repeat:no-repeat;
width:110px;
height:119px;
color:transparent;
margin-top:27px;
}
You can use a quirk of the padding property to do this: the values for padding-top/padding-bottom when specified as percentages are calculated based on the width of the element (this also applies for margins).
Minimal example:
a {
background: url('http://i.stack.imgur.com/4kaLj.png');
background-size: contain;
display: block;
padding-top: 12.6324%;
}
The 12.6324% comes from the aspect ratio of your image: height/width*100 = 310/2454*100 = 12.6324%.
display: block; causes the element to take 100% width of it's container. Together, this solves your problem, as long as you know the aspect ratio of your images so you can calculate these percentages, this will work.
Jsfiddle example.
So here's an odd approach to this - using display: table in order to avoid needing to know the number of menu items you'll have. It's not quite perfect - as the images can sometimes be a pixel off in size, but if this works for you then great :)
HTML:
<div class="container">
<nav class="nav">
<ul>
<li class="logo"><img src="//placehold.it/145x157" /></li>
<li><img src="//placehold.it/110x119" /></li>
<li><img src="//placehold.it/110x119" /></li>
<li><img src="//placehold.it/110x119" /></li>
<li><img src="//placehold.it/110x119" /></li>
<li><img src="//placehold.it/110x119" /></li>
<li><img src="//placehold.it/110x119" /></li>
<li><img src="//placehold.it/110x119" /></li>
<li><img src="//placehold.it/110x119" /></li>
<li><img src="//placehold.it/110x119" /></li>
</ul>
</nav>
</div>
CSS:
.nav {
display: table;
width: 100%;
}
.nav ul {
display: table-row;
}
.nav li {
list-style: none;
padding: 0px;
margin: 0px;
display: table-cell;
vertical-align: bottom;
}
.nav img {
max-width: 100%;
border: solid 1px #000;
}
JSFiddle Example.
However if the slight difference in cell size is a problem for you (it is quite noticable), but you are happy to have a hard-coded requirement in css of the number of items in your menu, try using the same markup with the followng CSS instead:
.nav ul {
margin: 0px;
padding: 0px;
font-size: 0px;
}
.nav li {
list-style: none;
padding: 0px;
margin: 0px;
display: inline-block;
vertical-align: bottom;
max-width: 9%;
}
.nav li.logo {
max-width: 11%;
}
.nav img {
max-width: 100%;
border: solid 1px #000;
}
Jsfiddle example.

Using CSS justify to distribute width of body with image and text under

Can't seem to get this working - i have a group of images with text under them set to :block , when i try to get the ul or li class containing the images and the a class , i'm unable to get anything i've tried to work. I want to keep images and text under the image centered regardless of page width.
Set up jsfiddle here http://jsfiddle.net/89bnF/488/
HTML
<li class="single">
<ul>
<li><img src=""><a>Top Dawg</a></li>
<li><img src=""><a>THEEOhiostate</a></li>
<li><img src=""><a>slambodians</a></li>
<li><img src=""><a>Hollywood Shuffler</a></li>
<li><img src=""><a>Thunderbolts</a></li>
<li><img src=""><a>MeWantee</a></li>
<li><img src=""><a>Cincy Slammers</a></li>
<li><img src=""><a>Mahafaha</a></li>
<li><img src=""><a>SF Drug Lords</a></li>
<li><img src=""><a>Hudy Delight</a></li>
<li><img src=""><a>long shot</a></li>
<li><img src=""><a>Green Guy</a></li>
</ul>
</li>
CSS
.single ul {
text-align: justify;
border: 1px solid red;
}
.single li {
display: inline-block;
position: relative;
top: 1.2em;
}
.single ul:before{
content: '';
display: block;
width: 100%;
margin-bottom: -1.2em;
}
.single ul:after {
content: '';
display: inline-block;
width: 100%;
}
img {
width:100px;
height:30px;
background:black;
}
If you remove the position:relative and introduce the margin: 0 auto; it should centre the <li>
.single li {
display: inline-block;
top: 1.2em;
margin: 0 auto;
}

How can I style a list item to contain images without any gaps between them?

I'm trying to style a set of images contained within an unordered list so that:
The <ul> occupies the full width of the containing element
Each <li> occupies 25% of the width of the <ul>
The images contained in the <li>scale proportionately as the screen is resized
The 8 total images arrange in two stacked rows of 4 images each
There are no gaps, either vertically or horizontally, between the images
I can accomplish the first four objectives in the list above, but can't seem to lose the gap between the first and second row of images.
Here's the markup:
<div class="container">
<ul>
<li><img src="http://davidangerer.com/i/sample.jpg" alt="Image Description" /></li>
<li><img src="http://davidangerer.com/i/sample.jpg" alt="Image Description" /></li>
<li><img src="http://davidangerer.com/i/sample.jpg" alt="Image Description" /></li>
<li><img src="http://davidangerer.com/i/sample.jpg" alt="Image Description" /></li>
<li><img src="http://davidangerer.com/i/sample.jpg" alt="Image Description" /></li>
<li><img src="http://davidangerer.com/i/sample.jpg" alt="Image Description" /></li>
<li><img src="http://davidangerer.com/i/sample.jpg" alt="Image Description" /></li>
<li><img src="http://davidangerer.com/i/sample.jpg" alt="Image Description" /></li>
</ul>
</div>
And the (post-reset) CSS:
.container {
width: 90%
max-width: 960px;
margin: 6em auto;
}
.container ul {
float: left;
display: block;
width: 25%;
height: auto;
}
.container img {
max-width: 100%;
height: auto;
width: auto\9; /* fixes a bug in ie8 */
}
Thanks in advance for any help you can provide.
Cheers,
David
You will have to set display:block; on the images and the lis to avoid that ugly inline margin between them. Check out this working jsFiddle
.container img {
display:block;
max-width: 100%;
height: auto;
width: auto\9; /* fixes a bug in ie8 */
}
.container ul li{ display:block; }
image are inline-boxes and stands on baseline (defaut value : vertical-align:baseline;). This explains the gap underneath. you can either fix it like:
.container img {
max-width: 100%;
height: auto;
width: auto\9; /* fixes a bug in ie8 */
vertical-align:top;
}
or
.container img {
max-width: 100%;
height: auto;
width: auto\9; /* fixes a bug in ie8 */
vertical-align:bottom;
}
or make your inline-boxe into a block-boxe
.container img {
max-width: 100%;
height: auto;
width: auto\9; /* fixes a bug in ie8 */
display:block;;
}

css - Displaying images with horizontal scroll bar

I'm trying to display a number of images horizontally inside of a fixed-width div. I would like to use a horizontal scroll bar to display the images which do not fit inside the div.
However, the images are displaying vertically, rather than horizontally. Is there a way to force them to display side-by-side?
div#event {
width: 150px;
overflow-x: scroll;
overflow-y: hidden;
}
div#event ul { list-style: none; }
div#event img {
width: 100px;
float: left;
}
<div id="lasteventimg">
<ul><li><img src="./gfx/gallery/image1.jpg" /></li>
<li><img src="./gfx/gallery/image2.jpg" /></li>
<li><img src="./gfx/gallery/image3.jpg" /></li>
</ul>
</div>
Correct code for arbitrary number of images, if you don't know ul width:
#lasteventimg {width:150px;overflow-x:scroll;overflow-y:hidden;}
ul {list-style:none; display:block;white-space:nowrap;}
li {width: 100px;display:inline;}
<div id="lasteventimg">
<ul>
<li><img src="./gfx/gallery/image1.jpg" /></li>
<li><img src="./gfx/gallery/image2.jpg" /></li>
<li><img src="./gfx/gallery/image3.jpg" /></li>
<li><img src="./gfx/gallery/image4.jpg" /></li>
<li><img src="./gfx/gallery/image5.jpg" /></li>
</ul>
</div>
You will have to display the list items inline or float them and give the ul a very large width to avoid items moving to the next line:
ul {
width: 10000px; // for example
white-space: nowrap;
}
li {
float: left:
// or
display: inline;
}
I'd go with
div#lasteventimg ul li {
display: inline;
}
To make sure the li elements aren't rendered as block elements.
Well first of all, you need to change your styles from #event to #lasteventimg. Then, if you set the width of the ul to be wide enough to accommodate all the images, you should see the behavior that you're trying to get:
div#lasteventimg {
width: 150px;
overflow-x: scroll;
overflow-y: hidden;
}
div#lasteventimg ul { list-style: none; width: 300px; }
div#lasteventimg img {
width: 100px;
float: left;
}
<div id="lasteventimg">
<ul><li><img src="./gfx/gallery/image1.jpg" /></li>
<li><img src="./gfx/gallery/image2.jpg" /></li>
<li><img src="./gfx/gallery/image3.jpg" /></li>
</ul>
</div>
You need to change your list, so the elements are rendered horizontally rather then the default vertical.
#imagelist li
{
display: inline;
list-style-type: none;
}
Why don't you try this?
ul
{
width: 10000px;
white-space: nowrap;
}
li
{
display: block;
float:left;
}

Side by side items with images using CSS

I am trying to display images side by side using just css. The problem is that the images are not displaying inline but as a list (I am using the <ul> tag to accomplish this). Maybe I am using the wrong type of tag or my css is just wrong (see below):
ul {
float: left;
width: 15em; /* width is changed */
margin: 0;
padding: 0;
list-style-type: none;
list-style-position: outside;
}
li {
float: left;
width: 100%;
margin: 20%;
}
try this:
ul.gallery {
height:425px;
width:200px;
list-style-type: none;
}
ul.gallery li {
border:1px solid #BBBBBB;
float:right;
height:50px;
margin-right:7px;
margin-top:7px;
width:50px;
}
And the (x)html
<ul class="gallery">
<li><img src="images/0001.jpg" /></li>
<li><img src="images/0001.jpg" /></li>
<li><img src="images/0001.jpg" /></li>
<li><img src="images/0001.jpg" /></li>
<li><img src="images/0001.jpg" /></li>
<li><img src="images/0001.jpg" /></li>
<li><img src="images/0001.jpg" /></li>
<li><img src="images/0001.jpg" /></li>
<li><img src="images/0001.jpg" /></li>
</ul>
Why not use span tags rather than ul/li? Using the list tags is probably why you're getting a list.
are you trying to do this?
ul
li img 1 /li
li img 2 /li
/ul
if so, your css should be on the li element
li { display:block float:left padding: 0 margin: 0}
ul { padding:0 margin 0;}

Resources