Optimal use of responsive image (right size for the template)
I'm trying to figure out how I can best use responsive images (srcset / sizes) to fit my template and choose the right image to load.
My template has basically 3 breakpoints:
Up to 1000px: everything is in one column occupying 100% of the width
Up to 1600px: everything is in two column with an approximate ratio of 2/3 - 1/3
Above 1600px: two colums 4/5 - 1/5 but inside the first 4/5 column I have a smaller one
For the template under 1000px, I use the following in sizes:
(max-width: 1024px) calc(100vw - 20px)
That works fine, it occupies the space I want and that's an easy one because I don't need to take other columns into account.
The tricky part comes from the other ones because the size of an image inside one of the columns depends on the size of the other columns. In any case, I would like the image to occupy 100% of the available space but with "sizes" I'm struggling to achieve something that works.
For up to 1600px I have:
(max-width: 1600px) 66vw
This should correspond to the ratio used in the template but in reality it never achieves the goal of fitting the image inside the parent div.
For the last breakpoint, I have the same issue, it either falls too short either too large.
Here is an example of a picture:
<amp-img src="https://www.laurentwillen.be/wp-content/uploads/sites/21/2018/05/xiaomi-redmi-note-5-face-up-down.jpg" alt="xiaomi redmi note 5" width="4000" height="1240" class="alignleft size-full wp-image-2620" srcset="https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down.jpg 4000w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-300x93.jpg 300w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-768x238.jpg 768w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-1024x317.jpg 1024w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-1400x434.jpg 1400w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-900x279.jpg 900w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-700x217.jpg 700w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-500x155.jpg 500w" sizes="(max-width: 1024px) calc(100vw - 15px), (max-width: 1600px) 66vw, 50vw" /></amp-img>
If I suppress "sizes", it makes it worse as it displays the image in its full size. AMP requires width and height to be displayed so I can't get rid of that and as I'm using PHP, I can't find width and height with the appropriate values before the page is loaded.
Do you have any suggestions?
Thanks
Laurent
image responsive amp-html
add a comment |
I'm trying to figure out how I can best use responsive images (srcset / sizes) to fit my template and choose the right image to load.
My template has basically 3 breakpoints:
Up to 1000px: everything is in one column occupying 100% of the width
Up to 1600px: everything is in two column with an approximate ratio of 2/3 - 1/3
Above 1600px: two colums 4/5 - 1/5 but inside the first 4/5 column I have a smaller one
For the template under 1000px, I use the following in sizes:
(max-width: 1024px) calc(100vw - 20px)
That works fine, it occupies the space I want and that's an easy one because I don't need to take other columns into account.
The tricky part comes from the other ones because the size of an image inside one of the columns depends on the size of the other columns. In any case, I would like the image to occupy 100% of the available space but with "sizes" I'm struggling to achieve something that works.
For up to 1600px I have:
(max-width: 1600px) 66vw
This should correspond to the ratio used in the template but in reality it never achieves the goal of fitting the image inside the parent div.
For the last breakpoint, I have the same issue, it either falls too short either too large.
Here is an example of a picture:
<amp-img src="https://www.laurentwillen.be/wp-content/uploads/sites/21/2018/05/xiaomi-redmi-note-5-face-up-down.jpg" alt="xiaomi redmi note 5" width="4000" height="1240" class="alignleft size-full wp-image-2620" srcset="https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down.jpg 4000w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-300x93.jpg 300w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-768x238.jpg 768w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-1024x317.jpg 1024w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-1400x434.jpg 1400w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-900x279.jpg 900w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-700x217.jpg 700w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-500x155.jpg 500w" sizes="(max-width: 1024px) calc(100vw - 15px), (max-width: 1600px) 66vw, 50vw" /></amp-img>
If I suppress "sizes", it makes it worse as it displays the image in its full size. AMP requires width and height to be displayed so I can't get rid of that and as I'm using PHP, I can't find width and height with the appropriate values before the page is loaded.
Do you have any suggestions?
Thanks
Laurent
image responsive amp-html
add a comment |
I'm trying to figure out how I can best use responsive images (srcset / sizes) to fit my template and choose the right image to load.
My template has basically 3 breakpoints:
Up to 1000px: everything is in one column occupying 100% of the width
Up to 1600px: everything is in two column with an approximate ratio of 2/3 - 1/3
Above 1600px: two colums 4/5 - 1/5 but inside the first 4/5 column I have a smaller one
For the template under 1000px, I use the following in sizes:
(max-width: 1024px) calc(100vw - 20px)
That works fine, it occupies the space I want and that's an easy one because I don't need to take other columns into account.
The tricky part comes from the other ones because the size of an image inside one of the columns depends on the size of the other columns. In any case, I would like the image to occupy 100% of the available space but with "sizes" I'm struggling to achieve something that works.
For up to 1600px I have:
(max-width: 1600px) 66vw
This should correspond to the ratio used in the template but in reality it never achieves the goal of fitting the image inside the parent div.
For the last breakpoint, I have the same issue, it either falls too short either too large.
Here is an example of a picture:
<amp-img src="https://www.laurentwillen.be/wp-content/uploads/sites/21/2018/05/xiaomi-redmi-note-5-face-up-down.jpg" alt="xiaomi redmi note 5" width="4000" height="1240" class="alignleft size-full wp-image-2620" srcset="https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down.jpg 4000w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-300x93.jpg 300w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-768x238.jpg 768w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-1024x317.jpg 1024w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-1400x434.jpg 1400w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-900x279.jpg 900w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-700x217.jpg 700w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-500x155.jpg 500w" sizes="(max-width: 1024px) calc(100vw - 15px), (max-width: 1600px) 66vw, 50vw" /></amp-img>
If I suppress "sizes", it makes it worse as it displays the image in its full size. AMP requires width and height to be displayed so I can't get rid of that and as I'm using PHP, I can't find width and height with the appropriate values before the page is loaded.
Do you have any suggestions?
Thanks
Laurent
image responsive amp-html
I'm trying to figure out how I can best use responsive images (srcset / sizes) to fit my template and choose the right image to load.
My template has basically 3 breakpoints:
Up to 1000px: everything is in one column occupying 100% of the width
Up to 1600px: everything is in two column with an approximate ratio of 2/3 - 1/3
Above 1600px: two colums 4/5 - 1/5 but inside the first 4/5 column I have a smaller one
For the template under 1000px, I use the following in sizes:
(max-width: 1024px) calc(100vw - 20px)
That works fine, it occupies the space I want and that's an easy one because I don't need to take other columns into account.
The tricky part comes from the other ones because the size of an image inside one of the columns depends on the size of the other columns. In any case, I would like the image to occupy 100% of the available space but with "sizes" I'm struggling to achieve something that works.
For up to 1600px I have:
(max-width: 1600px) 66vw
This should correspond to the ratio used in the template but in reality it never achieves the goal of fitting the image inside the parent div.
For the last breakpoint, I have the same issue, it either falls too short either too large.
Here is an example of a picture:
<amp-img src="https://www.laurentwillen.be/wp-content/uploads/sites/21/2018/05/xiaomi-redmi-note-5-face-up-down.jpg" alt="xiaomi redmi note 5" width="4000" height="1240" class="alignleft size-full wp-image-2620" srcset="https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down.jpg 4000w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-300x93.jpg 300w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-768x238.jpg 768w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-1024x317.jpg 1024w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-1400x434.jpg 1400w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-900x279.jpg 900w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-700x217.jpg 700w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/05/xiaomi-redmi-note-5-face-up-down-500x155.jpg 500w" sizes="(max-width: 1024px) calc(100vw - 15px), (max-width: 1600px) 66vw, 50vw" /></amp-img>
If I suppress "sizes", it makes it worse as it displays the image in its full size. AMP requires width and height to be displayed so I can't get rid of that and as I'm using PHP, I can't find width and height with the appropriate values before the page is loaded.
Do you have any suggestions?
Thanks
Laurent
image responsive amp-html
image responsive amp-html
edited Jan 2 at 14:18
fstanis
3,41311130
3,41311130
asked Dec 30 '18 at 14:06
LaurentLaurent
450518
450518
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978268%2foptimal-use-of-responsive-image-right-size-for-the-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978268%2foptimal-use-of-responsive-image-right-size-for-the-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown