CSS:内容区域必须采用 100% 高度

发布于 2024-11-26 20:58:40 字数 2487 浏览 1 评论 0原文

这让我发疯...我正在 http://one29090testvdscom.accounts.combell 构建页面。净/nl。该页面具有页眉、左侧菜单、内容和页脚。宽度固定为 960px,水平居中。这一切都有效。但是,如果内容中的文本很少,我希望灰色内容区域始终采用可用的屏幕高度,以便页脚位于页面下方。

页面看起来像:

  <body>

    <!-- Centered container that contains site -->
    <div id="centeredcontainer">            
        <!-- Area with header -->
        <div id="header">                
            header here                              
        </div>            
        <!-- Area that contains main menu on the left and content on the right -->
        <div id="mainmenuandcontent">
            <!-- Main menu on the left -->
            <div id="mainmenu">
                main menu here                   
            </div>
            <!-- Content on the right -->   
            <div id="content">
                 body here
            </div>                
            <!-- Clears the floats so that next elements can use margins normally -->
            <div class="clearer"></div>
            <!-- Red line under content -->
            <div id="RedLineUnderContent"></div>
        </div>                         
        <!-- Area with languages -->
        <div id="languages">
          footer here           
        </div>                       
    </div>            
</body>

相关的 CSS 是:

body
{
    font-size: 12px;
    font-family:Century Gothic,Helvetica,Verdana,Arial,sans-serif; 
    line-height:1.5em;      
    margin: 0; 
    padding: 0;   
    background-color: Black;
}

#centeredcontainer  
{
    width: 960px;    
    margin-left: auto ;
    margin-right: auto ;
}

#header
{
    margin-bottom: 20px; 
    margin-top:20px;  
}

#mainmenuandcontent
{  
    width: 960px;
    clear: both;       
    position: relative;    
}

#mainmenu
{        
    float: left;   
    width:180px; 
    padding:10px;
}

#content
{
    float: left;
    background-color: #403F3F;        
    width: 760px; 
    min-height: 400px;
    color:White;
}

#RedLineUnderContent
{
    height: 20px;
    background: #A10C10;
    margin-left: 200px; 
}

#languages
{        
    margin-top: 10px;   
    margin-left: 200px; 
    margin-bottom:20px;  
    text-transform:uppercase;  
}

.clearer
{
    clear:both;
}

this is driving me crazy... I'm building the page at http://one29090testvdscom.accounts.combell.net/nl. It's a page that has a header, left menu, content and footer. Width is fixed at 960px, centered horizontally. This all works. But then, if there is little text in the content, I would like that the gray content area always takes the available screen height, so that the footer is down the page.

The page looks like:

  <body>

    <!-- Centered container that contains site -->
    <div id="centeredcontainer">            
        <!-- Area with header -->
        <div id="header">                
            header here                              
        </div>            
        <!-- Area that contains main menu on the left and content on the right -->
        <div id="mainmenuandcontent">
            <!-- Main menu on the left -->
            <div id="mainmenu">
                main menu here                   
            </div>
            <!-- Content on the right -->   
            <div id="content">
                 body here
            </div>                
            <!-- Clears the floats so that next elements can use margins normally -->
            <div class="clearer"></div>
            <!-- Red line under content -->
            <div id="RedLineUnderContent"></div>
        </div>                         
        <!-- Area with languages -->
        <div id="languages">
          footer here           
        </div>                       
    </div>            
</body>

And the relevant CSS is:

body
{
    font-size: 12px;
    font-family:Century Gothic,Helvetica,Verdana,Arial,sans-serif; 
    line-height:1.5em;      
    margin: 0; 
    padding: 0;   
    background-color: Black;
}

#centeredcontainer  
{
    width: 960px;    
    margin-left: auto ;
    margin-right: auto ;
}

#header
{
    margin-bottom: 20px; 
    margin-top:20px;  
}

#mainmenuandcontent
{  
    width: 960px;
    clear: both;       
    position: relative;    
}

#mainmenu
{        
    float: left;   
    width:180px; 
    padding:10px;
}

#content
{
    float: left;
    background-color: #403F3F;        
    width: 760px; 
    min-height: 400px;
    color:White;
}

#RedLineUnderContent
{
    height: 20px;
    background: #A10C10;
    margin-left: 200px; 
}

#languages
{        
    margin-top: 10px;   
    margin-left: 200px; 
    margin-bottom:20px;  
    text-transform:uppercase;  
}

.clearer
{
    clear:both;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

末骤雨初歇 2024-12-03 20:58:40

据我所知,没有跨浏览器的方法可以单独用 CSS 来做到这一点。

我昨晚刚刚使用 JS 做了类似的事情:

<head>
   ....
    <script type="text/javascript">
        function resize() {
            var frame = document.getElementById("main");
            var windowheight = window.innerHeight;
            document.body.style.height = windowheight + "px";
            frame.style.height = windowheight - 180 + "px";
        }
     </script> 
</head>
<body onload="resize()" onresize="resize()">
...

这将调整 id 为“main”的元素的大小,使其成为窗口的可见高度减去 180px(180is 是我的标题的高度)

I know of no cross browser way to do this in CSS alone.

I just did something similar to this last night using JS though:

<head>
   ....
    <script type="text/javascript">
        function resize() {
            var frame = document.getElementById("main");
            var windowheight = window.innerHeight;
            document.body.style.height = windowheight + "px";
            frame.style.height = windowheight - 180 + "px";
        }
     </script> 
</head>
<body onload="resize()" onresize="resize()">
...

This will resize the Element with id "main" to be the visible height of the window less 180px (180is is the height of my header)

怕倦 2024-12-03 20:58:40

也许您可以使用类似于 此页面的 CSS,它实现了 100% 高度布局

Perhaps you could use CSS similar to this page, which achieves a 100% height layout?

等风来 2024-12-03 20:58:40

我想我找到了使用 jquery 的解决方法。我将以下脚本添加到母版页中:

    <script type="text/javascript">
        var contentHeight;
        $(document).ready(function () {
            contentHeight = $('#content').height();
            Resize();
            $(window).resize(function () {
                Resize();
            });
        });                    
        function Resize() {
            currentWindowHeight = $(document).height();                                
            if (contentHeight < currentWindowHeight - 160) {
                $('#content').css('height', (currentWindowHeight - 160) + "px");   
            }                               
        }
    </script>

因此,每次页面加载后,它都会获取内容区域原始高度的高度。然后它根据窗口高度调整其大小,以便当内容高度小于窗口高度时,它会调整大小以采用窗口高度(减去页眉和页脚高度)。如果调整浏览器窗口的大小,它会执行完全相同的操作(使用原始内容高度,而不是当前内容高度!)。

当然,我更喜欢 CSS 解决方案,所以如果有人想出一个好主意,请分享:)

I think I found a workaround using jquery. I added the following script to the masterpage:

    <script type="text/javascript">
        var contentHeight;
        $(document).ready(function () {
            contentHeight = $('#content').height();
            Resize();
            $(window).resize(function () {
                Resize();
            });
        });                    
        function Resize() {
            currentWindowHeight = $(document).height();                                
            if (contentHeight < currentWindowHeight - 160) {
                $('#content').css('height', (currentWindowHeight - 160) + "px");   
            }                               
        }
    </script>

So after each page load, it gets the height of the original height of the content area. Then it resizes it according to the window height, so that when the content height is smaller than the window height, it resizes to take the window height (minus header and footer heights). If the browser window then is resized, it does exactly the same (using the original content height, not the current!).

Of course, I would have preferred a CSS solution, so if anyone comes up with a bright idea, share it :)

陌若浮生 2024-12-03 20:58:40

为此,您不需要 JavaScript。请参阅我对相关问题的回答,“使用CSS将div设置为剩余高度,上方和下方的div高度未知”。内容将扩展以填充可用区域。

You don't need JavaScript for this. Please see my answer for a related question, "Set div to remaining height using CSS with unknown height divs above and below". The content will expand to fill the available area.

入怼 2024-12-03 20:58:40

您可以将容器的 min-height 设置为 100%,但您还必须将父容器的 min-height 设置为 100%,否则它将不起作用。
对于不太现代的浏览器,有时需要将 body 甚至 html 标签设置为高度 100%(不是 min-height!)。

You can set the min-height of the container to 100%, but you also have to set the parent containers to min-height 100% otherwise it won't work.
For less modern browsers it sometimes is needed to set the body and even the html tag to height 100% (not min-height!).

和影子一齐双人舞 2024-12-03 20:58:40

在容器上放置高度:100%?

2016 年更新:使用视口相对单位而不是百分比或像素,例如 width: 100wv;高度:100wh;。该单位代表浏览器窗口大小的百分比,因此忽略任何父元素大小。

Putting a height: 100% on the container?

Updated 2016: Use viewport-relative units instead of percentages or pixels, e.g. width: 100wv; height: 100wh;. The unit stands for a percentage of the browser window size, thus ignoring any parent element sizes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文