使两列高度相同

时间:2013-06-12 23:36:50

标签: jquery html5 css3 asp.net-mvc-4

我试图制作两个具有相同高度的柱子,但事实并非如此。 image

我的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强制相等?

更新 fiddle

3 个答案:

答案 0 :(得分:0)

看起来您的div高度相同。请为.man-content类添加背景颜色以供测试。

.main-content{
    float: left;
    width: 60%;
    margin-left: 5%;
    background-color: #006400; /*new*/
}

答案 1 :(得分: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);

答案 2 :(得分: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);