Home >Web Front-end >CSS Tutorial >How to Create a Responsive Square-Shaped Div with CSS?

How to Create a Responsive Square-Shaped Div with CSS?

Barbara Streisand
Barbara StreisandOriginal
2024-11-11 09:59:02957browse

How to Create a Responsive Square-Shaped Div with CSS?

How to Create a Responsive Square-Shaped Div with CSS?

Previously asked, this question seeks a method to alter a div's height dynamically to match its width. The width is expressed in percentages, so as the parent element's width diminishes, the div should scale down proportionally.

To address this, we can employ CSS's padding-bottom property. By setting padding-bottom as a percentage, we can create a ratio between the div's height and width, ensuring they remain equal.

Here's an example of the code:

<div>

In this code, the outer div establishes a square shape using width: 20% and height: 0. The padding-bottom of 20% sets the ratio between the height and width, resulting in a 1:1 aspect ratio. The inner div contains the actual content.

Additionally, a JavaScript Fiddle demonstration is provided to showcase the functionality: [link to Fiddle]

The above is the detailed content of How to Create a Responsive Square-Shaped Div with CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn