Reputation: 1985
If an HTML element has following CSS property, what would be its position on the screen?
position: absolute;
bottom: 0;
left: 0;
right: 0;
top: 0;
Upvotes: 0
Views: 2760
Reputation: 3241
If height and width are specified, it will be top left
in all major browsers. If top
is not specified, then it will be bottom left
. In other words, top
takes precedence over bottom
and left
takes precedence over right
.
If you don't have height and width set (defaulting to auto
), then the the top
, bottom
, left
and right
are used to calculate them. If your parent container is 20px
by 20px
and you have:
{
top: 5px;
bottom:0;
left:5px;
right:0;
position:absolute;
}
then the element will be 15px
by 15px
, i.e.
([height of parent] - [top] - [bottom] by [width of parent] - [left] - [right])
positioned at top: 5px left:5px
within the parent container.
Upvotes: 1
Reputation: 92853
If this code
div{
position: absolute;
bottom: 0;
left: 0;
right: 0;
top: 0;
}
in the css means it's takes the entry width & height of the screen
or it's parent
.
you can achieve same effect with width:100%
& height:100%;
like this:
div{
position: absolute;
width:100%;
height:100%;
top:0;
left:0;
}
but both have major difference in first example if you add padding & border
on the element then they are not add
to width & height
to that element but in second they add padding & border value in the height & width
.
Check these two example in which only padding & border
create huge different.
&
Upvotes: 0
Reputation: 13037
The element will fill the entire containing block content area. Its content area is defined by width and height, and no padding or anything else. Its containing block is nearest positioned -non static- ancestor.
For instance, top: 0
is saying to the element to have an offset 0 from the top of its containing block. So to start at the begining of its content area. And bottom: 0
is saying to spread the length until reach left extrem of the content area.
The same for right
and left
.
Of course, second declaration is ignored if you declare an explicit width
or height
.
It do make sense. As the specification says:
The box's position (and possibly size) is specified with the 'top', 'right', 'bottom', and 'left' properties. These properties specify offsets with respect to the box's containing block. Absolutely positioned boxes are taken out of the normal flow. This means they have no impact on the layout of later siblings. Also, though absolutely positioned boxes have margins, they do not collapse with any other margins.
The key is possibly size.
Upvotes: 0
Reputation: 19345
It seems that most browsers actually has the element fill the window (or the parent element, if the parent element has absolute or relative position)
If you add a width
declaration to the CSS, however, the left
will be enforced and the right
will be ignored.
If you add a height
declaration, the bottom
will be ignored.
Upvotes: 0
Reputation: 201866
Its top left corner will be in the top left corner of the enclosing positioned element or, in the absence of such an element, in the top left corner of the canvas. Its width and height will be the same as those of the enclosing positioned element (or the canvas).
Upvotes: 0
Reputation:
It would fill its parent container. The position on the screen would therefore be top/left: 0, 0
Upvotes: 1