Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make child width equal to parent height with CSS only

Tags:

html

css

enter image description here

As described by the image there are two elements: A parent (dark gray) and child (not so dark gray). The width and height of the parent is fluid. The ratio of the child i 1:1 or y:y where y is equal to the height of the parent.

I've tried to find ways to solve this using flex, calc, padding etc but have reached the end of the road. Any ideas how to solve this with pure CSS are much appreciated.

EDIT: I realize now I should have added more details regarding the usage of this scenario. As well as what I consider to be a dynamic height. Dynamic height for me suggests that the height is decided by the amount of content it contains. So I added some HTML to clarify. The .content div may be unnecessary if you can put the content directly in the .container div. But that depends on how you write the CSS:

<div class="container">
  <div class="content">
    Here is some text. It can be long and it can be short. 
    It will affect the height of the .container thus also 
    the height and width of the .square.
  </div>
  <div class="square">1:1</div>
</div>
like image 793
Trevald Avatar asked Nov 30 '16 13:11

Trevald


1 Answers

I think it is not possible to do what you try!You can't get parents height without JS. But maybe there is another solution. Does your parent container also has a fixed proportion?

like image 194
Martin P. Avatar answered Nov 14 '22 23:11

Martin P.