background image doesn't shrink as screen size in firefox - html

I have a div called .outer which contained a div called .film, and the div .film has some of image divs.
The problem is .outer div doesn't shrink it as the screen size in Firefox like this picture:
As you can see, the div is shrinking well in Chrome(left side) but not in Firefox(right side).
Are there any ways to shrink the .outer div in Firefox just like Chrome does?
This is the code:
* {
margin: 0;
padding: 0;
}
section, div {
position: relative;
}
.outer {
margin: 0 auto;
width: 500px;
height: 500px;
display: flex;
flex-flow: row;
justify-content: center;
overflow: hidden;
}
.film {
width: 90%;
margin: 0 .8%;
display: flex;
flex-flow: column;
justify-content: center;
flex-shrink: 0;
padding-bottom: 33.3333%;
background-image: url('https://i.imgur.com/3p6TLYE.jpg');
background-size: cover;
background-position: center center;
overflow: hidden;
}
.film:nth-child(2n-1) {
opacity: .4;
transform: scale();
}
<div class="outer">
<div class="film"></div>
<div class="film"></div>
<div class="film"></div>
</div>

First thing which comes to mind is, setting display, width and height property of html and body tag.
Have you tried the following?
html, body {
display: block;
width: 100%;
height: 100%;
}

Possible solution I can find is that changing your 500px to auto that works in firefox I have tested while now the maximum width is changed which can be controlled by max-width tag just change your outer with my code :
.outer {
margin: 0 auto;
width: auto;
max-width: 500px;
height: 500px;
display: flex;
flex-flow: row;
justify-content: center;
overflow: hidden;
}

I think I found the solution and add the answer for the people who are having the same problem like me.
In Firefox, you should not use the px units if you want to shrink your div as the screen size. Use the padding-bottom instead of a height of the image like this:
.outer {
margin: 0 auto;
width: 80%; // basic size of the holder(outer). Use percentage unit to control it.
display: flex;
flex-flow: row;
justify-content: center;
overflow: hidden;
}
.film {
width: 90%;
margin: 0 .8%;
display: flex;
flex-flow: column;
justify-content: center;
flex-shrink: 0;
padding-bottom: 70.3333%; // Set the height of the images.
background-image: url('https://i.imgur.com/3p6TLYE.jpg');
background-size: cover;
background-position: center center;
overflow: hidden;
}
.film:nth-child(2n-1) {
opacity: .4;
transform: scale();
}
<div class="outer">
<div class="film"></div>
<div class="film"></div>
<div class="film"></div>
</div>

Related

Scroll div with overflow y with image is distorting image in chrome and firefox but not safari

I have long image that I want to display in a container with a scroll bar. Perhaps I'm not going about this the right way, but at the moment I have a container(work-sample) with a project-container with the image inside. It displays fine in safari but for some reason when viewed in chrome and safari on macs, the long image is all scrunched together.
#work-sample {
flex: 1 3 auto;
flex-basis: 3;
display: flex;
flex-direction: column;
justify-content: flex-end;
width: 100%;
height: 400px;
position: relative;
overflow-y: scroll;
padding-right: 1rem;
padding-top: 1rem;
padding-bottom: 1rem;
}
.project-scroll {
overflow-x: hidden;
overflow-y: scroll;
object-fit: cover;
}
#project-long {
width: 100%;
height: 100%;
text-align: center;
}
<div id="work-sample" class="work-sample">
<div class="project-scroll">
<img id="project-long" src="../media/incompatible-image.png">
</div>
</div>
There is a height setting of 100% in the class for the img. This makes the img distorted as it's forced to fit that height. This snippet removes this so the height will default to auto and be shown in proportion to the overall image's aspect ratio.
#work-sample {
flex: 1 3 auto;
flex-basis: 3;
display: flex;
flex-direction: column;
justify-content: flex-end;
width: 100%;
height: 400px;
position: relative;
overflow-y: scroll;
padding-right: 1rem;
padding-top: 1rem;
padding-bottom: 1rem;
}
.project-scroll {
overflow-x: hidden;
overflow-y: scroll;
object-fit: cover;
}
#project-long {
width: 100%;
/* height: 100%; REMOVED */
text-align: center;
}
<div id="work-sample" class="work-sample">
<div class="project-scroll">
<img id="project-long" src="https://picsum.photos/id/1015/200/600">
</div>
</div>
I think the issue is because object-fit is on the parent to your img but should be on img itself.
#work-sample {
flex: 1 3 auto;
flex-basis: 3;
display: flex;
flex-direction: column;
justify-content: flex-end;
width: 100%;
height: 400px;
position: relative;
overflow-y: scroll;
padding-right: 1rem;
padding-top: 1rem;
padding-bottom: 1rem;
}
.project-scroll {
overflow-x: hidden;
overflow-y: scroll;
}
#project-long {
width: 100%;
height: 100%;
object-fit: cover; /* this property moved from .project-scroll */
text-align: center;
}
<div id="work-sample" class="work-sample">
<div class="project-scroll">
<img id="project-long" src="https://generative-placeholders.glitch.me/image?width=1200&height=500">
</div>
</div>

Charts not using available space with Flex

I have a problem with Flex for Google Charts. Div with charts is not using available height. Am I missing something? (colors and blue borders are for development purpose so I'm sorry for that...)
CSS
.pc-metrics-chart {
display: flex;
flex-direction: column;
width: 100%;
height: 65%;
overflow: hidden;
background-color: var(--yellow);
}
.metric-ch{
height: auto;
flex : 1;
justify-content: stretch;
align-items: flex-end;
position: relative;
background-color: var(--success);
}
.ManagementCharts {
flex: 1;
overflow: hidden;
}
HTML
<div class="pc-metrics-chart" id="pc-metrics-chart-proj">
<div class="metric-ch">
<div id="totalProjectChart1" class="ManagementCharts">
</div>
</div>
</div>
I was trying to play with the position, alignments, height of the chart as well, but I'm using it's auto height and I thought that Flex will fill available space under the chart:
It is possible to use height property and set it to 100% to fill the remain space:
body {
margin: 0;
}
.pc-metrics-chart {
display: flex;
flex-direction: column;
width: 100%;
height: 100vh;
overflow: hidden;
background-color: yellow;
}
.metric-ch{
height: auto;
flex : 1;
justify-content: stretch;
align-items: flex-end;
position: relative;
background-color: lightgreen;
}
.ManagementCharts {
flex: 1;
overflow: hidden;
background-color: red;
height: 100%;
}
<div class="pc-metrics-chart" id="pc-metrics-chart-proj">
<div class="metric-ch">
<div id="totalProjectChart1" class="ManagementCharts">
Here should be chart placed
</div>
</div>
</div>

Display images in flexbox at 100% height without scrollbar

I'm trying to display an app bar and three images in a column, that uses 100% of the height of the screen. The images are supposed to use the whole width of the column with the rest being cut off. I can get it working with just divs, but I'm having trouble when using images.
Here is a version to illustrate how it should look like. This has an app bar of height 50 and three "images" that fill the rest of the space:
https://codepen.io/Meerpohl/pen/zYxRKRV
And here is what I get with images. The images stretch the heights of my divs and ultimately of everything else, resulting in that scrollbar. Instead I need thin slices of the images.
html, body {
height: 100%;
margin: 0;
}
.root {
height: 100%;
display: flex;
flex-direction: column;
}
.appbar {
height: 50px;
text-align: center;
background-color: coral;
}
.container {
flex: 1;
}
.item {
height:33.33%;
overflow:hidden;
}
img {
width: 100%;
object-fit: cover;
}
<div class="root">
<div class="appbar">
This is a nice app bar
</div>
<div class="container">
<div class="item">
<img src="http://www.kleines-meerwasseraquarium.de/wp-content/uploads/2016/02/Zitronengrundel.jpg">
</div>
<div class="item">
<img src="http://www.kleines-meerwasseraquarium.de/wp-content/uploads/2016/02/Zitronengrundel.jpg">
</div>
<div class="item">
<img src="http://www.kleines-meerwasseraquarium.de/wp-content/uploads/2016/02/Zitronengrundel.jpg">
</div>
</div>
</div>
https://codepen.io/Meerpohl/pen/eYmVdro
The code is the same in both cases. One just uses text instead images.
.item {
position: relative;
height:33%;
overflow:hidden;
}
img {
position: absolute;
transform: translateY(-50%);
width: 100%;
object-fit: cover;
}
this should work i think!
i'm on the train right now, so I can't give you a pen.
You can position the image absolute in the parent div (this should be relative) and translateY so it is centered.
Hope this is what you want to do ;)
use this!
img {width: 100%; object-fit: cover; max-height: 33.33vh; }
Try this:
html, body {
height: 100%;
margin: 0;
}
.root {
height: 100%;
display: flex;
flex-direction: column;
}
.appbar {
height: 50px;
background-color: coral;
display: flex;
justify-content: center;
align-items: center;
}
.container {
height: calc(100vh - 50px);
display: flex;
flex-direction: column;
}
.item {
overflow: hidden;
display: flex;
flex: 1;
}
img {
width: 100%;
object-fit: cover;
}
added calc to .container and display:flex on .item, removed some unused properties.
Codepen: https://codepen.io/Liveindream/pen/NWPygpx

Image inside flex parent with max-height and max-width and object-fit contain

Image stretches if I don't use object-fit contains. Stretches in width, losing aspect ratio.
object-fit contain fixes that.
The problem is, the element itself is not contained, just the visible image. Which means if I make the image clickable, the whole element area (even outside the image) is clickable.
https://jsfiddle.net/nyysyngp/10/ (or see code below)
I just want the visible image to be clickable. This seems to work on Firefox, but not Chrome.
body, html
{
margin: 0;
padding: 0;
background-color: red;
display: flex;
height: 100%;
width: 100%;
}
#media
{
display: flex;
background-color: #262423;
justify-content: center;
align-items: center;
flex-direction: column;
flex-grow: 1;
}
#media_split
{
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
align-items: center;
}
#media_image_container
{
height: 50%;
width: 100%;
flex-grow: 1;
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
background-color: green;
}
#media_image
{
object-fit: contain;
max-height: calc(100% - 4em);
max-width: calc(100% - 4.7em);
min-height: 100px;
min-width: 100px;
cursor: pointer;
}
#media_tv
{
height: 50%;
width: 100%;
flex-grow: 1;
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
background-color:blue;
}
<div id='media'>
<div id='media_split'>
<div id='media_image_container'>
<img id='media_image' src='https://i.imgur.com/F26h0tq.jpg'>
</div>
<div id='media_tv'></div>
</div>
</div>
Well some months later I found a solution. Just by adding "position: absolute" to #media_image the problem went away, which in my case didn't break anything else.
In #media_image_container remove display: flex; and add text-align: center;
It will fix the issue.

How to fit an image vertically in a flex container with direction: column

I'm trying to fit an image vertically in a flex container which has a specific height.
The flex-direction is column, and the image is contained in a flex-item with flex-basis: 100%.
In addition, the image's max-height is 100%.
As you can see in the example, the image does not fit into the red container.
#container {
background-color: red;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 200px;
width: 320px;
justify-content: space-between;
}
#container > * {
padding: 5px;
}
#img {
flex: 0 1 100%;
/* height: 100%; */
}
img {
max-height: 100%;
max-width: 100%;
}
<div id="container">
<div id="img">
<img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=img&w=340&h=500">
</div>
<div>
Something else
</div>
</div>
Shouldn't the image shrink to fit vertically into the flex container, according to the specification?
The workaround I found is to set the height of #img to 100%, but I have the sensation that it's not the way it should be done.
As an additional note, if I set flex-direction: row to the container, it fits the image horizontally (which is the behaviour I would expect).
You wrote:
The workaround I found is to set the height of #img to 100%, but I have the sensation that it's not the way it should be done.
Actually, it is the way it should be done. The predominant implementation of the spec requires that the height property be applied to the parent when using percentage heights on the child. (Although this is slowly evolving. See my second reference below.)
References:
Working with the CSS height property and percentage values
Heights rendering differently in Chrome and Firefox (includes alternative solutions)
CSS height property definition (W3C)
For some reason, I couldn't get the <img> to behave (probably because it's a replaced element). So I removed it, and used the .img div with an image as background.
Relevant Changes
.container {
...
justify-content: center;
align-items: center;
}
.img {
flex: 1 0 auto;
background: url(https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png) no-repeat;
background-size: contain;
background-position: center;
width: 100%;
}
SNIPPET
.container {
background-color: red;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 200px;
width: 320px;
justify-content: center;
align-items: center;
}
.container > * {
padding: 5px;
}
.somethingElse {
outline: 1px dashed yellow;
background: rgba(128,0,255,.3);
color: white;
}
.img {
flex: 1 0 auto;
background: url(https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png) no-repeat;
background-size: contain;
background-position: center;
width: 100%;
}
<figure class="container">
<div class="img">
</div>
<figcaption class="somethingElse">
True Dimentions: 512 x 512 px
</figcaption>
</figure>
try this css. its works fine.
#container {
background-color: red;
/*display: flex;*/
flex-direction: column;
flex-wrap: wrap;
height: 200px;
width: 320px;
justify-content: space-between;
}
#img {
height: 85%;
width: 100%;
}