1

我试图使两列具有相同的高度,但事实并非如此。 图片

我的html代码:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8" />
   <title>@ViewBag.Title</title>
   <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
   <meta name="viewport" content="width=device-width" />
   @Styles.Render("~/Content/css")
   @Scripts.Render("~/bundles/modernizr")
   @Scripts.Render("~/bundles/jquery")

   <script type="text/javascript">
        jQuery(document).ready(function () {
        var divone = jQuery(".main-content").height();
        var divtwo = jQuery(".sidebar").height();
        var maxdiv = Math.max(divone, divtwo);
        jQuery(".main-content").height(maxdiv);
        jQuery(".sidebar").height(maxdiv);
    });
 </script>
</head>
<body>
@RenderBody()
<header>
    <h1>Document Heading</h1>
    <div class="nav">
        <ul class="menu">
            <li>@Html.ActionLink("Home", "Index", "Home")</li>
            <li>@Html.ActionLink("About", "About", "Home")</li>
            <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
            <li>@Html.ActionLink("Sermon", "Sermon", "Home")</li>
        </ul>
    </div>
</header>

<div class="main-content">

    <h2>Column 1</h2>
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</p>
</div>
<div class="sidebar">
    <h2>Column 2</h2>
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</p>
    <ul>
        <li><a href="#">Link 1</a></li>
        <li><a href="#">Link 2</a></li>
    </ul>
</div>
<footer>FOOTER</footer>
</body>
</html>

它是 asp.net mvc 4 _Layout.cshtml。部分CSS是:

body {
font-size: .85em;
font-family: "Segoe UI" , Verdana, Helvetica, Sans-Serif;
margin: 0;
padding: 0;
background-color: #CCCCFF;
}


 .main-content
{
float: left;
width: 60%;
margin-left: 5%;
}

.sidebar
{
float: right;
width: 35%;
background-color: #00FFFF;
}

footer
{
margin: 0px;
clear: both;
background-color: #FF6600;
font-size: .8em;
text-align: center;
height: 50px;
position: absolute;
bottom: 0;
width: 100%;
padding-top: 20px;
}

为什么两列的高度不同,即使它们被 jquery 强制相等?

更新: 小提琴

4

3 回答 3

0

看起来你的 div 高度是一样的。请为 .man-content 类添加背景颜色以进行测试。

.main-content{
    float: left;
    width: 60%;
    margin-left: 5%;
    background-color: #006400; /*new*/
}
于 2013-06-12T23:58:12.893 回答
0

我会用jquery来做。

var column1height = $(column1).innerHeight();
var column2height = $(column2).innerHeight();
if (column1height > column2height){
  highestheight = column1;
}else{
    highestheight = column2;
}
$(column1).css('height',highestheight);
$(column2).css('height',highestheight);
于 2013-06-13T00:02:37.897 回答
0

自己解决了。

var divone = jQuery(".main-content").height();
var divtwo = jQuery(".sidebar").height();
var mainheight = $(window).height() - $('header').height() - $('footer').height();
jQuery(".main-content").height(mainheight);
jQuery(".sidebar").height(mainheight);
于 2013-06-13T13:30:10.647 回答