CSS: Position sticky to bottom when enter viewport - css

I am trying to add a sticky element that stays on the bottom of the page only when it enters the viewport. The idea is that the element would be placed somewhere in the middle of the page and would not be shown immediately when the user visits the page.
Here's my progress so far:
div {
height: 300px;
}
.sticky {
position: -webkit-sticky;
position: sticky;
bottom: 0;
height: 30px;
width: 100vw;
background: yellow;
}
<div style="background: red;">Scroll</div>
<div style="height: 300px; background: orange;"></div>
<div class="sticky">Sticky Section</div>
<div style="background: green;"></div>
<div style="background: blue;"></div>
The problem is that my code does the opposite of what I want it to do. The element should only stick past its original location.
Changing bottom: 0 to top: calc(100vh - 30px) on .sticky seems to work fine on desktop, but I noticed it's not adequate for Safari on iOS. With this 'hacked' solution, whenever the user scrolls up, the bottom bar increases in size and forces the sticky element to jump.

Sticky position can be a little confusing and frustrating at times.
The key to solving this problem is to keep in mind that a sticky element will appear at the top of its parent element. So by splitting or encapsulating the page's content into 2 sections we can create your desired outcome.
The first section contains content and no sticky element,
The second section contains content and the desired sticky element which will appear at the top of this element
We can decide where on the page the sticky element first appears by where we create the split between the 2 sections or more appropriately where the 2 section starts.
Below is example code where the sticky element appears in the middle of the page.
//
.sticky {
position: -webkit-sticky;
position: sticky;
bottom: 0;
height: 30px;
width: 100vw;
background: yellow;
}
<!-- Content you want before the sticky element -->
<div>
<div style="height: 300px; background: red;">Scroll</div>
<div style="height: 300px; background: orange;"></div>
</div>
<!-- Content you want after the sticky element -->
<div>
<!-- The sticky element will appear as if its been placed here -->
<div style="height: 300px; background: green;"></div>
<div style="height: 300px; background: blue;"></div>
<div class="sticky">Sticky Section</div>
</div>

Related

How to make sidebar sticky on page scroll?

How to make sidebar sticky on page scroll?
<div class="header">This is header</div>
<div class="row category-page">
<div class="category-filter col-2">Filter</div>
<div class="category-content col-10">ontent</div>
</div>
<div class="footer">This is footer</div>
I tried to do it with the position:sticky, but the result is not the same:
When the page is scrolled, only the content is scrolled. And when you scroll to the end of the content, the sidebar itself is already scrolling.
.category-filter {
position: sticky;
height: 100%;
top: 0;
}
The following is needed - when the page is scrolled, both the sidebar and the content should be scrolled. And which element ends first, that one should stick to the visible part of the screen.
Preferably in css, since I don't know how to work with js yet.
I really hope that I explained it clearly =))
Sticky position generally works fine in CSS, but it does not in one case: it looks up the node tree (HTML document) and if it sees any overflow: hidden in parent elements, it does not work.
See this codepen when it works.
Example
However, the same example does not work with overflow: hidden:
<div class="container">
<div class="box"></div>
<div class="box2"></div>
</div>
// CSS
.container {
height: 300vh;
width: 100vw;
background-color: aqua;
**overflow: hidden;**
padding: 20rem;
}
.box {
height: 20rem;
width: 5rem;
background-color: orange;
position: sticky;
top: 0;
}

Fixed elements in a fixed navbar

I'm trying to make a navbar that has a fixed position and has elements inside of it that are also fixed:
.navbar {
position: fixed;
top: 0;
bottom: 0;
right: 0;
}
.content-area {
overflow: scroll;
}
.top-area {
height: 100px;
position: fixed;
top: 0;
}
I have two areas that should be fixed to the top and the bottom and a content area in the middle that's overflow scrolls under the bottom area. When I add position: fixed; to .top-area or .bottom-area they disappear to the top. Why can't I fix an element onto the navbar?
The html looks like this:
<div class="navbar">
<div class="top-area">
</div>
<div class="content-area">
<p> Content here </p>
</div>
<div class="bottom-area">
</div>
</div>
Fixed Elements are taken out of the normal flow of html page. Try to set the z-index value to these classes. Give higher e.g. z-index: 100 to those which you want to be on the top.
If you could show your html, I could fix it more precisely for you :).

Inside div will not snap to top of container div

Before you read further, I am utilizing the OneByOne Jquery Slider plugin, the plugin content is what I am attempting to snap to the top of its container div. To read more about OneByOne, here is a link: http://codecanyon.net/item/jquery-onebyone-slider-plugin/684613
I am attempting to snap an inside div to the top of its container div, here is my (what I think is anyway), the related CSS:
#banner{
position: absolute;
}
.oneByOne1{
margin-right: auto;
margin-left: auto;
width: 960px;
height: 420px;
position: relative;
overflow: hidden;
}
#banner .oneByOne_item{
position: absolute;
width: 960px;
height: 420px;
overflow: hidden;
display: none;
}
The div I am attempting to snap to the top is the "banner" div, and the container div is "content", but with the OneByOne plugin, it's recognized as the oneByOne div block coded above, here is my relevant HTML:
<div id="content">
<div id="banner">
<div class="oneByOne_item">
<img src="img/storefront.jpg" class="item_1_1" />
</div>
<div class="oneByOne_item">
<img src="img/livemusic.jpg" class="item_1_2" />
</div>
<div class="oneByOne_item">
<img src="img/brokerecord.jpg" class="item_1_3" />
</div>
</div>
</div>
Here is a link to my current site: http://raider.grcc.edu/~ryanduffing/recordstore/
<div class="search_line"> has position:relative and top: -100px. The image inside has a height of 61px, so the div around it does too.
Because you have it positioned relatively, it is still taking up space in the DOM where it naturally would appear, but your top value is "pulling" it up to where it appears visually. Because it's still taking up space in the DOM, it's pushing your #content down the 61px.
I can't see a good reason to leave it how you've done it, so if you can alter it without breaking anything else, I'd recommend changing it to
.main_header .search_line {
position: absolute;
top: 39px;
right: 0;
}

Using percentages to manipulate height of divs jQuery Mobile

I want to create a content page in jQuery mobile in which I have 2 divs one on top the other within the content area in jquery mobile. The issue is that when the screen size changes the lower div goes on top of the upper div. What I want is that when the screen size changes it maintains the same div structure of one on top the other. I am wondering if I can use a grid but am not sure grids work for 1 column but 2 rows?
My code is below:
CSS
#songName {
text-align:center;
width: 100%;
height: 20px;
}
#songPicture {
text-align:center;
width: 100%;
height: 50%;
}
HTML
<div data-role="content" style="height: 100%">
<div id="songName"></div>
<div id="songPicture"></div>
</div>
Please note the content of the divs comes from JSON and while the id songName is plain text the id songPicture content looks like this :
<img src='"image url+"' style='position: absolute; left: 50%; margin-left: -128px; top: 50%; margin-top: -171px; width: 256px; height: 256px;' />
Use ui-grid-solo class for each div.
<div class='ui-grid-solo'>
Contents
</div>
Reference: http://jquerymobile.com/demos/1.2.0-beta.1/docs/content/content-grids.html

2 divs (filling entire page)

I have one horizontally div on the top of my page with a height of 50px.
And now I want to put another div right below it which will fill the rest of entire page (should work with any kind of resolution).
Does anyone know how to do this only with CSS?
I'd appreciate any help. Thanks!
Although this is not stacking the div's it is very simple solution. Make a div that is 100% height and then place a div inside that is 50px in height.
<body style="height: 100%; width: 100%;">
<div style="height: 100%; width: 100%;">
<div style="height: 50px; width: 100%;">Header</div>
<!--Rest of Content-->
</div>
</body>
There are ways to do this. Here is an example using absolute positioning and a wrapper. Obviously ignore the colors-- they're just there so you can see what's going on.
<body style="margin: 0;height: 100%; background-color: yellow;">
<div style="background-color: green; height: 50px">top stuff</div>
<div style="position: absolute; top: 50px; bottom: 0; left:0; right: 0; background-color: blue">main stuff</div>
</body>
This is an example of a layout that is somewhat problematic with "pure" CSS but trivial with tables.
Firstly there is no way of expressing (ignoring CSS expressions, which you tend to want to avoid) "rest of the page" or "100% minus 50px" so the general solution to this problem is.
Create a container that is 100% height;
Put the header at height 50px;
The content simply takes up the rest of the space. Any styling is applied to the container not the content.
So:
<div id="container">
<div id="header"></div>
<div id="content"></div>
</div>
with:
html, body. #container { height: 100%; }
#container { height: 100%; min-height: 100%; }
#header { height: 50px; }
It gets trickier if you want a footer. That is typically positioned absolutely at the bottom and padding is used on the container so nothing appears under it.

Resources