css如何在html页面的底部定位元素

时间:2018-05-04 06:35:41

标签: html css css3

enter image description here

在蓝色if-else之后,我的页面上没有任何内容。表示<div> 页面结束后

我想要的是将蓝色<div>降低到页面底部。 在某种意义上,即使有人调整了浏览器窗口的大小,页面的底部也应该始终位于页面底部。<div>。 (相对于视口高度。

如何使用CSS实现此目的? 谢谢!

<div>

5 个答案:

答案 0 :(得分:0)

你的第一个div是200px,h标签大约200px但你的屏幕尺寸大于400px所以有空隙,将底部0px添加到蓝色div

答案 1 :(得分:0)

试试吧

   <!doctype html>
    <html>
    <body>
    <div style="height:200px;background-color:red;">
    </div>
    <h1>Some other content</h1>
    <h1>Some other content</h1>
    <h1>Some other content</h1>
    <div style="position: fixed;left: 0px;bottom: 0px;width: 100%;background-color:blue;color:white">
        footer
    </div>

    </body>
    </html>

答案 2 :(得分:0)

试试这个

<!doctype html>
<html>
<body>
<div style="height:200px;background-color:red;">
</div>
<h1>Some other content</h1>
<h1>Some other content</h1>
<h1>Some other content</h1>
<div style="height:25px;bottom:0px;background-color:blue;color:white">
    footer
</div>

</body>
</html>

答案 3 :(得分:0)

编辑:移动响应

CSS部分:

body { margin:0; padding:0; border:0; outline:0; }
section { height:100%; position:relative; display:block; }
section div { padding:10px; min-height:100%; }
footer { display:block; margin-top:10px; height:40px; line-height:40px; position:relative; text-align:center; background:blue; color:#fff; }

HTML部分:

<section>
    <div>
        <h1>Testing</h1>
        <h1>Heading 1</h1>
        <p>What is Lorem Ipsum?<br>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        <p>Why do we use it?<br>
        It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
        <p>Where does it come from?<br>
        Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
        <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
        <p>Where can I get some?<br>
        There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>
    </div>
    <footer>
        Footer
    </footer>
</section>

用您自己的文本替换所有虚拟文本。虚拟文本用于测试滚动。

为什么不尝试在页脚之前添加另一个div元素或section元素?这个div元素应该包含你所有的h1。

在section / div元素中,放入另一个div元素 - 这次使用类名或内联样式。这种内联样式应该有一个确定的高度,比如大约900-1000px。

<section style="height:auto;">
    <div style="height:1000px;">
        <h1>Heading 1</h1>
        <h1>Heading 1</h1>
    </div>
</section>
<div style="margin-top:20px; height:25px; line-height:25px; background-color:blue; color:#fff;">footer</div>

只要您的内容大约在900px以上,页脚就会始终位于底部。你需要有一些占用空间的东西才能使页脚位于底部,假设你不想在向下滚动时将其固定在屏幕上。

答案 4 :(得分:0)

  1. 如果您希望您的页脚内容在页面滚动期间始终位于页面底部,并且在滚动期间也可以向用户显示然后只需添加 position:fixed;bottom:0; 这将保留在页面底部甚至是页面滚动的页脚,是的,你必须定义页脚的宽度,如果你想要特定的宽度或使用width:1024px或你需要什么,如果你想左右对齐然后添加{{根据您的需要1}}和left:0

    right:0

  2. 阅读您的其他评论后 如果我没有错,那么你想要这个代码

  3. 进行以下更改

    1. 在body标记中添加<div style="height:25px;background-color:blue;display:block;color:white;position:fixed;bottom:0;left:0;"> footer ,以便您的正文标记看起来像这样
    2. style='margin:0;padding:0;'

      1. 根据需要在页脚div之前添加一些<body style='margin:0;padding:0;'>标记,以便页脚位于所需的底部。

      2. <br />添加到页脚div

        margin-bottom:0;padding-bottom:0;

      3. 最终代码看起来像这个

        <div style="height:25px;background- 
           color:blue;display:block;color:white;margin-bottom:0;padding-bottom:0;">
         footer
        </div>

        enter image description here

        我希望这会解决。