I'm trying to get some images to scale responsively in their parent container using the Bootstrap img-responsive class, but it's not going well. In this scenario, I want to display between 2 and 4 images in a grid pattern on the screen, and for the images to be responsive inside of their parent div.
The problem is that the images don't shrink with the img-container class.
https://jsfiddle.net/fcLv3750/2/
HTML
<div class="container-fluid">
<div class="row">
<div class="row-inner">
<div class="col-sm-6">
<div class="img-container">
<img src="http://i.imgur.com/gHDJC06.jpg" class="img-responsive">
</div>
</div>
<div class="col-sm-6">
<div class="img-container">
<img src="http://i.imgur.com/gHDJC06.jpg" class="img-responsive">
</div>
</div>
</div>
</div>
<div class="row">
<div class="row-inner">
<div class="col-sm-6">
<div class="img-container">
<img src="http://i.imgur.com/gHDJC06.jpg" class="img-responsive">
</div>
</div>
<div class="col-sm-6">
<div class="img-container">
<img src="http://i.imgur.com/gHDJC06.jpg" class="img-responsive">
</div>
</div>
</div>
</div>
</div>
CSS
.row-container {
background-color: red;
padding: 5px;
max-height: 50%;
height: auto;
}
.img-container {
padding: 4px;
max-height: 100%;
border: 5px;
border-color: black;
border-style: solid;
}
.col-sm-6 {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.row-inner {
background-color: orange;
height: 50vh; //I want the page contents to resize based on browser window height (no scrolling)
width: auto;
}
NOTE: The following will make the images become responsive, but it throws off the grid by making the img-container the full height of the row, which I prefer not to do. (I do not want the img-container height to be 100%)
https://jsfiddle.net/bm83ts0p/2/
.img-container {
padding: 4px;
height: 100%; //changed from max-height:100%
border: 5px;
border-color: black;
border-style: solid;
}
img{
max-height: 100%
}
A) What is causing the img-responsive class to be ignored?
B) What can I do to make the images responsive, and the img-container div not be 100% height.
EDIT: A key feature is that the content be resized to the browser window with no scrolling, hence the height: 50vh; for each of the 2 rows.
EDIT2: Here is the desired result (which only works when the images are at max-resolution with no scaling. Large images or smaller browser window produce the problems listed above)
I have deleted your wrapper "row-inner" from your code as it causes the clearfix issue. Mostly it is reason.
HTML:
<div class="container-fluid">
<div class="row">
<div class="col-sm-6">
<div class="img-container">
<img src="http://i.imgur.com/gHDJC06.jpg" class="img-responsive">
</div>
</div>
<div class="col-sm-6">
<div class="img-container">
<img src="http://i.imgur.com/gHDJC06.jpg" class="img-responsive">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="img-container">
<img src="http://i.imgur.com/gHDJC06.jpg" class="img-responsive">
</div>
</div>
<div class="col-sm-6">
<div class="img-container">
<img src="http://i.imgur.com/gHDJC06.jpg" class="img-responsive">
</div>
</div>
</div>
</div>
Columns in Boostrap grid immediately follows row. e.g row>col to avoid any kind of clearfix issue. Hope it helps.
JS Fiddle : https://jsfiddle.net/dncwdvkq/
Related
I am doing a simple page with a bootstrap grid and I am having some problems with placing the middle row occupying the full height in the middle.
These are the 3 rows: page
I want to place the row with the logo in the middle, but I am having problems with it. I placed height: 100%;, height: auto;. I even placed the container height to 100% and auto and nothing works!
I think there is a concept about bootstrap heigh that I am missing, because its the heigh that I have problems
html:
<div class="container-fluid">
<div class="row">
<div class="col-md-1 col-2">
<img id="menuIcon" (click)="toggleRightSidenav()" onmouseover="this.src='../../../assets/Images/generic/menuIcon_hover.png'" src="/assets/Images/generic/menuIcon.png" onmouseout="this.src='/assets/Images/generic/menuIcon.png'">
</div>
<div class="col-md-10 col-8">
</div>
<div class="col-md-1 col-2">
<img id="helpIcon" routerLink="/guide" onmouseover="this.src='../../../assets/Images/home/helpIconHover.png'" src="/assets/Images/home/helpIcon.png" onmouseout="this.src='/assets/Images/home/helpIcon.png'">
</div>
</div>
<div class="row row align-items-center mh-100" >
<div class= "col-md-12">
<img id="logo" src="/assets/Images/home/Logo.png">
</div>
</div>
<div class="row align-items-center justify-content-around fixed-bottom">
<div class="col-md-2 col-sm-2">
<img id="addButton" routerLink="/addPlayer" onmouseover="this.src='../../../assets/Images/generic/addIcon_hover.png'" src="../../../assets/Images/generic/addIcon.png" onmouseout="this.src='/assets/Images/generic/addIcon.png'" />
</div>
<div class="col-md-2 col-sm-2">
<p>Players Waiting: {{playerWaiting}} </p>
</div>
<div class="col-md-2 col-sm-2">
<p>Listed Players: {{playersListed}}</p>
</div>
<div class="col-md-2 col-sm-2">
<img id="searchButton" routerLink="/search" onmouseover="this.src='../../../assets/Images/generic/searchIcon_hover.png'" src="../../../assets/Images/generic/searchIcon.png" onmouseout="this.src='/assets/Images/generic/searchIcon.png'" />
</div>
</div>
</div>
css:
div{
border: yellow 1px solid;
}
#menuIcon{
width: 100%;
height: auto;
}
#helpIcon{
width: 100%;
height: auto;
}
#addButton{
width: 100%;
height: auto;
max-width: 127px;
}
#searchButton{
width: 100%;
height: auto;
max-width: 127px;
}
https://www.codeply.com/p/Xd0xi5hFNc
Couple of things.
To be able to fill several rows, you'd need a height for the container div.
Current HTML
<div class="container-fluid">
Update as
<div class="container-fluid d-flex vh-100 flex-column">
Now you can easily make your middle div fill the remaining gap by adding a fill-height class.
Current HTML
<div class="row row align-items-center mh-100" >
Update as
<div class="row row align-items-center fill-height">
Add this to your css
.fill-height {
flex: 1;
}
.fixed-bottom {
position: sticky;
}
jsFiddle
For screens, I put padding at 0 for container-fluid but the screen still does not fill the screen that is less than or equal to width 1024.
.container-fluid {
background-color: #e2e5f4;
height: 600px;
margin-bottom: 200px;
padding: 0;
}
What works:
at 1024- width: 101%;
at 768- width: 112%;
at 320- width: 112%;
HTML code:
<div class="container-fluid p-0">
<div class="row border bg-danger p-0">
<div class="col-md-7 p-0">
<div class="MededX">
MEDedX
</div>
<div class="capture_tittle">
liunhliunh
<br> lunliu luinu liuni
<br> nllniun Nilo ahh
</div>
<div class="subCapture_tittle">
LOerm Swaminarayan swmainarayan swaminarayan swaminarayan
</div>
</div>
<div class="col-md-5 p-0 ">
<div>
<img class= "hex" src="hex.png">
</div>
<div>
<img class= "medIpad" src="medipad.png">
</div>
<div>
<img class= "plantsLeft" src="plantsLeft.png">
</div>
</div>
</div>
</div>
What's the problem? How do I fix it?
Use a custom wrapper and set its width to 100%.
.container-full {
margin: 0 auto;
width: 100%;
}
or a fast solution
style="width:100%"
I am making all the images in my bootstrap grid the same height using an image wrapper and then retaining their native aspect ratios by letting their widths scale independently (see snippet below). Now I want to horizontally center them in their divs to make the layout tidier. I tried to use the image-container class below, but it hasn't worked in the various divs I've applied it to (currently just makes them disappear). Is there some way to horizontally center the images using the wrapper properties perhaps? I'd appreciate any helpful nudges. Thanks!
I'm looking for a successful way to:
keep the images responsively scaled to the same height with different aspect ratios (as seen in the snippet below)
horizontally center the images while retaining their responsive scaling (the point above)
See the snippet below for how the images are responsively scaled to the same height; the commented out image-container divs are one of my unsuccessful attempts to horizontally center the images after the scaling.
.thumbnail img{
max-width: 100%; /* do not stretch the bootstrap column */
}
.img-wrapper {
position: relative;
padding-bottom: 130%;
overflow: hidden;
width: 100%;
}
.img-wrapper img {
position: absolute;
top:0;
left:0;
width:auto;
height:100%;
}
.image-container {
display: flex;
justify-content: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
<!--<div class="image-container">-->
<div class="thumbnail">
<div class="img-wrapper">
<img src="http://via.placeholder.com/150x300"/>
</div>
</div>
<!--</div>-->
</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
<!--<div class="image-container">-->
<div class="thumbnail">
<div class="img-wrapper">
<img src="http://via.placeholder.com/200x350"/>
</div>
</div>
<!--</div>-->
</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
<!--<div class="image-container">-->
<div class="thumbnail">
<div class="img-wrapper">
<img src="http://via.placeholder.com/150x300"/>
</div>
</div>
<!--</div>-->
</div>
</div>
display: flex in combination with justify-content: center actually does center your images; the problem is that you have absolute positioning inside the flex:
.img-wrapper img {
position: absolute;
}
Simply removing the above code will cause your images to be centered. Note that you'll probably also want to remove top and left, as those will have no effect with the removal of position: absolute:
.thumbnail img {
max-width: 100%;
/* do not stretch the bootstrap column */
}
.img-wrapper {
position: relative;
padding-bottom: 130%;
overflow: hidden;
width: 100%;
}
.img-wrapper img {
/* position: absolute;
top: 0;
left: 0; */
width: auto;
height: 100%;
}
.image-container {
display: flex;
justify-content: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
<div class="image-container">
<div class="thumbnail">
<div class="img-wrapper">
<img src="http://placehold.it/150x300" />
</div>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
<div class="image-container">
<div class="thumbnail">
<div class="img-wrapper">
<img src="http://placehold.it/200x350" />
</div>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
<div class="image-container">
<div class="thumbnail">
<div class="img-wrapper">
<img src="http://placehold.it/150x300" />
</div>
</div>
</div>
</div>
Hope this helps! :)
I am struggling with creating a responsive image gallery. I am using CSS and bootstrap, i tried with tag and as background, I couldnt get it to work.
so, this is what I want to achieve:
https://www.socialprintstudio.com/products/
image grid without image warp or overlapping
When you make your browser smaler, image aspect ratio stays the same, only the size changes and later on you get 2 per row, that I can do with col-lg-4 col-md-6 etc...
I would also prefer if I could do this with img tag as I think it is easier to fade one image to another on hover.
this is my HTML
<div class="item " data-categoryid="2">
<div class="item-image">
<div class="img-bg" style="background: url("http://laravel.dev/images/bon2.jpg"); display: none;"></div>
<div class="img-bg" style="background: url("http://laravel.dev/images/bon.jpg");"></div>
</div>
<div class="item-name">Some name</div>
<div class="item-price">price €</div>
<div class="item-button">
Button.
</div>
</div>
As you said, using bootstrap's .col classes is the way to go here:
Here's a resizable jsfiddle: https://jsfiddle.net/aL1ndzgg/1/
Images, by default, will keep their aspect ratio, unless you specify both the width and the height.
For the hover effect you can have only one of the images for each box set as as position: absolute;; that way, the other image will set the size of the box.
.single-image-container {
margin-bottom: 20px;
position: relative;
}
.single-image-container img {
width: 100%;
}
.blocker {
position: relative;
overflow: hidden;
}
.hover-image {
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: opacity 125ms ease-in-out;
}
.single-image-container:hover .hover-image {
opacity: 1;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="col-xs-6 col-md-4 single-image-container">
<div class="blocker">
<img src="http://placehold.it/400x400" alt="img">
<img class="hover-image" src="http://placehold.it/400x400/f00" alt="img2">
</div>
</div>
<div class="col-xs-6 col-md-4 single-image-container">
<div class="blocker">
<img src="http://placehold.it/400x400" alt="img">
<img class="hover-image" src="http://placehold.it/400x400/f00" alt="img2">
</div>
</div>
<div class="col-xs-6 col-md-4 single-image-container">
<div class="blocker">
<img src="http://placehold.it/400x400" alt="img">
<img class="hover-image" src="http://placehold.it/400x400/f00" alt="img2">
</div>
</div>
<div class="col-xs-6 col-md-4 single-image-container">
<div class="blocker">
<img src="http://placehold.it/400x400" alt="img">
<img class="hover-image" src="http://placehold.it/400x400/f00" alt="img2">
</div>
</div>
<div class="col-xs-6 col-md-4 single-image-container">
<div class="blocker">
<img src="http://placehold.it/400x400" alt="img">
<img class="hover-image" src="http://placehold.it/400x400/f00" alt="img2">
</div>
</div>
</div>
</div>
To make your images responsive add the following CSS:
.item img {
max-width: 100%;
width: 100%;
height: auto;
}
So as long as the image source is square the image will resize to its bootstrap grid
You can also try the following: CSS Tricks: Responsive Images
I want to align different size images to the bottom of a div but the images are also in their own divs so the other answers don't work.
I have the following HTML:
<div class="row footer-links">
<section>
<div class="col-md-4">
<p>
Proudly presented and sponsored by
</p>
</div>
<div class="col-md-8 col-xs-12 logo-links">
<div class="row logo-bottom">
<div class="col-md-2 col-xs-12 logo-border col-md-offset-1">
<img id="rasv" class="center-block" src="img/rasv.png" alt="RASV logo">
</div>
<div class="col-md-2 col-xs-12 logo-border">
<img class="center-block" src="img/victoria-farmers.png" alt="Victoria Farmers logo">
</div>
<div class="col-md-2 col-xs-12 logo-border">
<img class="center-block" src="img/anz.png" alt="ANZ logo">
</div>
<div class="col-md-2 col-xs-12 logo-border">
<img class="center-block" src="img/pwc.png" alt="PWC logo">
</div>
<div class="col-md-2 col-xs-12 logo-border">
<img class="center-block" src="img/vac.png" alt="VAC logo">
</div>
</div>
</div>
</section>
</div>
I used the following CSS:
.logo-border {
border-right: 1px solid #d1d3d4;
height: 54px;
vertical-align: bottom;
display: table-cell;
}
Here is a Code Pen for more information.
You were really close.
The reason it wasn't working was because the columns are floated. Due to this, the elements weren't behaving like a table-cell, therefore rendering vertical-align: bottom useless.
You could simply add float: none and it will work as desired.
Updated Example
.logo-bottom .logo-border {
border-right: 1px solid #d1d3d4;
height: 54px;
vertical-align: bottom;
display: table-cell;
float: none;
}
As a side note, I increased the specificity of the selector .logo-border to .logo-bottom .logo-border so that float: left was overridden.