I'm trying to give a div (position: fixed) the width of 100% (relating to it's parent div). But I've got some problems...
EDIT: The first problem is sovled by using inherit, but it still doesn't work. I think the problem is that I'm using multiple divs that take the 100%/inherit width. You can find the second problem on the jsfiddle update: http://jsfiddle.net/4bGqF/7/
Fox example
#container {
width: 800px;
}
#fixed {
position: fixed;
width: 100%;
}
and the html
<div id="container">
<div id="fixed">Sitename</div>
<p>
blaat
</p>
</div>
Or you can try it out: http://jsfiddle.net/4bGqF/
The problems seems to be that the fixed element is always taking the width of the window/document. Does anyone know how the fix this?
I can't give my fixed element a fixed with, because I'm using the jScrollPane plugin. It depends on the content whether there's a scrollbar or not.
Thanks a lot!
PS: The text of the 2 divs are on top of each other. This is just an example so that doesn't really matter.
This question is related to
jquery
css
jquery-plugins
You can also solve it by jQuery:
var new_width = $('#container').width();
$('#fixed').width(new_width);
This was so helpful to me because my layout was responsive, and the inherit
solution wasn't working with me!
As many people have commented, responsive design very often sets width by %
width:inherit
will inherit the CSS width NOT the computed width -- Which means the child container inherits width:100%
But, I think, almost as often responsive design sets max-width
too, therefore:
#container {
width:100%;
max-width:800px;
}
#contained {
position:fixed;
width:inherit;
max-width:inherit;
}
This worked very satisfyingly to solve my problem of making a sticky menu be restrained to the original parent width whenever it got "stuck"
Both the parent and child will adhere to the width:100%
if the viewport is less than the maximum width. Likewise, both will adhere to the max-width:800px
when the viewport is wider.
It works with my already responsive theme in a way that I can alter the parent container without having to also alter the fixed child element -- elegant and flexible
ps: I personally think it does not matter one bit that IE6/7 do not use inherit
There is an easy solution for this.
I have used a fixed position for parent div and a max-width for the contents.
You don't need to think about too much about other containers because fixed position only relative to the browser window.
.fixed{_x000D_
width:100%;_x000D_
position:fixed;_x000D_
height:100px;_x000D_
background: red;_x000D_
}_x000D_
_x000D_
.fixed .content{_x000D_
max-width: 500px;_x000D_
background:blue;_x000D_
margin: 0 auto;_x000D_
text-align: center;_x000D_
padding: 20px 0;_x000D_
}
_x000D_
<div class="fixed">_x000D_
<div class="content">_x000D_
This is my content_x000D_
</div>_x000D_
</div>
_x000D_
This solution meets the following criteria
As far as I'm aware, this criteria cannot be met without Javascript (unfortunately).
This solution uses jQuery, but could also be easily converted to vanilla JS:
function fixedHeader(){_x000D_
$(this).width($("#wrapper").width());_x000D_
$("#header-filler").height($("#header-fixed").outerHeight());_x000D_
}_x000D_
_x000D_
$(window).resize(function() {_x000D_
fixedHeader();_x000D_
});_x000D_
_x000D_
fixedHeader();
_x000D_
#header-fixed{_x000D_
position: fixed;_x000D_
background-color: white;_x000D_
top: 0;_x000D_
}_x000D_
#header-filler{_x000D_
width: 100%;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div id="wrapper">_x000D_
<div id="header-fixed">_x000D_
This is a nifty header! works even when resizing the window causing a line break_x000D_
</div>_x000D_
<div id="header-filler"></div>_x000D_
_x000D_
[start fluff]<br>_x000D_
a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>_x000D_
a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>_x000D_
a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>_x000D_
[end fluff]_x000D_
_x000D_
</div>
_x000D_
Fixed positioning is supposed to define everything in relation to the viewport, so position:fixed
is always going to do that. Try using position:relative
on the child div instead.
(I realize you might need the fixed positioning for other reasons, but if so - you can't really make the width match it's parent with out JS without inherit
)
Use this CSS:
#container {
width: 400px;
border: 1px solid red;
}
#fixed {
position: fixed;
width: inherit;
border: 1px solid green;
}
The #fixed element will inherit it's parent width, so it will be 100% of that.
Here is a little hack that we ran across while fixing some redraw issues on a large app.
Use -webkit-transform: translateZ(0);
on the parent. Of course this is specific to Chrome.
http://jsfiddle.net/senica/bCQEa/
-webkit-transform: translateZ(0);
You need to give the same style of the fixed element and its parent element. One of these examples is created with max widths and in the other example with paddings.
* {_x000D_
box-sizing: border-box_x000D_
}_x000D_
body {_x000D_
margin: 0;_x000D_
}_x000D_
.container {_x000D_
max-width: 500px;_x000D_
height: 100px;_x000D_
width: 100%;_x000D_
margin-left: auto;_x000D_
margin-right: auto;_x000D_
background-color: lightgray;_x000D_
}_x000D_
.content {_x000D_
max-width: 500px;_x000D_
width: 100%;_x000D_
position: fixed;_x000D_
}_x000D_
h2 {_x000D_
border: 1px dotted black;_x000D_
padding: 10px;_x000D_
}_x000D_
.container-2 {_x000D_
height: 100px;_x000D_
padding-left: 32px;_x000D_
padding-right: 32px;_x000D_
margin-top: 10px;_x000D_
background-color: lightgray;_x000D_
}_x000D_
.content-2 {_x000D_
width: 100%;_x000D_
position: fixed;_x000D_
left: 0;_x000D_
padding-left: 32px;_x000D_
padding-right: 32px;_x000D_
}
_x000D_
<div class="container">_x000D_
<div class="content">_x000D_
<h2>container with max widths</h2>_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
<div class="container-2">_x000D_
<div class="content-2">_x000D_
<div>_x000D_
<h2>container with paddings</h2>_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_
fixed position is a bit tricky (indeed impossible), but position: sticky is doing the trick beautifully:
<div class='container'>
<header>This is the header</header>
<section>
... long lorem ipsum
</section>
</div>
body {
text-align: center;
}
.container {
text-align: left;
max-width: 30%;
margin: 0 auto;
}
header {
line-height: 2rem;
outline: 1px solid red;
background: #fff;
padding: 1rem;
position: sticky;
top: 0;
}
Source: Stackoverflow.com