How to force a responsive element to retain its aspect ratio [duplicate]
This question already has an answer here:
- Maintain the aspect ratio of a div with CSS 13 answers
I have a responsive
div that contains a large amount of text and is the same width as its container.
<div>This is a long string of text, but the real text will be a lot longer.</div>
How can I force the
div to retain a height which is 50% of its width and stop the contents from overflowing the boundaries using only HTML and CSS?
It should look like this
What is the CSS padding trick?
First some information about containing blocks for reference.
9.1.2 Containing blocks
In general, generated boxes act as containing blocks for descendant boxes; we say that a box "establishes" the containing block for its descendants. The phrase "a box's containing block" means "the containing block in which the box lives," not the one it generates.
Now, here's the trick.
* Percentages: refer to width of containing block
It is intuitive that horizontal padding percentages would reference the width of the containing block. However, vertical padding percentages also refer to the width, not the height.
What does that mean for maintaining aspect ratio?
What this means is that if you have an element which is the same width as its parent, and you set
padding-bottom: 50%, the element will be half as wide as it is tall.
<div style="padding-bottom: 50%;"></div>
However, if the element is less than the full width of its parent, because the padding property refers to the parents width the numbers will change.
For example, if the element is half the width of its parent and you set
padding-bottom: 50%, it will be as tall as it is wide.
<div style="width: 50%; padding-bottom: 50%;"></div>
This is because percentages for the padding property are relative to the containing block, not the element.
To make an element which is half its parent's width retain an aspect ratio of 2:1 (half as tall as it is wide), cut the percentage in half.
<div style="width: 50%; padding-bottom: 25%;"></div>
How can I use this in the real world?
If the element in question contains any content which takes up space in the document flow, the height will change. To avoid this you can take advantage of another CSS trick.
10.1 Definition of "containing block"
4. If the element has 'position: absolute', the containing block is established by the nearest ancestor with a 'position' of 'absolute', 'relative' or 'fixed', in the following way:
In the case that the ancestor is an inline element, the containing block is the bounding box around the padding boxes of the first and the last inline boxes generated for that element. In CSS 2.1, if the inline element is split across multiple lines, the containing block is undefined.
Otherwise, the containing block is formed by the padding edge of the ancestor.
When an element is set to
position: absolute it will be positioned in reference to its closest ancestor with
position: relative or
position: fixed. If the element has no such ancestors, it will positioned in reference to the root element.
This means, if you set
position: relative on this new responsive
div, you can insert a child with
position: absolute and tell it to be the same size as the parent using
top: 0; width: 100%; bottom: 0;.
To make sure the contents won't overflow the boundaries set
<div style="position: absolute; top: 0; width: 100%; bottom: 0; overflow: auto;"></div>
This will create an element which retains its aspect ratio and can contain content without changing the aspect ratio.
<div style="padding-bottom: 50%; position: relative;"> <div style="position: absolute; top: 0; width: 100%; bottom: 0; overflow: auto;"> Content... </div> </div>