On a two-column page, how can I grow the left div to the same height of the right div using CSS or Javascript?

后端 未结 11 2753
野趣味
野趣味 2020-12-15 07:17

I\'m trying to make a two-column page using a div-based layout (no tables please!). Problem is, I can\'t grow the left div to match the height of the right one. My right di

11条回答
  •  悲哀的现实
    2020-12-15 07:47

    It can be done in CSS! Don't let people tell you otherwise.

    The easiest, most pain-free way to do it is to use the Faux Columns method.

    However, if that solution doesn't work for you, you'll want to read up on this technique. But be warned, this is the kind of CSS hackery that will make you wake up in a cold sweat in the middle of the night.

    The gist of it is that you assign a large amount of padding to the bottom of the column, and a negative margin of the same size. Then you place your columns in a container that has overflow: hidden set. More or less the padding/margin values allow the box to keep expanding until it reaches the end of the wrapper (which is determined by the column with the most content), and any extra space generated by the padding is cut off as overflow. It doesn't make much sense, I know...

    Content
    Longer Content
    #wrapper { overflow: hidden; } #col1, #col2 { padding-bottom: 9999px; margin-bottom: -9999px; }

    Be sure to read the entire article I linked to, there are a number of caveats and other implementation issues. It's not a pretty technique, but it works fairly well.

提交回复
热议问题