How to Reference One CSS Rule Within Another

Is it possible to reference one CSS rule within another?

No, you cannot reference one rule-set from another.

You can, however, reuse selectors on multiple rule-sets within a stylesheet and use multiple selectors on a single rule-set (by separating them with a comma).

.opacity, .someDiv {
filter:alpha(opacity=60);
-moz-opacity:0.6;
-khtml-opacity: 0.6;
opacity: 0.6;
}
.radius, .someDiv {
border-top-left-radius: 15px;
border-top-right-radius: 5px;
-moz-border-radius-topleft: 10px;
-moz-border-radius-topright: 10px;
}

You can also apply multiple classes to a single HTML element (the class attribute takes a space separated list).

<div class="opacity radius">

Either of those approaches should solve your problem.

It would probably help if you used class names that described why an element should be styled instead of how it should be styled. Leave the how in the stylesheet.

Calling a CSS class inside another class?

You can't actually do a reference (one of CSS's major failings), but you can do this:

.btn:active, .red {
/* Block A: Most (or all) of what used to just be in .red below */
}

.btn:active {
/* Block B: Stuff *just* for .btn:active, if any */
}

.red {
/* Block C: Stuff *just* for .red, if any */
}

The comma means that the definitions in the body of Block A apply separately to each of those selectors, and so they apply to any ".btn" elements that are ":active", and separately apply to any ".red" elements.

Block B and Block C are optional. They're for any definitions you only want to apply to the given selector. You usually list these after Block A because rules of equal specificity are applied top-to-bottom, so you can override anything from Block A that you want to in Block B or Block C, and those blocks will "win".

What is the proper way to apply a css rule to an element under another one?

Your CSS rule h3 p {background-color:red} is implying that you want to make the background of <p> tag within an <h3> tag red. So that would be:

<h3>h3 tag
<p>paragraph</p>
</h3>

This is called nesting. But you cannot nest a paragraph within a heading, this is invalid. However you could nest the paragraph inside a div like this:

<div>This is a div
<p>paragraph</p>
</div>

And your CSS div p {background-color:red} would work.

Target a css class inside another css class

Not certain what the HTML looks like (that would help with answers). If it's

<div class="testimonials content">stuff</div>

then simply remove the space in your css. A la...

.testimonials.content { css here }

UPDATE:

Okay, after seeing HTML see if this works...

.testimonials .wrapper .content { css here }

or just

.testimonials .wrapper { css here }

or

.desc-container .wrapper { css here }

all 3 should work.

How to reference another style class for complex selectors with material-ui

MUI uses CSS-in-JS

Use $ruleName to reference a local rule name within the same makeStyles style sheet.

const useStyles = makeStyles({
Header: {
fontWeight: "bold",
display: "flex"
},
CloseButton: {
padding: 0,
borderWidth: 0,

"&:before": {
content: "'x'"
},

"$Header > &": { // <-- pay attention to usage of $
marginLeft: "auto"
}
}
});

Below is what your CSS would look like:

Sample Image



Related Topics



Leave a reply



Submit