This question already has answers here:
Fixed position but relative to container
(31 answers)
Closed 4 years ago.
I am relatively new to CSS. I have run into a problem where I am trying to fix an element next to its parent element. I am able to do so with the following code:
Parent element:
#search_results{
position:relative;
}
Child element:
.total {
position: fixed;
top:10px;
width:250px;
left: 75%;
/*overflow: hidden;*/
margin-left: -125px;
}
This works fine until the browser window is resized. When that occurs, the fixed element overlaps its parent element. You can see my problem here:
Twittiment
I am trying to fix the child element to the top of the page and the right-hand side of the parent element. Any ideas?
Edit:
You can use position: sticky; which can be relative to the parent element.
body > div {
height: 300px;
background-color: #ddd;
overflow: auto;
margin-top: 70px;
}
div > div {
height: 1000px;
position: relative;
}
span {
display: block;
height: 20px;
background-color: tomato;
position: sticky;
top: 0;
}
<div>
<div>
<span>This is a relatively sticky header</span>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus voluptas pariatur ullam, dolores veritatis vero possimus nisi corrupti, provident aspernatur harum ab aliquam expedita assumenda, blanditiis aliquid id consequuntur distinctio.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus voluptas pariatur ullam, dolores veritatis vero possimus nisi corrupti, provident aspernatur harum ab aliquam expedita assumenda, blanditiis aliquid id consequuntur distinctio.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus voluptas pariatur ullam, dolores veritatis vero possimus nisi corrupti, provident aspernatur harum ab aliquam expedita assumenda, blanditiis aliquid id consequuntur distinctio.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus voluptas pariatur ullam, dolores veritatis vero possimus nisi corrupti, provident aspernatur harum ab aliquam expedita assumenda, blanditiis aliquid id consequuntur distinctio.</p>
</div>
</div>
Old Answer:
As per CSS Spec, the element positioned fixed is fixed to the viewport and not the containing element.
So the short answer is NO, you cannot have a fixed position element relative to it's parent element. You can use position: absolute; instead and tweak the top left right bottom parameters on the run using jQuery/JS.
Of course you can, just need an extra div!
<div class="fixed-wrapper">
<div class="close-wrapper">
<div class="close"></div>
</div>
</div>
body
background: gray
height: 8000px
.fixed-wrapper
position: fixed
top: 20px
left: 0
right: 0
.close-wrapper
max-width: 1200px
position: relative
.close
background: #fff
width: 30px
height: 30px
position: absolute
right: 0
border: 1px solid #515151
&:before,&:after
width: 25px
height: 1px
background: #515151
content: ''
position: absolute
top: 50%
left: 50%
display: block
#include transform(translate(-50%, -50%) rotate(45deg))
&:after
transform: translate(-50%, -50%) rotate(-45deg)
See this fiddle I made for you :-)
http://codepen.io/marinagallardo/pen/mJyqaN
The best way to achieve this is to give parent element a transform css.
eg:
.relative{
transform: translateX(0); // this will act like relative parent
}
.fixed{
position: fixed;
left:0;
top:0;
width:100%; // width will be relative to the width of .relative
}
What you want to use is position:absolute . This places the child element according to it's parent element.
Some readings here : http://www.w3schools.com/cssref/pr_class_position.asp
Related
I'm trying to make a layout with a fixed navbar, a fixed-size content area, and a scrollable sidebar. This is a pretty standard setup (though usually the sidebar is fixed while the content scrolls, but that's just semantics), but it's important that the content's box doesn't overflow the overall container, since I'm filling it with an image that gets an object-fit applied so it can scale happily within the viewport while maintaining its aspect ratio.
I actually have a functioning version of it using grid (embedded and linked below). The problem there is that if I want the sidebar to go away, I have to change the styling on the parent divs, which is messy (but doable) in the component system I'm using. Ideally I'd be able to simply set the sidebar to display: none and the content would fill the space.
I've tried converting the whole thing to flexbox, and making the second row into a nested grid, but I can't seem to do either of those while retaining the overall height lock.
Here's the working grid (and a CodePen).
html,
body {
margin: 0;
}
.wrapper {
height: 100vh;
display: grid;
grid-template-columns: 1fr 150px;
grid-template-rows: auto 1fr;
}
nav {
height: 50px;
grid-row: 1;
grid-column: 1 / span 2;
background-color: darkseagreen;
}
.content {
background-color: salmon;
position: relative;
}
img {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
object-fit: contain;
}
.sidebar {
overflow-y: scroll;
background-color: cornflowerblue;
}
<div class="wrapper">
<nav>Navbar should stay fixed</nav>
<div class="content">
<img src="http://placehold.it/640x360">
</div>
<div class="sidebar">
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Porro repudiandae, laboriosam dolorum mollitia fugit autem officiis explicabo minima! Maxime ea a unde alias laboriosam vel pariatur delectus. A, quas ratione?</div>
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque temporibus sunt, nesciunt et assumenda asperiores fuga aperiam nulla voluptas reprehenderit iusto molestias blanditiis corrupti, nobis ab id dolorum obcaecati ullam!</div>
</div>
</div>
Is there a way to craft this so the second row becomes a flexbox while keeping its height from expanding, so I could collapse the sidebar without having to re-layout a whole grid? (If there's a more clever way to get the img scaling I'm attempting, I'm open to that, too.)
Simply update the template to grid-template-columns: 1fr auto; and make the width of sidebar to be equal to 150px;
html,
body {
margin: 0;
}
.wrapper {
height: 100vh;
display: grid;
grid-template-columns: 1fr auto;
grid-template-rows: auto 1fr;
}
nav {
height: 50px;
grid-row: 1;
grid-column: 1 / span 2;
background-color: darkseagreen;
}
.content {
background-color: salmon;
position: relative;
}
img {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
object-fit: contain;
}
.sidebar {
overflow-y: scroll;
background-color: cornflowerblue;
width:150px;
}
<div class="wrapper">
<nav>Navbar should stay fixed</nav>
<div class="content">
<img src="http://placehold.it/640x360">
</div>
<div class="sidebar" style="display:none;">
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Porro repudiandae, laboriosam dolorum mollitia fugit autem officiis explicabo minima! Maxime ea a unde alias laboriosam vel pariatur delectus. A, quas ratione?</div>
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque temporibus sunt, nesciunt et assumenda asperiores fuga aperiam nulla voluptas reprehenderit iusto molestias blanditiis corrupti, nobis ab id dolorum obcaecati ullam!</div>
</div>
</div>
I try to achieve effect similar to Photoshop multiply via code on my project. At the same time i try to exclude some elements that are in child div to be affected by it (like text layers) - and i hit in the wall. Tried already adding additional div's setting different z-index's or go with absolute position.
Here you can find pen with example of the problem:
HTML
<div class="main">
<div class="inner">
<h1>Some header</h1>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quia eligendi est eius unde autem dolore adipisci perspiciatis laboriosam reiciendis placeat! Eveniet, quam? Vitae sit saepe quam delectus fugiat, dolores necessitatibus.</p>
</div>
</div>
CSS
.inner {
text-align: center;
padding: 50px 0;
background: #0079ff;
mix-blend-mode: multiply;
backface-visibility: hidden;
color: white;
}
.main {
background-repeat: no-repeat;
background-image: url('https://preview.ibb.co/fMY2f9/Bg1.jpg');
background-size: cover;
}
Summary: Child element's of 'child div' are affected by multiply. Is there a way to prevent that?
Screenshots:
Use pseudo element for the background to avoid this:
.inner {
text-align: center;
padding: 50px 0;
color: white;
}
.main {
background-repeat: no-repeat;
background-image: url('https://preview.ibb.co/fMY2f9/Bg1.jpg');
background-size: cover;
position: relative;
z-index: 0;
}
.main::before {
content: "";
position: absolute;
z-index: -1;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #0079ff;
mix-blend-mode: multiply;
}
<div class="main">
<div class="inner">
<h1>Some header</h1>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quia eligendi est eius unde autem dolore adipisci perspiciatis laboriosam reiciendis placeat! Eveniet, quam? Vitae sit saepe quam delectus fugiat, dolores necessitatibus.</p>
</div>
</div>
This question already has answers here:
One flex/grid item sets the size limit for siblings
(6 answers)
Closed 6 years ago.
I'm trying to create a flexbox layout that does something I thought would be a little easier, but I'm having trouble finding the right way to do it.
I want to have a row of items with dynamic height that allows one child to grow as tall as need be, but limits the height of the other item so that content is cut off.
I want to use flexbox, so browser issues associated with that are not an issue, but I would like to avoid any use of JavaScript in the solution.
Any ideas? This might be a trivial problem, but I'm having trouble finding anything with the search terms I've been using. Thanks!
Here's a CodePen demo in case you'd like to modify it for use in your answer.
This is my reference flexbox layout:
.row {
display: flex;
}
.info {
flex: 0 0 200px;
}
.description {
flex: 1 1 auto;
}
<div class="row">
<div class="info">This should grow dynamically</div>
<div class="description">This should be limited in height by the .info div</div>
</div>
Flexbox can't do that natively but it is possible.
You will need an inner element inside the second child which is positioned absolutely.
Here the extra content is/can be hidden with overflow:hidden...or revealed by adding overflow:auto.
.wrapper {
display: flex;
width: 80%;
margin: 1em auto;
border: 2px solid red;
}
.child {
flex: 1;
border: 2px solid green;
}
.child:nth-child(2) {
position: relative;
overflow: auto;
/*overflow: hidden; */ /* removed for demo purposes */
}
.inner {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="wrapper">
<div class="child">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis tenetur, laboriosam! Ab facilis, officia id delectus eaque expedita quia, incidunt eligendi aut, minus temporibus tenetur.</div>
<div class="child">
<div class="inner">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae molestiae, libero inventore nobis et veritatis, laborum vitae, vel eaque omnis ad adipisci quia velit blanditiis qui. Cum voluptas quisquam itaque possimus accusamus repellendus quia iure
asperiores. Unde, rerum nihil maiores nisi, iusto voluptate id cumque incidunt, perspiciatis facilis perferendis explicabo.
</div>
</div>
</div>
I have div of variable width which I want to center using this code:
div {
background: red;
max-width: 400px;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
https://jsfiddle.net/xooqvyqL/1/
this works well for centering, however the problem becomes when you shrink window size down (lets say you view it on smaller screen/mobile) then you get like a 'padding' around the element taking space, which is not wanted behavior.
This is different as if was like this:
https://jsfiddle.net/xc4w4aph/5/
Note: this is not centered because of variable width (I would use negative margin if it was known width or javascript but I want to do this with pure css) but just for demo purposes if you shrink window size on this example you dont get any kind of 'padding' outside the div taking space.
I am missing something with translate? I dont want this 'padding' behavior.
Because you have set the div to position absolute, it collapses and the margins are reset and the div no longer has a default width of 100%
To restore the maximum, but restricted width, add width:100% before the max-width and everything goes back to normal.
JSfiddle Demo
div {
background: red;
width: 100%;
max-width: 400px;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facilis sint enim molestias modi officia fuga corporis, ipsa dicta tenetur, aut dignissimos, perspiciatis cumque assumenda, voluptas harum quis qui cum eligendi voluptatibus dolore! Quos hic
architecto odio repudiandae aliquid quisquam quidem beatae voluptatem sint praesentium. Tempore eveniet dolorum aspernatur, asperiores neque.</p>
</div>
Use this css code :
*{
margin:0;
padding:0;
}
div {
background: red;
max-width: 400px;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
I have 2 divs 1 of parent
.box {
width: 960px;
position: relative;
}
#content {
width: 100%;
position: absolute;
background: yellow;
background: 0, 0, 0, 0.5;
padding: 30px 20%;
}
<div class="box">
<div id="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore blanditiis veniam autem repellat consequatur hic magnam, molestiae debitis doloremque quam
</div>
</div>
When I am using the padding for content div it overflows it's parent. By the way I must use the width:100%; for #content because I want the padding in the content div because I'm using background color for it so, how can I fix it?
Here is the fiddle
Use box-sizing
*{box-sizing: border-box}
here is the Demo
*{box-sizing: border-box}
:root{
padding-top: 40px
}
.box{
width:480px;
position:relative;
}
#content{
width:100%;
position:absolute;
background:yellow;
background:0,0,0,0.5;
padding:30px 20%;
}
<div class="box">
<div id="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore blanditiis veniam autem repellat consequatur hic magnam, molestiae debitis doloremque quam
</div>
</div>
you should use position:relative ...