You're not matching the size of the srcset with the max-width you want in sizes. In your code to show rufy1 the viewport should be at least 850w, otherwise the browser will follow the second rule.
With your srcsetyou want rufy1 when the viewport has a max width of 600px and rufy2 when the viewpoert has a max width of 2900px. In order to accomplish it:
Basically, the value (not the image, the number) from srcset will be yout rule to the media-query.
With your rules for max-width the first image would have a width: 680px and the second one a max-width: 799px. rufy2is being shown because 1200px is higher than the first rule max-width: 600px.
EDIT:
If you want a fallback image in case none of the rules are met, you can use this new approach:
With this syntax you are specifying each source to a rule, notice the media and srcset attributes inside each <source>, and in case none of the rules apply, it falls back to the <img>
Ok, but the width of the slot when the viewport is 1200px is written to be 799px, and 850w is closer to 799px than 900w, so I think that rufy1.jpg should be shown. I wasn't able to understand, Tiago. Sorry, but can you help me with one more snippet?
I'm serious. For example:
Why
rufy2.jpg
is shown when the viewport is 1200 pixels wide?You're not matching the size of the
srcset
with themax-width
you want insizes
. In your code to showrufy1
the viewport should be at least850w
, otherwise the browser will follow the second rule.With your
srcset
you wantrufy1
when the viewport has a max width of 600px andrufy2
when the viewpoert has a max width of 2900px. In order to accomplish it:Basically, the value (not the image, the number) from
srcset
will be yout rule to the media-query.With your rules for
max-width
the first image would have awidth: 680px
and the second one amax-width: 799px
.rufy2
is being shown because1200px
is higher than the first rulemax-width: 600px
.EDIT:
If you want a fallback image in case none of the rules are met, you can use this new approach:
With this syntax you are specifying each source to a rule, notice the
media
andsrcset
attributes inside each<source>
, and in case none of the rules apply, it falls back to the<img>
Here's an example
Ok, but the width of the slot when the viewport is 1200px is written to be 799px, and 850w is closer to 799px than 900w, so I think that
rufy1.jpg
should be shown. I wasn't able to understand, Tiago. Sorry, but can you help me with one more snippet?Why is
jinbe.png
always shown when I resize the browser window?What is the expected output in this example? It seems like you're struggling with CSS and precedence.
It sounds trivial but that is what CSS (Cascading Style Sheets) is, I can't stress enough to read about it.
With these rules you're saying:
Confusing, isn't? I'd recommend read an article on responsive design, as you can see it can become a pain.
In any case, I copied and paste your code into a pen just changing to dummy images and it seems to be working? Can you try it?
Let's review the queries:
Here's the browser at 1200px
Here's at 1000px
Finally at 900px
It doesn't work here. Tiago, could I write directly to you privately in any way, maybe a chat?
Sure! Hit me up!
Where?
~I'l DM ai Twitter~
Better yet, I sent you an email