CSS resizing div and images inside div on window resizing - css

I have a single div with several images next to one another. Let’s say I have 7 in a row for this example.
Now, when I resize the browser, I’d like them to stay in the same row next to each other and resize their height and width accordingly.
How would I go about this?
Current example
<div>
<img src=“img1.jpg” />
<img src=“img2.jpg” />
<img src=“img3.jpg” />
<img src=“img4.jpg” />
<img src=“img5.jpg” />
<img src=“img6.jpg” />
<img src=“img7.jpg” />
<\div>

Try this:
div { display: grid; grid-template-columns: repeat(auto-fit, minmax(1px, 1fr)) }
img { width: 100%; height: auto; }
<div>
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
</div>
Explanation:
display: grid is the newest addition to the CSS layout techniques, and is the most elegant. It's widely supported in evergreen browsers, so all is good.
grid-template-columns: repeat(auto-fit, minmax(1px, 1fr)) finds a way to fit within 1 fraction of the whole width (so variable with the container size, which if you place this in an empty page, will vary with screen size too) with a minimum of 1px and auto-fit to whole width, each image.
width: 100% is needed to make sure the image isn't skewed.
height: auto has the same anti-distorting function if you want to use a full IMG tag with the width and height properties (which I fully recommend due to performance issues if you aren't building an HTTP/2 compatible site) or if you edit the height accidentally using some other CSS.
By the way, this works no matter how many images you have here, so no need to change the fractions or the percentages every time!
One last thing: when you have lists of any sort, I'd replace the <div> with an <ul> or <ol> if I were you, and have <li>s spread like this with some more CSS magic needed but a much more semantic DOM.
Image credits: IMDB.

A quick and dirty way is to use calc and float
div img {max-width: calc(100%/7);float:left;}
<div>
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
</div>

As suggested by Jon P below, you can use calc function of css. The other better way could be running media queries.
#media (min-width:1200px){
div{
width:500px;
}
div > img{
width:20%;
height:auto;
}
}

The very simplest way is to turn the container into a flexbox (which is also more widely supported than css-grid).
#imgs {
display: flex;
}
#imgs img {
width: 100%;
}
<div id="imgs">
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
</div>

Related

CSS columns not working with images

I have four images with different heights and I would like to do a masonry-like column grid using CSS columns. The following CodePen example doesn't fill the third column when setting columns to 3:
https://codepen.io/glennreyes/pen/pwjOmy
.columns {
columns: 3;
}
<div class="columns">
<img class="image" src="http://lorempixel.com/400/400" alt="" />
<img class="image" src="http://lorempixel.com/400/500" alt="" />
<img class="image" src="http://lorempixel.com/400/600" alt="" />
<img class="image" src="http://lorempixel.com/400/400" alt="" />
</div>
I want three images at the top and the fourth image to one column underneath. What am I missing to fill the content from top/left to bottom/right in a masonry style correctly?
It appears that the source of the problem is the display value of the images.
Images are, by default, display: inline.
If you switch them to display: block, the column property works.
revised codepen

How can I make these images center aligned?

I want to make the grey images center aligned. Please guide me how can I do this.
Here is what I have tried:
<div id="responsivearea" style="margin-top: 50px;">
<div class="img-center">
<img style="clear:none;" class="size-thumbnail wp-image-2707" src="http://www.inspuratesystems.com/nayajeevan/wp-content/uploads/2014/11/visa-logo2-150x150.jpg" alt="visa logo" width="150" height="150" />
<img style="clear:none;" class="size-thumbnail wp-image-2705" src="http://www.inspuratesystems.com/nayajeevan/wp-content/uploads/2014/11/nethope-logo1-150x150.jpg" alt="nethope logo" width="150" height="150" />
<img style="clear:none;" class="size-thumbnail wp-image-2704" src="http://www.inspuratesystems.com/nayajeevan/wp-content/uploads/2014/11/ILO-logo1-150x150.jpg" alt="ILO logo" width="150" height="150" />
</div>
</div>
Here is the site.
Simply use:
.img-center{text-align: center;}
As suggested already, magin auto... using your existing img-center class
.img-center{
margin:auto;
vertical-align:middle; /* If you mean vertically aligned */
}
text-align can work but is a little funny in cross browser support...
margin auto generally does the job and should work
otherwise just wrap them with center tags, while people may frown upon using center tags..THEY WORK CROSS BROWSER!! so they will NOT fail!
Otherwise, if you mean the footer images/logos on the site.. They are in a P tag without any class.. You can simply apply a text-align:center: to that p tag.
Set margin to auto. The browser will align the images to center for you
img {
margin: auto;
}

responsive images inside a full width div

I have this markup:
<div class="girls" style="text-align:center; margin-top:100px">
<img src="images/1.png" />
<img src="images/2.png" />
<img src="images/3.png" />
<img src="images/4.png" />
and this css (I'm using Twitter Bootstrap) :
img {
height: auto;
max-width: 100%;
vertical-align: middle;
border: 0;
-ms-interpolation-mode: bicubic;
}
The images have equal width and height and are displayed inline.
On my resolution are ok, fit the entire width (1366px), but on lower resolutions the images don't fit.
So, I need to keep the proportions on every screen resolution ( lower than 1366px in my case)
I've found this picturefill
Which I think is helpful for me, but I'm thinking that it's a simpler solution for my case because I have 4 images which I need to display them horizontally and make them scale on every resolution.
Thanks!
You can set the style width attribute of the images to 25%, without specifying height. That's gonna work if you're always putting 4 images, they have the same width between them and your container div is always at 100%.
HTH
Francisco
If you are using Twitter Bootstrap, then use markup properly like in Twitter Bootstrap documentation:
<div class="row-fluid">
<div class="span3">
<img src="http://placehold.it/350x400"/>
</div>
<div class="span3">
<img src="http://placehold.it/350x400"/>
</div>
<div class="span3">
<img src="http://placehold.it/350x400"/>
</div>
<div class="span3">
<img src="http://placehold.it/350x400"/>
</div>
<div>
http://jsfiddle.net/zNLBG/

Cropped images in my gallery but they are stacked on top of each other. How do I get them next to each other instead?

Here is the code:
<div class="crop">
<img src="image1.jpg" alt="image1.jpg" />
<img src="image2.jpg" alt="image2.jpg" />
</div>
.crop{
float:left;
margin:2px;
overflow:hidden; /* this is important */
position:relative; /* this is important too */
width:320px;
height:240px;
}
.crop img{
position:absolute;
top:-0px;
left:-0px;
}
I'm guessing I need to add something to my CSS? I know a solution would be to put the images as separate divs like this:
<div class="crop">
<img src="image1.jpg" alt="image1.jpg" />
</div>
<div class="crop">
<img src="image2.jpg" alt="image2.jpg" />
</div>
But I have next/previous arrows in my gallery so I need the images to be in the same div otherwise the arrows won't work.
I think the reason they are both appearing in the same place is because they are sharing the same css class, just name them differently with different top/left coordinates.

Possible for a row of inline images to not trigger horizontal scrollbar?

Does anyone know if it is possible to display a group of inline images horizontally in a row (or any element at that), allowing the group to extend beyond the right edge of the screen, without triggering the horizontal scrollbar?
A quick example:
<div class="page_container" style="width:900px; position:relative">
<div class="image_set">
<img src="xxx.jpg" alt="" width="200" height="400" />
<img src="xxx.jpg" alt="" width="200" height="400" />
<img src="xxx.jpg" alt="" width="200" height="400" />
<img src="xxx.jpg" alt="" width="200" height="400" />
...etc
</div>
</div>
The catch is that the image_set div that I would like to extend to, or beyond the right edge of the browser, is contained in a 900px div. The image_set needs to extend outside of its container, and to, or past the edge of the browser window.. without triggering a scrollbar.
The 900px wide page_container element needs to interact normally with the browser window (triggering scrollbars), and the element must contain the images. However, the images must be allowed to flow off the right edge of the browser window (visually)
So I'm wondering if there is a way to accomplish this with css only, without javascript?
Just make the container div have a fixed width and then give it the overflow: hidden proprty. So something like:
<div class="image_set" style="width:100%; overflow: hidden;">
<img src="xxx.jpg" alt="" width="200" height="400" />
<img src="xxx.jpg" alt="" width="200" height="400" />
<img src="xxx.jpg" alt="" width="200" height="400" />
<img src="xxx.jpg" alt="" width="200" height="400" />
</div>
By default overflow is auto which will apply scroll bars when needed thus setting it to hidden will mean that anything which does break the container will just disappear without needing scrollbars, and since for you that is the edge of the window, the effect will be complete.
In order to have the containing div cover the width of the entire page, you must first alter its layout model from block (which determines its width via its parent div) to absolute or fixed.
With the property position: absolute a div is positioned relative to its first parent with a relative position, this is usually the window itself, thus you can make the container div for the filmstrip use this property:
<div class="image_set" style="position: absolute; width:100%; overflow: hidden;">
</div>
So basically what you want to prevent is a double scroll bar?
try:
<div class="image_set" style="width:100%;position: relative;">
Here ya go: http://jsfiddle.net/Vppxp/
CSS
#filmstrip {
border-bottom: solid 5px rgb(220,220,220);
border-top: solid 5px rgb(220,220,220);
overflow-x: auto;
padding: 5px 0;
white-space: nowrap;
}
#filmstrip .cell {
background: rgb(220,220,220);
display: inline-block;
height: 100px;
width: 100px;
}
#filmstrip .cell + .cell {
margin-left: 1px;
}
HTML
<div id="filmstrip">
<div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div>
</div>

Resources