What does "Position: relative" do without top, right, left, bottom? [duplicate] - html

This question already has answers here:
Is position: static identical to position: relative, with no other properties specified?
(2 answers)
Closed 6 years ago.
I'm learning and studying the HTML/CSS codes of some of the most popular websites, and I just found that in most of them there is the "position:relative" property without the usual top, left, right, bottom properties.
Does this affect the page flow in some way, or it acts like the static property?

Short answer: position: relative becomes a positioned element:
This would mainly affect only it's child elements, with position: absolute relative to this element now, not the HTML. (Thanks Michael_B)
width: xx% is also relative to this parent element if there is not fixed widths along the hierarchy.
z-index now works with this element.

When you define absolute position for a element. The position it will base on the closest relative parent. If there's no relative parent, the position will base on the window page.

Related

CSS position:absolute and position:relative siblings - am I missing something? [duplicate]

This question already has answers here:
Why aren't my absolutely/fixed-positioned elements located where I expect?
(3 answers)
Closed 3 months ago.
Every doc I read about absolute position says:
"element is positioned relative to its closest positioned ancestor"
But when I use it with the sibling with position: relative that is before it, element with position absolute don't want to position itself relative to the ancestor, but it takes into account the position of the relative sibling in the document flow.
I can't understand why is that. Absolute should be absolute, not relative to the whatever sibling element...
Can somebody explain it to me ?
<div>
<div style="position:relative;">element1</div>
<div style="position:absolute;">element2</div>
</div>
Absolute positioned elements are moved from their static position - that is, the position they would have in normal flow if they weren't absolute positioned.
But you haven't moved it anywhere. You need to give it at least one of top, bottom, left, right, or their flow-relative equivalents inset-block-start, inset-block-end, inset-inline-start, inset-inline-end or their shorthands.

Anchoring image to a specific position inside an overflowed div [duplicate]

This question already has answers here:
Position absolute but relative to parent
(5 answers)
Closed 2 years ago.
Given the following example, https://jsfiddle.net/mzv4nxo8/,
I want to position the image in the top right corner over the div rectangle, like I've shown in the example. This works fine as long as the div container does not overflow.
However, when you start scrolling, the image moves as well since the image is set to position: absolute. I want it to stay "anchored" to its initial position (i.e. before you start scrolling). The text inside the div rectangle needs to stay in the center position.
How do I accomplish that?
position: absolute positions the element according the the closest parent that has the position property different from the default value static.
Add this to your css and it will work
div {
position: relative;
}
I guess you can also use your .content selector

position relative in css without top and right mention

.classname {
position: relative;
}
position relative in css without top right left and bottom mention,
I read that this not change any position, but in some web page write position: relative; without top right left bottom mention and if I delete "position: relative;" then position will change.
Can I know what exact use of "position: relative; " without top right left and bottom mention.
This type of positioning is probably the most confusing and misused. What it really means is "relative to itself". If you set position: relative; on an element but no other positioning attributes (top, left, bottom or right), it will no effect on it's positioning at all, it will be exactly as it would be if you left it as position: static; But if you DO give it some other positioning attribute, say, top: 10px;, it will shift it's position 10 pixels DOWN from where it would NORMALLY be. I'm sure you can imagine, the ability to shift an element around based on it's regular position is pretty useful. I find myself using this to line up form elements many times that have a tendency to not want to line up how I want them to.
There are two other things that happen when you set position: relative; on an element that you should be aware of. One is that it introduces the ability to use z-index on that element, which doesn't really work with statically positioned elements. Even if you don't set a z-index value, this element will now appear on top of any other statically positioned element. You can't fight it by setting a higher z-index value on a statically positioned element. The other thing that happens is it limits the scope of absolutely positioned child elements. Any element that is a child of the relatively positioned element can be absolutely positioned within that block.
The most known use for setting position relative on a parent element is to position child elements, set to position absolute, relative to the parents position.

Relative vs Absolute in CSS [duplicate]

This question already has answers here:
Position Relative vs Absolute?
(10 answers)
Closed 7 years ago.
Can any one explain me in detail about relative and absolute in CSS. All the descriptions tells me that absolute can be placed any place I want (that means I can use top, bottom, etc). I can achieve the same with the relative as-well. I was just checking a small example in relative and absolute in W3schools, wherein relative occupies the whole line but absolute doesn't. I am confused with this.
http://www.w3schools.com/css/tryit.asp?filename=trycss_position_relative
Can anyone explain me with a perfect example about their difference?
I think you can get the better understanding to Css Positioning from this source
Quick Summary of the 4 Kinds of Positioning:
Static - Static positioning is the default, it is what happens when
you set no positioning. The element (the tag and its content) stays in
normal page flow.
Relative - The element's box is still part of the page flow, and but
its location is changed vertically and/or horizontally relative to its
own normal position in page flow.
Being part of flow (being in page flow) means that an element will
push later elements in flow further down, and be pushed further down
by elements in flow that are before the current element.
Example of Relative CSS positioning:
.fromorig {position: relative;
top: 200px;}
Fixed - The element's box is removed from the normal page flow, and you can set exact vertical and horizontal
position of the element relative to the browser window. Additionally,
the fixed element's contents will NOT scroll away like normal HTML
page contents do, they will stay in their current position in the
browser window. Fixed positioning was not supported by IE until
version 7.
Example of Fixed CSS positioning:
.nevermove {position: fixed;
top: 200px;
left: 200px;}
Absolute - The element's box is removed from the normal page flow, and you can set exact vertical and horizontal
position of the absolute element relative to the element it is inside.
Additionally, the absolute element's contents will scroll vertically
(and/or horizontally) like normal HTML page contents do.
Example of Absolute CSS positioning:
.moveit {position: absolute;
top: 200px;
left: 200px;}
see demo at:
CSS Positioning With example

What is the point of using absolute positioning in CSS? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
This link says
An absolute position element is positioned relative to the first
parent element that has a position other than static. If no such
element is found, the containing block is <html>
So absolute is in fact relative. So why not just use relative instead? Are there any practical use cases which can only be done by using absolute positioning but not relative?
So absolute is in fact relative.
NO, both are completely different, their rendering is completely different.
A relative positioned element is in the document flow whereas an absolute positioned element is out of the document flow.
Are there any practical use cases which can only be done by using absolute positioning but not relative?
Assume that you want a title on image hover, you need to position the title containing element absolute to the element holding image which has to be positioned relative. If you do not assign position: relative; to the element holding absolute positioned elements, your absolute positioned elements will just flow out in the wild.
Case
Case 2 (If element is not positioned relative, your absolute positioned elements will flow out in the wild)
So inshort, you can say that the absolute positioned elements DO NOT take any physical space on the document as they are out of the flow so inorder to save them flowing out in the wild, we use a container element positioned relative, where relative positioned do take space as they are in the flow so when you move your absolute positioned elements anywhere on the page, they won't affect any other elements position but if you move your position: relative; element anywhere on the page, yes it will affect other elements - static or relatively positioned around it.
An element with relative positioning remains within the flow of the document. Any additional positioning you provide will offset the element from this position.
An element with absolute positioning is removed from the flow of the document, and is positioned with respect to its first nonstatic parent element.
So let's say you have the following HTML structure, and an accompanying fiddle:
<div class="awesomeParent">
<ul>
<li>First Piggy</li>
<li>Second Piggy</li>
<li>Third Piggy</li>
<li>Fourth Piggy</li>
</ul>
</div>
Now checkout the same fiddle, modified a bit using relative and absolute positioning. See the difference?
In the second fiddle, using relative positioning, the space where the third list element should be is preserved. It's only being offset by the additional top: 40px; rule.
However, in the third fiddle, using absolute positioning, the space where the third list element should be is gone. In other words, that element is no longer in the normal flow of the document, and now the positioning rule top: 40px; is with respect to it's first nonstatic parent. In this case, this is the div .awesomeParent, which has a position: relative. If no parent had been found, then the element would have been positioned with respect to the html element. So in order to position an element absolutely within another element, you'll need to use position fixed, absolute or relative on the parent itself.
Images so you can compare the three: