Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize width-to-height ratio layout with CSS

Shulou Source: shulou.com Published: 2022-05-31 13:11:34 10月01日 Update

This article mainly introduces CSS how to achieve width-to-height ratio layout related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this CSS how to achieve width-to-height ratio layout article, let's take a look.

Width is twice the height (proportional scaling)

Implementation idea: based on the parent element, the child width:100%; (that is, 100% of the parent width) and padding-top:50% (that is, 50% of the parent width. According to the css specification, if padding is expressed as a percentage, padding: 100% is equal to the width of the parent element)

Why not directly `width: 50%; height: 50% investors?

Then the height becomes 50% of the parent's height (undesirable, unless the parent's width and height are equal).

Original title:

The red block element is vertically centered in the center of the screen.

The red block-level elements are 20px away from the left and right edges of the screen.

The content in the red block level element is centered horizontally and vertically.

The height of the red block-level element is always 50% of the width of the red block-level element.

The aspect ratio of the red part is 2:1

Source code:

The aspect ratio is fixed. Overflow: hidden; position: absolute; left: 10px; right: 10px; top: 50%; transform: translateY (- 50%); background-color: # 0c8ac5;} .son {width: 100%; padding-top: 50% Background-color: # c03035;} span {position: absolute; top: 50%; left: 50%; transform: translate (- 50%,-50%); font-size: 20px; color: # D8D8D8;} img {float: left Margin-right: 10px; border: 2px solid # fdf6e3;}

Wang Ye, a disciple of Wudang School (who has now left Wudang), the heir to Feng Hou Qimen, one of the eight wonders, realized the way of Heaven by divination, but made the choice to join the troubled times. The appearance of a warm and good appearance, modest and courteous as warm water, things are often listless, do not seem to wake up. Leisurely and soft-tempered, it often means making concessions with an apologetic smile. He doesn't seem to care about anything, he looks like a leisurely Taoist, but he is unequivocal when it comes to the safety of his family, and has even made an uncool move about it. The strength of a monk who accidentally made an apology by paying homage to his ancestors was even the best among the younger generations. for the time being, there was only one completely unexpected capsizing in the sewer. This is the end of the article on "how to achieve width-to-height ratio layout in CSS". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve breadth and higher level than layout of CSS". If you still want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Elements red width layout height content knowledge behavior that is screen article Wudang lukewarm vague leisurely leisurely listless for this troubled times monks Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL NVidia Microsoft Huawei