I want to create a slider, so here is the code:
div#list_container {
position: relative;
width: 100%;
}
div#first_list {
position: absolute;
left: 0px;
top: 0px;
}
div#second_list {
position: absolute;
left: 70%;
top: 0px;
}
<div id="list_container">
<div id="first_list">
<h1>Smth</h1>
</div>
<div id="second_list" class="aim_list">
<h1>Smth</h1>
</div>
</div>
<h2>Following elements</h2>
It kinda works (I would just change left property to move them), but since parent (div#list_container) is positioned as relative, so it doesn't cover children elements, so another elements, which will go after slider are shown above it. How can I fix it?
When you change position of child elements to absolute, they are no longer relative to their parent element. Either make parent absolute while changing children's position to relative, or add height to parent.
I also highly recommend implementing this using CSS Flexbox, otherwise it will be very difficult to maintain. See if you can work with this:
#list_container {
position: absolute;
width: 100%;
display: flex;
justify-content: space-around;
}
#list_container > *{
flex-grow: 1;
}
The problem is that, since your child elements are with absolute positioning, there is nothing that actually expands the box of the parent element. To fix it, you can add height to the div#list_container, depending on your design target.
Related
This question already has answers here:
Position Relative vs Absolute?
(10 answers)
Difference between style = "position:absolute" and style = "position:relative"
(10 answers)
Closed 2 years ago.
So css positions can be relative and absolute, among other types. Also, usually the parent container is set to position: relative; , and the children is set to position: absolute; to place children relative to its parent.
So when I set the below css example, child1 will be at the bottom left corner of parent container;
.parent{
position: relative;
}
.child1{
position: absolute;
bottom: 0;
left: 0;
}
But, I am wrapping other elements in the child1 class like below, basically child1 is acting like another container relatively positioned in the parent container:
<div class="parent">
<div class='child1'>
<div class-'childOfchild1'>
<h1> Some text or button </h1>
</div>
//some other divs...
</div>
</div>
Where, childOfchild1 will have a position: absolute; so its relative to child1. However, if I turn child1 into relative, it changes its position to the parent. I also tried to set the childOfchild1 to the bottom left corner of child1, but it seems like it doesn't follow the logic I have in mind.
Can someone explain what happens to this? What happens when a relative container is placed within another relative container? Hopefully with examples. Thank you!
An element with position: relative is laid out in normal flow and then shifted from that position the distances specified by the left, right, top and bottom properties.
Ancestors with position: relative have no direct effect on it.
Elements with position: absolute are positioned with respect to the closest ancestor that has position: some-value-that-is-not-static (which includes ancestors with either position: absolute or position: relative).
Positioning is one of the spearheads of CSS. You do well to ask the question because it is not always easy to understand.
To summarize CSS positioning, from the moment you have a position different from static (the default value), your element becomes the new positioning reference for all its direct and indirect children (grandchildren).
There are different potential behaviors of a positioned element though: if we take the case of an element with an absolute position, this is how the browser will position your element:
if its direct parent is positioned (absolute or relative), it will be its positioning reference.
if its direct parent is not positioned, it will go back up to its grandparent, then to its grandparent until it finds a positioned item
otherwise it takes body as a positioning reference (the last positioned parent)
Here is a code example that illustrates some of the cases. You will find in the example squares, the smallest is also the deepest in HTML. (grandson), they all have a semi-transparent color, if it gets dark it means they are overlapping.
The code example with StackOverflow tool
.relative {
position: relative;
}
.absolute {
position: absolute;
}
/* Second */
.ex2 .child {
position: absolute;
bottom: 0;
left: 0;
}
/* Third */
.ex3 .child {
position: relative;
bottom: -10px;
left: 10px;
}
.ex3 .grandchild {
position: absolute;
bottom: 0;
left: 0;
}
/* Fourth */
.ex4 .grandchild {
position: absolute;
bottom: 0;
left: 0;
}
div {
float: left;
margin: 20px;
width: 200px;
height: 200px;
background: rgba(255, 34, 56, .25);
}
div div {
margin: 0;
width: 5em;
height: 5em;
}
div div div {
width: 2em;
height: 2em;
}
<div class=" ex1 normal">
<div class="child">
<div class="grandchild"></div>
</div>
</div>
<div class="ex2 relative">
<div class="child">
<div class="grandchild"></div>
</div>
</div>
<div class="ex3 relative">
<div class="child">
<div class="grandchild"></div>
</div>
</div>
<div class="ex4 relative">
<div class="child">
<div class="grandchild"></div>
</div>
</div>
To summarize: it's all about the question "does this positioned element have a positioned parent, if not, what is the closest positioned parent?"
Do not hesitate to ask further if you have more question.
I'll be glad to help.
Have a nice day!
relatively positioned elements are positioned relative to their normal position "normal flow", and when you adjust the position"top, left,.." it will refer to its normal position as a starting point. so when a relative container positioned inside another relative one, they will not be affected by each other
I want to move a child element to the bottom of its parent via absolute positioning.
Ie The parent will finish and then the element will appear directly after. ie the Parents bottom margin will be aligned with the childs top margin.
I have somewhat achieved this via padding however I don't see how to make this work when the child's size is changeable? ie I have a hardcoded padding size.
<body>
<footer class="footer">
<p> I am the footer </p>
<div class="after">
<h2> I want to appear after the footer</h2>
<img src="http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/4/11/1397210130748/Spring-Lamb.-Image-shot-2-011.jpg"/>
</div>
<h3> I should be in the footer</h3>
</footer>
</body
css ->
.footer {
position: relative;
left: 30px;
padding-bottom: 500px;
}
.after {
position: absolute;
bottom: 0px;
left: 0px;
}
codepen -> http://codepen.io/ianw92/pen/pvXYeV
Absolutely positioned elements are removed from the normal flow. By definition, they therefore won't take other elements into consideration when being positioned. As you are seeing, you would need to hardcode some value.
I'd suggest avoiding absolute positioning and using a flexbox solution to change the order of the elements:
Updated Example
.footer {
display: flex;
flex-direction: column;
}
.footer .after {
order: 3;
}
I have two divs that I would like to place one on top of the other, so I can create a tab system in an applet I am making. These two divs reside within a parent div, that uses auto height because I do not know the exact height of the other two divs (both children will be of same height). I can position the two divs one on top of the other with absolute positioning when the parent uses relative positioning, but the auto height doesn't respond (most likely because of absolute positioned children) creating a border line of an empty div instead of a wrapper with elements inside.
See problem here: http://jsfiddle.net/h5bjt69s/
<div id = "parent">
<div id = "redDiv"></div>
<div class = "clearfix"></div>
<div id = "blueDiv"></div>
</div>
I tried modeling a solution from this, but I believe the auto height throws things off.
Position absolute but relative to parent
How can I wrap the two divs with the parent div and still maintain the overlaying of the two children?
This:
both children will be of same height
Actually solves your problem:
Position one div using position: static; it will determine the height of the parent
Position the other div(s) using position: absolute (it will appear on top)
Updated Fiddle
Here are the changes
#blueDiv {
background-color: blue;
width: 100%;
height: 50px;
position: relative;/*changed*/
top: 0px;
left: 0px;
z-index:2;/*added*/
opacity:0.7;
}
DEMO
Another Style
#blueDiv {
background-color: blue;
width: 100%;
height: 50px;
/*position: relative;removed*/
opacity:0.7;
}
#redDiv {
background-color: red;
width: 100%;
height: 50px;
visibility: visible;
position: absolute;
top: 0px;
left: 0px;
z-index: 0;/*added*/
}
Updated
I've got a div that contains a photo tiling style I've been working on. The parent div over all the photos is position:relative while the divs inside holding the photos are position:absolute
I have to use 'position:absolute` for the children to get the layout I want but the problem arises when the parent div (either .daddy or .floatcont) doesn't register with a height and appears empty.
How can I make the parent register a height so I can put content below it on the page?
Code here: http://codepen.io/jeremypbeasley/pen/iBgsp
.floatcont {
position: relative;
background: pink;
width: 90%;
margin: 5%;
}
.floatpic {
position: absolute;
width: 40%;
margin-bottom: 10vh;
}
Absolute positioned elements are removed from the flow, thus ignored by other elements. So you can't set the parents height according to an absolutely positioned element.
In your case, I have come up with one solution. Update your .sixth class like below.
.floatpic.sixth {
top: 270vh;
width: 50%;
z-index: 6;
position:relative;
}
Updated CodePen
In this example:
html
<div style="width:50%;overflow:hidden">
<div id="inboxHeader">
<div id="inboxCount"><p>Earth</p></div>
</div>
</div>
css
#inboxHeader{
background-color:yellow;
height :300px;
position: relative;
}
#inboxCount{
position: absolute;
bottom: 0;
float:right;
}
Earth is in the bottom left corner. So how can I shift it to the bottom right corner?
Set right:0 instead of float:right
http://jsfiddle.net/8np2f/4/
As it's an absolutely positioned element change float:right; for right: 0px;
If it was positioned relatively then you would need to float it to the right however absolute positioning removes the element from the flow of the DOM.
One caveat however, make sure the parent element has it's position set either to relative or absolute as required, or the child element could position itself against the highest in the DOM tree that has a position set.
float-ing has no effect on absolute-ly positioned elements. Set the right attribute instead.
So, change this:
#inboxCount{
position: absolute;
bottom: 0;
float:right;
}
To this:
#inboxCount{
position: absolute;
bottom: 0px;
right: 0px;
}
HTML
<div style="width:50%;overflow:hidden">
<div id="inboxHeader">
<div id="inboxCount">
<p>Earth</p>
</div>
</div>
</div>
CSS
#inboxHeader {
background-color:yellow;
height :300px;
position: relative;
}
#inboxCount {
position: absolute;
bottom: 0;
right: 0; /* No need for float:right with absolute positioning */
}
I've update your Demo
You must add the rule right: 0 in the id inboxCount.
Here's an example: http://zip.net/brmZth
Hope this helps
Just posting a descriptive answer so it might help someone.
position property can be used to tackle this problem
<div id="outer" style="width: 400px; height: 400px; border: 1px solid red; position: relative">
<div id="inner" style="width: 200px; height: 200px; border: 2px solid yellow; right:0;bottom:0;position:absolute;">
</div>
jsfiddle to check a DEMO
If we set relative positioning on 'outer' div, any elements within 'outer' div will be positioned relative to 'outer' div. Then if we set absolute positioning on 'inner' div, we can move it to the bottom right of 'outer' div using 'right' and 'bottom' properties.
Relative :
If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document.
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.
Absolute : When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go. And a type of positioning that allows you to literally place any page element exactly where you want it.
Any element that is a child of the relatively positioned element can be absolutely positioned within that block.
References :
http://www.barelyfitz.com/screencast/html-training/css/positioning/
https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/