amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

amp-img width / height restrictions

Open gkteck opened this issue 10 years ago • 6 comments

As per the doc, amp-img must be given an explicit size (as in width / height) in advance.

With this restrictions, how can we build a responsive design ?

gkteck avatar Dec 25 '15 21:12 gkteck

@gkteck The width and height is used to determine the aspect ratio of the image before download. It can still be responsive.

Add layout="response" to the amp-img tag to get basic responsive behavior (sizing to container). More details here https://www.ampproject.org/docs/guides/responsive/control_layout.html

cramforce avatar Dec 25 '15 22:12 cramforce

@cramforce, I tried with both layout="response" and layout="fixed". Both were not giving the expected result and finally I had to get rid of amp-img.

gkteck avatar Dec 27 '15 06:12 gkteck

Do you want to paste your HTML somewhere? Maybe jsbin.com? So we can debug it for you. On Dec 26, 2015 10:25 PM, "gkteck" [email protected] wrote:

@cramforce https://github.com/cramforce, I tried with both layout="response" and layout="fixed". Both are not giving the expected result and finally I had to get rid of amp-img.

— Reply to this email directly or view it on GitHub https://github.com/ampproject/amphtml/issues/1248#issuecomment-167388699 .

cramforce avatar Dec 27 '15 14:12 cramforce

I have a few samples of layout=responsive here: http://jsbin.com/dekavu/edit?html,output

dvoytenko avatar Dec 28 '15 17:12 dvoytenko

Hi Here is the jsbin link http://jsbin.com/puluvubosu/1/edit?html,output Please check the image just below the "About us" <amp-img layout="fixed" width="66" height="107"

gkteck avatar Jan 02 '16 07:01 gkteck

@gkteck thanks for your example. Two things here:

First, layout=responsive would work fine here, but it'd require you do ensure that all floats you do have a non-zero height, e.g. "width", or "min-width" CSS specified. For instance, you have ".logo" defined as:

.logo {
    margin-top: 12px;
    float: left;
}

And redefined for @media (max-width: 768px) as width: 325px; float: inherit !important;. This is the reason why up to 768px screen width the image works correctly and after 768px it disappears. This is due to float:left and no width. With this style, the div.logo reports to the image that it has 0px width available to layout - this is just how float works. I believe in this particular case, the issue is easily fixed by just adding width:100% to the .logo class. This will communicate the correct available width to the content under this div.

Second. amp-img is not the only way you can display a logo. Other options include a DIV with a background image and SVG - both allowed in AMP and in this case could be more applicable then amp-img. This does not change layout issues described above, however, you'd still need address float:left sizing in either case. I'm afraid floats are just rather capricious. Perhaps why they are used less and less nowadays.

dvoytenko avatar Jan 04 '16 17:01 dvoytenko