In the CSS Positioned Layout Module Level 3 (Working draft) in chapter 6.2. Sticky positioning we have this definition:
(Emphasis mine)
A stickily positioned box is positioned similarly to a relatively positioned box, but the offset is computed with reference to the nearest ancestor with a scrolling box, or the viewport if no ancestor has a scrolling box.
What are these scrolling boxes?
Further down the document there is an issue about the term scrolling boxes
Issue 6 Sticky positioning should really be defined in terms of the nearest scrollable ancestor, but there is currently no such term defined elsewhere in CSS. CSSOM View refers to "scrolling boxes." CSS Overflow has yet to pull in the relevant text from CSS Box, and CSS Box has an old, confusing definition of "flow root" which is almost (but probably not quite) what we want here. This spec refers to "flow root," since that’s the closest thing currently specced somewhere, but this is not optimal.
Does anybody know, where I can find further information (this draft is from May 2016)? I especially want to switch on or off, if a box is a scrolling box or not.
As #alex said a scrolling box is a box where the value of overflow is set to a value different from visible (the default one). I cannot confirm but I concluded this based on this previous answer where overflow is creating some issues with sticky element.
As I explained there, if you have an element with overflow:hidden as an ancestor of the position:sticky this one will stop working because its offset will be calculated based on that box (with overflow:hidden) because it's the nearest ancestor with a scrolling box. Since we used hidden we cannot scroll this box so we cannot see the sticky behavior.
Here is a basic example:
.wrapper {
height:200vh;
border:2px solid;
}
.wrapper >div {
position:sticky;
top:0;
height:20px;
background:red;
}
<div class="wrapper">
<div></div>
</div>
In the below example, the viewport will be used for the reference because we have no scrolling box. Now let's add overflow to the wrapper:
.wrapper {
height:200vh;
border:2px solid;
overflow:scroll;
}
.wrapper >div {
position:sticky;
top:0;
height:20px;
background:red;
}
<div class="wrapper">
<div></div>
</div>
Now our sticky element will consider the wrapper for the reference but since we don't have any overflow, we won't have any scroll so there is no way to trigger the sticky behavior. Also scrolling the viewport will do nothing.
If you add some overflow by adding another element inside we can trigger the sticky behavior:
.wrapper {
height:200vh;
border:2px solid;
overflow:scroll;
position:relative;
}
.wrapper >div {
position:sticky;
top:0;
height:20px;
background:red;
}
.wrapper > span {
position:absolute;
top:100%;
height:50px;
left:0;
right:0;
background:blue;
}
<div class="wrapper">
<div></div>
<span></span>
</div>
We can clearly see how the scroll of the wrapper is controlling the sticky element and the scroll of the viewport is doing nothing thus we can conclude that our wrapper is the the nearest ancestor with a scrolling box
Considering the last issue in the specification we can also read that:
Sticky positioning should really be defined in terms of the nearest scrollable ancestor, but there is currently no such term defined elsewhere in CSS. CSSOM View refers to "scrolling boxes."
So probably a scrollable ancestor is the same as an ancestor with a scrolling box.
Related
I want to make a div that follows the viewport. I am not able to use fixed positioning. For some reason the div doesn't follow the viewport properly, it always 'lags' behind, especially when changing scroll direction. You can see what I mean on plunker here.
That is the first part of the problem. The second is that I need the div to immediately move into the viewport when it appears. (Clicking anywhere in the output panel in the plunker will toggle the div to appear). The *ngIf part of the appearing is important because in my actual app I am using a component with entry animations instead of a div, so no [hidden] I'm afraid.
So why can't I use fixed positioning?
Basically, fixed positioning causes the element to calculate the width differently to its sibling elements when the viewport contains scrollbars. I also can't use the overflow: scroll trick on the element because it has shadows that get clipped by the viewport. It also looks ugly having two scrollbars.
Here is a fixed centered div in a wrapper using flex.
Demo Here
#wrapper{
position:absolute;
width:100%;
height:100%;
position:fixed;
display: flex;
justify-content: center;
}
#my-app{
position:relative;
width:50%;
height:50%;
background-color:green;
}
<div id="wrapper">
<div id="my-app">
</div>
</div>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Divs on for my site:
Header
Banner (inside the banner div: is another div call innerbanner )
Main Content
Second Content
FooterWrap
I have several divs tags on a site I'm trying to develop, and 2 of the div tags I'm using an absolute position ( 1 for Banner and the 2nd one for the Footer), however when I use the absolute positioning for the banner it pushing my main content div upward making it invisible, but my footer doesn't have that problem. Can you assist me?
Absolute Positioning Inside Relative Positioning
http://css-tricks.com/absolute-positioning-inside-relative-positioning/
if you want to set a div absolute position make sure that the div which contains it has a relative position so it won't get out of the box and disorder the elements, try this for example:
html:
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
css:
.container{
width:75%;
height:500px;
border:1px black solid;
position: relative;
}
.box1{
width:50px;
height:50px;
border:1px solid red;
position:absolute;
top:0;
left:0;
}
.box2{
width:50px;
height:50px;
border:1px solid green;
position:absolute;
bottom:0;
right:0;
}
http://jsfiddle.net/ahmedskaya/DZ2DF/
Please check by defining some height to your parent div of absolute positioned (banner) div in css. This might be the reason of content div pushing upward. Since parent of absolute positioned div don't draw height automatically or by absolute positioned content, and collapsed to its minimum height.
So you might need to define certain height of your parent div in your css in order to make it cover the height area of your absolute positioned div.
Also since your content div would (probably) have normal content (not positioned absolutely) it would be drawing height like a normal div with them. And so footer div couldn't push upside beyond its height.
This should fix your issue.
A parent div must completely contain the contents to a child div. Due to an odd circumstance, that child div might be wider than the page view itself. Problem being, the parent div seems to always be limited to the page width, no matter what it contains.
http://jsfiddle.net/e5Lkq/1/
contains:
<div class="outer clearfix">
<div class="inner">Oversized content here.</div>
</div>
.inner{
background-color:red;
height:50px;
width:1800px;
}
.outer{
border:5px solid blue;
/* overflow:hidden; */
}
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
In this example, I need the outer div (with the blue border) to expand around the child's contents.
I've come across this problem before - something about a quirky rendering of the "body" style - but I've been unable to locate similar questions again this time. Setting overflow to hidden for the parent just cuts off the child element. I could always throw in a js to to resize after loading, but I'd rather avoid this. Any thoughts?
Thanks,
Jeremy
PS I see this similar item, but the answer is jquery. This might just have to be the way of it:
https://stackoverflow.com/questions/8360465/expand-parent-div-width-to-fit-children-divs-horzontal-scroll-website
What you want is similar to a shrink-wrap. http://jsfiddle.net/e5Lkq/2/
.outer { display: table }
To get old IE support, you can use inline-block (on a by-default inline element) inside a block container. See this and this (if you're really concerned about support). But I strongly encourage you to drop support (if you can). Is the extra 8% worth the effort? Also note that the percentages are different depending on the site.
floating the parent appears to work:
.outer{
border:5px solid blue;
float:left;
}
see: http://jsfiddle.net/n6WLG/
Have a look at, http://thomaspalumbo.com
I have this CSS for my website's container:
.graybox {
padding: 0 30px 30px 30px;
background: #ededed;
position:absolute;
left:0;
right:0;
}
Then I have a container on top of that to center that info.
The .graybox container spreads the width of the page like I want but now my footer div is hidden, according to firebug is it actually behind? And up on the page?
Is there a fix for this?
While I'm here can anyone explain the white space on the right side of the page. It comes into effect once the page is resized smaller.
You can use the CSS z-index property to make sure your footer is in front of the content. Z-index only works when the element is positioned though. So make sure you add position:relative to your footer
#footer{
position:relative;
z-index:999;
}
Read more: http://www.w3schools.com/cssref/pr_pos_z-index.asp
EDIT
Just checked out the code of your website, and I don't understand why your graybox is positioned absolutely, this will only make things more complex. The same goes for your menu, why position it absolute, why not just add it in the right order in the HTML in the first place?
EDIT
If you want to center your content but with a background that has a 100% width then you can simply add a container div like so:
HTML
<div class="container">
<div>lorem ipsum....</div>
</div>
CSS
.container{
background:red;
}
.container div{
width:400px;
margin:0 auto;
background:yellow;
}
See JSFiddle here: http://jsfiddle.net/HxBnF/
Currently you cannot do this because you have a container which you set at 980px, don't ever do that unless you are sure you don't want anything to wrap over it, like in this case the background of a div in that container.
in the div style, just assign a z-index value greater than any other z-index such as
.divClass{
position: absolute;
z-index: 1 //if other elements are still visible chose a higher value such as 20 or even higher.
}
In my long journey to update my CSS skills from the deprecated dust that they have turned into, I've been playing around with certain CSS properties —particularly z-index —I'm noticing something strange or maybe there's a certain condition.
Example:
http://jsfiddle.net/mEpgR/
The element div1's parent is cont, I've made div1's position property set to absolute, yet when I shift it, it's moving relative to its parent. I was under the impression that items set to absolute positioning are outsider regular flow and move only relative to browser port as their parent.
What am I missing?
If the fiddle link does not work, code:
CSS:
.cont {
position:relative;
height:200px;
top:200px;
left:100px;
background: green; width: 200px;
}
.div1 {
background:red;
position:absolute;
top:50px;
}
HTML:
<div class="cont">
<div class="div1">DIV1</div>
</div>
An absolute positioned element is positioned relative to the first parent element that has a position other than static. Since you have it inside a parent with relative it will be absolutely positioned relative to this parent.
You might be looking for fixed position, which is relative to browser window.