8
I’m having some difficulties with the precedence of CSS selectors to create a dynamic background (image transition.) in the first/div section presented on the site. I currently have the following scenario:
HTML:
<section id="intro" class="intro main style1 dark fullscreen">
<div class="content container small">
<header>
<h2 class="shade"></h2>
</header>
<p class="shade">[TEXTO]</p>
<footer>
<a href="#one" class="button style2 down">Mais</a>
</footer>
</div>
</section>
...
<section id="intro4" class="intro main style1 dark fullscreen">
<div class="content container small">
<header>
<h2 class="shade"></h2>
</header>
<p class="shade">[TEXTO]</p>
<footer>
<a href="#one" class="button style2 down">Mais</a>
</footer>
</div>
</section>
CSS: (the "ideal" would be, where I have specific properties by ID and generic properties that apply to all, by class.)
#intro {
background: url('images/overlay.png'), url('../images/intro.jpg');
}
#intro2 {
background: url('images/overlay.png'), url('../images/intro2.jpg');
}
#intro3 {
background: url('images/overlay.png'), url('../images/intro3.jpg');
}
#intro4 {
background: url('images/overlay.png'), url('../images/intro4.jpg');
}
.intro {
background-size: 256px 256px, cover;
background-attachment: fixed, fixed;
background-position: top left, bottom center;
background-repeat: repeat, no-repeat;
}
To avoid repetition and do something like:
#intro {
background: url('images/overlay.png'), url('../images/intro.jpg');
background-size: 256px 256px, cover;
background-attachment: fixed, fixed;
background-position: top left, bottom center;
background-repeat: repeat, no-repeat;
}
...
#intro4 {
background: url('images/overlay.png'), url('../images/intro4.jpg');
background-size: 256px 256px, cover;
background-attachment: fixed, fixed;
background-position: top left, bottom center;
background-repeat: repeat, no-repeat;
}
I thought about resorting to property !important
, which would cause the class selector (which would normally be overridden by background-size: initial
and background-attachment: initial
, since ids have precedence over classes. ) did not have to be repeated, approaching a concept DRY (Don’t Repeat Yourself) - Suggestions on how to improve leave "more" DRY, are welcome.
I’ve done it in properties like font-size: 200% !important
and clearly worked OK. However, in selectors with more than one value, as the case of background-size
and background-attachment
, the property inspector (in my case, Chrome Dev Tools.) says the property below would be invalid:
background-attachment: fixed !important, fixed !important;
While the next:
background-attachment: fixed, fixed !important;
Does not bring the expected result.
Could someone tell me if:
(1) It is possible to add !important
on a property shorthand multi-valued
(2) If possible, how to do so, following best practices and
(3) If there is another way to achieve the same result, preserving the current HTML structure (because I need some generic models only in those <sections>
/<divs>
, although each one has its own background with image. ), what would be and how to implement.
Are you generating that code on the server, or is HTML/CSS already done that you have to use? So you could make some improvements to be more DRY...
– Sergio
HTML/CSS manual. I’m a fan of SASS/SCSS but in this case I’m not compiling anything, not Node.js or anything like that. I seek to be DRY in everything, to avoid that macaroni of switches and unnecessary tags that keep polluting the code. And it’s precisely because I don’t use SASS on this project that I’m having this difficulty - if I were, I’d just do a
@extend
, that would have the same result in CSS that I don’t want, but it would be easier to maintain and update.– nmindz