I have a wrapper with some padding, I then have a floating relative div with a percentage width (40%).
Inside the floating relative div I have a fixed div which I would like the same size as its parent. I understand that a fixed div is removed from the flow of the document and as such is ignoring the padding of the wrapper.
<div id="wrapper"> <div id="wrap"> Some relative item placed item <div id="fixed"></div> </div> </div>
body { height: 20000px } #wrapper { padding: 10%; } #wrap { float: left; position: relative; width: 40%; background: #ccc; } #fixed { position: fixed; width: inherit; padding: 0px; height: 10px; background-color: #333; }
Here is the obligatory fiddle: http://jsfiddle.net/C93mk/489/
Does anyone know of a way to accomplish this?
I have amended the fiddle to show more detail on what I am trying to accomplish, sorry for the confusion: http://jsfiddle.net/EVYRE/4/
Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.
To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.
Add width:auto; to the element with position:fixed; to make its width equal to the width of its parent element.
You should use display: table; It will shrink to the size of it's contents and can also be centered and positioning without having to assign a given width.
You can use margin for .wrap container instead of padding for .wrapper:
body{ height:20000px } #wrapper { padding: 0%; } #wrap{ float: left; position: relative; margin: 10%; width: 40%; background:#ccc; } #fixed{ position:fixed; width:inherit; padding:0px; height:10px; background-color:#333; }
jsfiddle
Try adding a transform to the parent (doesn't have to do anything, could be a zero translation) and set the fixed child's width to 100%
body{ height:20000px } #wrapper {padding:10%;} #wrap{ float: left; position: relative; width: 40%; background:#ccc; transform: translate(0, 0); } #fixed{ position:fixed; width:100%; padding:0px; height:10px; background-color:#333; }
<div id="wrapper"> <div id="wrap"> Some relative item placed item <div id="fixed"></div> </div> </div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With