Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a completely "layout-neutral" HTML container element?

Tags:

html

css

Sometimes I want to put a wrapper element around several other HTML elements with the sole purpose of setting up a convenient CSS selector to refer to all the contained elements:

<TAG id="just-a-handy-wrapper">
    <abc ...>
        ...
    </abc>
    ...
    <pqr ...>
        ...
    </pqr>
</TAG>

...and in the CSS:

#just-a-handy wrapper * {
    ...
}

I find this easier to manage and maintain than the alternative of assigning a common class to all the items captured by the #just-a-handy wrapper * selector above.

In this example, I've used fictitious tags <abc>, ..., <pqr>, etc., for the contained elements to stress the fact that I'm looking for a solution that works irrespective of the nature of the specific tags among the contents.

I've also used the fictitious tag TAG as a placeholder for the desired "wrapper tag", because my question is precisely about the best HTML tag to use for this purpose. By "best" I mean most "universal" in the types of elements it can contain in valid HTML5, and "most layout-neutral".

IOW, the ideal HTML tag would the one where the page including the code above would always be rendered exactly the same as one where the <tag ...> and </tag> lines were removed, or commented out:

<!-- <tag id="just-a-handy-wrapper"> -->
    <div ...>
        ...
    </div>
    ...
    <div ...>
        ...
    </div>
<!-- </tag> -->

A div, for example, is not "layout-neutral" (the browser will generally have strong ideas about how to layout a div), therefore it would not do to set tag equal to div. Here's a simple example of this:

like image 404
kjo Avatar asked Sep 12 '15 20:09

kjo


People also ask

How many types of containers are there in HTML?

There are 3 common container types : block containers. inline containers. inline-block containers.

What is a generic container for HTML elements to which we can apply styles?

The <div> HTML element is the generic container for flow content. It has no effect on the content or layout until styled in some way using CSS (e.g. styling is directly applied to it, or some kind of layout model like Flexbox is applied to its parent element).

Which element is often used as a container in HTML?

The <div> element is often used as a container for other HTML elements. The <div> element has no required attributes, but style , class and id are common.


1 Answers

Yes, there is a CSS for that supported by major browsers

display: contents

E.g. <section class="container"><div>Parent is virtually not rendered</div></section>

.container {
   display: contents;
}
like image 124
elixon Avatar answered Sep 27 '22 19:09

elixon