Image not fully responsive despite of using 'img-fluid' class - html

I used img-fluid class inside my img tag but my image is not fully responsive. It is responsive for smaller screen but becomes unresponsive after the screen is enlarged to a certain dimension.
I've read How to make an image responsive using bootstrap without having it take up the entire width of the division? and Bootstrap: img-responsive vs img-fluid but couldn't solve the issue.
Here's how I tried
HTML:
<!-- THE HEADER -->
<div class="container-fluid header">
AUST CSE
</div>
<!-- IMAGE JUST BELOW HEADER -->
<div class="wrapper" style="background: blue">
<img src="images2/banner.jpg" class="img-fluid">
</div>
CSS:
.wrapper {
width: 100%;
}
In smaller screens, the page looks like this:
however, in larger screen, the image don't occupy 100% of the space and looks like this:
I want the image to occupy 100% of the width and scale up its height, just like it does when the screen is smaller.

img-fluid uses max-width: 100%;. Once the containing element is the same size or larger than the image's width, it will stop resizing.
Two options:
1) Use an image with a resolution that is at least the widest width of your container element. If the width of your container element does not have a fixed top end (i.e. will always be 80% of viewport width), then pick a sufficiently large image so that it will look good on the majority of displays (lookup stats on most common browser resolutions).
#import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css' );
/* For demo purposes and is not required. */
.demo {
background-color: rebeccapurple;
}
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="demo">
<img class="img-fluid" src="https://via.placeholder.com/1000x1000">
</div>
</div>
<div class="col-md-8">
<p>
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
</p>
</div>
</div>
2) Override .img-fluid so that the image will resize beyond native resolution. The drawback here is the image will get grainy. The smaller the native resolution, the more grainy it will become when scaled to large areas. You can see in my example that the text is quite fuzzy.
#import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css' );
/* For demo purposes and is not required. */
.demo {
background-color: rebeccapurple;
}
/* Will override all instances of .img-fluid. */
.img-fluid {
width: 100%;
}
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="demo">
<img class="img-fluid" src="https://via.placeholder.com/100x100">
</div>
</div>
<div class="col-md-8">
<p>
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
</p>
</div>
</div>
</div>
I've also included a scoping example below which allows you to override only specific images to extend past their native resolution.
#import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css' );
/* For demo purposes and is not required. */
.demo {
background-color: rebeccapurple;
}
/* Scoped so that we can target specific images. */
.img-fluid.img-full-width {
width: 100%;
}
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="demo">
<img class="img-fluid img-full-width" src="https://via.placeholder.com/100x100">
</div>
</div>
<div class="col-md-8">
<div class="demo">
<img class="img-fluid" src="https://via.placeholder.com/100x100">
</div>
</div>
</div>
</div>

If you want the image to span all the way across the width of the page on larger screens, then you need to make sure that your image width is just as large as the screen width.
Based off of your issue, it looks like your dimensions are not large enough. The img-fluid class will resize your image, but only to the max of its dimensions.
There are 1 of 2 things you can do to fix it.
(The preferred method) Pick an image that has the correct width for the max size screen you want. (Most of the time, that would be 1920px)
You can add width: 100% to your image so that it will span the full width of your page. But, if the width of your image is smaller than your screen, then the image will not be as clear, which is why it's best to use images that are the correct dimensions.
Example:
Here is an image that has smaller dimensions (your issue): JSFiddle
.container {
border: 2px solid red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<h2>Image</h2>
<p>The .img-fluid class makes the image scale nicely to the parent element (resize the browser window to see the effect):</p>
<img class="img-fluid" src="https://cdn-static.denofgeek.com/sites/denofgeek/files/styles/main_wide/public/2015/11/main_0.jpg?itok=k1tyTR75" alt="HL2">
</div>
Here is an image that has larger dimensions (1920px): JSFiddle
.container {
border: 2px solid red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<h2>Image</h2>
<p>The .img-fluid class makes the image scale nicely to the parent element (resize the browser window to see the effect):</p>
<img class="img-fluid" src="https://s.aolcdn.com/hss/storage/midas/f8eff2a90708d58814bb4adc93634cbb/205752037/half-life-2-15622-1920x1080.jpg" alt="HL2">
</div>

Related

Always show image regardless of desktop browser dimensions

For the below image I need to always show the whole background image regardless of the desktop window size and always ensure that the text layout remains the same, ie, the button is always sitting at the bottom of the section and not have a big blue gap below it.
*Note my image is just an example, the white rectangle is really a picture of a product but I have removed it because I cannot release such photos.
How best can I achieve this? This modern web design of maintaining the same look across different desktop browser screens is really troubling me because the methods I have used mean the design is not consistent across different browser screens, ie, the white rectangle gets clipped at the bottom or the text content height is too small for the blue section height and we get a big blue gap below it.
My techniques I have tried:
Use a bootstrap 3.3 row (the website uses this version of the library no choice in this) with 2 columns with the dimensions 3/12 and 9/12. This works but sometimes I get a big blue gap under the text content (button) when I would really like it to spread out vertically evenly.
Make the section have a CSS background image. Set the image to 'cover'. This results in clipping of the bottom of the white rectangle in certain desktop window dimensions.
Any advice on how best to approach this design using CSS3, Bootstrap 3.3 or etc.?
.blue-section {
background-color: #1caaf2;
color: #fff;
padding-top: 10px;
padding-bottom: 10px;
}
.blue-section h1,
.blue-section h2,
.blue-section h3,
.blue-section h4,
.blue-section h5,
.blue-section p {
color: #fff;
}
.blue-bk {
background: url('https://i.stack.imgur.com/Sgm8G.png') !important;
-webkit-background-size: contain !important;
-moz-background-size: contain !important;
-o-background-size: contain !important;
background-size: 100% !important;
background-repeat: no-repeat !important;
width: 100%;
}
.banner-img {
height: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<section class="section-container blue-section">
<div class="container">
<div class="row">
<div class="col-xs-3">
<img class="img-responsive banner-img" src="https://i.stack.imgur.com/Sgm8G.png" />
</div>
<div class="col-xs-9 mht-db-header-information">
<h1 class=""><strong>Lorem ipsum Lorem ipsum Lorem ipsum </strong></h1>
<h3>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</h3>
<div class="text-center">
<a class="button-primary" href="">Button</a>
</div>
</div>
</div>
</div>
</section>
<section class="section-container blue-section blue-bk">
<div class="container">
<div class="row">
<div class="col-xs-3">
</div>
<div class="col-xs-9 mht-db-header-information">
<h1 class=""><strong>Lorem ipsum Lorem ipsum Lorem ipsum </strong></h1>
<h3>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</h3>
<div class="text-center">
<a class="button-primary" href="">Button</a>
</div>
</div>
</div>
</div>
</section>
All images:
White rectangle only (in production will be an image of a product):
Background image I use:

Unable to design a HTML/CSS block, can't get images to align using bootstrap

I'm trying to design the following block, given in image
The background image of building is separate from the human image, how can I use bootstrap grid system to align the images and text in this way, also keep the aspect ratio of images?
The background image is spread to 100% but the the content and human image is centered and aligned with other content
Use the building image as background for your body tag and the human image as an background for either .container or .row class.
Also the human image should be aligned right.
Something like
body {
/* image just for reference*/
background: url('http://www.eliteconcreterestoration.com/blog/wp-content/uploads/concrete-office-park-buildings.jpg');
background-repeat: no-repeat;
background-size: 100% 100%;
}
.container {
height: 100%;
/* image just for reference*/
background: url('https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcS2-Dj0_UAhag-zIDaVGoV2LuCIy62nGvt_zNJoeILF1VqM3EXOdK20qR6N');
background-repeat: no-repeat;
background-position: right;
}
.jumbotron {
background: transparent !important;
}
.jumbotron h1 {
font-size: 36px;
color: white !important;
}
.jumbotron .text{
color:white;
font-size:12px;
}
<html>
<head>
<link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<div class="row">
<div class="jumbotron">
<h1>A good <span style="color:lightgreen;">investment</span> pays the best <span style="color:orange;">interest</span></h1>
<p class="text">lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum </p>
<p><a class="btn btn-primary btn-lg" style="background:lightgreen;" href="#" role="button">Register</a><a class="btn btn-primary btn-lg" style="background:orange;" href="#" role="button">Learn more</a>
</p>
</div>
</div>
</div>
</body>
</html>
With this design, you don't (strait forward that is) The hand part will mess up your grid layout. Or merge images, or use a smaller version of your human image that will stay inside the grid (all blocks are squares with Bootstrap, and there is NO layering beyond background image of parent element.).
* Edit As Kishore Kumar Points out, you can. Something like this:
<body> <!-- has CSS background with buildings -->
<div class="container"> <!-- has CSS background with human, float: right --> <div class="row">
<div class="col-md-3">And more stuff for layout...</div>
</div
</div>
</body>
Here is how i would do.
create a section and give it the background image with background-size: cover; property and then use a container inside the section and put my grid.
<section class="building-bg">
<div class="container">
<div class="row">
<div class="col-md-8">
<!-- text content -->
</div>
<div class="col-md-4">
<!-- png image with some negative margins or translate property -->
</div>
</div>
</div>
</section>

How to create borders in bootstrap when div is 0 height

Using Twitter-Bootstrap, I have a section of a page that includes a sidebar (which does not take up the whole page, just spans down to the bottom of this section). I have several sections to the right of the sidebar, each of which contain an image on one side, some text on the other, and require a bottom-border.
I'm having trouble with the bottom-border. Since each section is contained in a div, and everything's floated using Bootstrap columns, the divs are 0 height and the borders all float to the top.
EDITED for clarification: I need two distinct columns in this section - a left-hand one (spanning 3 columns) that contains only the sidebar (and takes up the 3 columns all the way to the bottom, leaving blank space after the list), and one (spanning the remaining 9 columns) that contains the story-sections but that stays on the right-hand side of the page.
EDITED to include image (grey lines are included for reference and will not show up on the page, black lines are the borders I'm trying to apply)
Attempted solutions:
Bottom-border on individual elements: I need one solid line across the bottom of each section, so I can't apply bottom-border to both the image and the text.
Clearfix: Since I have this sidebar, I can't use a clearfix, because it pushes everything down past the sidebar.
Overflow: Using overflow causes the images to shrink (which I don't quite understand, but it doesn't help with the border either).
How can I create bottom borders for each of these sections?
<section class="stories">
<div class="container">
<h2>Section header</h2>
<div class="col-sm-3 sidebar">
<h6>list of stories</h6>
<ul>
<li>list-item</li>
<li>list-item</li>
<li>list-item</li>
<li>list-item</li>
</ul>
</div>
<section class="story-section">
<div class="col-sm-3">
<img src="http://placehold.it/330x220">
</div>
<div class="col-sm-6">
<h6>keyword</h6>
<h3>Header of section</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eleifend dictum neque sed laoreet ...</p>
</div>
</section>
// (there are three more sections with the same class of "story-sections", plus other code on the page, but I've tried to simplify it as much as possible here.)
</div>
</section>
css:
html, body {
height: 100vh;
}
.stories {
.sidebar {
height: 100vh;
}
.story-section {
border-bottom: 1px solid $grey;
}
}
I'm still fairly new with Bootstrap, so there may be a very simple solution, but I haven't been able to track it down. Any help is very welcome!
if you want to have a sidebar with 3 columns + a left section with the remaining 9 columns, then you need to set col-*-9 in your story-section
Note: in bootstrap, you need to have .row right below .container or between nested col-*
html,
body {
height: 100vh;
}
.sidebar {
height: 100vh;
background: lightblue
}
.story-section {
border-bottom: 1px solid grey;
}
img {
margin-top: 10px
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<section class="stories">
<div class="container">
<div class="row">
<h2>Section header</h2>
<div class="col-xs-3 col-sm-3 sidebar">
<h6>list of stories</h6>
<ul>
<li>list-item</li>
<li>list-item</li>
<li>list-item</li>
<li>list-item</li>
</ul>
</div>
<section class="story-section col-xs-9 col-sm-9">
<div class="row">
<div class="col-xs-6 col-sm-6">
<img class="img-responsive" src="http://placehold.it/330x220">
</div>
<div class="col-xs-6 col-sm-6">
<h6>keyword</h6>
<h3>Header of section</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eleifend dictum neque sed laoreet ...</p>
</div>
</div>
</section>
<section class="story-section col-xs-9 col-sm-9">
<div class="row">
<div class="col-xs-6 col-sm-6">
<img class="img-responsive" src="http://placehold.it/330x220">
</div>
<div class="col-xs-6 col-sm-6">
<h6>keyword</h6>
<h3>Header of section</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eleifend dictum neque sed laoreet ...</p>
</div>
</div>
</section>
</div>
</div>
</section>

Issue with text exceeding the container (bootstrap)

as you can see on this jsfiddle, if you play with the window size, the lorem ipsum text sometimes goes outside the white container.
I can't figure out why, because when I look at the code, everything seems to be embedded within the main container so I would expect the text to adapt the fluidly adapt to the window size.
What is the issue?
Thanks,
<section>
<div class="container content">
<div class="row">
<div class="col-md-3 bordering">
<h2>Qui <b>sommes-nous?</b></h2>
<h3>Actifs depuis </h3></div>
<div class="col-md-9">
<div class="title-block">
<p>
Lorem ipsum dolor sit amet, connecteur adipiscin
<p>
etc.
It is because you have applied a fixed width to .container:
.container {
width: 1260px; /* remove or edit this line */
}

How to get an image to stick to the bottom of a div AND overlap the div above it

I'm trying to get an image to stick to the bottom of one div AND overlap the div above it.
fiddle is here: http://jsfiddle.net/5U34m/1/
There are 2 divs side by side the image in the right side
- if the content of the left is shorter than the right, the image should overlap the row above it
- if the content of the left is longer than the height of the image then the image should stick to the bottom of the right div.
- the image should also be centered in the div
here is the HTML:
<div class="container clearfix">
<div class="wrap header">
<div class="grid_6 logo">
<img src="http://cba.thelibertylab.com/wp-content/uploads/cba-logo-placeholder.png" />
</div>
<div class="grid_6 phone">
<!-- <p>p. 905-579-5302</p> -->
</div>
</div>
<div class="wrap upper-content">
<div class="grid_6">
<p>Nunc id porttitor lectus, et auctor ante. Morbi ullamcorper quam in leo auctor tempor. </p>
<p> Morbi a enim nibh. Vestibulum molestie augue libero, vitae fringilla massa eleifend quis. </p>
</div>
<div class="grid_6 headshot"><div class="img-container"><img src="http://www.manncontractors.com.au/media/pics/site/imagecache/1/1/1176381DF49D6256D968FFB72490208C.jpg" height="400" width="300"/></div></div>
</div>
<div class="wrap lower-content">
<div class="grid_6">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
</div>
<div class="grid_6">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
</div>
</div>
</div>
The CSS on the fiddle does not work correctly - I'm not sure what to try from here.
Your problem is multiple-fold.
You actually want the text to push down the image when it is longer, so your image should be UNDER your text in the HTML, not adjacent to it. Your structure should be like this:
<div> <!-- First row contains the text -->
<div class="grid_6 txt>YOUR TEXT</div>
<div class="grid_6></div>
</div>
<div class="grid_12"></div><!-- This one to prevent the float from stacking -->
<div> <!-- Second row contains the image and has height = 0 -->
<div class="grid_6></div>
<div class="grid_6 headshot">
<div class="img-container">
<img src="yourimg.jpg">
</div>
</div>
</div>
The CSS
First of all clear the styling height:100%; from .container. This styling rule inhibits the element from expanding and makes it impossible for children to inherit height. (Check for yourself in the console)
If you want the image to truly stick to the bottom, you will have to set .grid6.headshot { line-height: 0; } The line-height property actually inserts unwanted space in elements where you don't want it.
Replace .img-container property top: 70px; with margin-top: -450px; (your image height)
Set a min-height on the grid_6 div which has text in it, equal to your image height (450px) - your header height (94px) + some padding (10px) => 346px; If you don't do this your image will overlap your navigation
For #media only screen and (max-width: 767px), set .img-container { margin-top: 0; } else the image will overlap the text on mobile.
I've updated your fiddle here: http://jsfiddle.net/5U34m/4/
Note:
Consider using id's to target specific divs. The classes here are quite a mess.
Consider using responsive CSS frameworks (% and ems) instead of fixed-width ones.
You'll need to use position: absolute for the styling of your image. Then use the Left, Right, Top, Bottom attributes to position it as you like.
To adjust what element goes on top of each other, use the z-index attribute!