I am new to HTML/CSS and curious why div stretch correctly in below example, but not image?
Is the image calculated differently than div?
Is there an MDN documentation i can read more about this behavior?
body {
background: #222;
padding: 32px;
}
.card {
background-color: white;
padding: 32px;
border-radius: 8px;
}
img {
width: auto;
margin: 0px -32px 0px -32px;
}
.gooddiv {
width: auto;
height: 100px;
margin: 0px -32px 0px -32px;
border: 2px solid red
}
<div class="card">
<img alt="SG Image" src="https://images.unsplash.com/photo-1508964942454-1a56651d54ac?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1035&q=80" />
<div class="gooddiv"></div>
</div>
For a <div>, a width: auto setting will mean 100% of its parent element, so it will stretch.
For an image, width: auto (which is also the default if you don't define width at all) will mean that (unless there is a height setting) the image is displayed at its original size and therefore not stretch or shrink. If you define width: 100% or similar (or a height setting) , it will adjust.
I want to add that if an image is stretched beyond its original proportions, it will look distorted and have a bad quality, so it really wouldn't make sense to stretch an image by default to the size of its parent element, which might easily be larger than the image itself.
To avoid the mentioned distortion due to stretching beyond original size, a common way to handle that is to use max-width: 100%; (and also max-height: 100% if you defined height for the parent), thereby leaving width and height at their default auto (= original size). That way you'll stretch the image to full width if its at least as wide as the parent originally, or have it displayed at original size if its smaller (avoiding bad quality due to stretching beyond original size). In the snippet below, I only used max-width: 100%; (i.e. everything else at default settings), which limits the image width to the parent element width (minus padding), avoids stretching beyond original width and adjusts the height automatically, keeping the original height/width ratio. (BTW. I erased the negative margins you added, which wouldn't make sense in this context)
Note: Setting width: 100% and height: 100% is not a good idea for images since in most cases this will distort the height/width proportion of the picture, making it look bad (unless it's an abstract graphic pattern where a disproportion between height and width doesn't matter).
body {
background: #222;
padding: 32px;
}
.card {
background-color: white;
padding: 32px;
border-radius: 8px;
}
img {
max-width: 100%;
margin: 0;
}
.gooddiv {
width: auto;
height: 100px;
margin: 0px -32px 0px -32px;
border: 2px solid red
}
<div class="card">
<img alt="SG Image" src="https://images.unsplash.com/photo-1508964942454-1a56651d54ac?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1035&q=80" />
<div class="gooddiv"></div>
</div>
Related
I have a box with a height of 0.1em and a container with font size 14px. When I position my box inside the container with a top with percentage, sometimes the box's computed height is larger depending on the percentage position.
I want to make my element respond by simply changing the font size of the container (I cannot use rem for reasons). eg. container starts at 10px then goes 12px, 14px, etc.
Is there a way to tell the browser to always render my box's height with more pixels (or less pixels) so that it always looks the same regardless of top position?
The SCSS I have to reproduce it (see the codepen for it working):
.container {
font-size: 14px;
padding: 2em;
.background {
width: 2em;
height: 10em;
background: lightblue;
position: relative;
.line {
height: 0.1em;
width: 100%;
position: absolute;
left: 0;
top: 0;
background: black;
}
}
}
Demo of the problem I have: https://codepen.io/jared-hexagon/pen/KKKvxKQ
Tested in Chrome OSX latest with MacBook retina display and a low dpi display - both have the problem.
I am building a chat message and I want to fit an image inside the text bubble. The bubble has a fixed width and should scale its height depending on the image aspect ratio.
I am unable to scale the div to maintain the img aspect ratio. All of the answers found use an <img> tag, while I am using a div and none of that fixed my issue.
This is the desired outcome:
What I have achieved instead is either a very small img (size of an empty speech bubble if I do not impose any vertical or horizontal dimension) or a long img if I impose the width and keep the height dynamic.
.speech-right-wrapper {
max-width: 90%;
display: flex;
flex-direction: column;
}
speech-right-message-container {
display: flex;
justify-content: flex-end;
align-items: flex-start;
}
.speech-right-bubble {
position: relative;
background: rgb(154, 226, 255);
box-shadow: 0px 1px 2px gray;
border-radius: 15px 0 15px 15px;
display: flex;
}
.bubble-background {
width: 200px;
height: auto;
overflow: hidden;
}
.bubble-background-img {
position: absolute;
border-radius: 15px 0 15px 15px;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: hidden;
background: no-repeat;
background-size: cover;
background-position: center;
}
<div class="speech-right-wrapper">
<div class="speech-right-message-container">
...
<div class="speech-right-bubble">
<div class="bubble-background">
<div class="bubble-background-img" v-bind:style="{ 'background-image': 'url(' + require('../assets/img/profiles/' + storyPath + bubble.bubble_background) + ')' }" v-bind:alt="bubble.bubble_background"></div>
</div>
...
</div>
...
</div>
</div>
UPDATE
For clarity, I need the <div class="bubble-background-img"> to be fix in width (say 200px) and height: auto to maintain the image aspect ratio; and I need the <div class="speech-right-bubble"> to scale to fit the image inside.
Also, unfortunately I need to use a <div> tag because v-bind:src seems not to work for local images. If I do:
<img class="bubble-background-img" v-bind:src="'../assets/img/profiles/' + storyPath + bubble.bubble_background"/>
I get this:
You should probably find a workaround for your v-bind:src issue since the correct way to display a content image is via the HTML img element.
That said, there is a solution, but it relies on using the CSS content property for element replacement, which is a fairly recent technology that's apparently still not supported in Microsoft Edge. The following CSS code is the gist of it and worked for me in testing in Firefox 68:
div { content: ('image.jpeg'); max-width: 200px; }
(In theory, a variation of this technique should also work with the more backward-compatible CSS ::before and ::after pseudo-elements, but the image inexplicably doesn't scale in my testing. I didn't spend much time experimenting though.)
I'm not a hundred percent certain what you are going for, but you could try setting the background-size as a percentage?
background-size: 100%;
background-position: center center;
Using a percentage in background size lets you scale one or more axis according to the parent container. (e.g. background-size: 100% 100%; would scale width and height, and give an effect equivalent to the contain value).
Would that work?
(Personally, I would load the image using an element as that's what they are there for, and you could use object-fit as appropriate, but obviously depends on how you've architected the view...)
I have a div profile_pic which has the following CSS:
#profile_pic{
position:absolute;
border:1px solid #E1E3E4;
left:25px;
top: 25px;
width: 200px;
height: 200px;
}
Since profile picture for my application can be any image (of any size), the div or image, should be flexible to adapt to one another. I have tested a profile picture with the dimensions of 300px width and 300px height and the image renders perfectly in the the div. However, when I upload a picture with say, 550px width and 400px width the image is appearing "squashed" which is understandable.
There are two options, 1. resizing the image so that the whole image appears in the div and 2. cropping the image so that the image adapts to the div size. I do not mind adopting either of these approaches but I am unable to implement how these approaches in code.
I have tried to set:
#profile_pic {width: 50%}
#profile_pic img {width:100%}
But it just does not work. How can I get the div (or image) to always fit in the div's size without the image losing it's quality?
You could just add background-size:contain; to the div that has the image (assuming you are setting the background image the image you want.
losing quality is another thing, scaling say a 50x50px image to 100x100 is going to lose quality, so it would probably be best to set a minimum size the profile picture can be.
You may set max-width and max-height in order to resize image to fit inside the box without overflow, add line-height and text align to center image in case it has not the same box ratio.
#profile_pic,
.profile_pic2 {
position: absolute;
border: 1px solid #E1E3E4;
left: 25px;
top: 25px;
width: 200px;
height: 200px;
line-height: 197px;
/* since image is the one and single child */
text-align: center;
border: solid;
/*demo purpose */
}
.profile_pic2 {
left: 250px;
}
.profile_pic2 +.profile_pic2 {
left: 450px;
}
#profile_pic img, .profile_pic2 img {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
/* set on middle baseline setted at 200px */
}
<div id="profile_pic">
<img src="//lorempixel.com/640/480">
</div>
<div class="profile_pic2">
<img src="//lorempixel.com/480/640">
</div>
<div class="profile_pic2">
<img src="//lorempixel.com/480/480">
</div>
I have a div with width:100px and height:100px (say)
Inside that, there is just an image, for which height is always fixed to 100px.
I want to make the image horizontally center.
Here there are 3 cases:
image's width is equal to div's width, no issues
image's width is less than div's width, I can use margin: auto here
image's width is more than div's width
I want the center part of the image to be visible inside the div.
means, if image's width is 120px and as div's width is 100px and overflow:hidden
I want image's 10th px to 110th px to be visible (so, the left: 10px and right: 10px of image are hidden under the div )
Is this possible through some CSS property?
(I dont know the width of image which is loading! so I want it to be dynamic.
Also want to avoid javascript side calculations to find the extra amount of width and giving margin-left: -ve value bla bla.. )
Also, I can't give the image as background-image for the div!
See: http://jsfiddle.net/thirtydot/x62nV/ (and without overflow: hidden to easily see the centering)
This will work in all browsers, with the possible exception of IE6.
For .imageContainer > span, the margin-left is derived from the width, and the width is an arbitrary number which controls the maximal image width that will be supported. You could set width: 10000px; margin-left: -4950px; to support really wide images, if required.
HTML:
<div class="imageContainer">
<span><img src="http://dummyimage.com/100x100/f0f/fff" /></span>
</div>
CSS:
.imageContainer {
border: 1px solid #444;
overflow: hidden;
width: 100px;
height: 100px;
margin: 15px;
text-align: center;
}
.imageContainer > span {
display: block;
width: 1000px;
margin-left: -450px; /* -(width-container width)/2 */
}
.imageContainer > span > img {
display: inline-block;
}
I try to center a div on my page. It should use just relative values, so its size depends just on the window size.
I found a solution and it centers the box, but it seems that the margin isn't calculated correctly. Instead the size of the body gets bigger then the window. In the following example firebug tells me that the box #container has size 1265x335 on my screen. When inspecting #content its size is 506x134 and 2px boarder on each side, which matches my calculation. But the top margin is 316.
Have I got something wrong?
The margin should be 25% of 335, right?
How can I fix this?
Here's the HTML:
<body>
<div id="container">
<div id="content">
nothing...
</div>
</div>
</body>
And here the CSS:
html {
height: 100%;
}
body {
height: 100%;
margin: 0em 0em 0em 0em;
}
#container {
height: 100%;
}
#content {
text-align: center;
width: 40%;
height: 40%;
margin-top: 25%;
margin-left: auto;
margin-right: auto;
margin-bottom: auto;
border: 2px solid black;
}
From http://www.w3.org/TR/CSS2/box.html:
The percentage is calculated with respect to the width of the generated box's containing block. Note that this is true for 'margin-top' and 'margin-bottom' as well.
So you get 25% of 1265 which explains the behavior you are seeing. Unfortunately, I don't have any solutions to offer.
Edit: CSS3 makes this statement dependent on whether the containing block is horizontal (you would want it to be vertical). I don't think that any browsers implement the required block-progression property however (it might have unintended side-effects anyway).