- absolute vs relative position width & height
- 1 Answer 1
- CSS Layout — The position Property
- The position Property
- position: static;
- Example
- position: relative;
- Example
- position: fixed;
- Example
- position: absolute;
- Example
- position: sticky;
- Example
- Positioning Text In an Image
- CSS position relative : width not considered
- 1 Answer 1
absolute vs relative position width & height
i know what is absolute & relative position but some points are still not cleared to me. for reference css:
- relative div takes 100% width automatically but absolute div only takes content width. why?
- when i give height 100% there is no effect in the relative div but absolute div takes 100% height. why?
- when i give margin-top:30px it’s shift absolute div also but when i give top:30px then only relative div shift. why?
- when i don’t give top:0 , left:0 to the absolute div it’s takes above div height. why?
1 Answer 1
- Setting position:absolute removes the element in question from the normal flow of the document structure. So unless you explicitly set a width it won’t know how wide to be. you can explicitly set width:100% if that is the effect you’re after.
- An element with position:relative on the whole behaves in the same way a normal position:static element does. Therefore, setting height:100% will have no effect unless the parent element has a defined height. In contrast absolute positioned elements are removed from the document flow so are free to adjust to whatever height their containing element currently has.
- This is probably something to do with the parent elements in your HTML but I can’t help further unless you provide the full HTML and CSS of your page.
- The default value of the top and left properties is auto. This means the browser will calculate these settings for you and set them to where the element would be rendered if it didn’t have position:absolute .
thanks for your answers.I like the 1st & 4th point it’s very useful but for the 3rd point please implement the value in the above code for better understanding
@sandeep, your code above doesn’t include any information on the parent element of the these two divs. I need to know how the parent is styled in order to answer your question fully.
But, isn’t position:absolute still relative to it’s parent element? w3schools.com says «The element is positioned relative to its first positioned (not static) ancestor element». What does that actually mean? What is an ancestor element and does it have to be absolute positioned? How do you find an elements ancestor element?
From point #2 I understand that it is necessary for a parent element with position: relative to have a certain height e.g. 95vh or else the child element with position: relative does not have any effect. Is my understanding correct?
@AgentZebra An ancestor refers to any element that is connected but further up the document tree — no matter how many levels higher.
CSS Layout — The position Property
The position property specifies the type of positioning method used for an element (static, relative, fixed, absolute or sticky).
The position Property
The position property specifies the type of positioning method used for an element.
There are five different position values:
Elements are then positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the position value.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right properties.
An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page:
Here is the CSS that is used:
Example
position: relative;
An element with position: relative; is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.
Here is the CSS that is used:
Example
position: fixed;
An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.
A fixed element does not leave a gap in the page where it would normally have been located.
Notice the fixed element in the lower-right corner of the page. Here is the CSS that is used:
Example
position: absolute;
An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.
Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.
Here is the CSS that is used:
Example
div.relative <
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
>
div.absolute position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
>
position: sticky;
An element with position: sticky; is positioned based on the user’s scroll position.
A sticky element toggles between relative and fixed , depending on the scroll position. It is positioned relative until a given offset position is met in the viewport — then it «sticks» in place (like position:fixed).
Note: Internet Explorer does not support sticky positioning. Safari requires a -webkit- prefix (see example below). You must also specify at least one of top , right , bottom or left for sticky positioning to work.
In this example, the sticky element sticks to the top of the page ( top: 0 ), when you reach its scroll position.
Example
div.sticky <
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;
>
Positioning Text In an Image
How to position text over an image:
CSS position relative : width not considered
I try to build a pure CSS tree. I encountered a problem with horizontal lines between blocks (two blocks are at the same level). I isolated the problem in the following jsfiddles: https://jsfiddle.net/8Lsv1ypd/3/ https://jsfiddle.net/8Lsv1ypd/4/ Html :
.first < background-color: #dc3545; color: #fff; font-size: 1.2rem; border: 1px #ccc solid; border-radius: 20px; padding: 5px 10px; margin-top: 10px; >.second < background-color: #6f42c1; color: #fff; font-size: 1.2rem; border: 1px #ccc solid; border-radius: 5px; padding: 5px 10px; margin-top: 10px; margin-left: 10px; >.second::before
- position : absolute / relative / static / fixed
- display : block / inline
- overflow : auto / visible;
I already look at the following questions :
And the following article :
you need inline-block .. pseudo element are inline element by default . when setting absolute they become block element but remain inline when relative
and for the second issue, position:absolute need a reference, so you need to add position: relative; to the span element to have left/top working like you want
Yeaah . great proposal : jsfiddle.net/bhn4kso9 but I can’t get ride of the blank space before the text «Second».
1 Answer 1
When the CSS position (in .second::before) is set to relative, the width (fixed in pixels) is not considered, only the vertical line is displayed and width is «forced by the browser» to 1 pixel.
A pseudo element is an inline element by default, setting position:relative will not change this thus you cannot apply width and height to the element. Then the borwser is not forcing the width to 1px , it’s the border you have set that is equal to 1px . The height also isn’t working and the height of the element and the border is defined by the font property.
Increase the height and you will see that nothing will change:
.first < background-color: #dc3545; color: #fff; font-size: 1.2rem; border: 1px #ccc solid; border-radius: 20px; padding: 5px 10px; margin-top: 10px; >.second < background-color: #6f42c1; color: #fff; font-size: 1.2rem; border: 1px #ccc solid; border-radius: 5px; padding: 5px 10px; margin-top: 10px; margin-left: 10px; >.second::before
Now increase the font-size and you will see some changes
.first < background-color: #dc3545; color: #fff; font-size: 1.2rem; border: 1px #ccc solid; border-radius: 20px; padding: 5px 10px; margin-top: 10px; >.second < background-color: #6f42c1; color: #fff; font-size: 1.2rem; border: 1px #ccc solid; border-radius: 5px; padding: 5px 10px; margin-top: 10px; margin-left: 10px; >.second::before
When the CSS position (in .second::before) is set to absolute, the width is not taken into account and the horizontal line is displayed, but the line is not joining the two block.
When adding position:absolute the element become a block level element thus you can know control its width and height and both are considered in your case but your element is positionned relatively to the viewport since there is no positionned ancestor. It’s hidden because you set a negative left value so you cannot see the border you have set.
You need to make the span position:relative to make the pseudo element positionned relatively to the span:
.first < background-color: #dc3545; color: #fff; font-size: 1.2rem; border: 1px #ccc solid; border-radius: 20px; padding: 5px 10px; margin-top: 10px; >.second < background-color: #6f42c1; color: #fff; font-size: 1.2rem; border: 1px #ccc solid; border-radius: 5px; padding: 5px 10px; margin-top: 10px; margin-left: 10px; position:relative; >.second::before
10.3.1 Inline, non-replaced elements
The ‘width’ property does not apply ref
10.6.1 Inline, non-replaced elements
The ‘height’ property does not apply. The height of the content area should be based on the font, ref
Floats, absolutely positioned elements, block containers (such as inline-blocks, table-cells, and table-captions) that are not block boxes, and block boxes with ‘overflow’ other than ‘visible’ (except when that value has been propagated to the viewport) establish new block formatting contexts for their contents. ref
In the absolute positioning model, a box is explicitly offset with respect to its containing block
If the element has ‘position: absolute’ , the containing block is established by the nearest ancestor with a ‘position’ of ‘absolute’, ‘relative’ or ‘fixed’, . If there is no such ancestor, the containing block is the initial containing block. ref