Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text squashed when using position: absolute;

Tags:

css

I have a text box that will expand across the screen when I do not use position: absolute;, however upon using it, the text is all in one column per word, and the box is very tiny.

What is causing this? Or what can cause this? I've been trying overflow settings, different sort of positions and z-indexes, etc.

like image 819
Steven Matthews Avatar asked Oct 22 '25 17:10

Steven Matthews


1 Answers

This is a good question because it highlights an important aspect of absolutely positioned elements.

If you don't specify the width of an absolutely positioned element, or if you don't specify the left and right offsets, then the width is computed to be a shrink-to-fit width similar to what is done for table cells.

The details are given in the CSS specification, Chapter 10: http://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-width

There are also some subtle consequences when you place an absolutely positioned element such that it triggers an overflow condition or when one of the edges if out of the view port. In these cases, a absolutely positioned block may have a computed width smaller than you specified.

The key is to consider what type and how much content the block will hold and provide a clear constraint for setting the width, either with a fixed value of a relative value.

Example

If you absolutely position the following:

<div class="abs ex3"><b>Small amount of text:</b> shrink-to-fit</div>

as shown in http://jsfiddle.net/audetwebdesign/SHxPR/, then the computed width of the block will be smaller than the width of the page. The block will expand in width and then height as needed to accommodate more text.

like image 87
Marc Audet Avatar answered Oct 24 '25 06:10

Marc Audet