Break out of container

CSS

Sometimes, you may need an element to break out of its container and take up the full width of the viewport. This can be useful for creating full-width sections, images, or other elements that need to extend beyond their parent container.

Here is a simple CSS snippet to achieve this:

.full-width {
  width: 100vw;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}

Explanation

  • width: 100vw;: This sets the width of the element to 100% of the viewport width.
  • position: relative;: This allows the element to be positioned relative to its normal position.
  • left: 50%;: This moves the element 50% to the right of its container.
  • transform: translateX(-50%);: This shifts the element back to the left by 50% of its own width, effectively centering it in the viewport.

Usage

To use this class, simply add class="full-width" to the element you want to break out of its container:

<div class="full-width">
  <!-- Your content here -->
</div>

This will make the element take up the full width of the viewport, regardless of its parent container’s width.