When I add such CSS into <style>
tags:
* { display:block; }
It is never interpreted correctly. Instead, what do I see? Somehow everything inside <style>
becomes the part of html body. E.g.:
* { display:block; }
<p>paragraph</p> <phrase>phrase</phrase> <pet>pet</pet>
This happens anywhere. For the first time, I thought this is the problem with StackSnippets. (i.e. the live demo for Stack Overflow, the one I've provided above), but then I checked with code pen. Then with jsfiddle. Then I've gone ahead and made a file on my server, giving it all contents I inserted in the snippet above.
The outcome is always the same. The CSS gets included in the html, though it is applied. (the only fix is to create a stylesheet and include it using <link>
)
The most interesting thing, is, that it seem to happen only with display:*
. E.g., the following works:
* { color:green; background:red; border:2px solid orange; border-radius:5px; }
<p>paragraph</p> <phrase>phrase</phrase> <pet>pet</pet>
But once I put in the styles of the last snippet display:*
, the styles are, again, magically included in HTML.
* { color:green; background:red; border:2px solid orange; border-radius:5px; display:inline-block; }
<p>paragraph</p> <phrase>phrase</phrase> <pet>pet</pet>
Why does it happen?
A block element fills the entire line, and nothing can be displayed on its left or right side. The display property also allows the author to show or hide an element. It is similar to the visibility property.
Using CSS. CSS can be added to HTML documents in 3 ways: Inline - by using the style attribute inside HTML elements. Internal - by using a <style> element in the <head> section.
The display property specifies the display behavior (the type of rendering box) of an element. In HTML, the default display property value is taken from the HTML specifications or from the browser/user default style sheet. The default value in XML is inline, including SVG elements.
The JavaScript style display property is meant for setting and returning the display type of a specified element. Most HTML elements have the inline or block display types. The content that inline elements feature floats on their left and right sides.
It's styling the <head>
element and everything in it, including the very <style>
element your CSS resides in, because the CSS appears as character data within the <style>
element. A <link>
element on the other hand doesn't have any content — it points to a separate resource altogether, so there is nothing inside the element itself to be displayed.
Most browsers implement <head>
as display: none
(and some propagate that value to every descendant), which you are able to override by targeting them with a display
style. The rest of the properties are still applied to <head>
and its descendants regardless of whether you do this, but without it, they simply won't show up in your page so you don't really see it happening. That's really all there is to it — there isn't anything else that's special about <head>
or its related elements.
In other words, as far as CSS is concerned, the following (yes, a <style>
element with a style
attribute...):
<style style="display: block; font-family: monospace"> p { color: red; } </style>
Is no different from this:
<code style="display: block; font-family: monospace"> p { color: red; } </code>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With