How do I scale a row of images to fit container width? - html

I want to display a grid of images that will scale automatically to fit the available width. The number of images per row is arbitrary and the available width can vary as the user re-sizes the window. The original images also may be of different widths but need to be displayed with the same width each. I'd prefer to avoid tables and need a CSS-only solution. The html I envisage is something like:
<div class='img-grid'> <!-- one per page, typically fixed 70% of page width -->
<!-- can be an arbitrary number of rows per grid -->
<div class='row'> <!-- width would be 100% of img-grid width -->
<!-- can be an arbitrary number of images per row -->
<div class='img-wrapper'> <!-- wrapper needs to force images to same width -->
<img class='image' src="test1.jpg"/>
</div>
</div>
</div>
Seems simple but I'm getting confused with what css to use on each of the 4 classes to force the images to the same width, to scale that width so that the (arbitrary number of) images fit within the row without wrapping and to ensure everything re-scales as the browser window width changes.
Thanks
Nigel

You can do it with CSS table layout, see the demo follows.
JSFIDDLE DEMO
.row {
display: table;
border-collapse: collapse;
width: 100%;
}
.item {
display: table-cell;
vertical-align: top;
}
.item img {
display: block;
width: 100%;
height: auto;
}
<div class="row">
<div class="item">
<img src="//dummyimage.com/100x100/aaa" />
</div>
<div class="item">
<img src="//dummyimage.com/200x200/bbb" />
</div>
<div class="item">
<img src="//dummyimage.com/300x300/ccc" />
</div>
</div>

Flexbox is usefull in your scenario:
Add display:flex to the row:
And flex: 1 to the row items:
.img-grid { width: 70%; }
.row { width: 100%; display: flex; }
img { width: 100%; }
.img-wrapper
{
flex: 1;
}
<div class='img-grid'> <!-- one per page, typically fixed 70% of page width -->
<!-- can be an arbitrary number of rows per grid -->
<div class='row'> <!-- width would be 100% of img-grid width -->
<!-- can be an arbitrary number of images per row -->
<div class='img-wrapper'>
<img class='image' src="http://placeimg.com/440/680/any"/>
</div>
<div class='img-wrapper'>
<img class='image' src="http://placeimg.com/640/380/any"/>
</div>
<div class='img-wrapper'>
<img class='image' src="http://placeimg.com/540/480/any"/>
</div>
<div class='img-wrapper'>
<img class='image' src="http://placeimg.com/640/280/any"/>
</div>
<div class='img-wrapper'>
<img class='image' src="http://placeimg.com/740/480/any"/>
</div>
</div>
</div>

demo - http://jsfiddle.net/u471prst/
* {
box-sizing: border-box;
}
.img-grid {
width: 70%;
border: 2px solid red;
}
.row {
width: 100%;
border: 2px solid blue;
}
.img-wrapper {
width: 100%;
}
.img-wrapper img {
width: 100%;
height: auto;
}
<div class='img-grid'>
<!-- one per page, typically fixed 70% of page width -->
<!-- can be an arbitrary number of rows per grid -->
<div class='row'>
<!-- width would be 100% of img-grid width -->
<!-- can be an arbitrary number of images per row -->
<div class='img-wrapper'>
<!-- wrapper needs to force images to same width -->
<img class='image' src="http://placeimg.com/640/480/any" />
</div>
</div>
</div>

Related

Bootstrap row - force the same height of divs when collapsed on small devices

I'm trying to force the same height of both divs in a bootstrap row when the row collapses on small device.
So the code is pretty much this:
<div class="row">
<div class="col-md-6">
content
</div>
<div class="col-md-6>
content
</div>
</div>
The bottom picture tells a thousand words: the top part of the image is what happens on bigger devices: both divs have same height which is what I want - but when I open the page on a small device, the green div's height is way lower than the red ones. How can I force the same height when the row collapses?
Do you need to heights to be dynamic or can they be a set height? If so, you could force the height by giving your divs an id and setting the css for devices of a certain size like so:
HTML:
<div class="row">
<div class="col-md-6" id="row1">
content
</div>
<div class="col-md-6" id="row2">
content
</div>
</div>
CSS:
#media only screen and (max-width: 768px)
{
#row1{
height: 300px;
}
#row2{
height: 300px;
}
}
You could create a new class (example: content1,content2), then manually set the dimensions you require in a css file.
.content1 {
background-color: red;
height: 400px;
}
.content2 {
background-color: blue;
height: 400px;
}
<div class="row">
<div class="col-md-6 content1">
content1
</div>
<div class="col-md-6 content2">
content2
</div>
</div>

CSS - Keep image aspect ration flexbox

I have a small photo gallery with some photo's.
It looks good on pc (full size), but when I resize it (for phone), it does not keep it's aspect ratio and it only streches the with.
This is what it looks like right now: https://gyazo.com/a1f605bb410865579025644b0a267adf
Also, as you can see it goes to 1 image, and at a certain point it goes back to 2 images for a split second and it stays on 1 image after. How do I fix that too?
This is my CSS:
#images{
display: flex;
flex-wrap: wrap;
}
.image{
display: flex;
flex: 1;
margin: 5px;
min-width: 250px;
min-height: 187.5px;
object-fit: contain;
}
.image > img{
flex: 1;
}
This is my HTML:
<div id="images">
<div class="image">
<img src="f1.jpg">
</div>
<div class="image">
<img src="f2.jpg">
</div>
<div class="image">
<img src="f3.jpg">
</div>
<div class="image">
<img src="f1.jpg">
</div>
//it just goes on and on like this
//it's all temporary now, I will eventualy replace
//this with a simple loop.
</div>
Unless you have very specific requirements, I suggest Masonry:
a JavaScript grid layout library. It works by placing elements in
optimal position based on available vertical space, sort of like a
mason fitting stones in a wall. You’ve probably seen it in use all
over the Internet.
in combination with imagesLoaded For a versatile lightweight solution.
There are many ways to implement Masonry.
The following is my personal favorite.
All my comments are inside the snippet below
body {
background: #131418;
}
/* Step 1: start with resetting some defaults */
* {
margin: 0 auto;
padding: 0;
max-width: 100%;
}
/* Step 2: center things inside the grid and clear some space around it by setting a device based max-width and margin*/
.grid {
text-align: center;
max-width: 95vw;
margin: 2.5vw auto;
}
/* Step 3: how big should the gap be between grid items? remember that the total gap between two items would be double what you set here since both would have that amount set as their individual padding. Also add box-sizing:border-box to make sure the padding doesn't affect the total widh of the item */
.grid-item {
padding: 5px;
box-sizing: border-box;
}
/* Step 4: Add media queries (subjective) to make the whole grid resposive. */
#media (min-width: 500px) {
.grid-item {
width: 50%;
}
}
#media (min-width: 1000px) {
.grid-item {
width: 33.333%;
}
}
#media (min-width: 1700px) {
.grid-item {
width: 25%;
}
}
#media (min-width: 2100px) {
.grid-item {
width: 20%;
}
}
<!-- Made possible by the great work of David DeSandro # https://masonry.desandro.com -->
<!-- Part 1: Add the scripts -->
<!-- Step 1: Let's start by loading jQuery. jQuery is not required for masonary to function but makes things easier -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Step 2: Then load imagesloaded. imagesloaded makes sure the images are not displayed until they are fully loaded -->
<script src="https://unpkg.com/imagesloaded#4/imagesloaded.pkgd.min.js"></script>
<!-- Step 3: we load masonry -->
<script src="https://unpkg.com/masonry-layout#4/dist/masonry.pkgd.min.js"></script>
<!-- Part 2: Create the grid -->
<!-- Step 1: Start with a the main grid wrapper-->
<div class="grid">
<!-- Step 2: Add grid items--->
<div class="grid-item">
<img src="https://s-media-cache-ak0.pinimg.com/736x/00/37/03/0037037f1590875493f413c1fdbd52b1--cool-beards-inspiring-photography.jpg" />
</div>
<div class="grid-item">
<img src="https://s-media-cache-ak0.pinimg.com/736x/cd/90/d9/cd90d9de63fa2c8e5c5e7117e27b5c18--gritty-portrait-photography-studio-photography.jpg">
</div>
<!-- Step 3: repeat...--->
<div class="grid-item">
<img src="https://1.bp.blogspot.com/-9QM7ciGXRkQ/V1hsB-wNLBI/AAAAAAAAMoA/eYbSHs00PTAjrI4QAmvYAIGCUe1AuRAnwCLcB/s1600/bryan_cranston_0095.jpg">
</div>
<div class="grid-item">
<img src="http://webneel.com/sites/default/files/images/project/best-portrait-photography-regina-pagles%20(10).jpg" />
</div>
<div class="grid-item">
<img src="https://s-media-cache-ak0.pinimg.com/736x/dd/45/96/dd4596b601062eb491ea9bb8e3a78062--two-faces-baby-faces.jpg" />
</div>
<div class="grid-item">
<img src="http://www.marklobo.com.au/news/wp-content/uploads/2013/03/Melbourne_Portrait_Photographer_Mark_Lobo-Cowboy.jpg" />
</div>
<div class="grid-item">
<img src="https://format-com-cld-res.cloudinary.com/image/private/s--PcYqe7Zw--/c_limit,g_center,h_65535,w_960/a_auto,fl_keep_iptc.progressive,q_95/145054-8576001-Rob-Green-by-Zuzana-Breznanikova_7725_b_w.jpg" />
</div>
<div class="grid-item">
<img src="http://www.iefimerida.gr/sites/default/files/janbanning11.jpg" />
</div>
<div class="grid-item">
<img src="https://s-media-cache-ak0.pinimg.com/736x/66/bb/e7/66bbe7acc0d64da627afef440a29714b--portrait-photos-female-portrait.jpg" />
</div>
<div class="grid-item">
<img src="https://s-media-cache-ak0.pinimg.com/736x/25/34/b6/2534b6c18c659546463f13b2dc62d4ce--natural-portraits-female-portraits.jpg" />
</div>
<div class="grid-item">
<img src="https://s-media-cache-ak0.pinimg.com/originals/8d/67/12/8d671230ced871df8428b571ed6ec192.jpg" />
</div>
</div>
<!-- Part 3: the script call -->
<!-- Now that everything is loaded we create a script to trigger masonary on $grid. Note that this simply says: "if the images are fully loaded, trigger masnory on $grid. -->
<script>
$(".grid").imagesLoaded(function() {
$(".grid").masonry({
itemSelector: ".grid-item"
});
});
</script>
Recently, I discover the CSS property : object-fit
She's contain multiple options :
contain
fill
cover
none
scale-down
It's very easy, and keeps a good ratio of the image
Documentation : HERE
UPDATE - I've added an example.
#images {
display: flex;
flex-wrap: wrap;
}
.image {
flex: 1;
}
.image img {
width: 100%;
height: auto;
}
<div id="images">
<div class="image">
<img src="https://i.guim.co.uk/img/media/4f48e55216fe5c09963e6cac4ec2530cc08a4e36/0_0_600_600/master/600.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=add90ae66a8a0c9b35606346845539f9">
</div>
<div class="image">
<img src="https://i.guim.co.uk/img/media/4f48e55216fe5c09963e6cac4ec2530cc08a4e36/0_0_600_600/master/600.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=add90ae66a8a0c9b35606346845539f9">
</div>
<div class="image">
<img src="https://i.guim.co.uk/img/media/4f48e55216fe5c09963e6cac4ec2530cc08a4e36/0_0_600_600/master/600.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=add90ae66a8a0c9b35606346845539f9">
</div>
<div class="image">
<img src="https://i.guim.co.uk/img/media/4f48e55216fe5c09963e6cac4ec2530cc08a4e36/0_0_600_600/master/600.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=add90ae66a8a0c9b35606346845539f9">
</div>
</div>
For keeping image aspect ratio, you can define width:100% and height:auto.
It will adjust the image to the width of the container with keeping the aspect ratio.

Centering 2 images with label in a row in Bootstrap

The goal is that I want both images to have be side by side and centered in the middle of the row.
I tried to do that via adjusting the columns of the row
The problem is that even with trying to center via rows, it always looks a little off center and if I change the max-width to be a little bigger, the images are no longer side by side and are on top of one another
The height and width of the images are...
graft1/graft2 - height="333" width="500"
ivan1/ivan2 - height="542" width="400"
Here is my HTML
<section class="wrapper style1">
<div class="container">
<div id="content">
<!-- Content -->
<article>
<header>
<h2>Before and After</h2>
</header>
<div class="row">
<div class="div_baPics">
<img id="graft1" class="baPics" src="images/graft1.jpg" alt="">
<label for="graft1">Before</label>
<img id="graft2" class="baPics" src="images/graft2.jpg" alt="">
<label for="graft2">After</label>
</div>
</div>
<div class="row">
<div class="div_baPics">
<img id="ivan1" class="baPics" src="images/ivan1.jpg" alt="">
<label for="ivan1">Before</label>
<img id="ivan2" class="baPics" src="images/ivan2.jpg" alt="">
<label for="ivan2">After</label>
</div>
</div>
</article>
</div>
</div>
</section>
And here is the CSS for baPics
.baPics {
max-width: 30%;
}
.div_baPics {
text-align: center;
}
Since you're using Bootstrap, I went with its system. See this fiddle :
http://jsfiddle.net/Bladepianist/55gyp94n/
Well, i did use real image so that you could see the result but with that (when I tested anyway), your image should resize, following the screen.
.thumbnail {
border: none;
}
This code isn't needed, unless you don't want the border of the thumbnail ;).
Hope it will satisfy you and if that's the case, thumbs up :p.
You need to wrap img and corresponding label in a wrapper, like so:
/*Just to make a difference between pics*/
body {
background: grey;
}
/*Minimal CSS*/
.div_baPics {
text-align: center; /*Center alignment for the wrapper*/
font-size: 0; /*To remove the white space between pics*/
}
.pic {
display: inline-block;
}
.pic img {
display: block;
/*This should be set by default by Bootstrap*/
max-width: 100%;
height: auto;
}
.pic label {
display: block;
font-size: 16px; /*Or whatever font-size you use*/
}
<div class="div_baPics">
<div class="pic">
<img src="http://i.imgur.com/zNTWaR3.jpg" />
<label>Pic 1</label>
</div>
<div class="pic">
<img src="http://i.imgur.com/IqiJN2f.png" />
<label>Pic 2</label>
</div>
</div>

How can i stylize this structure and add a scrollbar?

Hello I want to stylize this structure HTML with CSS , need to create 3 row . 1.header 2.maincontent 3.footer ! and i need to add a scrollbar for all mainpage , just 1 scrollbar not 1 per each row...
Like is on structure of code I want the style for header , maincontent and footer. Waiting for help.
<div id="header">
<div id="headerLeft">
<div class="msgs">Mesazh</div>
<div class="points">Points</div>
</div>
<div id="headerRight">
<div class="hungry">Hungry: </div>Action:
</div>
<div id="maincontent">
<div class="output">LALALALALALALA</div>
</div>
<div id="footer">
<div id="footerleft">
<div class="onlinePlayer">Klevi</div>
</div>
<div id="footerCenter">
<div class="map">harta</div>
<div class="forum">forumi</div>
<div class="logout">logout</div>
</div>
<div id="footerRight">
<div class="details">details</div>
<div class="inventory">inventory</div>
<div class="support">support</div>
</div>
</div>
</div>
You have already asked about the scrollbar 2 other times today.
The basic way to create a layout is using floats to display divs next to each other. You put these divs in a container. You can make the columns fluid with a percentage or fixed.
The HTML for the header would look like
<div class="row">
<div class="two cols">a</div>
<div class="one cols">s</div>
</div>
First css is for the row or container of the div.
.row {
width: 100%;
max-width: 960px;
margin: 0 auto;
background: #fff;
}
The second css is the base code for each type of column.
.col, .cols {
margin-left: 4.40%;
float: left;
min-height: 1px;
position: relative;
}
Below controls the width for the different columns
.col:first-child, .cols:first-child {
margin-left: 0px;
}
.row .one.cols {
width: 30.4%;
}
.row .two.cols {
width: 65.2%;
}
.row .three.cols {
width: 99.99999999999999%;
}
The example below is based on foundation by ZURB
http://jsfiddle.net/vmbm55fo/

2 stacked containers to the right of an image

I'm trying to achieve the following layout for a search result box. Specifically a 100% width and height image that on the right has two stacked containers that equals the height of the image, each with differing background colours that are butted up right against the image.
All attempts to achieve this simple layout are failing miserably. The issue I keep hitting is the when using something like:
<div class="search-result-box">
<div class="row">
<div class="col-md-3">
<img src="" class="img-responsive" style="height: 196px;" height="196">
</div>
<div class="col-md-9">
...
</div>
</div>
</div>
The image doesn't quite fill the col-md-3 column completely and thus you see the column's background.
What's the best way to do this?
Bootstrap columns have a padding of 15px by default. Also the image width has to be 100%. You can do something like this:
<div class="search-result-box">
<div class="row">
<div class="col-md-3" style="padding: 0;">
<img src="" class="img-responsive" style="width: 100%;">
</div>
<div class="col-md-9">
...
</div>
</div>
</div>
Jsfiddle: http://jsfiddle.net/HM4gE/1/
I wouldn't use Bootstrap columns though to achieve this since you seem to have some fixed heights and widths for columns. Instead I would do it like this (given that the height and the width of the image is always 196px): http://jsfiddle.net/HM4gE/2/
Check browser support for calc() before using it: http://caniuse.com/calc
Here a possible answer:
<div class="search-result-box">
<div class="row">
<div class="col-md-3">
<img src="" class="img-responsive" style="height: 196px;" height="196" />
</div>
<div class="col-md-9">
<div class="title">Title</div>
<div>Link1</div>
</div>
</div>
</div>
.search-result-box {
display: table;
width: 100%;
}
.row {
display: table-row;
}
.row > * {
display: table-cell;
}
.col-md-3 {
background: orange;
width: 260px;
height: 196px;
}
.col-md-9 {
vertical-align:top;
background: grey;
}
.title {
background: #ccc;
width: 100%;
height: 150px;
}
http://jsfiddle.net/junkie/fAPQ6/2/