Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the different background image format (png and webp) of the same image in section tag

I have started using webp images in my site with <picture> tag. all set except this

<section class="sec-bg" style="background: url('images/bg.jpg');"> 

I don't know, how to set the different background image format (png and webp) of the same image. please give a solution for this inline CSS in the section tag.

for other images, I'm using below code

<picture>
  <source srcset="img/awesomeWebPImage.webp" type="image/webp">
  <source srcset="img/creakyOldJPEG.jpg" type="image/jpeg"> 
  <img src="img/creakyOldJPEG.jpg" alt="Alt Text!">
</picture> 
like image 639
manoj v Avatar asked Jun 27 '19 13:06

manoj v


People also ask

How do I put different background images on different pages in HTML?

Use the CSS line that you have already ( body {background-image:url(greentea. jpg);} ) on each page in it's <head><style></style></head> tags and just change the background image on every page. Be aware that it is advised to have a separate CSS file to avoid any inline styling.

How do I add a background image to a WebP in CSS?

If you already use WebP images for your HTML <img> tags and want to enable WebP for the CSS background as well, the proper way is to use multiple backgrounds in the CSS styling. In this case, all browsers will get WebP images, except for ones that do not support WebP. They will load the . png image.

How do I add a background image to a section tag?

To insert an image in HTML, use the image tag and include a source and alt attribute. Like any other HTML element, you'll add images to the body section of your HTML file. The HTML image element is an “empty element,” meaning it does not have a closing tag.

Which background property allows us to re use background images in different contexts?

CSS3 The background-size Property In CSS3 it is possible to specify the size of the background image, which allows us to re-use background images in different contexts.


2 Answers

I would suggest you to use JS to handle that issue. For example you can find all elements with background or background-image style on a page and after that just replace your webp image to jpeg version. So your script will look like:

document.deepCss= function(who, css){
  if(!who || !who.style) return '';
  var sty= css.replace(/\-([a-z])/g, function(a, b){
    return b.toUpperCase();
  });
  if(who.currentStyle){
    return who.style[sty] || who.currentStyle[sty] || '';
  }
  var dv= document.defaultView || window;
  return who.style[sty] ||
      dv.getComputedStyle(who,"").getPropertyValue(css) || '';
};
var elms = document.getElementsByTagName('*');
for (var i=0;i<elms.length;i++) {
  var url = document.deepCss(elms[i], 'background-image');
  if (url) {
    url=/url\(['"]?([^")]+)/.exec(url);

    if (url && url[1]) {
      elms[i].style.backgroundImage = "url("+url.replace('.webp', '.jpeg')+")"
    }
  }
}

It would be much easier for you to store the same image in two formats with the same name, for example test.jepg and test.webp, in this case you can just replace image extension by using the script which I provided above.

like image 143
Sergey Bondarenko Avatar answered Nov 15 '22 06:11

Sergey Bondarenko


If you don't want to use JavaScript to serve formats specific to the client consider using the <picture> tag with both sources and using CSS and position: absolute and z-index: -1 to push the image into the background.

To expand your example, here's how it would work:

HTML

<div class="sec-bg">
  <div class="sec-bg__image">
    <picture>
      <source srcset="img/awesomeWebPImage.webp" type="image/webp">
      <source srcset="img/creakyOldJPEG.jpg" type="image/jpeg"> 
      <img src="img/creakyOldJPEG.jpg" alt="Alt Text!">
    </picture> 
  </div>
 
  <div class="sec-bg__content">
    <p>Your section content goes here!</p>
  </div>
</div>

CSS

.sec-bg
{
  position: relative;
  z-index: 0;
}

.sec-bg__image
{
  position:absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-indez: -1;
}
like image 35
Maria LeRoux Avatar answered Nov 15 '22 07:11

Maria LeRoux