Absolute positioned element on a scrollable parent - html

I'm trying to have an child element (something like a toolbar) of a parent element to be positiond on its bottom edge. The bahavior should be the same as using position fixed for the browser view.
I'm using absolute position right now. Everyting is perfect until the parent needs to scroll its content. Then the toolbar moves along with the rest of the parent's content.
Could somebody explain me why the toolbar moves?
Is it possible to achieve that task without need any javascript?
* {
box-sizing: border-box;
}
.container {
position: relative;
width: 100px;
height: 150px;
overflow-y: auto;
border: 1px solid black;
}
.mock {
height: 200px;
background-color: red;
}
.tool-bar {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 40px;
background-color: blue;
}
<div class="container">
<div class="mock"></div>
<div class="tool-bar"></div>
</div>

The toolbar is inside the scrollable area, that's why it scrolled. Try this code:
HTML
<div class="container">
<div class="scroll">
<div class="mock"></div>
</div>
<div class="tool-bar"></div>
</div>
CSS
div.scroll { /* style of .container to scroll */ }

I have found an interesting fiddle that may help you. They are using position:fixed and the divs are not nested:
http://jsfiddle.net/b2jz1yvr/
<div class="fixedContainer">
This is experimental
</div>
<div class="otherContainer"></div>
.fixedContainer {
background-color:#ddd;
position: fixed;
padding: 2em;
left: 50%;
top: 0%;
transform: translateX(-50%);
}
.otherContainer {
height:1000px;
background-color:#bbb
}

Related

Sticky Div inside HorizontalScrollView

Im building a website based on a Horizontal Scroll View, this is made by an move interaction and a sticky section. Inside this sticky section i want to put an sticky div,then, when you scroll horizontaly, one div remains sticky meanwhile you scroll horizontally.
There is an example:
https://studiochevojon.com/
In this website you can horizontal scroll and have a sticky div in determinate moment.
There is my webflow project: https://preview.webflow.com/preview/designfeelings?utm_medium=preview_link&utm_source=dashboard&utm_content=designfeelings&preview=1bd0bbb81feac58ef0d75e3ee82d61d0&mode=preview
Can someone explain me how this works? I try all horizontal scroll tutorials but i dont know how to make this works.
Thank you all.
to be sticky a div needs the style: position: sticky;. Then it needs a broder where ti actually should stick to (top, bottom, left and/or right) and the distance (%, vw/vh, px...). Like in this example
body {
margin: 0;
padding: 0;
}
#wrapper {
position: absolute;
top: 0;
bottom: 0;
width: 500vw;
display: flex;
background-color: red;
}
.page {
width: 100vw;
padding: 5px;
}
#one {
background-color: yellow;
}
#two {
background-color: green;
}
#three {
background-color: grey;
}
#sticky {
display: flex;
position: sticky;
left: 0;
width: 100vh;
background-color: blue;
padding: 5px;
}
<div id="wrapper">
<div class="page" id="one">I'm page 1</div>
<div class="page" id="two">I'm page 2</div>
<div id="sticky">I'm the Sticky Box</div>
<div class="page" id="three">I'm page 3</div>
</div>

overflow-x also hides overflow-y

There are multiple questions named this way, but I didn't find one that applies to my case, so here I am:
In this snippet:
#container:hover {
overflow-x: hidden;
}
#container {
position: relative;
width: 400px;
height: 40px;
background: red;
margin: 2em;
}
#child {
position: absolute;
bottom: 0;
}
<div id="container">
<div id="child">
a<br/>
b<br/>
hover<br/>
me
</div>
</div>
You can see that overflow-x, which is applied when you hover the red box, will also hide the overflow-y (at least on Chrome). This is annoying because I have a tooltip that I would like to be able to overflow above the red box, and in the meantime I have a menu that will slide from the right side and that should stay hidden.
Is this a bug? Is there a workaround?
You can't change the way overflow-x and overflow-y behave (it's the same in Firefox and other browsers), but you can change the way your HTML is organized.
Put everything that you want to hide when overflowing in a single wrapper. Put your tooltip in another wrapper.
Something like this may suit your needs:
#container {
position: relative;
width: 400px;
background: #f77;
margin: 3em 2em;
}
#child {
overflow: hidden;
position: relative;
}
#menu {
position: absolute;
left: 100%;
top: 0;
background: #dd2;
transition: .2s;
}
#child:hover #menu {
transform: translateX(-100%);
}
#tooltip {
position: absolute;
bottom: 100%;
}
<div id="container">
<div id="child">
hover<br/>
me
<div id="menu">
menu
</div>
</div>
<div id="tooltip">
a<br/>
b
</div>
</div>
Is the clipping behavior a bug?
No, the clipping is in accordance with the spec.
UAs must clip the scrollable overflow area of scroll containers on the
block-start and inline-start sides of the box (thereby behaving as if
they had no scrollable overflow on that side).
In your case, the "block-start" side is the top, and the "inline-start" side is the left. That's why you can put your tooltip below the content, and it will trigger a scrollbar.
#container:hover {
overflow-x: hidden;
}
#container {
position: relative;
width: 400px;
height: 40px;
background: red;
margin: 2em;
}
#child {
position: absolute;
/* bottom: 0; */
top: 0;
}
<div id="container">
<div id="child">
hover<br/>
me<br/>
a<br/>
b
</div>
</div>
So why is it possible to scroll to content overflowing below the box, but not possible to simply make it visible? The reason is that when any overflow property is set to hidden, the entire box becomes a scroll container.
[A scroll container] allows the user to scroll clipped parts of its
scrollable overflow area into view.
You can use overflow: clip, which does not turn the box into a scroll container. If you clip in both direction, you can also adjust the distance at which clipping occurs as well using overflow-clip-margin :
#container:hover {
overflow-x: clip;
}
#container {
position: relative;
width: 200px;
height: 40px;
background: red;
margin: 2em;
}
#child {
position: absolute;
bottom: 0;
}
<div id="container">
<div id="child">
aazkopekzapoekzapoekzapoekzapoekpozakepozakepozakeoza<br/>
b<br/>
hover<br/>
me
</div>
</div>

Why is my element not sticking to the left when using position sticky in css?

I want to fix an element to the top and left of the screen using position sticky when scrolling a large div either vertically or horizontally. Fixing to the top is working fine, but fixing to the left is not.
This is my html page:
.sticky {
position: -webkit-sticky;
position: sticky;
left: 0;
top: 0;
}
.scroll-horizontally-and-vertically {
width: 4000px;
height: 2000px;
background-color: lightblue;
}
<div>
<div class="sticky">
<h1>please stick to top and left</h1>
</div>
<div class="scroll-horizontally-and-vertically"></div>
</div>
I also tried using either top or left alone, with the same result.
I must be missing something.
Why is the top position fixed, but not the left position?
How should I fix the page to get the desired behaviour?
The sticky element is a block level element inside another block level so this one is already taking 100% width if its parent element and there is no room for a left sticky behavior.
Add some border to better see:
.sticky {
position: -webkit-sticky;
position: sticky;
left: 0;
top: 0;
border:2px solid green;
}
.scroll-horizontally-and-vertically {
width: 4000px;
height: 2000px;
background-color: lightblue;
}
<div style="border:2px solid red;">
<div class="sticky">
<h1>please stick to top and left</h1>
</div>
<div class="scroll-horizontally-and-vertically"></div>
</div>
The green box can only stick inside the red one and the lightblue element is overflowing. Addinline-block to sticky element (to remove the width 100% constraint) and to the parent element (so it grows with the lightblue element) and you will have the expected result
.sticky {
position: -webkit-sticky;
position: sticky;
left: 0;
top: 0;
border:2px solid green;
display:inline-block
}
.scroll-horizontally-and-vertically {
width: 4000px;
height: 2000px;
background-color: lightblue;
}
<div style="border:2px solid red;display:inline-block;">
<div class="sticky">
<h1>please stick to top and left</h1>
</div>
<div class="scroll-horizontally-and-vertically"></div>
</div>
Erit Vortstenbosch Welcome to the community. I have checked your code its working fine.
Just set margin and padding to 0 for h1 tag.
Here is the modified code snippet.
.sticky {
position: -webkit-sticky;
position: sticky;
left: 0;
top: 0;
}
.scroll-horizontally-and-vertically {
width: 4000px;
height: 2000px;
background-color: lightblue;
}
h1 {
padding: 0;
margin: 0;
}
<div>
<div class="sticky">
<h1>please stick to top and left</h1>
</div>
<div class="scroll-horizontally-and-vertically"></div>
</div>

Responsive height of div issue

One example is better than a thousand words, so here you go:
https://jsfiddle.net/jesuxapo/os53cyc1/
As you can see, the height is responsive, but not completely. The problem is the <div id="k"> with fixed height of 150px. Try to play with it and I think you'll understand exactly what I mean. I want to get rid of this 'problem' somehow.
I could use the calc() of the css3, however it's not cross-browser(especially android and IE8-9).
Perhaps there's some other solution for this using html and css languages?
You may use the display:table properties (IE8 and later):https://jsfiddle.net/os53cyc1/1/
it will grow if content is more than 100vh all together
html, body {
padding: 0;
margin: 0;
height: 100%;
background: #333;
font-weight: bold;
color: #fff;
}
body {
display:table;
width:100%;
}
body>div {
display:table-row;
}
div {
border: solid 2px #FFFF00;
}
div#a {
position: relative;
background: #800000;
width: 100%;
height: 100%;
}
div#b {
position: absolute;
top: 0;
}
div#c {
position: absolute;
bottom: 0;
}
div#k {
height: 150px;
background: #008000;
}
<div id="k">
Hello, I'm K and I just broke your code
</div>
<div id="a"><br><br><br><br>
This is relative div with height of 100% and max-height of 500px
<div id="b">
This div is aligned to the top of the Red div
</div>
<div id="c">
This div aligned to bottom of the Red div
</div>
</div>

CSS - Positioning Conundrum

I'm working with absolute positioning within a relative div. The code is as such: http://jsfiddle.net/32mq5v6L/1/
HTML
<div id="container">
<div id="featured-posts">
<div class="slide"><img src="http://alien.devprose.com/starwars/wp-content/uploads/2014/12/star-wars-droid.jpg" /></div>
<div class="slide"><img src="http://alien.devprose.com/starwars/wp-content/uploads/2014/12/han-solo-1140x350.jpg" /></div>
</div>
<div id="other-content">
Other Content
</div>
</div>
CSS
#container { width: 100%; margin: 0 auto; background: #eee; }
#featured-posts { position: relative; width: 100%; height: auto;}
.slide { width: 100%; height: 20px; position: absolute; top: 0; }
#other-content { }
My problem is the other-content div appears underneath #featured-posts unless I apply a set height to that container, which I can't do since the goal is to make all of this responsive.
Where am I going wrong?
If you plan to have #other-content after positioned container, you will have to create new stacking context in order to move it above. One way to do it since it's not positioned is to set very little opacity:
#other-content {
z-index: 10;
opacity: .99;
}
Demo: http://jsfiddle.net/32mq5v6L/1/