How do I make CSS styles dependant on its parent element

Peter Stuart

I was wondering if it is possible to define the styles of an element depending on the value of the body ID.

It is difficult to explain but something like this would be ideal:

HTML:

<body id="home">

CSS:

body#home {
  a { /* code here */ }
  p { /* code here */ }
}

body#profile {
  a { /* different code here */ }
  p { /* different code here */ }
}

I know I can do this:

body#home a { /* code here */ }

but that becomes very repetitive.

I will be looking forward to your responses,

Peter

Stuart Kershaw

Be aware that while nesting styles like this can be supported using CSS frameworks, it should be well thought-out to maintain modularity and clean inheritance in your CSS. You can end up doing more harm than good. In particular, watch out for something know as the inception rule, described here:

http://thesassway.com/beginner/the-inception-rule

The Inception Rule: don’t go more than four levels deep.

Any change you make to your markup will need to be reflected into your Sass and vice versa. It also means that the styles are bounded for life to the those elements and that HTML structure which completely defeats the purpose of the "Cascade" part of "Cascading Style Sheets."

If you follow this path, you might as well go back to writing your CSS inline in your HTML (please don't).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make a <p> element display on the hover of its parent <div> using CSS?

From Dev

How can I make a <p> element display on the hover of its parent <div> using CSS?

From Dev

How to make a div and its children to ignore its parent styles?

From Dev

How do i make a TabPane fill its parent?

From Dev

How do i make a TabPane fill its parent?

From Dev

How to make an element (<a>) stretch to the full width of its parent element (container)?

From Dev

How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

From Dev

How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

From Dev

How do I get the dom node of a child element in a component without adding logic to its parent/children?

From Dev

How do I get the dom node of a child element in a component without adding logic to its parent/children?

From Dev

how do i make the opacity of a child element 100% while the parent element has a different opacity?

From Dev

How do I make a dropdown submenu appear directly below its parent <li>?

From Dev

How do I make a FrameLayout fill the height of its parent so the whole page scrolls together?

From Dev

How do I make a GridView display its data source's parent class fields

From Dev

How do I make a div extend its width beyond the parent's width?

From Dev

How do I make a dropdown submenu appear directly below its parent <li>?

From Dev

How do I make a child element toggle when the parent is clicked using jQuery?

From Dev

How do I get the parent of an element?

From Dev

How do I refer to parent element?

From Dev

How can I find an element with XPath using its parent?

From Dev

How can I make a flexbox parent have the width of its children?

From Java

How can I make an svg scale with its parent container?

From Dev

How do I select an element by its style from css (not inline) using webdriver (python)

From Dev

How do I clear all styles from a child element?

From Dev

How are styles within div's made 'safer' (i.e. so they do not override parent styles)

From Dev

Make an image fits its parent element

From Dev

D3/SVG. How do I position a child svg element beyond its parent's extent?

From Dev

How do I install nginx when a dependant package is broken?

From Dev

How to Make Inline SVG Scale to Parent Element with CSS

Related Related

  1. 1

    How can I make a <p> element display on the hover of its parent <div> using CSS?

  2. 2

    How can I make a <p> element display on the hover of its parent <div> using CSS?

  3. 3

    How to make a div and its children to ignore its parent styles?

  4. 4

    How do i make a TabPane fill its parent?

  5. 5

    How do i make a TabPane fill its parent?

  6. 6

    How to make an element (<a>) stretch to the full width of its parent element (container)?

  7. 7

    How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

  8. 8

    How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

  9. 9

    How do I get the dom node of a child element in a component without adding logic to its parent/children?

  10. 10

    How do I get the dom node of a child element in a component without adding logic to its parent/children?

  11. 11

    how do i make the opacity of a child element 100% while the parent element has a different opacity?

  12. 12

    How do I make a dropdown submenu appear directly below its parent <li>?

  13. 13

    How do I make a FrameLayout fill the height of its parent so the whole page scrolls together?

  14. 14

    How do I make a GridView display its data source's parent class fields

  15. 15

    How do I make a div extend its width beyond the parent's width?

  16. 16

    How do I make a dropdown submenu appear directly below its parent <li>?

  17. 17

    How do I make a child element toggle when the parent is clicked using jQuery?

  18. 18

    How do I get the parent of an element?

  19. 19

    How do I refer to parent element?

  20. 20

    How can I find an element with XPath using its parent?

  21. 21

    How can I make a flexbox parent have the width of its children?

  22. 22

    How can I make an svg scale with its parent container?

  23. 23

    How do I select an element by its style from css (not inline) using webdriver (python)

  24. 24

    How do I clear all styles from a child element?

  25. 25

    How are styles within div's made 'safer' (i.e. so they do not override parent styles)

  26. 26

    Make an image fits its parent element

  27. 27

    D3/SVG. How do I position a child svg element beyond its parent's extent?

  28. 28

    How do I install nginx when a dependant package is broken?

  29. 29

    How to Make Inline SVG Scale to Parent Element with CSS

HotTag

Archive