English 中文(简体)
在 div 内固定的非滚动页脚?
原标题:Fixed non scrolling footer inside a div?

I am making a small div on the center of the page which has a footer which is fixed but the div is scroll-able.
According to me there are two ways to do it:

  1. Using position:fixed : Fixed position actually work relative to the browser window but I want position relative to my small div
  2. Using position:absolute : Using bottom:0; I solved the problem initially but the footer scroll with the div text.

<% 1 > HTML :

<div id="wrapper">
    <div id="box">
        <div id="header">
            <h1>Heading</h1>
        </div>
        <div id="content">
           Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text 
        </div>
        <div id="footer">
            <p>Fooooooooooooooooooooooooooter</p>            
        </div>
    </div>
</div>​

<强度 > CSS :

#wrapper{
    width:600px;
    height:500px;    
    border:thin solid #c00;
}
#box {
    width:400px;
    height:300px;
    margin:100px auto;            
    border:medium dashed #c00;
    position:relative;    
    overflow:auto;
}
#content {
    background-color:rgba(0,0,208,0.1);   
}
#footer {
    position:absolute;
    bottom:0px;
    width:100%;
    height:50px;
    background-color:rgba(0,0,0,0.8);
    color:#fff;
}​

见:"http://jsfiddle.net/knoxxs/6W5uq/19" rel="noreferrer" >JSdidle

如何使页脚固定这个div?

最佳回答

解答: 在您的外部

您的盒子包装框样式 :

.box-wrap {
    width:400px;
    height:300px;
    margin:100px auto;  
    position:relative;
}

... 你取出 width , margin s和 position: : : : :

#box {
    height:300px;
    margin:0;
    border:medium dashed #c00;    
    overflow:auto;
}

在定位 时,您会考虑 边界

还有一件事: < code> position: mixed 不代表父/ 父, 而是与浏览器窗口相对, 所以在此情况下它不是方法 。

问题回答
<div id="wrapper">
    <div id= outer_box >
        <div id="box">
            <div id="header">
                <h1>Heading</h1>
            </div>
            <div id="content">
               {text}
            </div>
        </div>
        <div id="footer">
            <p>Fooooooooooooooooooooooooooter</p>            
        </div>
    </div>
</div>​

然后在元素中添加一些样式,如下文所示:http://jsfiddle.net/6W5uq/29/

基本上,您使页脚与外部框的底部对齐。我给内容添加了边距, 这样您仍然可以看到滚动时所有的边距, 也可以看到页脚上的边距, 这样您就可以完全使用滚动条 。

而不是使用 # footer, 您需要做出某些修改。 尝试以下内容 :

In #footer add margin-top:10px; instead of position:absolute; bottom:0px;

define this margin-top to the height you want the footer to be below the "some texts" div. This is happening because the footer is being set to the bottom of the div but as there is more text to come and the overflow is on, it sticks to the bottom of the height of the div rather than the bottom of the content





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...