html - padding 是在内容周围填充,但这并不完全正确

标签 html css

我对 CSS 很陌生。根据文档在内容周围填充。 在这个例子中是内容“礼物和特别优惠” 在这个简单的示例中,我将 padding-right 设置为 150px,根据文档,这意味着我应该在内容右侧留出 150px 的空间。但是我的内容右侧没有 150px 的空白空间。我一定是在这里遗漏了一些东西来传达理解?

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Chapter 5: Indestructible Boxes</title>
    <style type="text/css" media="screen">
    h3
    {
        margin:50px;
        padding:0px 150px 0px 0px;
        border:1px solid black;
    }
   </style>
</head>
<body>
    <div style="width:200px; background:red;">
    <h3>Gifts and Special Offers</h3>
    </div>
</body>
</html>

//托尼

最佳答案

Padding是内间距。

Margin是外间距。

当你给 h3 以下样式时,你实际上用 50px 从各个方向的外部间距和额外的 150px 内部间距包裹它向右。

margin: 50px;
padding: 0px 150px 0px 0px;

这使得水平间距为 50px + 150px + 50px = 250px

Resulted spacing

请注意,您的 div 只有 200px 宽,因此您很难获得预期的效果。

关于html - padding 是在内容周围填充,但这并不完全正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18419773/

相关文章:

html - 除了 inline-block 之外,还有其他方法可以让元素不换行吗?

html - CSS border-bottom 悬停在导航菜单上

javascript - 将 select2.js 与多个选择 optgroup 和 knockoutJS 一起使用

javascript - 地址栏中的谷歌翻译

php - 坚持在 Zend 框架中尝试 css mega 菜单

php - 从左到右插入记录到3列

iphone - 如何停止使用 CSS 进行背景分层?

html - CSS Sprites 在博客中不起作用

html - 如何创建圆形人字形

javascript - 为什么我无法让 Math.random() 和 if/else 语句的组合起作用?