Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CSS Attribute Selectors to target the src of background-image

Tags:

css

I am using using the background-image attribute to assign images to a range of div on my site. However, with background-image attributes, I also need to assign background-size to get it looking right.

This works fine mostly, but I need to change the background-size attribute based on the file type used in the background-image attribute. For example, as standard I want to user background-size: cover; but when the background-image is an SVG I want to use background-size: auto;

Is this possible using CSS attribute selectors? If not, any other solution?

My attempt (SCSS):

&.bg-image {
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    min-height: 500px;
    max-height: 700px;
    &[src$=".svg"] { background-size: auto; }
}
like image 207
dungey_140 Avatar asked Oct 06 '16 10:10

dungey_140


People also ask

How attribute selector is used in CSS?

CSS [attribute|="value"] Selector The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-).

How do you target all images whose extension is a JPEG CSS?

To do so, we can combine the attribute selector with the adjacent sibling selector, +. The snippet above will target every image with the source attribute ending with . jpg , then the adjacent selector will find the element next to it. In this case the figcaption will be added with the #a8b700 background color.

What is the CSS selector for an A tag contains the title attribute?

CSS [attribute~=value] Selector The [attribute~=value] selector is used to select elements with an attribute value containing a specified word.

Which of the following CSS property is used to set the background image of an element?

The background-image CSS property sets one or more background images on an element.


1 Answers

If background-image is your only inline CSS property, you can do this:

.bg-image {
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    min-height: 300px;
    max-height: 700px;
}

.bg-image[style^="background-image:"][style$=".svg)"] {
  background-size: 103px 94px;
}
<div class="bg-image" style="background-image: url(https://upload.wikimedia.org/wikipedia/en/8/80/Wikipedia-logo-v2.svg)"></div>

If background-imageis not the only property but it is the last one, you can use this selector: [style$=".svg)"].

Finally, the most general case, for any location of background-image in the style attribute use this selector: [style*=".svg)"].

Even with the loosest selector: [style*=".svg)"] (or jpg, or png...) the only declaration the selector can possibly apply is the background-image.

The other approach is to add data-type=svg or what have you to the divs and then target them in CSS [data-type=svg].

Or you could use img instead, as in your example.

like image 129
Tigran Avatar answered Oct 04 '22 04:10

Tigran