3

Is this a bug in firefox?

CSS,

html, body {
    height: 100%;
    margin: 0;
    padding: 0 0;
    /*border: 4px solid black;*/
}

.container-fluid {
    height: 100%;
    width: 100%;
    display: table;
    padding-right: 0;
    padding-left: 0;
    /*border: 4px solid blue;*/
}

.row-fluid {
    height: 100%;
    width: 100%;
    display: table-cell;
    vertical-align: middle;
    background-color:#990000;
    /*border: 4px solid red;*/
}

.img-container {
    height: 100vh;
    width: 100%;
    display: flex;
    align-items: center;
}

.img-container img{
    max-width:100%;
    max-height:100%;
    margin-left: auto;
    margin-right: auto;
}

HTML,

  <div class="container-fluid">
    <div class="row-fluid">
        <div class="img-container">
            <!-- <img src="http://placehold.it/400x450"> -->
            <img src="http://placehold.it/2000x450">
            <!-- <img src="http://placehold.it/400x480"> -->
        </div>
    </div>
</div>

Chrome,

enter image description here

The large image will be scaled down to fit the screen width which is what I want.

Firefox,

enter image description here

The image is not scaled down to fit the screen.

Any ideas how I can fix this?

EDIT:

@-moz-document url-prefix() {
    .img-container img {
        width: 100%;
        max-width: -moz-max-content;
    }
}

enter image description here

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Run
  • 54,938
  • 169
  • 450
  • 748

3 Answers3

1

Yes there is problem in firefox. It will not maintaining aspect ratio. To make it working just add width: 100%; to image will solve issue.

.img-container img {
    margin-left: auto;
    margin-right: auto;
    max-height: 100%;
    max-width: 100%;
    width: 100%;
}

Working Fiddle

Check same type of issue here.

Edit:

To solve issue for all size image use max-width: -moz-max-content;

@-moz-document url-prefix() {

    .img-container img { width: 100%; max-width: -moz-max-content; }
}

Updated Fiddle

Community
  • 1
  • 1
ketan
  • 19,129
  • 42
  • 60
  • 98
1

Based on a bug report (see below), this is a known issue with Firefox. (Although IE11 also fails to scale the image as desired).

This seems to solve the problem in Firefox:

Instead of:

.img-container img {
    max-width: 100%;
    max-height: 100%;
    margin-left: auto;
    margin-right: auto;
}

Try this:

.img-container img {
    width: 100%;           /* adjusted */
    height: auto;          /* adjusted */
    margin-left: auto;
    margin-right: auto;
}

DEMO

Another possible solution involves adding table-layout: fixed to the main container (.container-fluid). This method is detailed in this bug report:

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
1

Since you are using CSS table for the layout already, I'm suggesting this approach without flexbox. It works nicely on Chrome and Firefox according to my tests. I added a div around the img.

jsFiddle

body { margin:0; }

.img-container {
  display: table;
  table-layout: fixed; /*required for responsive width in Firefox*/
  width: 100%; /*required for fixed table layout*/
}
.img-container .image {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  height: 100vh; /*required for responsive height*/
}
.img-container .image img {
  max-width: 100%;
  max-height: 100%;
  vertical-align: middle; /*remove whitespace*/
}
<div class="img-container">
  <div class="image">
    <img src="http://placehold.it/400x300">
    <!-- <img src="http://placehold.it/2000x450"> -->
    <!-- <img src="http://placehold.it/400x480"> -->
  </div>
</div>

Alternatively, you can use pseudo element :before or :after + inline block for vertical alignment. No markup change is required.

jsFiddle

body { margin:0; }

.img-container {
  width: 100vw; /*required for responsive width in Firefox*/
  height: 100vh;
  text-align: center;
  font-size: 0; /*remove whitespace*/
}
.img-container:after {
  content: "";
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
.img-container img {
  max-width: 100%;
  max-height: 100%;
  vertical-align: middle;
}
<div class="img-container">
  <img src="http://placehold.it/400x300">
  <!-- <img src="http://placehold.it/2000x450"> -->
  <!-- <img src="http://placehold.it/400x480"> -->
</div>
Stickers
  • 75,527
  • 23
  • 147
  • 186