本文介绍了CSS中的两个相等的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有很多文章但是许多都没有更新,我不明白如果真的有一个最好的方法来建立CSS中的2个相等的列(不知道哪一个将是最长的)。

There seems to be many articles on the web, but many are not updated and I can't understand if there is really a BEST way to build 2 equal columns in CSS (without knowing which one of them will be the longest).

示例:

我想要实现的是B列将扩展到A列的大小。此外,如果B列长于A列,

What I want to achieve is that column B will strech all the way to the size of column A. Also, if column B will be longer than column A, then I want column A to strech all the way to the size of column B.

感谢

Joel

推荐答案

这应该可行,分开所需的特定样式,并在可能的地方评论。

This should work, ive seperated the specific styles needed, and commented where possible.

希望它有帮助。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Equal 2cols</title>

<style type="text/css">
    /*common styles*/
        html, body, h1, h2, h3{margin:0; padding:0;}
        .clearme{clear:both;}

    /*generic layout styles*/
        #container{width:960px; margin:0 auto;}
        #header{width:960px; height:100px; background-color:#999; margin:0 0 10px 0;}
        #footer{width:960px; height:100px; background-color:#999; margin:10px 0 0 0;}

    /*equal height specific styles*/
        #layout{clear:left; float:left; width:100%; overflow:hidden; background:#f60; /* column 2 background colour */}
        #content{float:left; width:100%; position:relative; right:50%; background:#f00; /* column 1 background colour */}
        #col1 {float:left; width:46%; position:relative; left:52%; overflow:hidden;}
        #col2 {float:left; width:46%; position:relative; left:56%; overflow:hidden;}
</style>

</head>

<body>
    <div id="container">
        <div id="header">
            <h1>header</h1>
        </div><!--/#header-->

        <div id="layout">

            <div id="content">
                <div id="col1">
                    <h2>A</h2>
                </div><!--/#col1-->

                <div id="col2">
                    <h2>B</h2>
                    <p>As long as you like</p>
                </div><!--/#col2-->

            </div><!--/#content-->
        </div><!--/#layout-->

        <div class="clearme"></div>

        <div id="footer">
                <h3>Footer</h3>
        </div><!--/#footer-->

    </div><!--/#container-->
</body>
</html>

这篇关于CSS中的两个相等的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:18