How can I make the green background to cover all the .section class?
I have this structure:
section // one section will have one container
->container // one container can have multiple elements
->elements // the background class set here should override the section
relation everywhere and I would like to define a "background" element that covers the whole section.
Note:
I am getting the data like(section/container/elements) this and it is not going to change.
There is possible to have multiple sections each with it's own background.
.section {
width: 100%;
background-color: yellow;
}
.container {
height: 300px;
padding: 30px;
margin: 100px;
background-color: red;
}
.background {
width: 100%;
height: 100%;
}
.red {
background: red;
}
.green {
background: green;
}
<div class="section">
<div class="container">
<div class="background green" />
<div>
some content
</div>
</div>
</div>
Link to jsfiddle
you Should style give tag section
background-color: green;
You could set the position of .section to relative, and set the position of .green to absolute.
Now the position, width & height will use .section as reference.
Then you could set these properties freely.
here is the code:
.section{
position: relative;
}
.green {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
and then you may have to adjust their z-index
.green {
z-index: -1;
}
.section {
z-index: 0;
}
absolute: The element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to its closest positioned ancestor, if any; otherwise, it is placed relative to the initial containing block. Its final position is determined by the values of top, right, bottom, and left. MDN
One approach, with explanatory comments in the code, is as below:
/* simple reset to ensure no browser-default padding/margin exists,
and forces all element-size calculations to include border-width
and padding inside the defined width: */
*,::before, ::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.section {
/* all values here are purely aesthetic, adjust to your own
requirements: */
border: 2px solid #000;
height: 80vmin;
width: 80vmin;
margin-block: 1em;
margin-inline: auto;
}
.container {
/* here we use grid layout on the .container element(s): */
display: grid;
/* we specify one grid-column and one grid-row, each of which
take 100% of the available space: */
grid-template-columns: 1fr;
grid-template-rows: 1fr;
/* sizing the grid to be 100% of the parent's width/height: */
width: 100%;
height: 100%;
}
/* all <div> elements that are children of the .container: */
.container > div {
/* are placed in the first grid-column and first grid-row: */
grid-column: 1;
grid-row: 1;
/* specifying a max-height and max-width of 100% to address
potential overflow; obviusly adjust to your requirements: */
max-height: 100%;
max-width: 100%;
}
/* styling the element that will hold the contents (so far as your
demo shows): */
.container > div:last-child {
/* hiding overflow on x-axis: */
overflow-x: hidden;
/* allowing scrolling on the y-axis to see
overflowing content: */
overflow-y: scroll;
/* defining some white-space between the element edges and contents
within: */
padding: 0.5em;
}
.blue {
background-color: hsl(180 65% 81% / 0.5);
}
.green {
background-color: hsl(120 93% 79% / 0.5);
}
.yellow {
background-color: hsl(60 100% 50% / 0.5);
}
<div class="section">
<div class="container">
<div class="background blue" />
<div>
Some very short content
</div>
</div>
</div>
<div class="section">
<div class="container">
<div class="background green" />
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat corporis hic sunt nam perferendis assumenda asperiores nobis quae consequatur aut ut quidem id, optio consequuntur ipsa labore consectetur nihil incidunt.</p>
<p>Minima quibusdam hic culpa, ea porro vitae asperiores! Vitae odit, delectus nesciunt asperiores maxime. Voluptatum velit repellendus in. Impedit laudantium asperiores nostrum neque laboriosam eius libero fugiat itaque inventore nulla?</p>
<p>Laboriosam quod, facilis ratione quas consequatur quis quisquam vel eius velit eveniet, excepturi voluptatem ea dicta quam eaque praesentium sequi illum, optio animi! Libero minima iste, maiores temporibus reiciendis repellendus.</p>
<p>Blanditiis reiciendis eveniet perspiciatis excepturi, nulla eum, rem atque. Perspiciatis quae sed autem officiis error, earum officia, nulla quod, velit deleniti pariatur. Obcaecati inventore, in adipisci eveniet, expedita a quaerat.</p>
<p>Quaerat labore vitae iure expedita eaque nostrum distinctio et, atque velit quisquam corporis animi, aut voluptatem itaque odio ipsum. Iusto asperiores eius at quos totam voluptatum, ratione voluptate cum quas.</p>
<p>Sed minima aliquid voluptatum minus dolores, iusto, ducimus doloribus! Aliquam quae, atque! Iusto nobis alias magnam minima, impedit iste illo iure illum doloremque debitis! Ipsa assumenda soluta fugiat ut esse.</p>
<p>Sint voluptatum suscipit laborum, aliquid eveniet odit blanditiis corporis voluptatibus, rem consectetur quam quidem saepe, doloremque, sit nihil rerum voluptas deleniti a expedita quo quod beatae modi. Incidunt, aliquid. Ea.</p>
<p>Minus, dicta sed maiores amet necessitatibus eaque magnam asperiores, aperiam, qui earum numquam sequi eius provident officiis. Illum fuga earum architecto nesciunt ut beatae mollitia quod sunt nulla. Ipsum, modi?</p>
<p>Eligendi, officiis maxime cupiditate consectetur voluptates est ullam odio, porro sint cum suscipit rerum nisi quibusdam non enim repudiandae facere fugiat repellendus possimus, sequi excepturi culpa iusto. Velit, sunt deleniti!</p>
<p>Ipsa deserunt dolorum molestias reiciendis. Ad dolorum eius suscipit, sapiente laboriosam dolor sequi, accusamus id consequuntur modi aliquid, architecto fuga illum non voluptatum quod. Molestiae deserunt maxime suscipit tenetur, tempora.</p>
<p>Id non fugit in vitae. Fuga voluptates sapiente, impedit atque, quasi, consequatur autem sunt dignissimos unde nam quam! Officiis quos, distinctio facilis illum explicabo incidunt quia pariatur minima, enim at.</p>
<p>Commodi consectetur repellendus ad assumenda eius voluptatibus tempora dolor et possimus quaerat aperiam nesciunt qui optio, sapiente ab libero iste, recusandae, suscipit obcaecati accusamus perferendis minus molestias. Nisi, inventore, explicabo?</p>
</div>
</div>
</div>
<div class="section">
<div class="container">
<div class="background yellow" />
<div>
Some very short content
</div>
</div>
</div>
JS Fiddle demo.
References:
box-sizing.
CSS Pseudo-elements.
display.
grid-column.
grid-row.
grid-template-columns.
grid-template-rows.
hsl().
:last-child.
margin.
margin-inline.
margin-block.
max-height.
max-width.
overflow.
overflow-x.
overflow-y.
padding.
vmin.
width.
Related
I'm trying to find a way to make an element resizable so that it will simultaneously reveal more of the element's content as the height increases while covering the content of the other element that it is taking space from.
An example of this is of Gmail's left sidebar, these two images show before and after the resize drag: Sidebar Before - Sidebar After
I was thinking it could be done with the CSS resize property, but while the lower element can be resized downwards, it won't resize upwards taking space from the above element, and the resize grabber also seems to be difficult to change its position from the bottom right to the top.
Thank you in advance.
You can use the resize property together with a flex setting that causes the other element to occupy the rest of the space.
html,
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
height: 80vh;
margin:1em;
}
.container>div {
padding: 1em;
border: 2px solid rgba(0, 0, 0, 0.5);
overflow: auto;
}
.top {
resize: vertical;
height: 50%;
}
.bottom {
flex: 1;
}
<div class="container">
<div class="top">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fuga repellat laborum maxime ipsam sed quisquam, numquam eligendi doloribus non nisi unde vero deleniti velit dolore nesciunt error, sapiente provident at sint sunt! Cumque nostrum, placeat nisi,
minima, maiores molestiae quas praesentium mollitia hic natus suscipit nemo accusantium, beatae itaque rem aut omnis? Quisquam, natus dicta eveniet! Voluptates repellat, assumenda fugit. At, culpa quos nulla. Dolorem et modi quisquam velit quaerat
aspernatur iste natus eum magnam vero, id eius expedita quo placeat maiores enim quas iusto amet dolor ullam aperiam sequi laboriosam. Omnis voluptas, a exercitationem cum quidem quaerat ullam deleniti.
</div>
<div class="bottom">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet a magni culpa quidem in et facilis, ducimus voluptate explicabo saepe repellat, eius sequi molestias voluptatum! Maiores animi quasi quia nisi, pariatur id aperiam! Veritatis error molestias
minima modi suscipit ipsam eius sed perspiciatis est nobis illo, incidunt itaque harum, distinctio?
</div>
</div>
I've been trying to create a 3 column layout containing cards that have a width of 1, 2 or 3 columns. To do this I've used CSS Grid with
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
My full code is here: Pen of the layout
I get some strange behaviour when the width of the container grows and shrinks. I would expect new columns to be added or removed based on the availability to add a new column of the minimum width (in this case 300px). THis doesn't seem to be happening - new columns are being added with a width <300px.
My SCSS is as follows:
.grid{
max-width:1020px;
margin:0 auto;
padding:10px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-auto-flow: dense;
grid-gap:10px;
}
.card{
padding: 5px;
background: #CCC;
grid-column: auto / span 1;
&:nth-child(2), &:nth-child(5) {
grid-column: auto / span 2;
}
&:nth-child(1), &:nth-child(6) {
grid-column: 1 / end;
}
}
Am I trying to do something that is beyond the capabilities of CSS Grid or am I doing something wrong in my code?
The issue is the implicit grid your are creating by defining grid-column:auto / span 2 which means that the element will take 2 columns so the grid need to at least contain 2 columns. When you the width is under 300px you explicitely create one column and the browse will implicitely create another one.
.grid {
max-width: 1020px;
margin: 0 auto;
padding: 10px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-auto-flow: dense;
grid-gap: 10px;
}
.card {
padding: 5px;
background: #CCC;
grid-column: auto / span 1;
}
.card:nth-child(2), .card:nth-child(5) {
/*grid-column: auto / span 2;*/
}
.card:nth-child(1), .card:nth-child(6) {
grid-column: 1 / end;
}
<p>Expected that new columns would only appear when 300px were available, but that doesn't seem to happen. New columns appear with much less available. Why???
<div class="grid">
<div class="card"><h1>grid-column: 1 / end</h1>Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam earum eum at nemo, illo voluptatem inventore, eveniet praesentium deleniti minus omnis saepe vitae explicabo similique natus? Est magnam aut veritatis?</div>
<div class="card"><h1>grid-column: auto / span 2;</h1>Id at accusantium, nisi error ipsa debitis corporis laudantium harum, dolorem odio beatae ad porro ullam perferendis tenetur odit eligendi, quisquam quasi rem? Placeat dolorum totam dignissimos tempore quia dolore?</div>
<div class="card"><h1>grid-column: auto / span 1;</h1>Provident maxime vitae perspiciatis voluptate quos rerum vel illo quas deleniti, voluptatem labore quibusdam. Eligendi, dolore, reprehenderit labore ipsum ipsam quod, nulla nihil harum dolor ipsa debitis quos officiis sed!</div>
<div class="card"><h1>grid-column: auto / span 1;</h1>Fugiat minus sequi vel commodi cum inventore in quae alias fuga quis voluptates perferendis nostrum tempore a maxime voluptas illo, officiis harum ipsam qui recusandae esse fugit asperiores. Architecto, eveniet.</div>
<div class="card"><h1>grid-column: auto / span 2;</h1>Quaerat delectus sint cumque inventore corporis alias consequatur totam nemo? Excepturi totam voluptatem voluptate! Exercitationem possimus amet voluptas corporis autem maiores nesciunt deserunt delectus! Ex praesentium ea debitis laborum doloribus.</div>
<div class="card"><h1>grid-column: 1 / end;</h1>Illo inventore perferendis officia nisi voluptatum temporibus nemo laudantium fuga suscipit? Aliquid nihil rem obcaecati vitae placeat temporibus cumque nostrum illum cum, ab dicta sequi voluptatum saepe, ut, voluptatibus suscipit.</div>
</div>
Removing this you will still have issue because of grid-column: 1 / end; which means that start from column 1 to the area named end but you didn't specify any area with that name so the browser will implicitely create one.
You can clearly notice for the above example that the last column is not following the logic of minmax(300px, 1fr) because it's the column created for end
I suspect you want to use grid-column: 1 / -1; which mean from 1 until the end:
.grid {
max-width: 1020px;
margin: 0 auto;
padding: 10px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-auto-flow: dense;
grid-gap: 10px;
}
.card {
padding: 5px;
background: #CCC;
grid-column: auto / span 1;
}
.card:nth-child(2), .card:nth-child(5) {
/*grid-column: auto / span 2;*/
}
.card:nth-child(1), .card:nth-child(6) {
grid-column: 1 / -1;
}
<p>Expected that new columns would only appear when 300px were available, but that doesn't seem to happen. New columns appear with much less available. Why???
<div class="grid">
<div class="card"><h1>grid-column: 1 / end</h1>Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam earum eum at nemo, illo voluptatem inventore, eveniet praesentium deleniti minus omnis saepe vitae explicabo similique natus? Est magnam aut veritatis?</div>
<div class="card"><h1>grid-column: auto / span 2;</h1>Id at accusantium, nisi error ipsa debitis corporis laudantium harum, dolorem odio beatae ad porro ullam perferendis tenetur odit eligendi, quisquam quasi rem? Placeat dolorum totam dignissimos tempore quia dolore?</div>
<div class="card"><h1>grid-column: auto / span 1;</h1>Provident maxime vitae perspiciatis voluptate quos rerum vel illo quas deleniti, voluptatem labore quibusdam. Eligendi, dolore, reprehenderit labore ipsum ipsam quod, nulla nihil harum dolor ipsa debitis quos officiis sed!</div>
<div class="card"><h1>grid-column: auto / span 1;</h1>Fugiat minus sequi vel commodi cum inventore in quae alias fuga quis voluptates perferendis nostrum tempore a maxime voluptas illo, officiis harum ipsam qui recusandae esse fugit asperiores. Architecto, eveniet.</div>
<div class="card"><h1>grid-column: auto / span 2;</h1>Quaerat delectus sint cumque inventore corporis alias consequatur totam nemo? Excepturi totam voluptatem voluptate! Exercitationem possimus amet voluptas corporis autem maiores nesciunt deserunt delectus! Ex praesentium ea debitis laborum doloribus.</div>
<div class="card"><h1>grid-column: 1 / end;</h1>Illo inventore perferendis officia nisi voluptatum temporibus nemo laudantium fuga suscipit? Aliquid nihil rem obcaecati vitae placeat temporibus cumque nostrum illum cum, ab dicta sequi voluptatum saepe, ut, voluptatibus suscipit.</div>
</div>
To better illustrate both issues, here is a simplifed example for the first one:
.box {
display:grid;
grid-template-columns:100px; /* I defined one column*/
grid-gap:10px;
}
.box span:first-child {
grid-column:span 2; /* I will create another column*/
}
.box span {
height:50px;
background:red;
}
<div class="box">
<span></span>
<span></span>
</div>
And for the second one:
.box {
display:grid;
grid-template-columns:100px; /* I defined one column*/
grid-gap:10px;
}
.box span:first-child {
grid-column:1/ a_radom_name; /* I will create another column*/
}
.box span {
height:50px;
background:red;
}
.box span:last-child {
grid-column-end:a_radom_name; /* I can place other element on that column */
}
<div class="box">
<span></span>
<span></span>
</div>
If you inspect the grid you will notice that we will end with 2 columns (one implicit and one explicit)
The three properties grid-template-rows, grid-template-columns, and grid-template-areas together define the explicit grid of a grid container. ref
When grid items are positioned outside of these bounds, the grid container generates implicit grid tracks by adding implicit grid lines to the grid. These lines together with the explicit grid form the implicit grid. ref
I have a container with lots of content and hence I have set overflow: auto; to make it scrolling.
I also need a button in the bottom-right corner of this container at a fixed position (similar to a FAB button in Material Design).
The button is nicely placed in the bottom-right of the container. Unfortunately when I scroll the content, the button moves with it. My understanding of absolute positioning is that it will position the element relative to its parent (with position: relative;). So why is this button moving? What's the right way to fix it to the parent?
The expected behavior is that the button stays in the bottom-right corner regardless of the scroll position. For an example, see this page and search for the phrase "Animation of toolbar off-screen during scrolling".
Note that I am looking to make this work with absolute positioning, not fixed positioning. The button should be in bottom-right corner of its container, not matter how deeply the container is nested in the browser window.
* {
box-sizing: border-box;
}
.container {
position: relative;
overflow: auto;
height: 256px;
width: 256px;
margin-right: 16px;
border: solid 1px red;
padding: 4px;
}
.fab {
position: absolute;
right: 20px;
bottom: 20px;
color: white;
background-color: blue;
}
<div class="container">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate quidem.
<Button class="fab">
Ok
</Button>
</div>
Please see my JSFiddle here
There are many ways of achieving this, but I would prefer wrapping the .container with a .container-parent of the same height and width, and position: relative. This gives you a non-scrolling element to position the .fab button against.
<div class="container-parent">
<div class="container">
Lorem ipsum dolor sit amet...
</div>
<Button class="fab">
Ok
</Button>
</div>
With CSS:
.container-parent {
position: relative;
height: 256px;
width: 256px;
}
.fab {
position: absolute;
bottom: 20px;
right: 20px;
}
Here's the fiddle: http://jsfiddle.net/t702Lknz/4/
Alternatively, you could use your original markup, with .fab having a fixed position. The CSS would be like:
.fab {
position: fixed;
top: 225px;
left: 190px;
}
Here's the fiddle for that: http://jsfiddle.net/t702Lknz/3/
You can do this by adding a wrapper div for the text. The wrapper div will its width and height set to 100% and overflow set to auto, so it will have the scroll box and your FAB can be positioned within the outer div like so:
* {
box-sizing: border-box;
}
.fab {
position: absolute;
right: 20px;
bottom: 20px;
}
.container {
position: relative;
border: solid 1px red;
height: 256px;
width: 256px;
float: left;
margin-right: 16px;
}
.text-wrapper {
overflow: auto;
height: 100%;
width: 100%
}
<div class="container">
<div class="text-wrapper">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus
maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio
cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate
quidem.
</div>
<Button class="fab">
Ok
</Button>
</div>
I finally figured out how to do this. Since only the text should scroll, it should be inside its own scrolling container. This makes the button latch on to its parent without being affected by the scrolling. Here's the final code.
* { box-sizing: border-box; }
.container {
/* keep the add button in a fixed location relative to this container */
position: relative;
display: flex;
flexDirection: column;
height: 200px;
width: 200px;
margin-right: 16px;
border: solid 1px red;
padding: 4px;
}
.text-container {
/* ensure that only the text scrolls, not the button */
flex: 1;
overflow: auto;
}
.fab {
position: absolute;
right: 20px;
bottom: 20px;
color: white;
background-color: blue;
}
<div class="container">
<div class="text-container">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate quidem.
</div>
<Button class="fab">
Ok
</Button>
</div>
I've got a scrollable container. I want an overlay to stick to the bottom of this container, but it should only cover the contents of the scrollable area, it should not cover the scrollbar.
Desired result:
With my current solution the problem is that the overlay also covers the scrollbar.
I COULD solve the issue by setting pixel values for 'width' and 'right' of the overlay. But I don't want to have to do that.
.container {
position: relative;
border: solid 1px red;
height: 100px;
width: 200px;
float: left;
margin-right: 16px;
}
.scrollable {
height: 100%;
overflow-y: scroll;
}
.overlay {
position: absolute;
bottom: 0;
right: 0;
width: 100%;
height: 30px;
background: rgba(100, 200, 10, 0.5);
}
<section class="container">
<div class="scrollable">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus
maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio
cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate
quidem.
</div>
<div class="overlay"></div>
</section>
.container {
position: relative;
border: solid 1px red;
height: 100px;
width: 200px;
float: left;
margin-right: 16px;
}
.scrollable {
height: 100%;
overflow-y: scroll;
}
.scrollable p {
z-index:-2;
position:relative;
}
.overlay {
position: absolute;
bottom: 0;
right: 0;
left: 0;
width:100%;
height: 30px;
background: rgba(100, 200, 10, 0.5);
z-index:-1;
}
<section class="container">
<div class="scrollable">
<p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus
maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio
cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate
quidem.</p>
</div>
<div class="overlay"></div>
</section>
I checked your codes.
Please try it.
.overlay {z-index: -1;}
position:sticky; could be used :
(quotes from sitepoint introduction to sticky)
See position: sticky on Can I Use… for all the details.
Fortunately there a number of polyfills to choose from:
fixed-sticky by the Filament Group (requires jQuery)
position–sticky- by Matthew Phillips (Uses Modernizr for detection)
position: sticky, part of Philip Walton’s Polyfill.js library
position: sticky CodePen demo by Fabrice Weinberg (requires jQuery)
Stickyfill by Oleg Korsunsky (IE9+)
.container {
position: relative;
border: solid 1px red;
height: 100px;
width: 200px;
float: left;
margin-right: 16px;
}
.scrollable {
height: 100%;
overflow-y: scroll;
position: relative;
}
.overlay {
position: sticky;
bottom: 0;
right: 0;
width: 100%;
height: 30px;
background: rgba(100, 200, 10, 0.5);
}
<section class="container">
<div class="scrollable">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus
maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio
cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate
quidem.
<div class="overlay"></div>
</div>
</section>
Scrollbar is average 1em wide, you can give a try using only coordonates to size the width of this overlay.
.container {
position: relative;
border: solid 1px red;
height: 100px;
width: 200px;
float: left;
margin-right: 16px;
}
.scrollable {
height: 100%;
overflow-y: scroll;
}
.overlay {
position: absolute;
bottom: 0;
right: 1em;
left:0;
height: 30px;
background: rgba(100, 200, 10, 0.5);
}
<section class="container">
<div class="scrollable">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus
maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio
cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate
quidem.
</div>
<div class="overlay"></div>
</section>
screenshot in Chrome:
What should I expect? I expect the aside to grab the bottom of the viewport by 30px as you scroll down.
You need to run this in Firefox as it's the only browser that supports position: sticky.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
line-height: 1.5;
color: #fff;
padding: 3rem;
}
p {
padding: 30px;
}
img {
max-width: 100%;
display: block;
margin-bottom: 30px;
}
img:last-of-type {
margin-bottom: 0;
}
section {
background: #00ff7f;
max-width: 700px;
min-height: 1000px;
margin: auto auto 30px;
}
section:after {
content: '';
display: table;
clear: both;
}
article {
background: #1e90ff;
padding: 30px;
float: left;
width: calc(100% * 2 / 3 - 30px);
margin-right: 30px;
}
aside {
background: #ff6347;
float: left;
width: calc(100% * 1 / 3);
position: -webkit-sticky;
position: sticky;
bottom: 30px;
}
<section>
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, quas? Maxime dolore explicabo nostrum quidem suscipit eveniet libero quam voluptatibus, recusandae exercitationem assumenda voluptates nulla vel temporibus, fuga possimus dolores officiis veritatis quae maiores nisi impedit itaque? Quos optio libero, non nostrum iste, quo natus ex, eos sint itaque omnis? Illum eos, ducimus accusamus totam quasi consequuntur consectetur excepturi temporibus blanditiis aut laborum esse dolorum modi, explicabo illo eligendi cum architecto veniam?
</article>
<aside>
<img src="http://placekitten.com/404/404">
<img src="http://placekitten.com/404/404">
<img src="http://placekitten.com/404/404">
</aside>
</section>
<section>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur sunt, unde fuga, ex tempora nam molestiae autem maiores ducimus, debitis, impedit mollitia laboriosam! Nulla esse voluptatibus laudantium ipsa illo adipisci pariatur, nostrum perferendis quo saepe magnam, veritatis commodi doloribus inventore aspernatur cupiditate assumenda. Cupiditate porro dolorum quo magni ipsum adipisci sit repellendus dolore voluptatibus, omnis enim unde velit quidem expedita iusto modi maxime placeat reprehenderit dignissimos fuga ad atque soluta. Soluta velit natus qui ad hic suscipit dolorum vitae minus quisquam necessitatibus asperiores illum sed dolorem debitis, laborum, praesentium quos dicta molestiae. Iusto animi perferendis dignissimos ut necessitatibus nostrum earum pariatur velit reiciendis quibusdam veritatis neque saepe, mollitia, unde ab ipsam.
</p>
</section>
http://codepen.io/corysimmons/pen/adjabm?editors=1100
I expect the aside to grab the bottom of the viewport by 30px as you scroll down.
You're close. Sticky positioning means that the aside will hug the bottom of the viewport, but only when it would otherwise exceed the bounds of the viewport. Otherwise, it will behave like a regular, relatively positioned box. This is what distinguishes position: sticky from position: fixed — the latter means the box always hugs the bottom of the viewport, regardless of its surrounding layout and of the scroll position.
Because both your article and aside are floating, the aside element doesn't ever get pushed past the bottom of the viewport, and so it doesn't get a chance to stick to the viewport.
Removing the float declaration from the article element — thereby keeping it in the flow — allows it to push the aside beyond the viewport. Note that if the aside is taller than the viewport (depending on how you view the snippet), a bottom sticky position will scroll as far as it needs to get 30px (the value of bottom) away from the viewport before it begins to stick, after which it will scroll away once the bottom of its containing block (the section) exceeds 30px distance from the viewport.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
line-height: 1.5;
color: #fff;
padding: 3rem;
}
p {
padding: 30px;
}
img {
max-width: 100%;
display: block;
margin-bottom: 30px;
}
img:last-of-type {
margin-bottom: 0;
}
section {
background: #00ff7f;
max-width: 700px;
margin: auto auto 30px;
}
section:after {
content: '';
display: table;
clear: both;
}
article {
background: #1e90ff;
padding: 30px;
width: calc(100% * 2 / 3 - 30px);
margin-right: 30px;
}
aside {
background: #ff6347;
float: left;
width: calc(100% * 1 / 3);
position: -webkit-sticky;
position: sticky;
bottom: 30px;
}
<section>
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, quas? Maxime dolore explicabo nostrum quidem suscipit eveniet libero quam voluptatibus, recusandae exercitationem assumenda voluptates nulla vel temporibus, fuga possimus dolores officiis veritatis quae maiores nisi impedit itaque? Quos optio libero, non nostrum iste, quo natus ex, eos sint itaque omnis? Illum eos, ducimus accusamus totam quasi consequuntur consectetur excepturi temporibus blanditiis aut laborum esse dolorum modi, explicabo illo eligendi cum architecto veniam?
</article>
<aside>
<img src="http://placekitten.com/404/404">
<img src="http://placekitten.com/404/404">
<img src="http://placekitten.com/404/404">
</aside>
</section>
<section>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur sunt, unde fuga, ex tempora nam molestiae autem maiores ducimus, debitis, impedit mollitia laboriosam! Nulla esse voluptatibus laudantium ipsa illo adipisci pariatur, nostrum perferendis quo saepe magnam, veritatis commodi doloribus inventore aspernatur cupiditate assumenda. Cupiditate porro dolorum quo magni ipsum adipisci sit repellendus dolore voluptatibus, omnis enim unde velit quidem expedita iusto modi maxime placeat reprehenderit dignissimos fuga ad atque soluta. Soluta velit natus qui ad hic suscipit dolorum vitae minus quisquam necessitatibus asperiores illum sed dolorem debitis, laborum, praesentium quos dicta molestiae. Iusto animi perferendis dignissimos ut necessitatibus nostrum earum pariatur velit reiciendis quibusdam veritatis neque saepe, mollitia, unde ab ipsam.
</p>
</section>