This is easier to show in an image- http://puu.sh/n8WGw/0b01b020c7.png
I want the flex container to shrink as it's doing now, to match the height of the largest image, but I want the other images to match the height of the flex container. Whenever I try, they just become full height (their original height). Is there a way to accomplish this?
If I've not explained it well enough - I want the images to take up 100% of their li parent height, as shown by the blue box in the image attached.
We do not know what you did really, maybe overused flexbox rules ?
ul {
display: flex;
}
li {
list-style-type: none;
}
img {
height: 100%;/* in flex-child, the tallest will set rythm ... here it's about 200px; + gap height underneath if no reset of display or vertical-align */
}
<ul>
<li>
<img src="http://lorempixel.com/g/400/100/" alt="" />
</li>
<li>
<img src="http://lorempixel.com/g/400/200/" alt="" />
</li>
<li>
<img src="http://lorempixel.com/g/300/150/" alt="" />
</li>
<li>
<img src="http://lorempixel.com/g/400/100/" alt="" />
</li>
</ul>
or if flex set on li
ul {
display: flex;
width: 100%;
padding: 0;
}
li {
flex: 1;
overflow: hidden;
/* img will be cut off */
}
img {
min-height: 100%;/* again , tallest should set the rythm */
min-width: 100%; /* unless one doesn't fit the width */
display: block;
}
<ul>
<li>
<img src="http://lorempixel.com/g/400/100/" alt="" />
</li>
<li>
<img src="http://lorempixel.com/g/400/200/" alt="" />
</li>
<li>
<img src="http://lorempixel.com/g/300/150/" alt="" />
</li>
<li>
<img src="http://lorempixel.com/g/200/50/" alt="" />
</li>
</ul>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Some of the layouts I have been tasked with converting utilize the following layout (see image.) I'm wondering what the best way using CSS is to render a list of images accordingly. I don't want to split up the items into different row containers just to make things fit. I'm sure there's a way to create this a list.
The biggest problem I have is rendering the images with the appropriate margins in-between and having the far right image line up with the edge of the layout container (orange lines.)
Ideally, I'd like to maintain the html as something like:
<ul class="gallery-list">
<li class="gallery-list-item">
<img src="..." alt="..." />
</li>
<li class="gallery-list-item">
<img src="..." alt="..." />
</li>
</ul>
Thanks for your time and if there is a better solution, I'd be happy to close this and link to the solution.
This might be a solution, it might not. It makes a few assumptions like there always being four images per row but making them responsive.
The main points are:
We set the image container width a percentage of the parent container to control the number of images per row.
We use a right negative margin on the ul to offset the right margin applied to the li for image spacing.
We use calc to subtract the margin we use for spacing so all elements will fit on a single line and not begin to reflow.
I've used a couple media queries to show how you would change row count for smaller screens to make the grid responsive.
There's also a bit of padding and border applied to .container to demonstrate that the images are taking up the full width of the container.
This is just one way to do things.
body {
margin: 0;
}
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
.container {
width: 80%;
margin: 50px auto;
/* following adding for demo purposes */
padding: 50px 0;
border-left: 1px solid #CCC;
border-right: 1px solid #CCC;
overflow: hidden;
}
ul {
margin-right: -5px;
}
li {
float: left;
margin: 0 5px 5px 0;
width: calc( 50% - 5px );
}
img {
display: block;
width: 100%; /* or max-width: 100%; */
height: auto;
}
#media( min-width: 550px ) {
li {
width: calc( 33.333% - 5px );
}
}
#media( min-width: 750px ) {
li {
width: calc( 25% - 5px );
}
}
<div class="container">
<ul>
<li>
<img src="http://placehold.it/200x200/fc0/&text=1">
</li>
<li>
<img src="http://placehold.it/200x200/ccc/&text=2">
</li>
<li>
<img src="http://placehold.it/200x200/fc0/&text=3">
</li>
<li>
<img src="http://placehold.it/200x200/ccc/&text=4">
</li>
<li>
<img src="http://placehold.it/200x200/fc0/&text=5">
</li>
<li>
<img src="http://placehold.it/200x200/ccc/&text=6">
</li>
<li>
<img src="http://placehold.it/200x200/fc0/&text=7">
</li>
<li>
<img src="http://placehold.it/200x200/ccc/&text=8">
</li>
</ul>
</div>
There are a lot of ways you could go about this. I'm partial to flexbox - this example shows it with a fixed width on the gallery, but you can use a fluid one as well. I'd recommend doing some reading about flexbox, but here's a starting place:
.gallery {
display: flex;
flex-wrap: wrap;
width: 440px;
margin: auto;
padding: 0;
list-style: none;
}
.gallery img {
margin-right: 10px;
margin-bottom: 10px;
}
<ul class="gallery">
<li>
<img src="//placehold.it/100x100" alt="Artwork">
</li>
<li>
<img src="//placehold.it/100x100" alt="Artwork">
</li>
<li>
<img src="//placehold.it/100x100" alt="Artwork">
</li>
<li>
<img src="//placehold.it/100x100" alt="Artwork">
</li>
<li>
<img src="//placehold.it/100x100" alt="Artwork">
</li>
<li>
<img src="//placehold.it/100x100" alt="Artwork">
</li>
<li>
<img src="//placehold.it/100x100" alt="Artwork">
</li>
<li>
<img src="//placehold.it/100x100" alt="Artwork">
</li>
<li>
<img src="//placehold.it/100x100" alt="Artwork">
</li>
<li>
<img src="//placehold.it/100x100" alt="Artwork">
</li>
</ul>
You can also use display:flex and flex-wrap for this
I have changed markup from ul li's to div and img's and i have let the last image for every row to grow and fit the container
check this snippet
.gallery-list-item {
display: flex;
width: 100%;
margin-top: 10px;
}
div img:not(:first-child) {
margin-left: 10px;
}
div.gallery-list-item img:last-child {
flex: 1;
}
div.gallery-list {
display: flex;
flex-wrap: wrap;
justify-content: flex-start;
width: 450px;
border: 1px solid black;
padding: 0;
}
<div class="gallery-list">
<div class="gallery-list-item">
<img src="http://shushi168.com/data/out/123/36699123-images.png" alt="..." height=100 width=100/>
<img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>
<img src="http://shushi168.com/data/out/123/36699123-images.png" alt="..." height=100 width=100/>
<img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>
</div>
<div class="gallery-list-item">
<img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>
<img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>
<img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>
<img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>
</div>
</div>
Hope it helps
I am making an image slider and for some reason the parent element is adding whitespace at the bottom of parent div for no reason that I can tell. If there is a better way to set up the css then I would really appreciate a pointer. Here is the code:
HTML:
<div id="imgslider">
<ul class="bxslider">
<li>
<img src="http://localhost:8888/wordpress/wp-content/uploads/2015/08/DevelopImages_PureTisanes_LowRes-11.jpg" alt="" />
</li>
<li>
<img src="http://localhost:8888/wordpress/wp-content/uploads/2015/08/DevelopImages_PureTisanes_LowRes-1.jpg" alt="" />
</li>
</ul>
<div class="clear"></div>
CSS:
#imgslider {
display: block;
height: inherit;
}
.bxslider {
position: relative;
}
.bxslider img {
width: 100%;
max-height: 750px;
position: absolute;
}
.bxslider img:first-child {
top: 0;
left: 0;
z-index: 1;
}
There is a not closed div at the end :-)
Maybe this is what causing the issue:
<div id="imgslider">
<ul class="bxslider">
<li>
<img src="http://localhost:8888/wordpress/wp-content/uploads/2015/08/DevelopImages_PureTisanes_LowRes-11.jpg" alt="" />
</li>
<li>
<img src="http://localhost:8888/wordpress/wp-content/uploads/2015/08/DevelopImages_PureTisanes_LowRes-1.jpg" alt="" />
</li>
</ul>
</div>
<div class="clear"></div>
Not sure it will help but i have read somewhere "clear:both" cause adding some space at bottom so you may want to add "overflow:hidden" to the parent div (i think it is the div with id "imgslider")
#imgslider {
display: block;
height: inherit;
overflow:hidden;
}
I have a 100% width container I need to fill with 4 list items. Each list item has an image with text on the right of it. I need assistance aligning each image and text in the center of the list item and aligned center in the containing element. If the browser window is resized the text cannot wrap below the image and still be centered within the container.
I thought 100% container and 25% list items would work for the base sizing but I'm unsure of how to 'unwrap' the text around the image and center each item.
HTML:
<div class="container">
<ul class="products">
<li class="product">
<img src="http://placehold.it/50x50" />Asian Food</li>
<li class="product">
<img src="http://placehold.it/50x50" />Indian Food</li>
<li class="product">
<img src="http://placehold.it/50x50" />Asian Stuff</li>
<li class="product">
<img src="http://placehold.it/50x50" />Indian Stuff</li>
</ul>
CSS:
.container {
width: 100%;
margin: 0 auto;
overflow: auto;
background: grey;
}
.products {
margin: 0 auto;
list-style-type: none;
}
.product {
position: relative;
background: white;
float: left;
width: 25%;
text-align: center;
}
Here's my JSFiddle: http://jsfiddle.net/7N5HH/2/
Thank you!
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
I can't for the life of me get a set of images to line up on screen horizontally.
#full_image {
margin: 0;
padding: 0;
list-style: none;
white-space: nowrap;
overflow: hidden;
position: absolute;
}
#full_image ul li img {
display: inline;
margin: 0 auto;
width: 100%;
max-width: 100%
}
<div id="full_image">
<ul>
<li>
<img src="http://i.telegraph.co.uk/multimedia/archive/01636/saint-tropez-beach_1636818c.jpg" alt="" />
</li>
</ul>
<ul>
<li>
<img src="http://i.telegraph.co.uk/multimedia/archive/01636/saint-tropez-beach_1636818c.jpg" alt="" />
</li>
</ul>
<ul>
<li>
<img src="http://i.telegraph.co.uk/multimedia/archive/01636/saint-tropez-beach_1636818c.jpg" alt="" />
</li>
</ul>
</div>
You're creating a new list with each image, for starters; and each list is a block-level (not inline) element. Block elements start on a new line, by default.
Then, your display: inline is applied to the images, not to the li that contains them, which is still at block level.
Finally, list-style: none doesn't make sense on a div. I assume you mean to apply it to a list.
So:
#full_image {
margin: 0;
padding: 0;
list-style: none;
white-space: nowrap;
overflow: hidden;
position: absolute;
}
#full_image li {
display: inline;
}
#full_image li img {
margin: 0 auto;
max-width: 100%
}
<ul id="full_image">
<li>
<img src="http://i.telegraph.co.uk/multimedia/archive/01636/saint-tropez-beach_1636818c.jpg" alt="" />
</li>
<li>
<img src="http://i.telegraph.co.uk/multimedia/archive/01636/saint-tropez-beach_1636818c.jpg" alt="" />
</li>
<li>
<img src="http://i.telegraph.co.uk/multimedia/archive/01636/saint-tropez-beach_1636818c.jpg" alt="" />
</li>
</ul>
remove tags ul li and try again
Remove the list tags
http://jsfiddle.net/cxfNb/ they line up fine. If you want the bullet points, you'll have to remove the block style of the ul and li tags
<div id="full_image">
<img src="http://i.telegraph.co.uk/multimedia/archive/01636/saint-tropez-beach_1636818c.jpg" alt="" />
<img src="http://i.telegraph.co.uk/multimedia/archive/01636/saint-tropez-beach_1636818c.jpg" alt="" />
<img src="http://i.telegraph.co.uk/multimedia/archive/01636/saint-tropez-beach_1636818c.jpg" alt="" />
</div>