tl;dr is it a bad practice to change default display property in my CSS?
Its a good practice but choose carefully when to use it because it can cause some critical structure mistakes.
Why is it a good practice
The display
property is open for changes. It makes HTML simple and generic. HTML elements come with a default display
value that match the general behavior - what you would usually want. But they dont have to be kept and manipulated around to imitate another display
property. Think about <div>
for example. Obviously most of the times you want it to have display: block;
, but display: flex;
is much more suitable once in a while.
Lets look at a really common example of lists. <li>
comes with the display property of list-item
that breaks the lines for every new item.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
But horizontal lists are very common too. So why there is no special element for horizontal list items? Writing a special element for every common display
behavior adds complexity. Instead, the convention, as also suggested by W3C is to set the <li>
display property to inline
.
ul li {
display:inline;
}
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
display: inline-block;
as an alternative to float
float
has been used massively in page layout for many years. The problem is that it wasnt created for this task and was originally designed to wrap text around elements. A well-known float
issue is that non floated elements dont recognize floated children because they are being removed from the normal flow of the document. You also cannot centrally float an element. you are limited to left or right floats only.
display
is much more suitable for layout many times. display: inline-block;
tells browsers to place that element inline, but to treat it as though it were a block level element. This means that we can use inline-block
instead of floats to have a series of elements side by side. It is more intuitive and eliminates floats <div class="clearfix"></div>
which is an additional non semantic element in your HTML.
Floats are useful when there is a need to float an element so that other page content flows around it. But there is no need to always press them into the service of a complicated layout.
Things to avoid when changing display
When you change the display
property remember:
Setting the display property of an element only changes how the element is displayed, NOT what kind of element it is.
<span>
test case:
In HTML early versions <span>
is considered an inline-level element and <div>
is block-level. Inline-level elements cannot have block-level elements inside them. Giving the <span>
a display:block;
doesn't change his category. It is still an inline-level element, and still cannot have <div>
inside.
HTML5 introduced content models. Each HTML element has a content model: a description of the element's expected contents. An HTML element must have contents that match the requirements described in the element's content model. <span>
can contain only phrasing content. It means that still you cannot nest a <div>
(flow content) inside a <span>
. Giving <span>
a display:block;
still doesn't change it.
Avoid:
span {
display:block;
}
<span>
<div>
Still Illegal!
</div>
<span>
In conclusion, changing the default display
property is certainly our bread and butter. Remember that it only changes how the element is displayed, NOT what kind of element it is and use it correctly.
Now about the original two heading issue:
With respect to the comments:
Let's assume for the sake of the question, that we need to have two
headings. Or let's forget about the headings for the time being. - by the author
And also to the comment:
This question is not about resetting the display value globally. Using
selectors to target only the specific elements is implied. The
question is what we should do with these elements once selected. - by the person who set the bounty
Two headings side by side not only to handle mobile layout changes, can be done in many ways. The original example is simple and correct so its actually a good way.
h1, h2 {
display: inline;
}
<div class="container">
<h1>Header:</h1>
<h2>my header</h2>
</div>
It follows HTML rules and doesnt require any additional hacks.