为什么我的HTML元素之间会出现空格?

And*_*yNZ 5 html css web

我正在为我的妈妈背包客业务设计一个网站.我遇到的问题是在我的横幅图像和导航栏之间有一条空白线,您可以在图像中看到.我认为这与保证金有关,所以我将它设置为零,两个元素都无济于事.

还有第二个问题 - 为什么我的黑色边框也不包括主要内容?我认为,因为它的身体背景,它会绕过身体的每个元素.

我意识到可能有类似的问题,但我无法在任何地方找到答案.我会感激任何人的意见 - 这是我在这里发表的第一篇文章,所以如果我搞砸任何格式,我很抱歉.

我的网站图片可以在这里找到:

http://postimage.org/image/20dhjcdb8/

提前致谢.

我目前在index.html文件中有以下代码:

   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
        <link rel="stylesheet" type="text/css" href="swaggersstyle.css">
            <title>Oamaru Backpackers Hostel, Swaggers Backpackers - Home</title>
        </head>


        <body>
        <img src="final.jpg" id="banner"></img>
        <ul id="nav">
            <li class="links"><a href="index.html">Home</a></li>
            <li class="links"><a href="planning.html">Planning</a></li>
            <li class="links"><a href="construction.html">Construction</a></li>
            <li class="links"><a href="evaluation.html">Evaluation</a></li>
        </ul>

        <div id="mainc">
        <p>Make Yourself at Home</p>
    <p>Swaggers Backpackers is a converted old house located within walking distance of all the best parts of Oamaru. Explore the old victorian era buildings and shops of the city centre, or see the penguin colonies down the street. Swaggers is owned and operated by camp mum Agra, who makes all guests feel welcome, informed, and perhaps a bit mothered. </p>

        </div>   

    </body>
    </html>
Run Code Online (Sandbox Code Playgroud)

和以下CSS代码:

html{
    font-family: sans-serif;
    background-color:#464E54;
}

body{
    width: 960px;
    margin: auto;
    background-color: white;
    border: 5px solid black;
}

#banner{
    padding: 0px;
    margin: 0;
}

#nav {
    list-style-type: none;
    padding: 0px;
    margin: 0px;
    overflow: hidden;

}

#mainc {

    width: 960px;
    float: right;
    background-color: white;
    margin: 0;
}

.links {
    float: left;
    margin: 0px;
}

a:link, a:visited {

    display: block;
    width: 232px;
    font-weight: bold;
    color: grey;
    background-color: #dad8bf;
    text-align: center;
    padding: 4px;
    text-decoration: none;
    text-transform: uppercase;
    margin-top: 0px;
}

a:hover, a:active{

    background-color: #333333;
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*iva 3

我遇到的问题是在我的横幅图像和导航栏之间有一条空白的白线,您可以在图像中看到。我认为这与边距有关,因此我将这两个元素都设置为零,但无济于事。

在 HTML 中,图像默认是内联级别元素,因此它们遵循文本规则(并且下面会有空格以保持与“p”等字母的正确对齐)。您可以分配display: block给标题图像,或定义标题容器与图像具有相同的精确高度

还有第二个问题 - 为什么我的黑色边框没有覆盖主要内容?我认为既然它是一个身体背景,它就会围绕身体的每个元素。

因为浮动元素会从容器中弹出,所以您必须清除浮动以使用类似的方法扩展容器

<div style="clear: both"></div>
Run Code Online (Sandbox Code Playgroud)

或者使用一些重置/清除CSS,例如html5boilerplate提供的CSS 。