Absolutely!
Furthermore, we can enhance the layout by utilizing vw
and calc
.
To achieve this, simply adjust the width of the child elements to span 100% of the viewport width using vw
(viewport units), and then offset their positioning with a negative calculated value based on this, minus the width of the container. Apart from potentially setting a maximum width for the parent element, all other adjustments are automatically computed. This setup allows for dynamic resizing of the parent container, causing the children to adapt in size and alignment accordingly without requiring manual repositioning.
body,
html,
.parent {
height: 100%;
width: 100%;
text-align: center;
padding: 0;
margin: 0;
}
.parent {
width: 50%;
max-width: 800px;
background: grey;
margin: 0 auto;
position: relative;
}
.child {
width: 100vw; /* Children sized to match browser window */
margin-left: calc(-1 * ((100vw - 100%) / 2)); /* Align left edge with viewport edge */
height: 50px;
background: yellow;
}
<div class='parent'>
Parent Element
<div class='child'>Child Element</div>
</div>