How to set beckground image full div without using height - html

I have first div generated dynamically and second div I have placed it.
First div image is taking 200X200 by dynamically. Same size image i want to set it through css in second div.
But when I tried to set it and it's not taking full height 200 of the image.
If i mention min-height it's not coming properly for all resolutions. I don't want to specify min-height for the media queries.
How to set background image height automatically?
First Div structure
<div class="product-layout col-lg-3 col-md-3 col-sm-6 col-xs-6">
<div class="product-thumb transition">
<div class="image"><img src="../image/cache/catalog/savouries/karasevu-200x200.jpg" alt="Karasevu" title="Karasevu" class="img-responsive"></div>
<div class="caption">
<h4>Karasevu</h4>
<p class="price"> $0.00 </p>
</div>
<div class="button-group home-btn">
<button class="producthome-btn" type="button" onClick="cart.add('71');"><i class="fa fa-shopping-cart cart-icon"></i> <span class="">Add to Cart</span></button>
</div>
</div>
</div>
Second Div structure
<div class="more-link product-layout col-lg-3 col-md-3 col-sm-6 col-xs-12">
<div class="a-more-link"> VIEW MORE </div>
</div>

Use the background-size property.
.someClass {
background-image: url(someImage.jpg);
background-size: cover;
}

First method use background-size to 100%
.classname {
background-image: url(img.png);
background-size: 100%;
}
Second method use background-size to cover
.classname {
background-image: url(img.png);
background-size: cover;
}

Try using background-size:cover and you can center it by using background-position:center

Set your body element to 100% like this:
body{
height: 100%;
}
then use inherit or set your bg height to 100% like this:
.yourParentDiv{
height: 100%;
// your other code here
}

Related

Display image inside the box without stretching it

I developed an image gallery.
My problem is that I am using 100% height and sometimes the image stretches, ending up losing quality.
Is there a way to place the image in the center of the box without stretching it and in the center if it is not tall enough to fill the box completely?
Demo
My code
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="row">
<div class="drop dropp">
<div class="abc">
<ngb-carousel style="outline: none;" id="carousel" #carousel *ngIf="data" data-interval="false"
data-ride="carousel" data-pause="hover" (slide)="change($event)">
<ng-template *ngFor="let imgIdx of data; let i = index" [id]="i" ngbSlide>
<div class="picsum-img-wrapper">
<img [src]="imgIdx.image" style="border-radius: 8px; object-fit: fill;" class="img-responsive">
</div>
</ng-template>
</ngb-carousel>
</div>
</div>
<div class="row">
<div class="Upcard" *ngFor="let imgIdx of belowImageData; let i = index">
<img class="img-responsive" [src]="imgIdx.image" style="width: 100%; object-fit: fill; height: 100%; border-radius: 8px;">
</div>
</div>
</div>
</div>
**My Problem **
As you can see in the image, it fills the whole box but is deformed :( I want it not to lose quality and if it doesn't have enough height, stay centered on the box. I want the box to have a fixed height.
If you don't care about the old browsers you can use object-fit property on the img directly.
for example:
object-fit: cover;
https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
Otherwise you can add an empty div, give it the full size of the box, and use the image as a background of that div. Then use background-size property.
Hope it helps.
I have tested the code, you can try adding the below code in css.edit and take a look
.img-responsive {
object-fit: contain; height:200px!important;
}
or to need all images to fit properly you have to remove the div global height 100 percentage. and set the height specific for each div. so you don't need height parameter in .img-responsive class.

Auto adjust/crop the height of an image inside a bootstrap col, to be equal to height of another col

I have a boostrap row containing 2 columns, the left column contains some text, the right column contains an image.
I want to set the max-height of the right column to be equal to the height of the left column - which may change due to resizing of the window (has a min-height property).
I want to crop the image which I believe can be achieved using "object-fit:cover" within css
My code is as follows:
<div class="row" id="test-row">
<div class="col-2" id="test-left">
Some random text
</div>
<div class="col-10" id="test-right">
<img src="img_source" class="img-responsive">
</div>
</div>
I have looked online and at other posts but all seem to be based around the column not needing to be cropped or downsized.
This is possible if you are willing to make your image a background image.
<style>
#bg-image {
background-image: url('example.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
width: 100%;
height: 100%;
}
</style>
<div class="row" id="test-row">
<div class="col-2" id="test-left">
Some random text
</div>
<div class="col-10" id="test-right">
<div id="bg-image"></div>
</div>
</div>
Otherwise use a javascript window resize event to set the height.

Responsive image with the same height as another column in the same row

I am trying to place an image to the right of a form and I need the image to be the same height as the form div. I'm having a really difficult time accomplishing this without distorting the image.
The image I'm using is 1920 X 1200.
The code that I have got is as follows:
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-sm-4 govx-form">
<div class="form-background">
<?php echo do_shortcode(); ?>
<?php echo do_shortcode(); ?>
</div>
</div>
<div class="col-xs-12 col-sm-8 govx-form price-results" id="ap_pricing_results">
<img src="wp-content/images/fourHeroesLarger_edit.jpg" alt="" class="img-responsive">
</div>
</div>
</div>
I have of course tried to use css that makes the height match the height of the form but as soon as I do that it ruins the responsiveness of the image. Additionally, when I set a height I set the width to 100% to fill the div, but that distorts the image really bad.
I have also tried doing a background image with the following css styles:
.price-results {
background-image: url(/wp-content/images/fourHeros.jpeg);
background-repeat: no-repeat;
background-size: 335px;
background-position: top;
height: 130px;
}
This works on smaller screen sizes but again ruins the picture on larger screen sizes.
Have you tried:
.price-results {
background-image: url(/wp-content/images/fourHeros.jpeg) no-repeat;
background-size: 335px auto;
background-position: top;
}
Also, do you have a running website? It can help out in figuring it out.

How to show the entire picture but in a smaller width and height

I want to display a picture with background-image: url("mypic.png");
Html:
<div class="container-fluid">
<div class="row">
<div class="col-sm-4">
<div id="example1"></div>
</div>
<div class="col-sm-4">
<div id="example2"></div>
</div>
<div class="col-sm-4">
<div id="example3"></div>
</div>
</div>
</div>
For example, I have 686x350 image. I want that no matter what, it will display on the background of the #example1 and that ill see the full image, even if I resize the browser or something. Any suggestions?
To scale the background image to fit inside the div:
#example1{
background-size: contain;
}
To scale the background image to cover the whole div:
#example1{
background-size: cover;
}
See JSFiddle example
It sounds like you need the background-size: cover property:
#example1 {
background-size: cover;
}
This will scale the image to cover the entire element it is in, regardless of screen size / resolution.

Image overlapping parent div responsiveness

What I'm trying to achieve is the image outside of the parent div's boundaries, which works in 1280x1024. How can I make this effect responsive?
Html:
<div class='row'>
<div class="divider">
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 wow fadeInRight" data-wow-delay="1s" data-wow-duration="1.5s">
<img src="http://karsbarendrecht.nl/5/wp-content/uploads/2015/10/responsive.png" alt="laptop tablet phone responsive"/>
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 wow fadeInRight" data-wow-delay="1s" data-wow-duration="1.5s">
<h1>bla bla bla.</h1>
</div>
</div>
</div>
CSS:
.divider{
margin-top:100px;
min-height:175px;
margin-bottom:100px;
background-size: cover!important;
background-attachment: scroll;
background-image: url('http://karsbarendrecht.nl/5/wp-content/uploads/2015/10/triangles.svg');
}
.divider div{
top:-50px;
The following image show's my problem, with the top half being the desired effect. And the bottom half is what happens when resizing the screen to smaller resolutions.
The problem seems to be that the image is resizing itself but the parent div is not, in this case I would change the values in pixels to values in vh and vw (these are percentages of "viewport height" and "veiwport width": the height and width of the window you are resizing.) for example:
margin-top:100px;
would become
margin-top:8vh;
By placing both the background image and the front image in HTML, and adding the following css rules they both scale the same.
.divider div{
top:-50px; // placing the image slightly above the divider div
height:125%; // making the div containing the image bigger than the divider div
}
.divider img{
width:100%; // just for this image because it isnt wide enough
position:absolute; // make sure the image listens to top:-50px;
}
ps: the images scale because of twitter bootstrap's css:
img {
height: auto;
max-width: 100%;
}