简体   繁体   English

如何填充剩余宽度的 100%

[英]How to fill 100% of remaining width

Is there any work around to do something like this work as expected?是否有任何解决方法可以按预期做这样的工作? I wish there were something like that width:remainder;我希望有这样的width:remainder; or width:100% - 32px;width:100% - 32px; . .

width: auto; doesn't works.不起作用。

I think the only way possible is working around with paddings/margins, negative values, or float, or some html tags hack.我认为唯一可能的方法是处理填充/边距、负值或浮点数,或者一些 html 标签。 I tried also display:block;.我也试过显示:块;。

I like to get the same result as this, without tables http://jsfiddle.net/LJGWY/我喜欢得到与此相同的结果,没有表格http://jsfiddle.net/LJGWY/

在此处输入图片说明

<div style="position: absolute; width: 100%; height: 100px; border: 3 solid red;" id="container">
    <div style="display:inline; width: (100%-100px); border: 3 solid green;">Fill</div>
    <div style="display:inline; width: 100px; border: 3 solid blue;">Fixed</div>
</div>

Updated answer:更新的答案:

The answers here are pretty old.这里的答案很旧。 Today, this can be achieved easily with flexbox:今天,这可以通过 flexbox 轻松实现:

 .container { border: 4px solid red; display: flex; } .content { border: 4px solid green; flex-grow: 1; margin: 5px; } .sidebar { border: 4px solid blue; margin: 5px 5px 5px 0; width: 200px; }
 <div class="container"> <div class="content"> Lorem ipsum dolor sit amet. </div> <div class="sidebar"> Lorem ipsum. </div> </div>

Original answer:原答案:

Block level elements like <div> will fill 100% of the available width automatically.像 <div> 这样的块级元素会自动填充 100% 的可用宽度。 If you float one of them to the right, the contents of the other will fill the remaining space.如果将其中一个向右浮动,另一个的内容将填充剩余空间。

<div style="height: 100px; border: 3px solid red;" id="container">
  <div style="float: right; width: 100px; border: 3px solid blue;">Fixed</div>
  <div style="border: 3px solid green;">Fill</div>
</div>

http://jsfiddle.net/5AtsF/ http://jsfiddle.net/5AtsF/

For anyone looking over this now theres a newish css property method called calc which can perform this in a much more flexible fashion.对于任何查看此内容的人来说,现在有一个名为 calc 的新 css 属性方法,它可以以更灵活的方式执行此操作。

<div class="container">
    <div class="fixedWidth"></div>
    <div class="variableWidth"></div>
</div>

.fixedWidth{
width:200px;
}
.variableWidth{
width:calc(100%-200px);
}

As a word of warning, this is not very portable and support is ropey on mobile devices.作为警告的话,这不是很便携,并且在移动设备上的支持很糟糕。 IOS 6+ and andriod 4.4 i believe.我相信 IOS 6+ 和 andriod 4.4。 Support is significantly better for desktop though, IE 9.0+.不过,对桌面 IE 9.0+ 的支持要好得多。

http://caniuse.com/calc http://caniuse.com/calc

I have used a JS hack in the past to achieve this technique if anyone is incredibly stuck, a different layout is more advisable though as resize is slower.如果有人被卡住了,我过去曾使用 JS hack 来实现此技术,尽管调整大小较慢,但建议使用不同的布局。

window.addEventListener('resize', function resize(){
    var parent = document.getElementById('parent');
    var child = document.getElementById('child');
    child.style.width = parseInt(parent.offsetWidth - 200) + "px"; //200 being the size of the fixed size element
}, false);

If you don't know how big will be the fixed part you can use the flex 9999 hack.如果您不知道固定部分有多大,您可以使用 flex 9999 hack。

<div class="container">
  <div class="fixedWidth"></div>
  <div class="variableWidth"></div>
</div>
.container {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
}

.fixedWidth {
    flex: 1;
}

.variableWidth {
    flex: 9999;
}

This should do for you:这应该为你做:

<div style="position: absolute; width: 100%; height: 100px; border: 3px solid red;" id="container">
    <div style="float: right; width: 100px; border: 3px solid blue;">Fixed</div>
    <div style="display: block; margin-right: 100px; border: 3px solid green;">Fill</div>
</div>

See the jsFiddle参见 jsFiddle

This is assuming you're going to be removing the 3px borders from the end result (they overlap in the example because border width is not included in the width).这是假设您要从最终结果中删除 3px 边框(它们在示例中重叠,因为边框宽度不包含在宽度中)。

You can acheive this without change your markup with use display:table property for this:您可以使用display:table属性在不更改标记的情况下实现此目的:

.parent{
    position: absolute;
    left:0;
    right:0;
    height: 100px;
    border: 3px solid red;
    display:table;
}
.fill{
    margin-right: 100px;
    border: 3px solid green;
    display:table-cell;
    width:100%;
}
.fixed{
    width: 100px;
    border: 3px solid blue;
    display:table-cell;
}

Check the live example with no horizontal scrollbar检查没有horizontal scrollbar的实时示例

http://jsfiddle.net/WVDNe/5/ http://jsfiddle.net/WVDNe/5/

Another example but in better way check this:另一个例子,但更好地检查这个:

http://jsfiddle.net/WVDNe/6/ http://jsfiddle.net/WVDNe/6/

note: it not work in IE7 & below注意:它不适用于 IE7 及以下

Check this also也检查这个

http://jsfiddle.net/LJGWY/4/ http://jsfiddle.net/LJGWY/4/

It's work in all browsers.它适用于所有浏览器。

you can use table style.您可以使用表格样式。 create a div with table style and sub items be that's table-cell styles创建一个带有表格样式和子项的 div 是表格单元格样式

label text 标签文字

If you use CSS, one simple solution might be using the following code on the inside div : 如果使用CSS,一个简单的解决方案可能是在div内部使用以下代码:

div{
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
}

Try setting the position like so:尝试像这样设置位置:

<div style="position: absolute; width: 100%; height: 100px; border: 3 solid red;" id="container">
    <div style="position:absolute; left: 0; top: 0; right: 100px; border: 3 solid green;">Fill</div>
    <div style="position:absolute; top: 0; right: 0; width: 100px; border: 3 solid blue;">Fixed</div>
</div>

You could put the fixed div inside the the fill div .您可以将fixed div放在fill div

<div id="container">
    <div>Fill
        <div>Fixed</div>
    </div>
</div>

CSS CSS

#container{
    position:absolute;
    width:90%;
    height:100px;
    border:3px solid red;
}

#container div{
    height:95%;
    border:3px solid green;
    width:100%;
}

#container div div{
    height:95%; 
    width:100px;
    border:3px solid blue;
    float:right;
}

Example: http://jsfiddle.net/EM8gj/3/示例: http : //jsfiddle.net/EM8gj/3/

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

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