【问题标题】:Margin collapse causing a Div to move.保证金崩溃导致 Div 移动。
【发布时间】:2014-05-18 05:32:12
【问题描述】:

我正在处理 HTML 并且遇到了我认为是边距折叠的问题。当我取出“border:solid black 1px;”行时从我的 CSS 中的 Div #body 中,它会导致 Div #header 在页面上下降约 20%。我无法弄清楚它为什么这样做,或者如何解决它。我已经尝试将 body Div 边框设置为 0px 并且它做同样的事情。如果我完全取出 body Div,它会做同样的事情。

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Cast & Crew - Home Page</title>
<style type="text/css"> 

</style>
<link href="css2.css" type="text/css"                        
              rel="stylesheet" />

</head>

<body>
<div id="body">
<div id ="header">
    <span class="float"><img class="img" src = "face2.png" alt="Face mask"></span>
        Cast & Crew Restaurant

    <span class="floatr"><img class="img" src = "face.png" alt="Face mask"></span>


<table id="links">
<tr>
    <th><a href="Home.html"> Home</a></th>
    <th><a href="Menu.html"> Menu</a></th>
    <th><a href="plabout.html">About Us</a></th>
    <th><a href="Contact.html">Contact</a></th>
</tr>
</table>
</div>
<div class ="sidebar">
<table>
    <tr><td><a href="#app">Appetizers</a></td></tr>
    <tr><td><a href="#salad">Salads</a></td></tr>
    <tr><td><a href="#burger">Burgers</a></td></tr>
    <tr><td><a href="#sand">Sandwiches</a></td></tr>
    <tr><td><a href="#pasta">Pasta</a></td></tr>
    <tr><td><a href="#entree">Entrées</a></td></tr>
    <tr><td><a href="#pizza">Pizza</a></td></tr>

</table>
</div>
</div>
</body>
</html>

这是 CSS 代码

a:link {color:#FFFFFF;}
a:visited {color:#0066FF}
a:hover {color:#3385FF;}
a:focus {color:#B20000;}
a:active {background-color: white; color:black;}

.blankrow{height: 40px !important; 
    }

#body   {height: 1150px; width: 100%;
    border:solid black 2px;}


#header     {position: fixed;
    background:black;
    height: 160px;width: 100%;
    text-align: center;
        font-size:40pt;
        text-shadow:5px 4px 10px #FF0000,-5px -4px 10px #FFFFFF;
        color:#000000;

        }

html        {background-image: url("bg.jpg");
        background-repeat:no-repeat;
        background-position; 50%, 50%;
        background-attachment: fixed;
        min-height:100%;
        background-size:cover;
        color:#FFFFFF;
        }

.img    {width: 50px; height:85px;}

#links  {width:50%; 
    height:50px;
    font-size:22pt;     
    margin:auto;
    text-shadow:0px 0px 0px #000000;
    }

#menu   {height: 3200px;
    width: 900px;

    margin-top:15%;
    margin-left:17%;
    font-size:18pt;}

.sidebar {width: 150px;
    height: 230px;
    position: fixed;
    left: 40px;
    top: 300px;
    font-size:18pt;
    border:solid black 2px;}

.spacer{height: 10px !important; }

【问题讨论】:

  • 我在这里看不到那个问题jsfiddle.net/tilwinjoy/2MKfw 这是有和没有边界的 samwe...
  • 当我将文件保存在 Notepad ++ 中并在 Firefox、IE、Chrome 或 Safari 中运行时,我遇到了上面提到的问题。
  • 你检查小提琴了吗?它有效吗?如果是,问题出在其他地方……float 的班级在哪里?你确定你分享了整个代码吗?如果您分享的代码无法复制问题,我们该如何解决?
  • 它在小提琴中有效,但在其他任何地方都无效。浮动类尚不存在,它存在于我尚未启动的未来添加中。整个代码已共享。我不知道为什么它在小提琴中起作用,但在实际应用中却不起作用。
  • '应用'?您是否在“应用程序”中使用任何其他样式表或插件?您可以在其他任何地方复制该问题..或提供在线链接。?打开开发者控制台并检查页面,检查是否有任何样式被覆盖或添加了额外的样式..

标签: html border margin collapse


【解决方案1】:

为 #header 应用 top:0 将解决问题

【讨论】:

    猜你喜欢
    • 2010-12-14
    • 2013-05-28
    • 2019-01-31
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    相关资源
    最近更新 更多