How can I make <img> not affect div height - html

I want to make image inside this div container not to affect its container size when stretching, I'd like it to go outside up and bottom if that's possible without setting container height or by assigning negative top or bottom with position.
.container {
display: flex;
background: skyblue;
height: 300px;
margin-top:50px;
}
img {
position:relative;
width: 500px;
top:-30px;
}
.column1 {
flex: 1;
}
.column2 {
flex: 1;
}
<div class="container">
<div class="column1">Hello world</div>
<div class="column2">
<img src="https://food.fnr.sndimg.com/content/dam/images/food/fullset/2013/6/28/0/FNK_Apple-Pie_s4x3.jpg.rend.hgtvcom.826.620.suffix/1382545039107.jpeg" alt="">
</div>
</div>
So basically what i want is for image to not affect container size, when stretching image size of the div to stays the same while it can go outside but without setting height for the div like this.
EDIT: I want to make image not affect it's div size ( that i can do with the height ) but i also want it to overlay not only from the bottom but also from the top of the div ( like it is now but without using negative top position in the css )

You can always set position: absolute; on the image to bring it outside of the document flow which will have the effect you desire.
If you are not using flexbox another option will be to set float which will bring the image outside of the document flow as well.
A third option will be to set height to column2 to make it not care about children heights. But that's kind of a hack and I can't confirm it will work on all possible browsers.
.column2 {
height: 1px;
}
Also please keep in mind that by doing this the image will start to overlap elements that are below your container div.

There is a very nice and cool way to make a background image work like an img element so it adjust its height automatically. You need to know the image width and height ratio. Set the height of the container to 0 and set the padding-top as percentage based upon the image ratio.
It will look like the following:
div {
background-image: url('http://www.pets4homes.co.uk/images/articles/1111/large/feline-influenza-all-about-cat-flu-5239fffd61ddf.jpg');
background-size: contain;
background-repeat: no-repeat;
width: 100 % ;
height: 0;
padding-top: 66.64 % ; /* (img-height / img-width * container-width) */
/* (853 / 1280 * 100) */
}

Related

How to make a page scroll to bottom of image

I dont really have anything to show, but i'll try to explain.
I am using HTML and CSS and i have a background image the size of the moon. (3840x18509 pixels). How can i add this as a background on the page, get it to scale automatically to the screen, and make it scrollable? What i mean by that is that i want to be able to scroll all the way to the bottom of the long picture. using :cover is not doing it, and when i set height:18509px; The image gets wider than it's supposed to be so that it cuts out on the sides.
I want the website to be as wide as the original picture, and as high as the original picture. I want to show the entire picture in a way that it fits the screen width, but must be scrolled downwards to reach the bottom. Thanks in advance.
body, html {
margin: 0;
padding: 0;
border: 0;
background-image: url("HuronWP.png");
background-repeat: no-repeat;
background-size: cover; I dont want it to cut the image height,
i want to scroll down to see the rest.
}
}
If you know the exact size of the image you simply have to set the aspect-ratio of <body> to match the image: body { aspect-ratio: 3840 / 18509; }
body {
margin: 0;
background-image: url('https://www.tacoshy.de/stackoverflow/3840x18509.jpg');
aspect-ratio: 3840 / 18509;
}
Alternativly you could insert the image directly and use width: 100% to fit the screen while maintaining its aspect-ratio.
img {
width: 100%;
display: block;
}
/* for styling purpose only */
body {
margin: 0;
}
<img src="https://www.tacoshy.de/stackoverflow/3840x18509.jpg">
I think you want to scroll you image whenever it becomes wider or longer than your page. You can do this simply using css overflow property.
overflow:scroll;
Just adding this to our code it will work fine.
Whenever the image height or width will be larger than you container then it will automatically be a scrollable.
Here I have created a div class and put my image inside this div.
<div class="image">
<img
src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg"
alt="img"
/>
</div>
Now I am changing the height and width of my <img> so that it will overflow the <div> class.
height and width of image.
img {
height: 1000px;
width: 1000px;
}
height and width of outer div
.image{
height: 900px;
width: 900px;
overflow: scroll;
}
This property will be valid only when the outer div must have smaller height and width than the inner,

Img tag sizing in percentages

I've been trying to find an answer online so I didn't have to post, but I can't find a solution!
I'm using a carousel from bootstrap, and my images won't fit. I put the carousel inside a div named "slide". I set width and height on slide, and put a background color to make it easier to see. When I set width in percentage a on my img it resizes to take up the whole width of it's parent container. But when I set height on the img, it makes the image huge!! I want to stretch the image to fit, I don't care about keeping the original ratio. Also if I set the height in pixels or vh it has an affect, the only thing that doesn't work is percentages.
Guys, what am I missing here?
Width 100%
Code
Height 100%
Code
.slide {
width: 300px;
height: 300px;
overflow: hidden;
background: #000;
}
.slide img {
width: 100%;
height: 100%;
object-fit: contain; /* When you use Contain proparty you can see full image with black background*/
object-fit: cover; /* When you use cover proparty you can see it's cover a box as per main div width */
}
<!-- Here I use Display flex instant of float right -->
<div class="slide">
<img src="https://images.pexels.com/photos/1591447/pexels-photo-1591447.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260">
</div>
My suggestion is You can use cover CSS.

Responsive image size and position in CSS

I'm designing a simple website in HTML and CSS but got stuck. I'd like my website to have an image at the top. Any content that comes after that image should appear underneath the image, not on top of it (like it does with a background image). The image should be 0.6 (or some other fraction) the height of the viewport and centered. Any overflowing part should be cropped. However, in case the image is too narrow, it should be resized to fill the whole width of the viewport. It should never change its apect ratio. I'd also like to keep my site script-free and would like to have no absolute values (like px) in my source files.
Here are some doodles I made to explain my idea:
Image height is set to 0.6vh. It is centered. The overflowing areas are cropped. Text starts at the bottom.
Image height would be set to 0.6vh but it wouldn't cover the whole viewport's width. Instead, set the image width to match the viewport width (equal to the width of the page), ignoring the 0.6vh rule. Text still starts at the bottom.
I've been searching the internet for two days already but I couldn't really find quite what I need.
Additionaly, I am trying to solve this problem: currently, I am using vh to set the height of the image, but it gets weird when viewing on a mobile browser, because the value of vh changes when the address bar collapses.
[EDIT]
Rule:
The minimum width of the image should be the width of the viewport.
Then, if it's possible, it's height should match 0.6vh.
[EDIT 2]
I noticed that the answers work great by centering an image and keeping it's aspect ratio. But the image shouldn't be cropped vertically.
Another doodle:
I hope this doodle makes it easier to see what I am trying to achieve. Red line indicates the "0.6vh rule" while blue line represents the "mininimal width equals viewport width" rule. Green rectangle shows where the image should be.
[EDIT 4]
I added an image and some comments to the previous image:
Rather than using an image tag, try setting its properties in CSS. It's easier to maintain aspect ratio and centering this way. You may also want to specify a minimum and maximum height property to ensure things look nice.
body{
margin: 0; /* Stripping out margin and padding so .bg can be full width*/
padding: 0;
}
.bg {
box-sizing: border-box;
display: block;
width: 100%;
height: 60vh; /* 60vh = 60% of the viewport height*/
padding: 0; /* No padding */
margin: 0; /* No margins */
background-image: url('http://www.naturheilpraxis-kaelin.ch/bilder/fruehling-01.jpg');
background-repeat: no-repeat;
background-position: center center;
background-size: cover; /* Cover all available space. */
/* If you want the whole image visible try 'contain'*/
}
.main{
padding: 0 1em; /* L/R padding to look nice */
}
<body>
<div class="bg"></div>
<article class="main">
<h1>Your rad site</h1>
<p>Notice how the image is centered and it keeps its aspect ratio.</p>
</article>
</body>
[EDIT]
Okay! So here's the best I can do.
This doesn't crop anything vertically and there's no JS or hard values
BUT you do have to use an aspect ratio media query that you need to figure out based on your image.
This is the formula for the aspect ratio media query
Where H = image height, W = image width and D = percent of the viewport used (expressed as a decimal).
H / (W / (D))
For example, the image I used is 1000px x 300px. We're using 60% of the viewport so D = .6.
So 1000 / (300 / (.6)) >> 1000 / 500 >> 2/1 ratio.
body,html,.container{
margin: 0;
padding: 0;
}
.container {
position: relative;
overflow: hidden;
height: 60vh;
}
img {
position: absolute;
/* Position the image in the middle of its container. */
/* Hack*/
top: -9999px;
right: -9999px;
bottom: -9999px;
left: -9999px;
margin: auto;
min-height: 100%;
height: 100%;
}
#media only screen and (min-aspect-ratio:
2/1) {
/* Aspect ratio determined like so -- H / (W * (10/6)) */
.container img{
position: relative; /*Undoing the hack basically*/
right: auto;
top: auto;
left: auto;
bottom: auto;
width: 100%; /*rather than the width get cropped, it's full. height is auto*/
}
.container {height: auto; /* the image height determines the height */}
}
<div class="container">
<img src="http://placehold.it/1000x300" alt="" />
</div>
Make it full page and resize the browser to see how the image adapts.
I really hope that helps!
How about an approach like this:
body,html {margin: 0;padding: 0}
* {box-sizing: border-box}
/*For the demo I've made the height below 30vh, you can change that to 6vh*/
/*Use background-size contain, if your image's height is larger that width*/
.Image-Banner {width: 100%;height: 30vh;background: url('https://i.stack.imgur.com/sj9LO.png') no-repeat 50%;background-size: cover}
.Main-Content {padding: 1em}
<body>
<header class='Image-Banner'></header>
<main class='Main-Content'>
<div>Foo Bar</div>
<hr>
<aside>Lorem Ipsum...</aside>
</main>
</body>
Note: Click Run Code Snippet to see the result.
This should do exactly what you are asking for, I believe.
Created an example based on your information:
[Example][1]
[1]: https://jsfiddle.net/2Lke94qn/1/
Let say that we have two divs:
<div class="top">Top</div>
<div class="bottom">Bottom</div>
Try this style:
html, body{
height: 100%;
}
.top{
width: 100%;
height: 60%;
background-image: url("http://www.planwallpaper.com/static/images/HD-Wallpapers1.jpeg");
background-position: center;
}
.bottom{
background-color: grey;
height: 40%;
}

css image doesn't fill div

I have a DIV that is 100% width..... inside a grid system .. the total grid is 1140px..
anyway inside the div that is 100% i inserted a image...
problem is if the div background is black... there is like 1cm at the bottom that still shows the black color.
like this
http://jsfiddle.net/hb3QK/3/
<div style="background:black; width:100%;">
<img src="http://i42.tinypic.com/ofq9tg.png" >
should i be using a float on the image? or using a display:block?
here you do
http://jsfiddle.net/cancerian73/hb3QK/4/
img {
height: auto; /* Make sure images are scaled correctly. */
width: 100%; /* Adhere to container width. */
display:block;
}
add display: block; to style of image.
or you can add if you need to keep it inline.
line-height: 0;
to the image style.

How can I resize an image without stretching?

I want a <img> whose width is 40% of the page, and it gets stretched.
How can I resize it without stretching?
For example, if I have a image whose file originally looks like this:
____8888________
____8888________
____8888________
In my webpage, normally, it should looks like:
____8888________
____8888________
____8888________
As soon as I make the browser a little more narrow, the max-width(let's say 10 characters in this example) would take effect.
When that happens, I would like it to be:
____8888__
____8888__
____8888__
(just like it's been cut from the right side. Of course from both sides are better),
Rather than:
__888_____
__888_____
__888_____
Any trick (putting it into a <div>'s background) is okay.
Width and height are unknown.
Thank you all for your previous answers, but, sorry, I think I haven't put enough emphasis on "After limiting its width to 40% of the page", which means before width-limiting it should looks normal.
The trick is to put the image into a containing block element, eg a DIV. Once inside set the width of the image to 100%, this will instruct the browser to fit the image width flush with the left and right edges of the DIV.
You then control the width of the DIV via CSS, I find keeping the image in a block element makes manipulation much easier when creating fluid layouts.
Example:
img.stretchy {
width: 100%; /*Tells image to fit to width of parent container*/
}
.container {
width: 33%; /*Use this to control width of the parent container, hence the image*/
}
<div class="container">
<img src="http://i.stack.imgur.com/fv6Ib.jpg" alt="Beach Scene" class="stretchy" />
</div>
If you wan the image to be clipped/cropped in any way, set it to be larger than it's parent, and set the parent's overflow css to hidden.
Example:
img.clipped {
width: 150%; /*Scales image to 150% width of parent container*/
float: left; /*Floats image to left of container - clipping right hand side*/
float: right; /*Floats image to right of container - clipping left hand side*/
}
.container {
width: 33%; /*Use this to control width of the parent container, hence the image*/
overflow: hidden;
}
<div class="container">
<img src="http://i.stack.imgur.com/fv6Ib.jpg" alt="Beach Scene" class="clipped" />
</div>
Hope this helps...
Add this class to the img html tag, it will keep the image as it is, but will take the necessary specified space ie.40% x 40% without stretching the image
.img{
width:40%;
height:40%; //change to whatever your choice
/*Scale down will take the necessary specified space that is 40% x 40% without stretching the image*/
object-fit:scale-down;
}
Here's a few options. (see the demo of all these options here: http://jsfiddle.net/Squeegy/Gcrdu/ )
The first as a plain image of unknown size. This displays at whatever size it happens to be.
<img src="http://www.google.com/logos/classicplus.png">
But as it turns out, you can preserve the aspect ratio of an image if you only set the width, or only the height. The other dimension will adjust itself to keep things from stretching.
// HTML
<img src="http://www.google.co.jp/logos/classicplus.png" class="aspectshrink">
// CSS
img.aspectshrink {
width: 100px;
}
But when you use CSS background images you can do some creative cropping based on where anchor the background.
This says "Go"
// HTML
<div class="cropped-right"></div>
// CSS
.cropped-right {
width: 100px;
height: 100px;
background: url(http://www.google.com/logos/classicplus.png);
background-position: left center;
background-repeat: no-repeat;
border: 1px solid red;
}
And this says "gle":
// HTML
<div class="cropped-left"></div>
// CSS
.cropped-left {
width: 100px;
height: 100px;
background: url(http://www.google.com/logos/classicplus.png);
background-position: right center;
background-repeat: no-repeat;
border: 1px solid red;
};
Try to use ImageResizer.
Here's the link : http://imageresizing.net/
Do you mean cropping the image? If so look into CSS overflow property. Also you could put it into the background and centre it in the div