How to make position sticky work with this structure? - html

I imagine CSS looks out of place in my example but these styles are added by library, particularily styles in #top, #middle, #bottom. I need .header elements to stick to top.
I've tried all kinds of styles but no luck. Feel free to overwrite or add any styles. How to make this work with the HTML structure I have?
#top {
display: flex;
position: relative;
z-index: 0;
overflow: hidden !important;
max-height: 300px;
max-width: 300px;
border: 2px dashed #ec6161;
}
#middle {
padding-right: 19px;
margin-bottom: -34px;
overflow: scroll;
overflow-x: hidden !important;
min-width: 100%! important;
max-height: inherit !important;
box-sizing: content-box !important;
}
#bottom {
padding-bottom: 17px;
margin-right: -19px;
overflow: scroll;
overflow-y: hidden !important;
box-sizing: border-box !important;
min-height: 100% !important;
}
.header {
position: sticky;
top: 0;
z-index: 1;
background: #eee;
padding: 5px 7px;
font-weight: bold;
}
.content {
height: 100px;
padding: 5px 7px;
}
<div id="top">
<div id ="middle">
<div id="bottom">
<div>
<div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
</div>
</div>
</div>
</div>
Also: https://jsfiddle.net/bkn0e3gL/2/

Setting overflow: hidden on any parent divs seems to be causing the problem.
Check out this answer:
if you set overflow to hidden on any ancestor of your sticky element, then this ancestor element will be the scrolling container for your sticky element.
.header {
background: #B8C1C8;
border-bottom: 1px solid #989EA4;
border-top: 1px solid #717D85;
color: #FFF;
position: -webkit-sticky;
position: sticky;
top: -1px;
}
.content {
min-height: 200px;
}
#bottom {
padding-bottom: 17px;
margin-right: -19px; //overflow: scroll;
//overflow-y: hidden !important;
box-sizing: border-box !important;
min-height: 100% !important;
}
#middle {
padding-right: 19px;
margin-bottom: -34px; //overflow: scroll;
//overflow-x: hidden !important;
min-width: 100% ! important;
max-height: inherit !important;
box-sizing: content-box !important;
}
#top {
display: flex;
position: relative;
z-index: 0;
//overflow: hidden !important;
max-height: 300px;
max-width: 300px;
border: 2px dashed #ec6161;
}
<div id="top">
<div id="middle">
<div id="bottom">
<div>
<div>
<div class="header">Header</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque viverra.</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque viverra.</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque viverra.</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque viverra.</div>
</div>
</div>
</div>
</div>
</div>

Related

How do I create a border over div with content?

I have to create a div over which there is a 3px border, and this boundary is positioned over the content in the div, how can I do this without knowing the size of the block?
An example is in the image below:
My code: https://codepen.io/pen/yLObXvv
HTML:
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-md-4">
<div class="case-study">
<div class="case-study-image">
<img src="https://images.unsplash.com/photo-1551434678-e076c223a692?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" class="img-fluid" alt="Intro image"/>
</div>
<div class="case-study-content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
</div>
</div>
</div>
</div>
CSS:
body {
background-color: #04142d;
}
.case-study {
color: #fff;
display: flex;
margin-top: 2rem;
flex-direction: row;
background-color: #0E53DD;
border-radius: 1rem;
overflow: hidden;
}
.case-study-image {
flex: 0 0 50%;
width: 50%;
}
.case-study-image img {
height: 100%;
max-height: 20rem;
object-fit: cover;
oject-position: 0 0;
}
.case-study-content {
flex: 0 0 50%;
padding: 1rem;
}
Please Use CSS ::after Selector with position: absolute
The coordinates of an absolute positioned element are relative to its parent. It is positioned automatically to the starting point (top-left corner) of its parent element.
body {
background-color: #04142d;
}
.case-study {
color: #fff;
display: flex;
margin-top: 2rem;
flex-direction: row;
background-color: #0E53DD;
border-radius: 1rem;
position:relative;
max-width:500px;
}
img {
max-width:100%;
}
.case-study-image {
flex: 0 0 50%;
width: 50%;
}
.case-study-image img {
height: 100%;
max-height: 20rem;
object-fit: cover;
oject-position: 0 0;
}
.case-study-content {
flex: 0 0 50%;
padding: 1rem;
}
.case-study:after {
content: '';
border: 3px solid yellow;
position: absolute;
width: calc(100% - 6px);
height: calc(100% - 6px);
border-radius: 5px;
right: -10px;
top: -10px;
}
<div class="case-study">
<div class="case-study-image"> <img src="https://images.unsplash.com/photo-1551434678-e076c223a692?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" class="img-fluid" alt="Intro image"/> </div>
<div class="case-study-content">
<p>Lorem ipsum dolor sit amet</p>
</div>
</div>
<!DOCTYPE html>
<html>
<head>
<title>style</title>
<style>
div {
width: 200px;
height: 200px
background-color:white;
}
.move {
transform: (30px, 180px)
background: transparent;
border: 3px solid red;
}
</style>
</head>
<body>
<div class="move"></div>
<div></div>
</body>
</html>
you can use this and try to style it the way you want

How to make child element overlay the parent element?

I am using only CSS and Flexbox to build a responsive page. I have a child element that should "overflow" outside the parent element as shown here:
<div class="container-hero">
<div class="hero-content">
<h1>Tech Challenge</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit </p>
</div>
<div class="hero-img">
<img src="assets/image-1.jpg">
</div>
</div>
CSS
.container-hero {
display: flex;
justify-content: flex-start;
align-items: center;
overflow: hidden;
position: relative;
margin: 40px 0;
}
.hero-img {
flex-shrink: 0;
min-width: 100%;
min-height: 100%;
}
.hero-img img {
width: 100%;
height: auto;
}
.hero-content {
background-color: #D64C31;
color: #FFFFFF;
align-self: flex-end;
width: 50%;
position: absolute;
bottom:0;
left:0;
padding: 40px 60px;
}
Any help would be appreciated!
Like that?
<html>
<head>
<style>
.container {
background: #ccc;
width: 50%;
margin: 0 auto;
position: relative;
height: 700px;
}
.overflowing-element {
background: red;
width: 200px;
height: 200px;
position: absolute;
right: -200px;
top: 0;
}
</style>
</head>
<body>
<div class="container">
test
<div class="overflowing-element">
bla
</div>
</div>
</body>
</html>
Just works with fixed width of that overflowing element, or with JavaScript.
EDIT: You just edited your images and now I don't know really what you mean :D
I figure it out, thank you for your help!
My parent element had an overflow: hidden I disabled it and adjusted the child element as follows:
bottom: -40px
If you have any feedback or this is considered a bad practice please let me know. I am just starting out here :)
.container-hero {
display: flex;
justify-content: flex-start;
align-items: center;
/* overflow-x: hidden; */
position: relative;
margin: 40px 0;
}
.hero-img {
flex-shrink: 0;
min-width: 100%;
min-height: 100%;
}
.hero-img img {
width: 100%;
height: auto;
}
.hero-content {
position:absolute;
background-color: #D64C31;
color: #FFFFFF;
width: 50%;
padding: 40px 60px;
bottom: -20px;
left:0;
}
</div>
<div class="container-hero">
<div class="hero-content">
<h1>Tech Challenge</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit </p>
</div>
<div class="hero-img">
<img src="https://source.unsplash.com/800x300">
</div>
</div>
The property you are looking for is CSS Position.
Reference: CSS Position
.parent{
width:250px;
height: 20px;
background: yellow;
position:relative;
}
.child{
width:80px;
height: 100px;
background: purple;
position:absolute;
bottom: 0;
right:0;
}
<div class="parent">
<div class="child"></div>
</div>
Use the CSS positioning properties.
.container-hero {
position: relative; /* creates the container for absolutely positioned children */
}
.hero-content {
position: absolute;
bottom: -20px; /* use this offset to align vertically */
left: 20px; /* use this offset to align horizontally */
background-color: #D64C31;
color: #FFFFFF;
width: 225px;
padding: 40px 60px;
}
<div class="container-hero">
<div class="hero-content">
<h1>Tech Challenge</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit </p>
</div>
<div class="hero-img">
<img src="https://via.placeholder.com/500x250.png?text=hero image">
</div>
</div>

Horizontal scroll on one child element in parent

I have a page with several sections (see code snippet or JSFiddle example).
The user should only be able to scroll vertically on the page.
Now, aside from that, I would like to horizontally scroll through only one section: the projects (without affecting the rest of the page). That means that, apart from the projects section, the rest will have to stay still.
How could I achieve this partial horizontal scroll?
.container {
width: 500px;
height: 100%;
overflow-x: scroll;
background-color: lightgrey;
padding: 20px;
}
.projects {
width: 960px;
}
.project {
display: inline-block;
float: left;
width: 300px;
margin: 0 20px 20px 0;
background-color: grey;
}
<div class="container">
<h1>Page</h1>
<div class="main">
<h2>Main</h2>
<p>Lorem ipsum dolor sit amet.</p>
</div>
<div class="projects">
<h2>Projects</h2>
<div class="project">
<p>Project</p>
</div>
<div class="project">
<p>Project</p>
</div>
<div class="project">
<p>Project</p>
</div>
</div>
</div>
Try this.
.projects .project-inwrap {
width: 100%;
white-space: nowrap;
overflow: auto;
}
.container {
width: 500px;
height: 100%;
overflow-x: scroll;
background-color: lightgrey;
padding: 20px;
}
.projects .project-inwrap {
width: 100%;
white-space: nowrap;
overflow: auto;
}
.project {
display: inline-block;
width: 300px;
margin: 0 20px 20px 0;
background-color: grey;
}
<div class="container">
<h1>Page</h1>
<div class="main">
<h2>Main</h2>
<p>Lorem ipsum dolor sit amet.</p>
</div>
<div class="projects">
<h2>Projects</h2>
<div class="project-inwrap">
<div class="project">
<p>Project</p>
</div>
<div class="project">
<p>Project</p>
</div>
<div class="project">
<p>Project</p>
</div>
</div>
</div>
</div>
Try this
*{
box-sizing: border-box;
}
body{
overflow: hidden;
}
.container {
width: 500px;
height: 100vh;
overflow-x: hidden;
background-color: lightgrey;
padding: 20px;
}
.projects {
width: 100%;
white-space: nowrap;
overflow: auto;
}
.project {
display: inline-block;
width: 300px;
margin: 0 20px 20px 0;
background-color: grey;
}
<div class="container">
<h1>Page</h1>
<div class="main">
<h2>Main</h2>
<p>Lorem ipsum dolor sit amet.</p>
</div>
<div class="projects">
<h2>Projects</h2>
<div class="project">
<p>Project</p>
</div>
<div class="project">
<p>Project</p>
</div>
<div class="project">
<p>Project</p>
</div>
</div>
</div>

div above div (both bottom: 0px;)

I want to align elements inside div
a - > to the top
b - > to the bottom
c - > above b element (but also to the bottom of the div container)
#container { position: relative}
#a { position: absolute; top: 0px;}
#b { position: absolute; bottom: 0px;}
#C {????}
I didn't find a way for C
You can use another container for below divs.
<div id="container">
<div id="a"></div>
<div id="container-below">
<div id="b"></div>
<div id="c"></div>
</div>
</div>
#container { position: relative}
#a { position: absolute; top: 0px;}
#container-below { position: absolute; bottom: 0px;}
You can put c inside b and position it absolutely with bottom: 100%, thus c will always be on top of b.
.d {
position: relative;
height: 200px;
width: 200px;
float: left;
margin: 0 5px;
border: 1px solid green;
}
.a, .b, .c{
position: absolute;
width: 100%;
min-height: 40px;
left: 0;
}
.a {
top: 0;
background: lightgreen;
}
.b {
bottom: 0;
background: tomato;
}
.c {
bottom: 100%;
background: orange;
}
<div class="d">
<div class="a">a
</div>
<div class="b">b
<div class="c">c
</div>
</div>
</div>
<div class="d">
<div class="a">a
</div>
<div class="b"> Block b Lorem ipsum dolor amet Lorem ipsum dolor amet Lorem ipsum dolor amet Lorem ipsum dolor amet Lorem ipsum dolor amet Lorem ipsu
<div class="c">c
</div>
</div>
</div>
please start using flexbox and soon css grids :P
.p > div {
width: 100px;
height: 100px;
border: 1px solid red
}
.p {
height: 500px;
display: flex;
background-color: beige;
width: 200px;
flex-direction: column;
}
.b {
margin-top: auto;
}
<div class="p">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
</div>

Div width/height 100% of the free space inside another div

#external
{
background-color: #585858;
width: 200px;
height: 200px;
}
#internal
{
background-color: #111858;
width: 50px;
height: 100%;
}
<div id="external">
content 1
<div id="internal"></div>
</div>
In this case, how can I set a remnant height of external div to the internal div?
The problem is when I set 100% height to internal div, so internal height == external height.
Your request is a little unclear but flexbox can do what I think you are after.
p {
margin: 0;
}
.external {
background-color: rgba(255, 0, 0, 0.5);
width: 200px;
height: 200px;
display: inline-flex;
flex-direction: column;
margin: 10px;
}
.internal {
background-color: #111858;
100%;
flex: 1;
}
<div class="external">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<div class="internal"></div>
</div>
<div class="external">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nemo harum quia tempore! Sit, laboriosam, quam.</p>
<div class="internal"></div>
</div>
use this may help u
<style>
#external
{
background-color: #585858;
width: 200px;
height: 200px;
}
#internal
{
background-color: #111858;
width: 50px;
height: calc(100% - 20px);
}
.content
{
height:20px;
}
</style>
<div id="external">
<div class="content">content 1</div>
<div id="internal"></div>
</div>