It doesn’t actually eliminate the container’s CSS, it eliminates the container itself
layout, leaving only child elements.
The function of the property is to visually change one or more elements while maintaining the semantics of HTML, usually when the parent element’s markup (tag) does not matter in that context, causing the child elements to become the direct children of the grandfather (only visually):
<div> ← Avô
<div> ← Pai
<div></div> ← Neto
</div>
</div>
When adding property to div-father, o a div-neto becomes visually a "div-father" (direct daughter of div-grandfather), but I repeat, only visually in the layout. In the DOM, the div-father remains the div-father. It would be as if you had commented the tag (WOULD, but the tag remains active in the DOM):
<div> ← Avô
<!-- <div style="display: contents"> ← Pai -->
<div></div> ← Neto
<!-- </div> -->
</div>
The indication of use will depend a lot on the layout you want to build and the semantics you want to keep.
A hypothetical example would be: I want to build a horizontal menu where the first two links are two <li>
, but, for semantic reasons, these li
's must be within a tag <ul>
. The display: contents
makes it possible to "eliminate" the tag ul
(only visually) causing the li
's behave as part of the menu grid as well as other links <a>
, but the structure of the list remains intact in the DOM:
.content{
display: flex;
background-color: black;
}
.content > *, .content ul li, .content ul li a{
display: flex;
justify-content: center;
align-items: center;
flex: 1;
}
.content > *{
height: 50px;
background-color: lightgreen;
margin: 2px;
}
.content ul li{
background-color: yellow;
margin: 2px;
}
.content ul li a{
width: 100%;
height: 100%;
}
.content ul{ display: contents; }
<div class="content">
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
</ul>
<a href="#">Link 3</a>
<a href="#">Link 4</a>
</div>
Otherwise this would not be possible, that is, it would not be possible to use a list <ul>
for this purpose. See how the menu would look without the display: contents
in ul
:
See that the li
's cannot integrate into the menu grid because they are children of the element ul
, who in turn is the child of the element .content
. The display: contents
makes, only visually on the screen, the li
'the direct daughters of .content
, making them part of the menu grid along with the other two elements <a>
.
A reference article that addresses the subject in a very objective way (in English):
I found this article, quite interesting by the way, which may serve as some argumentative basis for the questions you asked > https://medium.com/@oieduardorabelo/css-como-display-Contents-funciona-1290a705df98
– Andrei Coelho
That’s right @Andreicoelho the subject is a bit complex. So I decided to leave the question there on the air....
– hugocsl
display: contents
makes the element a container similar to an extremely thin, flexible and invisible "film". Ideal for containers that have no related stylization and only serve to group other elements– Michael Pacheco