I know this is a basic question, but I haven't find the answer anywhere.
Are all CSS properties applicable to all html elements?
Or are there CSS properties that on apply to some html elements?
Thank you for your answers.
I know this is a basic question, but I haven't find the answer anywhere.
Are all CSS properties applicable to all html elements?
Or are there CSS properties that on apply to some html elements?
Thank you for your answers.
Not all properties apply to all elements. Some properties apply only to specific elements; what sort of restrictions apply is determined by three things:
The vast majority of these restrictions are determined by the CSS spec, with numerous examples (these are just three out of hundreds):
display: none
, since they don't generate any boxesheight
does not apply to elements with display: inline
In some cases, the document language such as HTML will have its own restrictions which may or may not override those given by CSS (usually, they do), and in yet other cases, browser or device limitations may prevent a property from applying to specific elements, or indeed any element. These are far less common, though; in fact, I can't think of any element that isn't a form element that restricts CSS in specific ways.
Note that this is not the same thing as every element having a value for every CSS property in the cascade. The cascade has no relation to whether or not a property applies to an element.
In addition to the answer of @BoltClock I would say that some properties are ignored in certain cases since they can be considered only if another property is set to a specific value.
Here is a simple example:
.box {
justify-content:center; /*Has no effect and it's ignored*/
height:100px;
border:1px solid;
}
.box-alt {
display:flex;
justify-content:center; /*is considered because of display:flex*/
height:100px;
border:1px solid;
}
<div class="box">
text
</div>
<div class="box-alt">
text.
</div>
So when you check the MDN doc you can see this:
And if I take the display
property which apply to all elements we can read this:
So yes you can apply all properties to all element BUT not all of them will be considered depending on your situation.