CSS 中哪个属性指定边框的宽度?

cssweb developmentfront end technology更新于 2024/4/18 5:32:17

在 CSS 中,"border"属性用于将边框应用于任何 HTML 元素,例如 div。此外,我们还可以设置边框、颜色、宽度等不同的样式。

在本教程中,我们将学习设置元素边框宽度的不同方法。此外,我们还将学习如何设置元素不同边的宽度。

使用 border-width CSS 属性设置边框宽度

"border-width"CSS 属性用于定义边框的宽度。用户可以传递四个值来设置不同边的宽度。但是,最后三个值是可选的。

使用单个值作为边框宽度将为所有四个边应用相同的边框宽度。如果我们传递两个值,它会将第一个值视为上下边框宽度,将第二个值视为左右边框宽度。

语法

用户可以按照以下语法使用 CSS 属性"border-width"设置边框宽度。

border-width: 上 右 下 左;
border-width: 上 下 右 左;
border-width: 上 下 右 左;

在上述语法中,首先,我们为所有边定义不同的宽度。然后,我们为上下和左右定义不同的宽度,最后为所有边定义相同的边框宽度。

示例

在下面的示例中,我们创建了 div 元素,并为 border 元素设置了"5px"的边框宽度。在输出中,用户可以观察到带有虚线样式的红色边框。

<html>
   <style>
      div {
         border-style: dashed;
         border-width: 5px;
         border-color: red;
         width: 600px;
         height: 100px;
      }
   </style>
   <body>
      <h3> Using the <i> border-width CSS property </i> to set the border width of the elemenet. </h3>
      <div>
         Welcome to the world of CSS.
      </div>
   </body>
</html>

示例

在下面的示例中,我们使用 CSS 属性"border-width"为元素的四边设置了不同的边框宽度。我们将上边框宽度设置为"5px",右边框宽度设置为"10px",下边框宽度设置为"15px",左边框宽度设置为"20px"。

在输出中,用户可以观察到 div 元素每边的不同边框宽度。

<html>
   <style>
      div {
         border-style: solid;
         border-width: 5px 10px 15px 20px;
         border-color: red;
         width: 600px;
         height: 200px;
         padding: 10px;
      }
   </style>
   <body>
      <h3> Using the <i> border-width CSS property </i> to set the border width of the elemenet </h3>
      <div>
         <p> top border - 5px; </p>
         <p> right border - 10px; </p>
         <p> bottom border - 15px; </p>
         <p> left border - 20px; </p>
      </div>
   </body>
</html>

使用 border CSS 属性设置边框宽度

"border"CSS 属性有三个值。第一个值指定边框宽度,第二个值指定边框样式,第三个值指定边框颜色。

这里,我们将重点介绍第一个用于设置边框宽度的值。

语法

用户可以按照以下语法使用"border"CSS 属性设置边框宽度。

border: 1rem solid blue;

示例

在下面的示例中,"border"CSS 属性的"1rem solid blue"值设置了 1rem 宽度、红色和纯色样式的边框。要更改边框宽度,我们需要更改第一个值。

<html>
   <style>
      div {
         border: 1rem solid blue;
         width: 500px;
         height: 200px;
         padding: 10px;
      }
   </style>
   <body>
      <h3> Using the <i> border CSS property </i> to set the border width of the elemenet </h3>
      <div>
         You are on the TutorialsPoint website!
      </div>
   </body>
</html>

示例

在 CSS 中,我们还可以使用"thin"、"medium"和"thick"值来设置边框宽度。"thin"值用于设置较窄的边框,"medium"值设置的边框宽度大于"thin"值,而"thick"设置的边框宽度大于"medium"值。

在下面的示例中,我们选取​​了三个 div 元素,并使用 CSS 属性"border"赋予了不同的边框宽度,用户可以在输出中观察到这些宽度。

<html>
   <style>
      p {
         width: 200px;
         height: 100px;
         margin: 10px;
      }
      .first {
         border: thin solid black;
      }
      .second {
         border: medium solid black;
      }
      .third {
         border: thick solid black;
      }
   </style>
   <body>
      <h3> Use the <i> border CSS property </i> to set the border width of the HTML element </h3>
      <p class="first"> Thin border </p>
      <p class="second"> Medium border </p>
      <p class="third"> Thick border </p>
   </body>
</html>

设置特定边的边框宽度

"border-top-width"CSS 属性允许用户设置顶部边框的宽度。此外,用户可以使用"border-right-width"、"border-bottom-width"和"borderleft-width"CSS 属性分别设置元素右侧、底部和左侧边框的宽度。

语法

用户可以按照以下语法,使用不同的 CSS 属性设置不同边的边框宽度。

border-top-width: 3px;
border-right-width: 6px;
border-bottom-width: 9px;
border-left-width: 12px;

示例

在下面的示例中,我们创建了四个不同的 div 元素。我们设置了第一个 div 元素的上边框宽度、第二个 div 元素的右边框宽度以及第三个和第四个元素的下边框和左边框宽度。

<html>
   <style>
      div {
         width: 500px;
         height: 100px;
         margin: 10px;
      }
      .one {
         border-top-width: 3px;
         border-style: dotted;
         border-color: red;
      }
      .two {
         border-right-width: 6px;
         border-style: solid;
         border-color: green;
      }
      .three {
         border-bottom-width: 9px;
         border-style: dashed;
         border-color: blue;
      }
      .four {
         border-left-width: 12px;
         border-style: double;
         border-color: yellow;
      }
   </style>
   <body>
      <h2> Set the border width for the different sides of the element</h2>
      <div class="one"> First div </div>
      <div class="two"> Second div </div>
      <div class="three"> Third div </div>
      <div class="four"> Fourth div </div>
   </body>
</html>

总结

学员学习了如何使用各种 CSS 属性设置 HTML 元素的边框宽度。我们学习了如何使用 CSS 属性"border-width"和"border"来设置边框宽度。此外,我们还学习了如何为元素的不同边设置不同的边框宽度。


相关文章


有用资源