Mysterious behavior when using CSS "object-fit:contain" property on images inside a flexbox container having a specific height - css

I've got a flexbox container in which I'm trying to display two images with drop-shadows side-by-side. I want them to take equal amounts of horizontal space even though the images differ in size. I'm using a container with style "display: flex" and using "object-fit: contain" for the images to cause them to scale. My code works if I don't give the container a specific height. If I give the container a specific height, such as 300px, the images scale down, but the drop-shadow appears at a distance from the image edges as though there's a box wrapping them. This seems odd behavior. Can anyone explain this odd-seeming behavior, and is there a way in which I can give the container a height and still get it to work?
Fiddle to illustrate: https://jsfiddle.net/Lej1a6vp/
html:
<div class="container">
<img class="image" src="http://via.placeholder.com/300x400" />
<img class="image" src="http://via.placeholder.com/400x300" />
</div>
css:
.container {
display: flex;
margin: 1em auto 3em auto;
width: 500px;
height: 200px;
justify-content: center;
align-items: center;
}
img {
box-shadow: 8px -8px 10px #00000080;
height: 100%;
width: 40%;
margin-left: 1em;
object-fit: contain;
}

If you set the height to auto it will make them the same width but height will be different, if you want them to be the same height and width then you have to use an image with the same aspect-ratio for this to work since it is not possible to make it the same width and height without cropping if aspect ratios are different.
There is a #FutureCSS property called aspect-ratio: 16 / 9; in which you can lock elements to a specific ratio but it's still not available in all browsers;
.container {
display: flex;
margin: 1em auto 3em auto;
width: 500px;
height: auto;
justify-content: center;
align-items: center;
}
img {
box-shadow: 8px -8px 10px #00000080;
height: auto;
width: 50%;
margin-left: 1em;
object-fit: contain;
}

BDB88 provided me with the clue I needed, and thanks for that. My objective is to make a responsive layout that will show the drop shadow around the images and not some ghostly outline that's at a distance, and I want to keep the images' aspect ratios. I also want to mandate a particular height for the container, and not have the images overflow outside of it. My use of "height: 100%;" for the img tag was what was causing the problem. Combining that with the "width: 40%;" was causing conflict because both requirements can't always be satisfied simultaneously. By changing to "max-height: 100%;" and "max-width: 40%;" for the img tag, I'm getting the behavior that I was after. The CSS is now (I made some additional edits to make the behavior more apparent when viewing and scaling the window to simulate larger/smaller screen sizes):
.container {
background: yellow;
display: flex;
margin: 1em auto 3em auto;
width: auto;
height: 200px;
justify-content: center;
align-items: center;
}
img {
box-shadow: 8px -8px 10px #00000080;
max-width: 40%;
max-height: 100%;
margin-left: 1em;
object-fit: contain;
}

Related

CSS image in percentage sized container

I have the following code:
https://codepen.io/rctneil/pen/NWwGQyr?editors=1100
Basically my issue revolves around the .card-logo elements.
I will be putting a variety of logo images in this location. I need them to be a maximum width of 50% of the parent card and a maximum height of 50% of the width of the parent card.
That itself wouldn't be too difficult, but I also need them to be less than that if the aspect of the image requires it. Eg, if it's a portrait image then the height should be 50% of the width of the card but the width of the logo needs to just be whatever it ends up being naturally.
Any ideas?
Here is my solution : https://codepen.io/Woralie/pen/dyZvzRP
You'll need to add a container for each logo image.
.card-logo {
position: absolute;
display: block;
max-width: 50%;
max-height: 100%;
margin-top: 0.75rem;
padding: 0.75rem;
background-color: white;
}
.card-logo-container {
display: flex;
justify-content: center;
align-items: center;
position: relative;
margin-top: -25%;
margin-bottom: 0.75rem;
padding: 25%;
min-width: 100%;
}
The trick is to make the container position: relative with padding: 25% and the image position: absolute with max-width: 50%. The container's top and bottom paddings take 2*25% of the total width, so when its height is 0, the image's height at max-height: 100% takes a maximum of 50% of the container's total width.

right and left padding in a flexbox container is stretching the child image

I have a flexbox, direction column, with four children, an image and three text elements.
The image has correct proportions when there's no padding in the container, but the more right and left padding I add in the container, the more vertically stretched the image gets. This is only happening on mobile devices, iPhone, iPad, etc.
It's as if the flexbox is treating the image as a text element, with more padding it stretches it to fit more content.
align-self: center; and align-items: center; are not working in this case
Here's a simplified code.
<li class="painting" >
<img src="src">
<h5 class="Title">{{title}}</h5>
<p class="Summary">{{summary}}</p>
<p class="Price">Price: {{price}}</p>
</li>
the CSS
li{
width: 22%;
margin: 3vh 2%;
padding: 1% 2%;
display: flex;
flex-direction: column;
}
img{
width: 100%;
height: auto;
margin: auto 0;
align-self: center;
flex: 0 0 auto;
}
h5{
margin: 1rem 0 0 0;
padding: 0 0 0.5rem 0;
}
p{
margin: 1rem 0 0 0;
white-space: pre-wrap;
flex-shrink: 1;
}
Wrapping the image in a non-flex container solves the problem, but I would like to understand the problem.
I don't get this behaviour and why exactly it happens on mobile devices. Could anyone shed some light on the issue?
You can make use of the object-fit property to avoid stretching the image.
Note: Avoid assigning percentage values for padding, margin etc. You can always make use of media-query for responsive padding.
To prevent your child image being stretched set a width on the image tag then constrain the width in css.
HTML:
<img src="src" width="400">
Then change your CSS:
img {
max-width: 100%;
}

How to make a responsive grid where margins decrease as grid content stays the same?

I am trying to make a grid similar to this one:
https://masteroverwatch.com/streams/sort/skillrating
where the margins around the grid become narrower and the grid stays the same width and aspect ratio until it absolutely has to become smaller via a break point.
any insight is greatly appreciated.
You should use margin: 0 auto;.
Here's a simple example:
.container {
width: 100%;
background: red;
height: 100vh;
}
.box {
width: 400px;
background: blue;
height: 100vh;
margin: 0 auto;
}
<div class="container"><div class="box"></div></div>

CSS DOM issue - cannot find a reason why property is being overwritten

The site in question is 1000freewebsites.com. The specific pages I'm struggling with are:
1000freewebsites.com/signup.php
1000freewebsites.com/login.php
This site uses the skeleton framework and Ryan Fait's sticky footer. On these pages I have a div with the ID of #bluestripe that should fill the vertical space between the header and the footer.
There are three parent elements; #html, #body and .wrapper. All are set to height:100%; in the stylesheet. #bluestripe is also set to height:100% and min-height:100%. As I understand it, this should achieve the effect I desire. Do I have my theory wrong?
Using Chrome Inspector I find that the height attribute is crossed out for .wrapper. If my theory is correct, this explains why #bluestripe is not expanding to fill the vertical space.
I cannot find any element that over rides .wrapper's height setting. Can you see what I am missing?
Your CSS rule for .wrapper has 2 height declarations. Get rid of the one setting height to auto.
.wrapper {
min-height: 100%;
height: auto !important; /* <- Get rid of this one */
margin: 0 auto -40px;
height: 100%;
}
this is your css:
.wrapper {
min-height: 100%;
height: auto !important; //height here
margin: 0 auto -40px;
height: 100% ;//height again here
}
you are defining two times the height and as the first one got !important its overriding the second one
this cause another error, because the paddings and the other elements are pushing the .container div down, so if you change a few properties you can get rid of this behavior:
#bluestripe {
background: #0099cc;
width: 100%;
padding: 40px 0px 40px 0px;
border-top: 10px solid #666666;
/*height: 100%; drop this line*/
}
.wrapper {
background: #0099cc; /*add this line*/
min-height: 100%;
margin: 0 auto -40px;
height: auto; /*acording to ryanfaits's css this is what mades the footer stick to the botom*/
}
this will made the .bluestripe shrink again but as the .wrapper still has the same background color, it doesn´t matters

Prevent image wrap (move) around on browser resize

On my main page, I have an image that moves around or wraps around on browser resize. I just want to change it so that the image gets cut from the right if the browser is small.
I have tried a few things to no avail:
Remove the float on the div id right and the relative positioning on right-image
Try min-width for div id right
Give height, widths in percentages
Added viewport settings.
In the template, the image is placed outside the main like:
<body>
<div id = "main">
<div id = "left">
<div id="left-title">Tag Line</div>
<div id="left-blurb">
Some blurb
</div>
<div id='left-signup'> SignUp! button</div>
</div>
<div id = "right">
<div id = "right-image"></div> <--- Image
</div>
</body>
Relevant css:
body {
margin: 0 auto;
height: auto;
overflow-x: hidden;
}
#main {
float: center;
width: 950px;
overflow: visible;
height: auto;
margin: 0 auto;
}
#left {
float: left;
width: 500px;
display: inline-block;
}
#left-title {
font-size: 3.4em;
margin: 25px 0px 20px 15px;
}
#left-blurb {
margin: 0px 20px 10px 15px;
}
#left-signup {
margin: 0px 0px 0px 90px;
}
#right {
float: right;
width: 600px;
height: 400px;
margin-top: -10px;
display: inline-block;
}
#right-image {
height: 100%;
width: 100%;
position: relative;
left: -50px;
top: 0px;
background: url(my_photo.jpg) no-repeat;
-moz-background-size: cover;
background-size: cover;
z-index: 0;
}
I think that is the css that is relevant to the question. But if it is not enough, the website I am talking about is https://www.mathnuggets.com
Will appreciate any insights.
You can add a media query to just remove it from the document when your document size is less than a certain number of pixels.
#media screen and (max-width:480px) {
#right-image{
display: none;
}
}
Otherwise, you can also adjust its positioning as you've done using the same method, just adjusting its position depending on window size. The issue I'm seeing in your code is that you're using a -10px margin that is causing it to overlap your other elements, so you could simply change that for certain viewport sizes.
In addition, you might want to consider changing your sizing method from being absolute using pixels and instead use percentages so that your image can flex a little as your viewport changes.
This site has a lot of great resources I think could help you to work with your existing design so that it can be more responsive as needed: http://blog.teamtreehouse.com/beginners-guide-to-responsive-web-design
If you use
position: absolute;
for ID=right div it will never go under left div

Resources