简体   繁体   中英

css - divs all the same except single property

Is there any kind of inheritance in css?

For example, divA, divB, divC all existing in the same spot with all the same properties, only differing by their z-index?

The idea being using jquery or whatnot to have different transitions between the sections.

Is anything like this possible or am I going about this the wrong way?

Is there any kind of inheritance in css?

Yes. Most properties can be given the value inherit which means "The same value for this property as the parent node has". This isn't the type of inheritance you are thinking of though.

For example, divA, divB, divC all existing in the same spot with all the same properties, only differing by their z-index?

CSS has no way to say that one rule-set should copy values from another one. You can do:

#divA, #divB, #divC  /* Or another selector that matches all the elements */
    { /* Common rules */ }
#divA { /* specific rules */ }
/* etc */

or various other strategies including generating your CSS using a script and using multiple classes on a single element.

You can specify multiple classes for a single element, eg

<div class="divA divB">

So with that you can do some clever trickery to get what you want.

CSS does have inheritance, that is one of the main principles of CSS.

jQuery can handle pretty much any scenario you want. If you provide a more detailed code writeup we can help more.

HTML:

<div id="container">
    <div id="divA">A</div>
    <div id="divB">B</div>
    <div id="divC">C</div>
</div>

CSS

#container div
{
    /*all your common styles here */
}
#divA { /*div specific style here */ }
#divB { /*div specific style here */ }
#divC { /*div specific style here */ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM