Possible to make parent honour width of child image? - html

I am trying to make a parent element honour the width of a child image.
It works on load, but if you resize the height of the viewport to force a change in the image height, the parent element 'remembers' the initial size of the image and maintains that width.
If you mimic the above with width instead of height, there is no problem.
Here is a video of the behaviour: http://jmp.sh/8VEOZS8
Here is a codepen: http://codepen.io/iamkeir/pen/YWgvdw
html, body { height: 100%; }
.wrapper {
display: inline-block;
height: 100%;
border: 1px solid black;
}
img {
height: 100%;
}
<div class="wrapper">
<img src="http://placehold.it/1280x960" />
</div>
I'm interested to know:
1) why this is happening
2) if there is a way to fix it
Thanks!

You can fix it by setting max-width and max-height attributes to the image so it wont overflow the wrapper or the window. Then you can set the display: inline-block; to the wrapper instead which is nowadays used in replacement of floating elements. I set the image to be display: block; just so it will display it as a block element and eliminate weird space around it etc.
html, body { height: 100%; }
.wrapper {
display: inline-block;
border: 1px solid black;
}
img {
display: block;
max-width: 100%;
max-height: 100%;
}
<div class="wrapper">
<img src="http://placehold.it/1280x960" />
</div>

Related

CSS — Max width image in a two row layout should shrink when constrained by parent

I've got an image (top row) and a caption (bottom row), The image has a max-width of 400px. Both rows are inside a wrapper with a max height of 100vh. As the code is now, the contents of the wrapper will overflow when its constrained by the viewport. Ideally the image should shrink (keeping its aspect ratio) while the caption is still visible at the bottom.
HTML:
<div class="wrapper">
<picture>
<img src="https://images.theconversation.com/files/350865/original/file-20200803-24-50u91u.jpg?ixlib=rb-1.1.0&q=45&auto=format&w=1200&h=1200.0&fit=crop" />
</picture>
<div class="caption">This is a responsible cat</div>
</div>
CSS:
html, body { height: 100%; }
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background: #202227;
display: flex;
justify-content: center;
align-items: center;
color: #fafafa;
border: 2px solid pink;
text-align: center;
}
.wrapper {
padding: 10px;
max-height: 100vh;
background: #37393d;
border: 5px solid #36badd;
}
img {
width: 100%;
max-width: 400px;
}
JSFiddle demo: https://jsfiddle.net/audunolsen/w30e4sgq/17/
You're example can't work the way you want it to, because a percentage-based max-height always uses the parent's height, not its max-height (see this answer). Instead, add the max-height: 100vh directly to the image.
Updated fiddle: https://jsfiddle.net/qw70tr2f/1/

Image gets bigger than a div container with a border and overflows

here's a simple problem for you to solve.
The image should be 90vw and the container should add a border to the image.
The border can't be applied directly to the image, since the image in further steps of coding will have some style directly applied to the html.
The current implementation causes the border to be smaller than the image. How can it wraps nicely around the image?
I really want something simple to keep it light and easy to understand for a newbie like me, so please no codes that do triple flips and pike jumps with gentle, graceful landings like I usually see on Stack Overflow.
HTML:
<div id="main-image-container-slideshow">
<img id="main-image-slideshow" src="http://localhost:8888/image/jpeg/campus1.jpg">
</div>
CSS:
#main-image-container-slideshow {
border: 5px solid black;
}
#main-image-slideshow {
width: 90vw;
}
I would make the container be 90vw AND have the container have the border. The image would then be the full width of the 90vw container. I made the image a block to remove any potential unwanted space underneath.
#main-image-container-slideshow
{ border: 5px solid black;
box-sizing: border-box;
width: 90vw;
/* If you want the container to be centered, add this, otherwise skip */
margin: 0 auto;
}
#main-image-slideshow
{
width: 100%;
display: block;
}
You can set the display property of the wrapper to flex and set the flex-direction property to column. And, if you decide to change the width of the image, then you won't have to change the display property in CSS.
#main-image-container-slideshow {
display: flex;
flex-direction: column;
border: 2px solid white;
}
#img {
width: 90vw;
}
html {
background-color: black; /* sets the background color to black to make it easier to see the border */
}
<div id="main-image-container-slideshow">
<img id="main-image-slideshow" src="https://img.freepik.com/free-photo/wall-wallpaper-concrete-colored- painted-textured-concept_53876-31799.jpg?size=626&ext=jpg">
</div>
The image used in the code snippet was found at freepik.
You can set the container width to 100vw and image width to 90% of the container's width which is equivalent to 90vw.
.image-container {
width: 100vw;
border: 5px solid #000000;
}
.image {
width: 90%;
}
<div class="image-container">
<img src="https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.png" class="image"/>
</div>

Have image within flexbox fill all vertical space and remain fully visible

I need to have an image to the left of a div, and have the image:
Be the same height as the div (which itself has non-fixed, content-dependent height);
Be fully visible;
Maintain its aspect ratio.
Flexbox seems perfect for the job, but when setting the image to 100% height, its dimensions retain the natural width and the content overflows under the div. See snippet below:
.container {
display: inline-flex;
background-color: green;
}
.image {
flex: 0 0 auto;
background-color: purple;
}
.image img {
display: block;
height: 100%;
}
.right {
flex: 1 1 auto;
width: 100px;
height: 100px;
background-color: yellow;
}
<div class="container">
<div class="image">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABOUlEQVRYhc2XPW6DQBCFP6bJEeLOMhKdS3dukOhT5FAcIAdJYfkKruzSnQvjCvsGSTUpdpEilMAiA7NPetXCvMfPzr5JlGBkwBbYAGtgBbz6tQdwBc7ACTgAl6Cq2s9c4UOhUtBAVv6evK9+12KqUCrUA4TbrH2NdKiBQmH/hHCbe18zyMCbwnFE8YZHX7vTQDGR+G8TxX8G0pFfe9fnSP8yUM4g3rBsG8j1ub99KGuvifh28A4sghrHOFh4TVDIdFiTGYuVQia49rqc8ekbLIGt4Hq7FTaCO1issBbcqWaFVaLwBbwYGfiW/mumheDChBUegksyVrgKLkZZ4Sy4DGeFk+AC5M1A/AYcBJdedwYGdsCl2YafwH1G8bvXjCeQRBHJzENpFLE8isEkitFstuE00fC9O8l4/gNz0YYudsoRxwAAAABJRU5ErkJggg==">
</div>
<div class="right">right div</div>
</div>
The red circle is clipped but I'd like it to be entirely visible, like so:
I've attempted placing the image in its own div and playing around with various overflow values with no success. Chrome will eventually display it right when using dev tools to disable then enable height: 100% on the img element, but this doesn't happen in Firefox.
This is somehow impossible as the browser need at least two iterations to correctly calulate the final width and this may create a cycle. Basically the broswer will first ignore the percentage height of the image to set the width/height of the container then will resolve the percentage height and after will calculate the width of the image to keep the ratio but it won't go back to adjust the width of the container again because it may lead to an infinite loop in some cases, that's why you will simply have an overflow.
Here is a hack that works only with Chrome where you can force the calculation of the width again by applying an animation.
.container {
display: inline-flex;
background-color: green;
}
.image {
background-color: purple;
}
.image img {
display: block;
height: 100%;
animation:hack 1s infinite linear;
}
.right {
width: 100px;
height: 100px;
background-color: yellow;
}
#keyframes hack {
to {
height:99.9%;
}
}
<div class="container">
<div class="image">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABOUlEQVRYhc2XPW6DQBCFP6bJEeLOMhKdS3dukOhT5FAcIAdJYfkKruzSnQvjCvsGSTUpdpEilMAiA7NPetXCvMfPzr5JlGBkwBbYAGtgBbz6tQdwBc7ACTgAl6Cq2s9c4UOhUtBAVv6evK9+12KqUCrUA4TbrH2NdKiBQmH/hHCbe18zyMCbwnFE8YZHX7vTQDGR+G8TxX8G0pFfe9fnSP8yUM4g3rBsG8j1ub99KGuvifh28A4sghrHOFh4TVDIdFiTGYuVQia49rqc8ekbLIGt4Hq7FTaCO1issBbcqWaFVaLwBbwYGfiW/mumheDChBUegksyVrgKLkZZ4Sy4DGeFk+AC5M1A/AYcBJdedwYGdsCl2YafwH1G8bvXjCeQRBHJzENpFLE8isEkitFstuE00fC9O8l4/gNz0YYudsoRxwAAAABJRU5ErkJggg==" >
</div>
<div class="right">right div</div>
</div>

resize image without div being resized

I looked over thousands of questions all of them they want to fit picture in a parent div. I can fit picture in a parent div but when I resize the picture to smaller size the div gets smaller as well. I tried max-width: 80% but the div gets smaller also. I don't want the div box to resize because there are other buttons and lists in the page that move with it. And I cant use background-image trick as well. The only solution is to set for example height: 150px for box div but that also gives me problem for smaller screen sizes. Can anybody be any help? This question probably will be flagged duplicated but I gave up on searching.
.box {
width: 100%;
float: left;
}
.picture {
border: none;
outline: none;
max-width: 100%;
vertical-align: middle;
}
<div class="box">
<img class="picture" src="https://www.w3schools.com/w3css/img_lights.jpg" />
</div>
I'm honestly not completly sure what your asking about but solving the size of a picture inside a div with paddings and margins is not what we want to do. there you have to use media-queries to get responsiveness.
try transform: scale(0.5)
scale let you resize your content dependent on how big your content was initially.
.box {
width: 100%;
float: left;
}
.picture {
border: none;
outline: none;
max-width: 100%;
vertical-align: middle;
transform: scale(0.5);
}
<div class="box">
<img class="picture" src="https://www.w3schools.com/w3css/img_lights.jpg" />
</div>
Your box element should have a height too. Also, set a position relative to it and a position absolute to the image. The child element should always be placed inside the parent with an absolute position. This way you can individually set sizes and positions.
You can use viewport height for div (vh) as per your need.
.box {
width: 100%;
height:100vh;
float: left;
border:2px green solid;
margin-bottom:10px;
}
.picture {
border: none;
outline: none;
max-width: 80%;
vertical-align: middle;
}
See the example: https://jsfiddle.net/srijan1709/fvezsnjb/10/
Edit-
You can use object-fit for adjusting the image inside div also. Set value of object-fit to scale-down or contain as per your need.
See the example: https://jsfiddle.net/srijan1709/fvezsnjb/27
Try making the div absolute and the image relative to it. I have added a border to the div to see if the image is moving by itself as a test:
.box {
position: absolute;
border: 5px dotted blue; // For testing (remove after done testing)
width: 100%;
float: left;
}
.picture {
position: relative;
padding: 100px 50px 50px 100px; // Moves the image within the div tag
outline: none;
max-width: 100%;
vertical-align: middle;
}
Expected Output:
EDIT: Adjust the padding and width values according to your code expectations.
Please See JSFiddle

SVG without width/height renders with a natural size

I have this SVG which doesn't have a width or height attribute
I have the following HTML
<div class="block">
<img src="https://s3-eu....vAmfIxVv/kiwi.svg">
</div>
With the following css
.block {
display: inline-block;
width: auto;
}
img {
width: 100%;
}
Although I want the svg to be 100% in width, it renders in chrome with some weird width/height (Only in firefox it has a dimensions of 0x0)
JSFIDDLE
So any suggestions where this natural width comes from and why isn't the width 100% ?
Is it possible to make the svg width 100% ?
If you want auto width on the <svg> elements, you should simply use display: block on the parent <div>, but use max-width: 100% on the SVG elements themselves. Chrome somehow fails to enforce proper width calculations when SVGs are contained within an inline-block element:
.block {
display: block;
}
img {
max-width: 100%;
}
Proof-of-concept example:
section {
border: 2px solid green;
margin: 20px;
background-color: yellow;
width: 500px;
height: 500px;
}
.block {
display: block;
}
img {
max-width: 100%;
}
.svg {
border: 1px solid red;
}
<section>
<div class="block svg">
<img src="https://s3-eu-west-1.amazonaws.com/uploads-eu.hipchat.com/46194/456229/JCHA4rtvAmfIxVv/kiwi.svg" alt="">
</div>
<div class="block">
<img src="http://placeholder.pics/svg/100x200" alt="">
</div>
</section>