Image set to "contain" on mobile is behaving weird

css3

#1

In this site: https://koyoor.in/ I have set first image to be contained in mobile instead of cover. The code I have is this:

@media only screen and (max-width: 768px)  {
  
  .slideshow__image {
  height: 100%;
  width: 100%;
  object-fit: contain;
  z-index: $z-index-base + 1;
  transition: $transition-image-slideshow;
  font-family: "object-fit: cover";
  object-position: center bottom !important;

  .placeholder-background {
    background-color: black;
  }

  .placeholder-svg {
    position: absolute;
    top: 50%;
    left: 50%;
    width: auto;
    max-height: none;
    max-width: none;
    @include transform(translate(-50%, -50%));
  }

}
  .slideshow {
  @include display-flexbox;
  position: relative;
  overflow: hidden;
  max-height: 300px !important;
    background-color: black !important;
    min-height:auto !important;
}
}

But when the page loads you can see the full size of the image for just a milisecond before it's correctly sized. Can someone help me know why? See here how the image shows full when the page is loaded and resizes almost right after: https://www.useloom.com/share/bc5ebe1f3a5c47418bb92363b66a014a

Thanks so much for all your help.


#2

I admit there's a lot of HTML5 and CSS3 that I'm not familliar with, but where did you get this from?

font-family: "object-fit: cover";

#3

Well, the image was originally set to cover, so I created the @media query to change the styling for mobile. I just copied all the code that is relevant to the slideshow image and changed the object-fit from cover to contain, which seemed like the obvious change.

Most of the rest of the code was already part of the original code. I already tried removing the font-family: "object-fit: cover"; or changing it to contain but this does not fix the issue. So I assume that is not what's causing the problem.

It's just not obvious to me what part of CSS could be causing the image to load as cover, and after the page is fully loaded, change to contain.


#4

I believe @Mittineague's point is that you are mixing two unrelated CSS declarations.

https://tympanus.net/codrops/css_reference/font-family/https://tympanus.net/codrops/css_reference/font-family/

https://tympanus.net/codrops/css_reference/object-fit/


#5

The original code is this:

I just copied it to create the new @media query for mobile. But that's the original code.

.slideshow__image {
  height: 100%;
  width: 100%;
  object-fit: cover;
  z-index: $z-index-base + 1;
  transition: $transition-image-slideshow;
  font-family: "object-fit: cover";

  .placeholder-background {
    background-color: $color-main-bg;
  }

  .placeholder-svg {
    position: absolute;
    top: 50%;
    left: 50%;
    width: auto;
    max-height: none;
    max-width: none;
    @include transform(translate(-50%, -50%));
  }
}

#6

Yes, and I have seen where broken CSS resulted in all of the CSS after the error being ignored. They were unpaired curly brace errors, but I don't know if an invalid rule might also cause a similar problem.


#7

I removed the font-family: "object-fit: cover"; and the issue is the same. The image loads in cover, and after the site loads, the image resizes to contain. This makes me assume that's not the issue.


#8

I don't know if SASS(?) would choke on buggy CSS.

Is there any improvement (even if not perfect) when you remove that font-family line?

EDIT
lol a bit of ninja posting mayhem going on.

I think you should leave that line out and investigate what can be done the remaining rules


#9

I have removed the line, but it's not getting any better.

You can see the site here: https://uayr6mtl2mnia03o-27980032.shopifypreview.com


#10

I'm not seeing any image resizing with iPad Safari. AFAIK (which is only a supposition) the viewport width is 768px, so the media query should be in effect. But maybe not.
Someone with a narrower viewport width will need to take a look unless anyone can see something obvious with the CSS that might be causing an image resize.


#11

@Mittineague Did you check with this link? https://uayr6mtl2mnia03o-27980032.shopifypreview.com/


#12

I did and suggest you check this link:

https://validator.w3.org/nu/?doc=https%3A%2F%2Fuayr6mtl2mnia03o-27980032.shopifypreview.com%2F


#13

@John_Betong I checked but I don't see anything related to the slideshow image which is the one I changed. I don't know a lot of this, therefore I don't know what I should be looking for in the link you shared. Is there something evident that is causing the issue that I'm missing?


#14

All web pages follow cascading rules and any invalid markup is resolved by a browser's best guess at what is trying to be achieved. Any incorrect solution will most likely affect all subsequent script.

Why not try to prevent browsers guessing by eliminating the errors and warnings?

Each error and warning is clearly shown, most with solutions.

If you do not understand a particular problem there are many experienced users here to offer assistance, some even supply full solutions which is not very helpful if you are trying to learn.


#15

@John_Betong I am trying to learn, and because of that I would not be confident enough to chage or fix other errors considering that this is a Shopify store and uses liquid and I think SCSS. I don't know if that validator link only checks for HTML or CSS errors, but again, the site is not in basic HTML and CSS so I'm not confident enough to start touching other parts of code other than the one I originally was editing. I really appreciate your input and help. I just hope someone could just point me in a more specific direction of what I could try to do to fix this considering what I said before. This site uses liquid and SCSS.

Thanks again, so much!


#16

One word: Backups

Make them, save them. When you need them, use them.

OK, maybe two words: Comments eg.

if ($var === 1) { 
//   $id = $var; // possibly the bug? cast to boolean? 
   $id = (bool) $var; // fix attempt

or CSS (// comments won't work for CSS)

.img { 
/*   object-fit: contain; */ 
   object-fit: cover;

Now that I think of it, also

Hack in your development environment, not the live site.


#17

@Mittineague thanks for your reply. Actually I decided to test with the most simple change. I went back to the original code and just changed from cover to contain:

.slideshow__image {
  height: 100%;
  width: 100%;
  object-fit: contain;
/* object-fit: cover; */
  z-index: $z-index-base + 1;
  transition: $transition-image-slideshow;
  font-family: "object-fit: cover";

When checking on any device you can see the same issue happening. While loading the image is on cover. After loading the image changes to contain. See site: https://znrowsn1fw9uf3pe-27980032.shopifypreview.com

After this I can only conclude that the issue is not from anything I'm doing in particular, but the code is already wrong in some way. Otherwise it should not be having this behavior when changing just one line of code.

Do you think this is a behavior generated from the object-fit property? or from other code conflicting with it? And why could this be happening only while the website is loading but gets fixed after it loads?


#18

I don't know, but object-fit wouldn't be my first suspect. My first guess would be the transition as the cause.

Maybe you are on slow connection? In any case some things can cause a page to "jump" or "shift" as they redraw from how they initially determine what the layout will be to what later information specifies what it should be. eg. if an image is 500x500 the browser will reserve that area in the display. If later the dimensions are specified to something different, that area will shrink / grow and the surrounding content will move to accommodate that change.

If you temporarily comment out
/* transition: $transition-image-slideshow; */
and
/* @include transform(translate(-50%, -50%)); */
even though that will likely break the slideshow, does it at least stop the image resize problem?


#19

@Mittineague it does not fix it. Removing or commenting out those lines does not fix the issue. Here I loaded the site 2 times: https://www.screencast.com/t/Z3yiYk2t7 Each time you can see how the image resizes and it's too noticeable.


#20

Some thoughts for your consumption that may or may not be of any value..

I notice that there seems to be some duplication of rules in the code in post #17. It seems possible that you may not be commenting out all of the iterations of image max-height and max-width and other relevent rules in CSS. The duplicate IDs show up as errors in the validator:
https://validator.w3.org/nu/?doc=https%3A%2F%2Fznrowsn1fw9uf3pe-27980032.shopifypreview.com%2F
Click the small button "Message Filtering" for a summary view.

Also noted that JS places the auto-resized dimensions inline which may require !important to override.

While the image resizing JavaScript is great in that it reduces the "weight" of the slide-show images for small devices, if you are testing the ability of the JS to resize the images by dragging your desktop browser narrower, you might be exceeding the computational capacity of your computer thereby causing the jerkiness when resizing the browser. Most users will not be resizing their browsers on the fly.

HTH