简体   繁体   English

填充父级内的剩余宽度,同时填充剩余宽度

[英]Fill remaining width inside parent that is also filling remaining width

I have a fixed-size navbar (using Bootstrap ) that contains four divs. 我有一个固定大小的导航栏(使用Bootstrap ),包含四个div。 These divs contain a logo, some links/dropdowns and a search box: 这些div包含徽标,一些链接/下拉菜单和搜索框:

┌────────────────────────────navbar (fixed-size)──────────────────────────────┐
│┌───logo───┐┌───dropdown───┐┌───────────links──────────┐┌────search box─────┐│
││fixed-size││  fixed-size  ││      variable width      ││  fill remaining   ││
│└──────────┘└──────────────┘└──────────────────────────┘└───────────────────┘│
└─────────────────────────────────────────────────────────────────────────────┘

I want the search box (the box on the far right) to fill the remaining space in the container. 我想要搜索框(最右边的框)填充容器中的剩余空间。 The search box comprises an input and a button: 搜索框包含一个输入和一个按钮:

┌─────────────────────────────────┐
│┌─────────────────────┐┌────────┐│
││        <input>      ││<button>││
│└─────────────────────┘└────────┘│
└─────────────────────────────────┘

The button has a fixed size but I want the input to scale horizontally, stretching the search box to fill its parent (the nav-bar). 该按钮具有固定的大小,但我希望输入水平缩放,拉伸搜索框以填充其父级(导航栏)。

Edit: To clarify, I want the search box to take up the (horizontal) space left over from when it's siblings (logo, dropdown and links) have taken up their space. 编辑:为了澄清,我希望搜索框占据其兄弟姐妹(徽标,下拉列表和链接)占用空间时留下的(水平)空间。 Since the search box is made out of an input and a button and the button will have a fixed width, I want the input to expand and push the search box's boundaries until the search box fills the space it's siblings left in the navbar 由于搜索框是由输入和按钮组成的,而按钮的宽度是固定的,我希望输入扩展并按下搜索框的边界,直到搜索框填满它在导航栏中留下的兄弟姐妹的空间

This is how I would do it: http://jsfiddle.net/vU52q/ 我就是这样做的: http//jsfiddle.net/vU52q/

You have your wrapper and child elements: 你有你的包装和子元素:

<div class="search">
    <input type="text" />
    <button type="submit">Go</button>
</div>

And you set the container to position: relative; 然后将容器设置为position: relative; and your submit button to position: absolute; 和你的提交按钮到position: absolute; and give the container enough padding for the button. 并为容器提供足够的填充按钮。

.search {
    box-sizing: border-box;
    background: #0f0;
    padding: 5px 100px 5px 5px;
    position: relative;
    width: 300px;
}
.search input {
    width: 100%;
}
.search button {
    position: absolute;
    top: 5px;
    right: 5px;
    width: 85px;
}

That way, the input is always the width of the parent element minus the width of the fixed width button. 这样,输入始终是父元素的宽度减去固定宽度按钮的宽度。

You could also play with inline alignment and css calc , but this is my preferred way. 你也可以使用内联对齐和css calc ,但这是我的首选方式。

If you want to play with calc, you can check this out, and it might provide more of an option to add additional fixed size elements: http://jsfiddle.net/ug7a9/ 如果你想使用calc,你可以检查一下,它可能提供更多的选项来添加额外的固定大小元素: http//jsfiddle.net/ug7a9/

This takes into account each section and explicitly adds it to the width calculation. 这会考虑每个部分并明确地将其添加到宽度计算中。

<div class="search">
    <button type="submit">Go</button><!--
    --><button type="submit">Go</button><!--
    --><input type="text" /><!--
    --><button type="submit">Go</button>
</div>

The comments are in there to prevent space in between elements (you can also do a font-size: 0) on the .search element. 这些注释用于防止.search元素之间的元素之间的空间(您也可以使用font-size:0)。

* {
    box-sizing: border-box;
}
.search {
    background: #0f0;
    position: relative;
    width: 300px;
}
.search input {
    display: inline-block;
    width: calc(100% - 50px - 50px - 50px);
}
.search button {
    display: inline-block;
    width: 50px;
}

You'd want to be a bit more explicit with your selectors (ie don't do * ), but for quick demo purposes I put this in. 你希望你的选择器更明确一些(即不要做* ),但为了快速演示,我把它放进去了。

you can use the calc function but not is cross-browser 你可以使用calc函数但不是跨浏览器

calc(100% - element size);

http://jsfiddle.net/8CQHP/ http://jsfiddle.net/8CQHP/

You can see the support in caniuse.com 您可以在caniuse.com上看到支持

I ended up changing my code and using the idea from the solution Chad provided in the comment to his answer. 我最终改变了我的代码,并将评论中提供的解决方案中的想法用于他的答案。 I also used his absolute-positioned button trick. 我还使用了他绝对定位的按钮技巧。 This is what I did: 这就是我做的:

I used a span for the search box and moved the button out of the search box straight into the nav: 我在搜索框中使用了一个范围,并将按钮从搜索框中直接移到导航栏中:

<nav>
    ...

    <span>
        <input type="text" placeholder="Search...">
    </span>
    <button class="searchBtn" type="button"></button>
</nav>

Using this css: 使用这个css:

span {
    display: block;         /*This is the trick from*/
    overflow: hidden;       /*the solution Chad provided*/
}

input {
    width: 100%;
    padding-right: 65px;    /*This is to make room*/
}                           /*for the button*/

button {
    position: absolute;
    right: 0px;
    top: 0px;
}

overflow: hidden on the span makes it all possible. overflow: hidden在跨度上使一切成为可能。 Read about it here: http://colinaarts.com/articles/the-magic-of-overflow-hidden/#making-room-for-floats 在这里阅读: http//colinaarts.com/articles/the-magic-of-overflow-hidden/#making-room-for-floats

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

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