Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When targeting a Picture element in CSS, should we use img or picture selector?

Tags:

html

css

image

So the new Picture element looks like this:

<picture>
  <source ... />
  <img browsers will fall back to this width="10" height="10" />
</picture>

In our CSS, we want to set say a background color.

picture {background-color: red};
img {background-color: yellow};

Will a Picture enabled browser just show a red background, while non enabled browsers show a yellow background? Or a combination of the two. Likewise, will an Picture enabled browser see the height/width attributes on the img element, or is the img element ignored completly?

like image 935
simbolo Avatar asked Aug 20 '14 04:08

simbolo


People also ask

What is better IMG or background image?

Img Tag. It is widely known that using the image tag is better SEO practice than using background images because of the screen reader implications.

What is the difference between the picture and IMG tags?

The <img> element is used to provide backward compatibility for browsers that do not support the element, or if none of the source tags matched. The <picture> tag is similar to <video> and <audio>. We add different sources, and the first source that fits the preferences is the one that will be used.

What element do you use for an image?

If you want to change the image completely, you'll need the picture element. In the same way that srcset builds upon the src attribute, the picture element builds upon the img element. The picture element wraps around an img element. <img src="image.

Should you use the picture element?

The way to know when to use the <picture> element is based on the use case you're trying to solve. If you're solving for the art direction use case, use the <picture> element. Anything else, you should stick to srcset and sizes .


1 Answers

The idea of the picture element is that that it simply provides source information for its enclosed img element, and that it is always the img element that is rendered, not the picture element.

However, I can't see anything normative in the spec that suggests that the picture element will be treated by default as anything other than an inline element, so I expect that you will be able to style it with a different display setting, give it padding etc., in the same way as you can do with span elements, in which case, the background-color will behave in the same way as a span element around an img element does today.

So targeting both might be appropriate. The backgrounds will simply layer as normal. But the img will be rendered, so in your scenario, the background behind the image will be yellow, assuming of course that the img has at least some degree of transparency.

like image 113
Alohci Avatar answered Oct 19 '22 23:10

Alohci