image size on container - html

I want to have an image that adjusts to container size.
Example: the container is 100x100 pixels and the image is 90x80 pixels, the smaller property of the image (height in this case) adjust to container size, and the bigger property overflow the container size.
How can I do this?
My code:
HTML
<div class="a-avatar">
<img src="#" alt="profile picture">
</div>
CSS
.a-avatar {
display: block;
width: 100px;
height: 100px;
}
.a-avatar img {
min-width: 100%;
min-width: 100%;
width: auto;
height: auto;
}

use background image, example:-
div.avatar {
background-image: url("paper.gif");
height: 500px; /* You must set a specified height */
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover; /* Resize the background image to cover the entire container
}

Related

Css background image doesn't display if height not set (why not adjusting automatically?)

I want to include background image which is oversized (4000px x 3000px) in the div,
in such a way that width will take max width of the screen and height will adjust to it.
I don't know why but it doesn't work if the height is not specified (like 1000px).
The image doesn't appear at all.
I wanted to make jsfiddle but there it works (probably because height is somehow specified automatically)
The part of code (HTML):
<section id="panels">
<h1>PANELS</h1>
<div class="section-img">
<!-- here i want the image -->
</div>
</section>
The part of code (CSS):
.section-img {
background-size: contain;
background-repeat: no-repeat;
background-position: center center;
background-image: url("path/to/my/image");
max-width: 100%;
width: 100%;
height: auto;
}
And with this code nothing appears (as it looks the height is 0px), how can i do the thing that height will adjust to size of width i.e. scales itself.
In your example, the div is inheriting the width of the parent section tag, taking into account any margin or padding on the body element. It has display: block so width is 100% , height is auto so it's the size of whatever inside. So in your case there's nothing inside the div tag, which means it's height: auto; value is 0.
.section-img {
background-size: contain;
background-repeat: no-repeat;
background-position: center center;
background-image: url("https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg");
max-width: 100%;
width: 100%;
height: 100px; // auto means 0 if there's nothing in your div element
display: block; // this is a default for every div tag
}
<section id="panels">
<h1>PANELS</h1>
<div class="section-img">
<!-- here i want the image -->
</div>
</section>
Just try this replace the auto with 100% in height and check.
.section-img {
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
background-image: url(https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg);
max-width: 100%;
width: 100%;
height: 100%;
display: block;
position:absolute;
top:20%;
bottom:0;
right:0;
left:0;
}
<section id="panels">
<h1>PANELS</h1>
<div class="section-img">
<!-- here i want the image -->
</div>
</section>
Are you like this .

How to make img behave the same as background-image?

I use an image as a background image, background-image: url(), and I also use this image placed inside <img src="">
It looks like the height of src image is shorter the height of the background image.
If I set a height for src image equals height of the background image, the src image will be disturbed.
What CSS properties should I set to make src image have the same height as background image, but it won't disturb the src image? Please note: I need to adjust ONLY in src image, not background image.
Please take a look at my sample in jsfiddle
HTML
<p>
This is background image
</p>
<div class="imageBG">
</div>
<p>
Below is a front image. Its height looks like less than the height in background image.
</p>
<div>
<img src="https://library.danahall.org/wp-content/uploads/2019/04/2560px-Bufo_periglenes2.jpg">
</div>
CSS
.imageBG {
background-image: url("https://library.danahall.org/wp-content/uploads/2019/04/2560px-Bufo_periglenes2.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
height: 353px;
}
img {
width: 100%;
/* height: 353px; */
}
Please note: Because the image I use is long, I have to set width: 100% for img. If I don't set that, a navigation bar will show at the bottom of the browser.
Consider object-fit and object-position
.imageBG {
background-image: url("https://library.danahall.org/wp-content/uploads/2019/04/2560px-Bufo_periglenes2.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
height: 353px;
}
img {
width: 100%;
height: 353px;
object-fit:cover;
object-position:center;
display:block; /*to make it behave as div and avoir whitespace issue*/
}
<p>
This is background image
</p>
<div class="imageBG">
</div>
<p>
Below is a front image. Its height looks like less than the height in background image.
</p>
<div>
<img src="https://library.danahall.org/wp-content/uploads/2019/04/2560px-Bufo_periglenes2.jpg">
</div>
Related for more details: Object-fit On A Canvas Element
Add position:fixed in CSS class. Then you can adjust the height.
.imageBG {
background-image: url("https://library.danahall.org/wp-
content/uploads/2019/04/2560px-Bufo_periglenes2.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
height: 353px;
position:fixed;
}
img {
width: 100%;
height: 353px;
}
Just add a class to the img containing div and set its height to 353px.
.image-container {
height: 353px;
}
img {
height: 100%;
width: 100%
}
As i see you src image is right and what you background image is doing is scaling the image to make it fit 100% and also your 353px, so your src image height was'nt wrong, it was the backgorund
if you use math and right proportions you would get this:
height: 252px; // this is the right proportions
right math one

Create Fluid Background Image with CSS

I am trying to build a simple fluid image that can resize based on screen size.
But I am having trouble to get the image smaller when the width of the browser gets smaller.
HTML:
<main>
<section class="slider-ctn">
<figure class="logo"></figure>
</section>
</main>
CSS:
.slider-ctn figure.logo {
margin: auto;
background-image: url('../Images/logo.200x100.png');
width: 200px;
height: 100px;
}
If you set the background-size to 100% 100% or cover the image will stretch to fit the container. Remove the width (or restrict the width by setting the parent's width) and set height: 0; padding-top: 50% on the figure to make the height of the figure half of the width, so the aspect ratio will match the image and be fluid.
* {margin:0;padding:0;}
.slider-ctn {
margin: auto;
width: 200px;
}
.slider-ctn figure.logo {
background: url('https://dummyimage.com/200x100/000/fff') top left no-repeat;
height: 0;
background-size: cover;
padding-top: 50%;
}
<main>
<section class="slider-ctn">
<figure class="logo"></figure>
</section>
</main>
Just add the following to your head tag with this code :
<meta name="viewport" content="width=device-width, initial-scale=1">
Another option using vw units and contain.
If you know the aspect ratio of your image you can use vw (view width) as your units for width and height.
I am using an image that has a 4x3 aspect ratio. If I want the width to always be 80% of the screen I will use 80vw. Then, my height will be 3/4 of my width, or 60vw.
Then be sure to use contain for your background sizing.
Try this css and see if it satisfies your needs:
.slider-ctn figure.logo {
margin: auto;
background-image: url('https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg');
background-size: contain;
background-repeat: no-repeat;
width: 80vw;
height: 60vw;
border: 5px solid red;
}
You should add contain as background-size to your CSS:
.slider-ctn figure.logo {
margin: auto;
background-image: url('../Images/logo.200x100.png');
background-position:center center;
background-repeat:no-repeat;
background-size:contain;
width: 200px;
max-width:100%;
height: 100px;
display:inline-block;
}
Don't forgot to add position and repeat to background properties everytime when you use background images.
Also I added max-width to fit within the parent container if it is smaller than the logo container.
Example here
NOTE: Don't use cover in background-size because that will cut your
logo to fit only within width limits, contain will fit width and
height.
I think setting up the max-width of the image to 100% should do the trick.
.slider-ctn figure.logo {
background-image: url('../Images/logo.200x100.png');
background-repeat:no-repeat;
background-size:contain;
background-position:center;
width: 200px;
height: 100px;
}

Prevent div from stretching its background-image

I have a div with a background-image defined as follows in my stylesheet:
.information_photo {
position: relative;
top: 30px;
width: 100%;
height: 200px;
background-image: url('http://www.ladyblitz.it/wp-content/uploads/2015/04/carbonara.jpg');
background-repeat: no-repeat;
background-position: center center;
background-size: 100% 100%;
}
<div class="information_photo"></div>
As you can see it stretches the original image, instead I want it to just focus on a part of the background-image without stretching or resizing it.
The 100% 100% background-size value means the background should stretch (100%) of the width of the element and (100%) of the height of the element. Have either of them set to auto, which will size the undefined dimension automatically, while preserving the images aspect ratio.
You can then choose which portion of the image is visible by adjusting the respective background-position style.
.information_photo {
position: relative;
top: 30px;
width: 100%;
height: 200px;
background-image: url('http://www.ladyblitz.it/wp-content/uploads/2015/04/carbonara.jpg');
background-repeat: no-repeat;
background-position: center -30px; /* Visible 30 px from the top */
/* 100% height, auto width */
background-size: auto 100%;
/* 100% width, auto height */
background-size: 100% auto;
/* or simply */
background-size: 100%;
}
<div class="information_photo"></div>

How to place divs with background-images in order with 100% width?

Explanation image:
https://dl.dropboxusercontent.com/u/397267/css-problem.jpg
Hello guys,
I need your help.
I would like to put some screenshots on a webpage. The screenshots shall not be displayed bigger than their original size (Picture 01). If they viewport is wider than the pictures original size, the picture should just be centered with no background.
When the viewport is smaller than picture, the picture should just be scaled down by keeping its proportions! (Picture 03)
I tried it that way, but this didn’t work well. background-size: contain does a good job resizing my screenshots (height/width), but the div keeps its height (Picture 04), so I need your help how I could fix that?
.w01-startseite {
position: relative;
width: 100%;
height: 882px;
min-height: 300px;
background: url(01_startseite.png) no-repeat;
background-position: top center;
background-size: contain;
}
You can try something like this:
/* HTML sample */
<div class="Screenshot" style="background-image: url(http://lorempixel.com/1200/1200/sports/)"></div>
<div class="Screenshot" style="background-image: url(http://lorempixel.com/1200/1200/sports/)"></div>
/* CSS */
.Screenshot {
overflow: hidden;
margin: 2em auto;
max-height: 1200px;
max-width: 1200px;
background-repeat: no-repeat;
background-position: 50%;
background-size: contain;
}
/**
* 1. Adjust this value to alter the aspect ratio.
*/
.Screenshot:before {
display: block;
padding-top: 50%; /* 1 */
content: "";
}
example: http://codepen.io/defo550/pen/GgMeBp