【问题标题】:div fit to top of pagediv 适合页面顶部
【发布时间】:2017-10-07 20:15:15
【问题描述】:

我正在尝试创建一个适合页面顶部并填充宽度的 div 元素。我有它填充宽度,但我无法让它适合页面顶部,div 上方有一个小间隙。

我已将主体设置为具有 0 边距和 0 填充,那么是什么在顶部造成了 ~20px 的间隙。

我的html如下

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Photography</title>    
    <style>
        p {
            padding-top: 20px;
            padding-right: 20px;
            padding-bottom: 20px;
            padding-left: 20px;
        }    
    </style>
</head>
<body bgcolor="#bbbbbb" style="margin:0;padding:0">

    <div style="background-color:#999999;margin:0;padding:0">
        <h1 style="color:#ffffff" >Photographs</h1>
    </div>

    <div>
        <p align="center" ><img src="IMG_2574.png" alt="Campground" style="width:;height:;"></p>
        <p align="center"><img src="IMG_2593.png" alt="Lake Sunset" style="width:;height:;"></p>
        <p align="center"><img src="IMG_2702.png" alt="Hudson River" style="width:;height:;"></p>
        <p align="center"><img src="IMG_3129.png" alt="Central Park" style="width:;height:;"></p>
        <p align="center"><img src="IMG_3229.png" alt="The Met" style="width:;height:;"></p>
        <p align="center"><img src="IMG_3384.png" alt="Break Dancer" style="width:;height:;"></p>
        <p align="center"><img src="IMG_3486.png" alt="Brooklyn Bridge" style="width:;height:;"></p>
        <p align="center"><img src="IMG_3656 2.png" alt="Rockefeller Center" style="width:;height:;"></p>
        <p align="center"><img src="IMG_3809 2.png" alt="Times Square" style="width:;height:;"></p>
        <p align="center"><img src="IMG_3824 2.png" alt="Top of the Rock" style="width:;height:;"></p>
        <p align="center"><img src="IMG_7620.png" alt="Lake Louise" style="width:;height:;"></p>
        <p align="center"><img src="IMG_7907.png" alt="Mt Hood" style="width:;height:;"></p>
        <p align="center"><img src="IMG_7950.png" alt="Multnomah Falls" style="width:;height:;"></p>
        <p align="center"><img src="IMG_8193.png" alt="Cape Kiwanda" style="width:;height:;"></p>
        <p align="center"><img src="IMG_8254.png" alt="Yaquina Head Lighthouse" style="width:;height:;"></p>
        <p align="center"><img src="IMG_8377.png" alt="Cape Perpetua" style="width:;height:;"></p>
        <p align="center"><img src="IMG_8441.png" alt="Simpson Reef" style="width:;height:;"></p>
        <p align="center"><img src="IMG_9711.png" alt="Nevada" style="width:;height:;"></p>
        <p align="center"><img src="IMG_9730.png" alt="Salt Flats" style="width:;height:;"></p>
    </div>
</body>
</html>

【问题讨论】:

    标签: html css


    【解决方案1】:

    只需添加这个:

    h1 {
        margin-top: 0; /* It will fix the top margin */
    }
    

    标题具有默认的顶部和底部边距。在 Chrome 中,默认边距为 0.67em,这将为您提供大约 20px。

    h1 {
        -webkit-margin-before: 0.67em;
    }
    

    这就是我们需要将其设置为零的原因。

    【讨论】:

    • 谢谢。当我使 div 位置:固定时,顶部空间再次出现。这是为什么呢?
    • 另外,尽量避免网络上文件名中的空格。有时可以,有时不行。一般来说,IMG_3824_2.pngIMG_3824 2.png
    【解决方案2】:

    您可以使用* 删除浏览器添加的任何边距或内边距,然后可以为您的元素提供任何自定义内边距或边距。

    在你的 CSS 顶部使用它:

    *{
       margin:0;
       padding:0
    }
    

    【讨论】:

      【解决方案3】:

      div{background-color:#999999;margin:0;padding:0}
      h1{margin-top:-8px;color:#ffffff;}
      <div \>
          <h1  >S.Field Photographs</h1>
      </div>

      【讨论】:

        【解决方案4】:

        包含以下代码

        <head>
        ....
        <style>
        #your_div_id {
                    height: 100%;
                }
        </style>
        ....
        </head>
        

        【讨论】:

          猜你喜欢
          • 2013-11-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多