Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a Fixed div to 100% width of the parent container

Tags:

html

css

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.

HTML

<div id="wrapper">   <div id="wrap">     Some relative item placed item     <div id="fixed"></div>   </div> </div> 

CSS

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/

like image 366
Jack Murdoch Avatar asked Jul 23 '13 09:07

Jack Murdoch


People also ask

How do you make a div full width of a parent?

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%.

How do you make a DIV fixed width?

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.

How do you keep width fixed in CSS?

Add width:auto; to the element with position:fixed; to make its width equal to the width of its parent element.

How do I keep my DIV from expanding?

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.


2 Answers

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

like image 56
YD1m Avatar answered Sep 16 '22 14:09

YD1m


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>
like image 40
Wojtek Kochanowski Avatar answered Sep 16 '22 14:09

Wojtek Kochanowski