JGallardo JGallardo - 1 year ago 59
Sass (Sass) Question

Sass - Target an ID restrict certain styles if it has matching class then add other styles set by class


I need to target a
with a specific ID that will show up once in every page. But each will have a unique class. Some of the styles for the section will not apply if that class is added dynamically. Trying to figure out how to best nest the styling using Sass.



Here is a demo on CodePen.
Because it has the class of
, the background should be grey not red.

enter image description here


<section id="featured-product" class="soldout">
<h1>This car is no longer avaiable</h1>
<img src="https://img.gta5-mods.com/q95/images/2013-lamborghini-veneno-hq-digitaldials/bb4811-venenomain.jpg">


#featured-product {
/* these styles always apply */
color: #f1f1f1;
font-family: 'Oswald', sans-serif;

/* These classes should not apply if the section has a class of .soldout */
background: #FC332E;
padding: 25px 30px 45px;

& .soldout {
/* These styles only apply when the element has matching ID and this class added in*/
background: #BDBEBA;
padding: 25px;
img { width: 400px; }

Answer Source

Remove the space between & and .soldout {

&.soldout {

The way you had it, was you were looking for .soldout WITHIN #featured-product because you had the space.

You weren't looking for #featured-product WITH the class of .soldout

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download