简体   繁体   English

将固定div设置为父容器的100%宽度

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

I have a wrapper with some padding, I then have a floating relative div with a percentage width (40%). 我有一个带有一些填充的包装器,然后我有一个浮动的相对div,百分比宽度(40%)。

Inside the floating relative div I have a fixed div which I would like the same size as its parent. 在浮动相对div内部,我有一个固定的div,我希望它的大小与其父级相同。 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从文档流中删除,因此忽略了包装器的填充。

HTML HTML

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

CSS 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/ 这是必须的小提琴: 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/ 我已经修改了小提琴,以显示我想要完成的更多细节,对于这种混淆感到遗憾: http //jsfiddle.net/EVYRE/4/

You can use margin for .wrap container instead of padding for .wrapper: 你可以使用.wrap容器的margin而不是.wrapper的padding:

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 的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% 尝试向父项添加变换(不必执行任何操作,可以是零翻译)并将固定子项的宽度设置为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> 

How about this ? 怎么样这个

$( document ).ready(function() {
    $('#fixed').width($('#wrap').width());
});

By using jquery you can set any kind of width :) 通过使用jquery你可以设置任何类型的宽度:)

EDIT: As stated by dream in the comments, using JQuery just for this effect is pointless and even counter productive. 编辑:正如评论中的梦想所说,使用JQuery只是为了这种效果是毫无意义的,甚至反效果。 I made this example for people who use JQuery for other stuff on their pages and consider using it for this part also. 我为那些在其页面上使用JQuery用于其他内容的人制作了这个示例,并考虑将其用于此部分。 I apologize for any inconvenience my answer caused. 对于我的回答造成的任何不便,我们深表歉意。

You could use absolute positioning to pin the footer to the base of the parent div. 您可以使用绝对定位将页脚固定到父div的基础。 I have also added 10px padding-bottom to the wrap (match the height of the footer). 我还在包装中添加了10px padding-bottom(匹配页脚的高度)。 The absolute positioning is relative to the parent div rather than outside of the flow since you have already given it the position relative attribute. 绝对定位是相对于父div而不是流的外部,因为你已经给它了位置相对属性。

body{ height:20000px }
#wrapper {padding:10%;}
#wrap{ 
    float: left;
    padding-bottom: 10px;
    position: relative;
    width: 40%; 
    background:#ccc; 
}
#fixed{ 
    position:absolute;
    width:100%;
    left: 0;
    bottom: 0;
    padding:0px;
    height:10px;
    background-color:#333;

}

http://jsfiddle.net/C93mk/497/ http://jsfiddle.net/C93mk/497/

man your container is 40% of the width of the parent element man你的容器是父元素宽度的40%

but when you use position:fixed, the width is based on viewport(document) width... 但是当你使用position:fixed时,宽度是基于视口(文档)宽度...

thinking about, i realized your parent element have 10% padding(left and right), it means your element have 80% of the total page width. 想一想,我意识到你的父元素有10%的填充(左和右),这意味着你的元素占总页面宽度的80%。 so your fixed element must have 40% based on 80% of total width 所以你的固定元素必须有40%,基于总宽度的80%

so you just need to change your #fixed class to 所以你只需要将#fixed类更改为

#fixed{ 
    position:fixed;
    width: calc(80% * 0.4);
    height:10px;
    background-color:#333;
}

if you use sass, postcss or another css compiler, you can use variables to avoid breaking the layout when you change the padding value of parent element. 如果您使用sass,postcss或其他css编译器,则可以使用变量来避免在更改父元素的填充值时破坏布局。

here is the updated fiddle http://jsfiddle.net/C93mk/2343/ 这里是更新的小提琴http://jsfiddle.net/C93mk/2343/

i hope it helps, regards 我希望它有所帮助,问候

On top of your lastest jsfiddle , you just missed one thing: 在最新的jsfiddle之上 ,你错过了一件事:

#sidebar_wrap {
  width:40%;
  height:200px;
  background:green;
  float:right;
}
#sidebar {
  width:inherit;
  margin-top:10px;
  background-color:limegreen;
  position:fixed;
  max-width: 240px; /*This is you missed*/
}

But, how this will solve your problem? 但是,这将如何解决您的问题? Simple, lets explain why is bigger than expect first. 简单,让我们先解释为什么比预期更大。

Fixed element #sidebar will use window width size as base to get its own size, like every other fixed element, once in this element is defined width:inherit and #sidebar_wrap has 40% as value in width, then will calculate window.width * 40% , then when if your window width is bigger than your .container width, #sidebar will be bigger than #sidebar_wrap . 固定元素#sidebar将使用窗口宽度大小作为基础来获取自己的大小,就像每个其他固定元素一样,一旦在此元素中定义了width:inherit#sidebar_wrap宽度值为40%,那么将计算window.width * 40%那么如果你的窗口宽度是比你大的时候.container宽度, #sidebar会比大#sidebar_wrap

This is way, you must set a max-width in your #sidebar_wrap , to prevent to be bigger than #sidebar_wrap . 这样,您必须在#sidebar_wrap设置最大宽度,以防止大于#sidebar_wrap

Check this jsfiddle that shows a working code and explain better how this works. 检查这个显示工作代码的jsfiddle并更好地解释它是如何工作的。

Remove Padding: 10%; 去除Padding: 10%; or use px instead of percent for .wrap 或者使用px代替.wrap百分比

see the example : http://jsfiddle.net/C93mk/493/ 看例子: http//jsfiddle.net/C93mk/493/

HTML : HTML:

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

CSS: CSS:

body{ height:20000px }
#wrapper {padding:10%;}
#wrap{ 
    float: left;
    position: relative;
    width: 200px; 
    background:#ccc; 
}
#fixed{ 
    position:fixed;
    width:inherit;
    padding:0px;
    height:10px;
    background-color:#333;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM