Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a child div same height as parent and make its width equal its height?

Suppose I want to place a div inside another div, but the child div must have the same height as the parent div and its width must equal its height. However, I can’t use absolute values for the height/width because the container’s (parent’s) dimensions aren’t absolute values.

Essentially something that looks sort of like this:

|----------------------------------------------------------|
|           |                                              |
|    child  |                                              |
|     div   |                    parent div                |
|           |                                              |
|----------------------------------------------------------|

I don’t know how to make the child’s width equal its height. It’s simple to set height: 100% on the child, but I’ve got no clue as to how I can set its width relatively.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you can use modern CSS, you can use

height: 100%;
aspect-ratio: 1;

on your child to force it into being a square.

(aspect-ratio is supported by 80% of users and all major browsers)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading