Can we use images in CSS background in Google AMP?

10,167

Solution 1

You can do like this :

CSS on head section

<style amp-custom>
    .img-background { background-image: url(assets/img/business1.jpg); }
</style>

HTML

<pre>
<div class="img-background"></div>
</pre>

Solution 2

AMP does not support inline styles (like your example), but you could add a background-image by implementing styles the proper way in a <style amp-custom> tag in the <head>.

Read more here: Supported CSS - AMP

Share:
10,167

Related videos on Youtube

Rakib Uzzaman
Author by

Rakib Uzzaman

Updated on June 04, 2022

Comments

  • Rakib Uzzaman
    Rakib Uzzaman almost 2 years

    Can we use images in CSS background in Google AMP?

    <pre>
    
        <div style="background-image: url(assets/img/business1.jpg);></div>
    
    
    </pre>
    
  • Tamil Selvan C
    Tamil Selvan C over 6 years
    How to display background image for dynamic loop content. eg display posts
  • Bachcha Singh
    Bachcha Singh over 6 years
    Yes, you can achieve. In loop you have to create unique class and css in head section for same class
  • Jim S
    Jim S over 5 years
    I am trying to use this method and I want the image to fill the width of the screen and then have the height be calculated so the aspect ratio is 16 x 9, as can be easily done with an amp-image. How do I get the div the right size? Should I use an amp-img, and then position my text on top using 'position:absolute'?