Home >Web Front-end >CSS Tutorial >How Can I Remove a DIV Element While Keeping Its Content Using CSS?
Removing DIV Elements While Preserving Content
In certain scenarios, you may want to remove a DIV element but keep the nested elements outside the DIV for responsive arrangements. A common approach involves duplicating the HTML and hiding it based on viewport size, but a more efficient solution exists.
The Power of display:contents
Leverage the CSS property display:contents to achieve this flexibility. display:contents causes the children of an element to bypass the element itself and appear as direct children of the parent element.
Example Implementation
To remove the DIV while preserving its elements, apply display:contents to the DIV to be removed. For instance, consider the following HTML structure:
<div class="container"> <div class="one"> <p>Content 1</p> </div> <p>Content 2</p> </div>
To remove the "one" DIV and place its content outside the container, implement the following CSS:
.container { display: flex; } .one { display: contents; }
With this approach, the "Content 1" paragraph will appear outside the container, preserving the intended layout for different screen sizes.
Benefits of display:contents
The above is the detailed content of How Can I Remove a DIV Element While Keeping Its Content Using CSS?. For more information, please follow other related articles on the PHP Chinese website!