How would I position a div to take up 100% of the page height even when the page has a scroll bar and I scroll to the bottom. My div is a modal and is positioned under a navbar, I set the min-height to 100vh, but it doesn't seem to expand to the bottom of the page when I scroll down. I'm using position absolute not fixed because, I want it to be relative to the navbar.
<header className ={styles.header}>
<span className={styles.header__item} onClick={() => handleTab(1)}>Library</span>
<div className={styles.img} onClick={()=>handleOptions()}>
</div>
<input className={styles.search} type ="text" placeholder="Search"></input>
<PlayerModal open={playerOpen}/>
</header>
.modalStyles{
min-height: 100vh;
width: 26vw;
position: absolute;
left: 74%;
top: 100%;
background-color: whitesmoke;
}
you can use position sticky instead of absolute and set top of this At the bottom of the nav.
#sticky {
position: sticky;
position: -webkit-sticky;
background: #f83d23;
width: 100px;
height: 100px;
top: 70px;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 0 6px #000;
color: #fff;
}
.extra,
#wrapper {
width: 75%;
margin: auto;
background-color: #ccc;
}
#wrapper {
height: 800px;
}
.extra {
height: 100px;
}
body {
font-family: georgia;
height: 1000px;
}
h4 {
text-align: center;
}
#media (min-height: 768px) {
#wrapper{
height: 2000px;
}
}
<h4>Scroll to see the sticky element <em>sticking</em></h4>
<div id="wrapper">
<div id="sticky">
sticky
</div>
</div>
<br />
<div class="extra"></div>
Related
I am trying to stretch a sticky element to size of the screen. I have the following HTML
.large {
height: 200vw;
width: 200vw;
}
.header {
left: 0;
top: 0;
color:white;
position: sticky;
width: 100px;
height: 100px;
background: black;
}
<div class="header">Header</div>
<div class="large">Content</div>
The problem is that this works but the element is not stretched. If I change width:100px to width:100vw the sticky to the left breaks. So it seems like I cannot specify relative width and use sticky to the left at the same time?
You can achieve this by adding a div around both elements and giving that div a display: inline-block;:
.container {
display: inline-block;
}
.large {
height: 200vw;
width: 200vw;
}
.header {
left: 0;
top: 0;
position: sticky;
width: 100vw;
height: 100px;
background: black;
}
<div class="container">
<div class="header"></div>
<div class="large"></div>
</div>
I'm working on a projet (HTML and CSS page) in which I have a navbar at the top of the page, a main container below the navbar, and a menu at the left.
The menu is hidden, and when I move the mouse to the left edge of the window, it appears, and overlaps the main container.
If I scroll the page down, the navbar scrolls, and the menu moves up until it reaches the top. Then it stops and keeps at this place.
I managed to achieve it, more or less. But I still have a problem.
To illustrate my project in a simple way, I took some basic code I found on css-tricks.com website and just modified it a bit to show my problem.
Here is the code :
#sticky {
position: sticky;
position: -webkit-sticky;
background: #f83d23;
width: 100px;
height: 100px;
top: 0px;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 0 6px #000;
color: #fff;
}
.extra,
#wrapper {
display:flex;
width: 75%;
margin: auto;
background-color: #ccc;
}
#wrapper {
height: 800px;
}
.extra {
height: 100px;
}
body {
font-family: georgia;
height: 1000px;
}
h4 {
text-align: center;
}
#media (min-height: 768px) {
#wrapper{
height: 2000px;
}
}
<h4>Scroll to see the sticky element <em>sticking</em></h4>
<div class="extra"></div>
<br />
<div id="wrapper">
<div id="sticky">
sticky
</div>
<div id=brol>
This part should be overlapped by the sticky element
</div>
</div>
<br />
<div class="extra"></div>
Here, the 'extra' div is my navbar, the main container is the grey part, and the sticky element is the menu.
What I would like is that the main container (the grey part) is really using the full width and height, meaning that the text in it should appear at the top-left corner and be party overlapped by the sticky div.
How can I achieve that?
I have used z-index for overlapping content:
#sticky {
position: sticky;
position: -webkit-sticky;
background: #f83d23;
width: 100px;
height: 100px;
top: 0px;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 0 6px #000;
color: #fff;
z-index: 2;
}
.extra,
#wrapper {
width: 75%;
margin: auto;
background-color: #ccc;
z-index:1;
}
#wrapper {
height: 800px;
}
.extra {
height: 100px;
}
body {
font-family: georgia;
height: 1000px;
}
h4 {
text-align: center;
}
#brol{
margin-top: -100px;
}
#media (min-height: 768px) {
#wrapper{
height: 2000px;
}
}
// added .sticky-parent
.sticky-parent {
width: 0;
}
#sticky {
position: sticky;
position: -webkit-sticky;
background: #f83d23;
width: 100px;
height: 100px;
top: 0;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 0 6px #000;
color: #fff;
}
.extra, #wrapper {
display: flex;
width: 75%;
margin: auto;
background-color: #ccc;
}
#wrapper {
height: 800px;
}
.extra {
height: 100px;
}
body {
font-family: georgia;
height: 1000px;
}
h4 {
text-align: center;
}
#media (min-height: 768px) {
#wrapper {
height: 2000px;
}
}
<div class="sticky-parent">
<div id="sticky">
sticky
</div>
</div>
<div id="brol">
This part should be overlapped by the sticky element
</div>
You can achieve this behavior by wrapping the sticky div inside a absolute positioned one. this will make the rest use the full available space.
Don't forget to add position:relative to it's parent, in your simplified case #wrapper to make sure it wont take the full width & height of the document or first relative parent it finds
#sticky {
position: sticky;
position: -webkit-sticky;
background: #f83d23;
width: 100px;
height: 100px;
top: 0px;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 0 6px #000;
color: #fff;
}
#mask {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.extra,
#wrapper {
position: relative;
display:flex;
width: 75%;
margin: auto;
background-color: #ccc;
}
#wrapper {
height: 800px;
}
.extra {
height: 100px;
}
body {
font-family: georgia;
height: 1000px;
}
h4 {
text-align: center;
}
#media (min-height: 768px) {
#wrapper{
height: 2000px;
}
}
<h4>Scroll to see the sticky element <em>sticking</em></h4>
<div class="extra"></div>
<br />
<div id="wrapper">
<div id="mask">
<div id="sticky">
sticky
</div>
</div>
<div id=brol>
This part should be overlapped by the sticky element
</div>
</div>
<br />
<div class="extra"></div>
I'm building a fullscreen modal, and I'm trying to center the content vertically when it is smaller than the screen, and to start at the top and allow scroll, when the hight is larger than the hight of the container. I'm trying to use position:fixed to position the container on the screen, and display:flex; align-items:center; to center the inner div. When the container is shorter than the inner div the top part of the inner div is cut out, even when I use: overflow-y:scroll.
Here is my code:
<div class="modal">
<div class="inner-w">
hello world
<div class="long-box">
</div>
</div>
</div>
.modal {
position: fixed;
bottom: 70px;
top: 0;
left:0;
right: 0;
display: flex;
align-items: center;
padding: 15px;
overflow: scroll;
}
.inner-w {
margin: 50px 0;
width: 100%;
}
.long-box {
height: 400px;
width: 100%;
border: 1px solid brown;
}
here is a jsfiddle: https://jsfiddle.net/benCarp/bh2Lfpo4/18/#&togetherjs=aKbe8NLJSR
add to .modal{flex-direction-column;} now you can remove the margin
.modal {
position: fixed;
bottom: 70px;
top: 0;
left:0;
right: 0;
display: flex;
flex-direction:column;
align-items: center;
padding: 15px;
overflow: scroll;
}
.inner-w {
width: 100%;
}
.long-box {
height: 400px;
width: 100%;
border: 1px solid brown;
}
<div class="modal">
<div class="inner-w">
hello world
<div class="long-box">
</div>
</div>
</div>
#godfather had an excellent suggestion to change the direction of the flex container from row to column with .modal{flex-direction-column;}. It better describes our layout, and the width and margin property aren't needed any more. However it is not enough. overflow: scroll (or "auto") property isn't inherited, and should be placed on the actual element that overflows - the .inner-w class.
Here is how the css should look:
.modal {
position: fixed;
flex-direction:column;
bottom: 70px; // kept for a button
top: 0;
left:0;
right: 0;
display: flex;
padding: 15px;
justify-content: center;
}
.inner-w {
overflow: auto;
}
.long-box {
height: 400px;
width: 100%;
border: 1px solid brown;
}
I try to understand position and values absolute and relative, but I have one problem.
Why, when I use the "right" attribute, my "A" point changes position when the resolution changes, but when it applies the "left" attribute, everything stays in place well, even when I change the resolution?
Please check on different resolutions:
- Left:
- Right:
Code:
- Left:
html,
body {
background-color: #f4f4f4;
font-family: "Roboto", sans-serif;
}
body {
display: flex;
justify-content: center;
}
main {
background-color: #fefefe;
min-width: 350px;
max-width: 700px;
width: 40vw;
padding: 2rem;
}
.map {
background:url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Simple_world_map.svg/2000px-Simple_world_map.svg.png")
no-repeat;
background-size: cover;
height: 60vh;
position: relative;
}
.absolute {
position: absolute;
left: 100px;
top: 20px;
color: blue;
}
<main>
<form>
<div class="map">
<div class="absolute">
<h1>A</h1>
</div>
</div>
</form>
</main>
Right:
html,
body {
background-color: #f4f4f4;
font-family: "Roboto", sans-serif;
}
body {
display: flex;
justify-content: center;
}
main {
background-color: #fefefe;
min-width: 350px;
max-width: 700px;
width: 40vw;
padding: 2rem;
}
.map {
background:url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Simple_world_map.svg/2000px-Simple_world_map.svg.png")
no-repeat;
background-size: cover;
height: 60vh;
position: relative;
}
.absolute {
position: absolute;
right: 447px;
top: 20px;
color: blue;
}
<main>
<form>
<div class="map">
<div class="absolute">
<h1>A</h1>
</div>
</div>
</form>
</main>
My question is why this is happening, why the use of "right" is so unstable?
Because the absolute position is "relative" to .map div that it has no fixed width (
it changes as the browser size changes, from min 350px to max 700px -
inherits this rule from the parent main). So the .map's right border continuously changes its position. The left border, on the other hand, "remains steady" so the h1 never change its position.
I try to explain better with an image:
Tried a few things(margin-auto, text align:center etc) to centre this relative div - which is the header in my responsive layout with no luck. Any other ways to try?
The problem is keeping it centered as the page expands/contracts
Its CSS properties are
#header {
height: 170px;
width: 100%;
overflow: visible;
padding: 10px;
margin-bottom: 7px;
position: relative;
z-index: 99;
}
How can a div appear visually centered when it's 100% width of its parent?
Check out this fiddle: https://jsfiddle.net/w6332ytc/
HTML:
<div class="wrapper">
<div class="inner">
Content
</div>
</div>
CSS:
.wrapper {
width: 100%;
background: #000;
height: 300px;
}
.inner {
width: 50%;
background: red;
margin: 0 auto;
}