Dwukolumnowy układ div z płynną lewą i stałą prawą kolumną

Chcę zrobić układ dwóch kolumn za pomocą DIVs, gdzie prawa kolumna będzie miała stałą szerokość 200px, a lewa zajmie całą pozostałą przestrzeń.

Jest to dość proste, jeśli używasz tabel:

<table width="100%">
  <tr>
    <td>Column 1</td>
    <td width="200">Column 2 (always 200px)</td>
  </tr>
</table>

Ale co powiesz na divy? Czy jest to możliwe? Jeśli tak, to jak?

Author: Salman A, 2011-04-13

7 answers

Poniższe przykłady są uporządkowane źródłowo, tzn. kolumna 1 pojawia się przed kolumną 2 w źródle HTML. To, czy kolumna pojawia się po lewej, czy po prawej jest kontrolowane przez CSS:

Fixed Right

#wrapper {
  margin-right: 200px;
}
#content {
  float: left;
  width: 100%;
  background-color: #CCF;
}
#sidebar {
  float: right;
  width: 200px;
  margin-right: -200px;
  background-color: #FFA;
}
#cleared {
  clear: both;
}
<div id="wrapper">
  <div id="content">Column 1 (fluid)</div>
  <div id="sidebar">Column 2 (fixed)</div>
  <div id="cleared"></div>
</div>

Fixed Left

#wrapper {
  margin-left: 200px;
}
#content {
  float: right;
  width: 100%;
  background-color: #CCF;
}
#sidebar {
  float: left;
  width: 200px;
  margin-left: -200px;
  background-color: #FFA;
}
#cleared {
  clear: both;
}
<div id="wrapper">
  <div id="content">Column 1 (fluid)</div>
  <div id="sidebar">Column 2 (fixed)</div>
  <div id="cleared"></div>
</div>

Alternatywnym rozwiązaniem jest użycie display: table-cell ; co powoduje, że kolumny o równej wysokości.

 86
Author: Salman A,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:33

Oto rozwiązanie (i ma pewne dziwactwa, ale daj znać, jeśli je zauważysz i że są problemem):

<div>
    <div style="width:200px;float:left;display:inline-block;">
        Hello world
    </div>
    <div style="margin-left:200px;">
        Hello world
    </div>
</div>
 11
Author: leetNightshade,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-13 08:16:06

CSS:

#sidebar {float: right; width: 200px; background: #eee;}
#content {overflow: hidden; background: #dad;}

HTML:

<div id="sidebar">I'm 200px wide</div>
<div id="content"> I take up the remaining space <br> and I don't wrap under the right column</div>

Powyższe powinno zadziałać, możesz umieścić ten kod w opakowaniu, jeśli chcesz dać mu szerokość i wyśrodkować go, overflow:hidden na kolumnie bez szerokości jest kluczem do uzyskania go zawierać, pionowo, jak w nie zawijać wokół bocznych kolumn (może być w lewo lub w prawo)

IE6 Może trzeba zoom:1 ustawić na # content div też jeśli potrzebujesz wsparcia

 6
Author: clairesuzy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-13 08:18:04

CSS Solutuion

#left{
    float:right;
    width:200px;
    height:500px;
    background:red;   
}

#right{
    margin-right: 200px;
    height:500px;
    background:blue;
}

Sprawdź działający przykład na http://jsfiddle.net/NP4vb/3/

JQuery Solution

var parentw = $('#parent').width();
var rightw = $('#right').width();
$('#left').width(parentw - rightw);

Sprawdź działający przykład http://jsfiddle.net/NP4vb/

 6
Author: Hussein,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-13 08:37:00

Niedawno pokazano mi tę stronę dla płynnych layoutów za pomocą CSS. http://matthewjamestaylor.com/blog/perfect-multi-column-liquid-layouts (Spójrz na strony demo w linkach poniżej).

Autor dostarcza teraz przykład dla układów o stałej szerokości. Zobacz też; http://matthewjamestaylor.com/blog/how-to-convert-a-liquid-layout-to-fixed-width .

Daje to następujące przykład(y)), http://matthewjamestaylor.com/blog/ultimate-2-column-left-menu-pixels.htm (dla dwóch kolumn układ jak ty po myślę)

Http://matthewjamestaylor.com/blog/fixed-width-or-liquid-layout.htm (dla układu trzech kolumn).

Przepraszam za tak wiele linków do tej strony facetów, ale myślę, że jest to niesamowity zasób.

 1
Author: Mr Moose,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-13 08:01:17

Myślę, że jest to prosta odpowiedź , to podzieli dzieci devs 50% każdy na podstawie szerokości rodzica.

 <div style="width: 100%">
        <div style="width: 50%; float: left; display: inline-block;">
            Hello world
        </div>
        <div style="width: 50%; display: inline-block;">
            Hello world
        </div>
    </div>
 1
Author: Anju313,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-03 03:35:33

#wrapper {
  margin-right: 50%;
}
#content {
  float: left;
  width: 50%;
  background-color: #CCF;
}
#sidebar {
  float: right;
  width: 200px;
  margin-right: -200px;
  background-color: #FFA;
}
#cleared {
  clear: both;
}
<div id="wrapper">
  <div id="content">Column 1 (fluid)</div>
  <div id="sidebar">Column 2 (fixed)</div>
  <div id="cleared"></div>
</div>
 0
Author: Serega Zgama,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-23 23:56:06