📜  padding top ratio (1)

📅  最后修改于: 2023-12-03 15:03:27.686000             🧑  作者: Mango

Padding Top Ratio

padding-top-ratio is a CSS property that allows you to specify how much padding an element should have at the top, relative to its width. This can be useful in situations where you want to maintain a consistent aspect ratio for an element, even as its size changes.

Syntax

The syntax for padding-top-ratio is as follows:

selector {
  padding-top-ratio: <ratio>;
}

The <ratio> value should be a number, and represents the aspect ratio you want to maintain. For example, if you want an element to have a 16:9 aspect ratio, you would set padding-top-ratio to 56.25% (since 9/16 = 0.5625).

Example

Here's an example of how you might use padding-top-ratio to create a responsive video player:

<div class="video-wrapper">
  <iframe src="https://www.youtube.com/embed/0ifCzcji4g4" frameborder="0" allowfullscreen></iframe>
</div>
.video-wrapper {
  position: relative;
  padding-top-ratio: 56.25%;
}

.video-wrapper iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

In this example, the .video-wrapper element has a padding-top-ratio of 56.25%, which maintains a 16:9 aspect ratio. The iframe within the .video-wrapper is absolutely positioned, with a width and height of 100%, so it fills the parent element (which is now sized according to the padding-top-ratio).

Browser Support

padding-top-ratio is not currently supported by any major browsers, and is not included in any CSS specifications. However, some libraries and frameworks (such as Bootstrap) include similar functionality using different names (such as embed-responsive). If you need to use padding-top-ratio in your project, you may need to use a preprocess such as PostCSS with a plugin like postcss-aspect-ratio-mini.