Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make multiple inline-block elems to stretch full width of the container?

Tags:

html

css

block

What is the best way to implement input field and button behavior as displayed here: enter image description here

like image 917
panfil Avatar asked Nov 15 '12 17:11

panfil


People also ask

How do inline-block elements add up to 100% width?

If you have a border or padding set for your divs, then you've created additional pixels that will prevent your divs from adding up to a 100% width. To fix this, make sure you've added box-sizing: border-box to the div's styles.

How do I increase the width of an inline-block?

Inline elements are built for text and thus they should flow inline with the text, and only be as wide as the text they contain. Thus, you can't set the width of an inline element. Block elements are made to fill all the available width by default and are thus on their own line rather than flowing inline.

Do inline elements take up the entire width of their container?

An inline element does not start on a new line. An inline element only takes up as much width as necessary. This is a <span> element inside a paragraph.

How do I change the width of an inline element?

To apply width, set css property 'display' to either 'block' or 'inline-block'. block: the element will sit in a single line. In such case you may want to set float so links are in the same line; inline-block; the element will have height, width, etc, and multiple elements will sit in the same line (block).


2 Answers

I like the answer of ScottS, but just to have an alternative: you could use table-like behaviour in CSS:

CSS

.formline{
    display: table;
}
.txt{
    display: table-cell;
    width: 100%;
}
input[type=text]{
    -moz-box-sizing: border-box; box-sizing: border-box;
    width: 100%;
}​

HTML

<div class=formline>
    <div class=txt>
        <input type=text>
    </div>
    <input type=submit value=submit>
</div>

http://jsfiddle.net/willemvb/VaFSP/

like image 134
Willem Van Bockstal Avatar answered Nov 05 '22 13:11

Willem Van Bockstal


Yes, with some html and css in place that uses some magic between float and overflow: hidden, you can see it working in this fiddle.

HTML

<div class="container">
    <div>Some Text</div>
    <form>
        <button>MyButton</button>
        <div class="stretcher"><input type="text" /></div>
    </form>
</div> 

CSS

.stretcher {
    overflow: hidden;
}

button {
    float: right;
}

input {
    width: 100%;
}
like image 26
ScottS Avatar answered Nov 05 '22 12:11

ScottS