Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/CSS Two auto-width columns

I'm having an incredibly difficult time achieving the following effect:

==========================================================
= Variable Width =  <input style="width: 100%" />        =
==========================================================

I am using the following HTML:

<dl>
  <dt>
    <label>Variable Width</label>
  </dt>
  <dd>
    <input style="width: 100%" />
  </dd>
</dl>

Please note that I've trimmed down the HTML for readability.

Can anyone suggest what CSS I should use to achieve this effect? I would prefer to not have to use display: table because I am looking for cross-browser compatibility that reaches IE7.

like image 954
Wex Avatar asked Sep 21 '11 18:09

Wex


People also ask

How do I put two columns together in HTML?

To merge table columns in HTML use the colspan attribute in <td> tag. With this, merge cells with each other. For example, if your table is having 4 rows and 4 columns, then with colspan attribute, you can easily merge 2 or even 3 of the table cells.

How do I make all the columns the same width in CSS?

The CSS to make all the columns equal in width is as follows. The table-layout: fixed; rule specifies that the table is to take its dimensions from the <table> element's width and the width of the first row of cells (or the <col> elements, if you have them).

How do I make two sections side by side in HTML?

Using float and margin The left div is styled with width:50% and float:left – this creates the green colored div that horizontally covers half of the screen. The right div is then set to margin-left:50% – this immediately places it to the right of the left div.

How do I put two elements side by side in a div?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.


1 Answers

This is "incredibly difficult" to do without <table> or display: table.. until you know how!

See: http://jsfiddle.net/thirtydot/aLgwt/

This works in IE7 and greater + all modern browsers.

dt {
    float: left
}
dd {
    overflow: hidden;
    padding: 0 4px 0 9px
}
dd input {
    width: 100%
}

An explanation of why it works is here.

like image 161
thirtydot Avatar answered Nov 11 '22 00:11

thirtydot