如何使用margin、border和padding使盒子模型中的元素完美配合?


通常,我们使用盒子模型来定义所有这些属性,并将它们组合在一起使用。盒子模型是一个用于定义盒子包含的不同 CSS 属性的模型,例如margin、border、padding以及最重要的内容。CSS 中的盒子模型通常定义 margin、border、padding 和内容等属性。

在了解如何使用 padding、margin、border 和内容在盒子模型中完美配合之前,让我们先了解一下这些属性应用于容器时实际的作用。

内容 - 内容是最重要且容器必须具有的属性。它定义了特定文本容器想要传递的信息或容器的目的。

填充 (Padding) - 填充是容器内内容文本周围的空间。也可以说它是内容与容器边界之间的内部空间。

语法

下面的语法将向您展示使用简写和特定属性的 padding 属性:

// Shorthand property
// sets the space around the all four sides of the content
padding: value;
// specific properties
// sets specified space on the specified side of the content
padding-top/left/right/bottom: value;

边框 (Border) - 正如名称所示,边框是容器边界周围的轮廓,用于指定网页上特定容器包含的区域。

下面的语法将展示如何使用 border 属性:

// Shorthand property
// specifies border on all four sides with given width, style and color

border: width style color;

// specific properties
// specifies border on the specified side of the container

border-top/left/right/bottom: width;
border-style: value;
border-color: value;

外边距 (Margin) - 外边距也用于提供空间,就像填充一样。外边距和填充之间存在差异:填充是边界和内容之间的空间,而外边距是容器外部的空间,用于指定两个相邻容器之间的间隙。

下面的语法将展示如何使用 margin 属性:

// Shorthand property
// sets the space around the all four sides of the container

margin: value;

// specific properties
// sets specified space on the specified side of the container

margin-top/left/right/bottom: value;

现在让我们了解一下如何在特定元素上应用这些属性,并在盒子模型中查看它们是如何工作的。

示例

下面的示例将解释如何将 margin、padding、border 和内容属性组合在一起以适应盒子模型:

<!DOCTYPE html>
<html>
<head>
   <style>
      .box-model{
         padding: 50px;
         border: 2px solid green;
         margin: 50px;
      }
   </style>
</head>
<body>
   <center>
      <h2> Using margin, border and padding to fit together in the box model </h2>
      <p> The changes in the below text will be visible once the box model properties applied on it. </p>
      <p class = "box-model">Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. </p>
   </center> 
</body>
</html>

在上面的示例中,我们一起使用了内容、填充、外边距和边框以适应一个盒子。您可以看到,一旦从其样式中删除填充和外边距,以及当它包含这些样式时,容器的外观差异。

让我们再看一个代码示例,在这个示例中,我们将使用两个具有不同内容的不同容器,并通过在特定侧面上设置特定的外边距和填充来分隔它们。

上面这个例子和这个例子的算法几乎相同。您只需要添加另一个具有不同类的内容容器,以便对其应用与第一个容器不同的样式。

示例

下面的示例将说明如何使用盒子模型属性来区分具有不同内容的两个容器:

<!DOCTYPE html>
<html>
<head>
   <style>
      .box-model1{
         border: 2px solid green;
         padding: 30px;
         margin-bottom: 30px;
      }
      .box-model2{
         border: 2px solid red;
         padding: 30px;
         margin-top: 30px;
      }
   </style>
</head>
<body>
   <center>
      <h2> Using margin, border and padding to fit together in the box model </h2>
      <p> The changes in the below text will be visible once the box model properties applied on it. </p>
      <p class = "box-model1">Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. </p>
      <p class = "box-model2"> The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming languages to web designing to academics and much more.</p>
   </center> 
</body>
</html>

在这个示例中,我们定义了两个具有不同内容的不同容器,并使用盒子模型属性 margin 和 padding 来区分它们。我们为上面的容器设置了相同的 margin-bottom,为下面的容器设置了 margin-top。这导致它们之间出现相同值的双倍间距。

在这篇文章中,我们学习了如何在盒子模型中一起使用 margin、padding、border 和 content 属性。我们借助于在不同场景下的两个代码示例,看到了这些属性的实际应用,以便更好地理解它们。

更新于:2023年8月31日

浏览量:138

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.