我們應該使用“border: none”還是“border: 0”?


在 CSS 中,“border” 屬性用於向 HTML 元素新增邊框。我們可以向 HTML 元素新增寬度、樣式和顏色的邊框。

當我們需要從任何元素中移除邊框時,會想到兩個邊框值。第一個值是“none”,第二個值是“0”。有時,這會讓人對應該使用哪個值感到困惑,在本教程中,我們將消除這種疑惑。

語法

使用者可以按照以下語法使用“none”或“0”作為“border”屬性的值。

border: none;
border: 0;

Border: none VS border: 0

在我們開始示例之前,讓我們看一下邊框的“none”和“0”值之間的區別。

Border: none Border: 0
它是 border-style: none CSS 屬性的簡寫形式。 它是 border-width: 0 CSS 屬性的簡寫形式。
border: none 隱藏了元素的邊框,但並沒有從元素中移除邊框。 border: 0 屬性會從元素中移除邊框,並且不會在瀏覽器中渲染邊框。
此外,它不會從瀏覽器中移除邊框,並佔用瀏覽器的記憶體。 它不佔用瀏覽器記憶體。
切勿使用“border: none”,因為它會影響應用程式效能。 始終使用 border: 0 來移除元素的邊框。

示例 1

在下面的示例中,我們建立了兩個不同的 div 元素,添加了內容,並賦予了不同的類名。對於第一個 div 元素,我們應用了“border: none”,第二個 div 元素的邊框為“border: 2px dashed green”。

在輸出中,使用者可以看到“border: none”隱藏了第一個 div 元素的邊框。

<html>
<head>
   <style>
      .first {
         border: none;
         background-color: lightblue;
         padding: 20px;
         text-align: center;
         width: 500px;
      }
      .second {
         border: 2px dashed green;
         padding: 10px;
         text-align: center;
         width: 500px;
         margin: 20px;
      }
   </style>
</head>
<body>
   <h3> Using the <i> border: none </i> CSS property to remove border of elements </h3>
   <div class = "first"> BMW, Audi </div>
   <div class = "second"> Apple, Banana </div>
</body>
</html>

示例 2

在下面的示例中,我們像第一個示例一樣建立了兩個不同的 div 元素。我們使用了“border: 0” CSS 屬性來移除第一個 div 元素的邊框。

<html>
<head>
   <style>
      .one {
         border: 0;
         background-color: yellow;
         padding: 20px;
         width: 200px;
      }
      .two {
         border: 2px solid red;
         padding: 10px;
         width: 200px;
         margin: 20px;
      }
   </style>
</head>
<body>
   <h3> Using the <i> border: 0 </i> CSS property to remove border of elements. </h3>
   <section class = "one"> This is a second with border: 0</section>
   <section class = "two"> This is a second with border: 2px solid red</section>
</body>
</html>

示例 3

在下面的示例中,我們使用了 border: 0、border: none、border-style: none 和 border-width: 0 CSS 屬性以及 div 元素。在這裡,我們建立了四個不同的 div 元素,添加了不同的內容,並賦予了不同的背景顏色。

此外,我們還為每個 div 元素使用了不同的邊框屬性。在輸出中,使用者可以看到所有屬性都移除了 div 元素的邊框。

<html>
<head>
   <style>
      .one {
         width: 300px;
         margin: 10px;
         padding: 10px;
         border: 0;
         background-color: greenyellow;
      }
      .two {
         width: 300px;
         margin: 10px;
         padding: 10px;
         border-width: 0;
         background-color: aqua;
      }
      .three {
         width: 300px;
         margin: 10px;
         padding: 10px;
         border: none;
         background-color: yellow;
      }
      .four {
         width: 300px;
         margin: 10px;
         padding: 10px;
         border-style: none;
         background-color: brown;
      }
   </style>
</head>
<body>
   <h3> Using the <i> border: 0, border: none, border-style: none, and border-width: 0 </i> CSS property to remove border of elements </h2>
   <section class = "one"> border: 0 </section>
   <section class = "two"> border-width: 0 </section>
   <section class = "three"> border: none </section>
   <section class = "four"> border-style: none </section>
</body>
</html>

從上面的教程中,我們可以得出結論,使用者應該使用“border: 0”或“border-width: 0”,而不是使用“border: none”,因為它移除了元素的邊框樣式,但邊框仍然存在。“border: 0”在一定程度上也能提高應用程式效能。但是,對於小型應用程式,我們看不到區別,但在大型應用程式中,它會產生影響。

更新於:2023年4月11日

618 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.