Menu

gitpiper

Constant width to height ratio css Code Snippet in 2025

layoutbeginner

Last Updated: 25 April 2025

Ensures that an element with variable width will retain a proportionate height value.

  • Apply padding-top on the :before pseudo-element, making the height of the element equal to a percentage of its width.
  • The proportion of height to width can be altered as necessary. For example a padding-top of 100% will create a responsive square (1:1 ratio).
<div class="constant-width-to-height-ratio"></div>
.constant-width-to-height-ratio { background: #9C27B0; width: 50%; } .constant-width-to-height-ratio:before { content: ''; padding-top: 100%; float: left; } .constant-width-to-height-ratio:after { content: ''; display: block; clear: both; }

css snippet similar to Constant width to height ratio For You in April 2025

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2025 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️