html - 这里有什么理由吗?
问题描述
<!DOCTYPE html>
<html>
<head>
<style>
#one{
background-color: brown;
text-align: center;
margin-top: 0;
}
#two{
background-color: rgb(86, 74, 153);
text-align: center;
margin-top: 0;
}
</style>
</head>
<body>
<div id="one">
<h1>heading</h1>
</div>
<div id="two">
<h2>welcome section</h2>
</div>
</body>
</html>
为什么在这里margin-top: 0
而margin-bottom: 0
不是在css中工作?在 css 中有这样的规则吗?如果我给两者都加上边框,它就可以工作。同样,当我给出 1px 的边框时,div 的高度会自动增加,边距变为零,但没有边框我不能将边距设置为零。为什么?请简要说明这条规则。
解决方案
h元素具有默认边距。你的h1和h2导致它。
#one h1, #two h2 {
margin-top:0;
margin-bottom:0;
}
推荐阅读
- python - 如何使用低级 pytorch API 实现卷积操作的变体?
- elixir - 在Phoenix中没有重定向的POST操作中响应错误页面的正确方法是什么?
- javascript - 手机号码的正则表达式使国家代码的括号成为可选的,破折号、圆点和空格是可选的
- powershell - 如何使用powershell从本地机器执行oracle sql脚本
- haskell - 创建路径列表
- python - Django:unique_together 用于外国领域
- javascript - JavaScript 加载器功能在 Firefox 中运行良好,但在 Chrome 中无法运行
- excel-formula - 删除重复的值并移动到另一个表
- reactjs - 如何在 React JS 的函数内部使用状态?
- oracle - 交互式网格在更新 (19.1) 时抛出 ORA-01858