目錄
為什麼我們應該使用邏輯屬性而不是普通的CSS屬性?
文法
範例1(邊距和內邊距邏輯屬性)
Example 3
範例3
首頁 web前端 css教學 CSS 中的邏輯屬性

CSS 中的邏輯屬性

Sep 14, 2023 am 10:29 AM

CSS 中的逻辑属性

在CSS中,邏輯屬性允許​​開發者根據網頁的邏輯結構而不是物理佈局來定義樣式。這意味著我們可以根據文字方向或內容流應用CSS。

主要使用邏輯屬性來設定HTML元素的邊距、內邊距和邊框。它包含了邊距、內邊距和邊框屬性的不同變體。

邏輯屬性可以根據區塊級和內聯尺寸進行定義。

  • Block dimension − The block dimension represents the perpendicular direction of the content flow. For example, in English text direction is left to right. So, block dimensions handle the top and bottom of the top and bottom of the top and bottom of the top and bottom of the top and bottom of the top and bottom of the top and bottom. element.

  • 內聯尺寸 - 內聯尺寸表示與內容或文字方向相同的方向。對於英語來說,左側和右側是內聯尺寸。

Let’s look at some commonly used logical properties in CSS.

  • Border-block − 它設定了上下邊框。

  • Border-inline − 設定左右邊框。

  • Border-block-start − It sets the top border.

  • Border-block-end − 它設定了底部邊框。

  • Margin-inline − It sets the left and right margins.

  • #Padding-inline − It sets the left and right padding.

  • Padding-inline-start − It sets the left padding.

  • Margin-inline-end − It sets the bottom padding.

  • Border-inline-end-width − 它設定右邊框的寬度。

  • Border-block-start-style − 它設定了頂部邊框的樣式。

In the above properties, users can observe that we require to use 'block' for top and bottom and 'inline' for left and right. Also, 'start' for left and top, and 'end' for right and bottom .

為什麼我們應該使用邏輯屬性而不是普通的CSS屬性?

透過觀察上述屬性的功能,首先想到的問題是我們是否可以使用普通的CSS屬性來實現相同的樣式,以及為什麼我們應該使用邏輯屬性。以下是您的答案。

有時候,我們需要為HTML元素設定左右邊距。我們可以使用margin屬性的'0 auto'值來實現,或分別使用margin-left和margin-right的CSS屬性。當使用'0 auto'時,如果先前已經應用了上下邊距的值,我們也會改變它們的值。因此,最好使用'margin-inline'的CSS屬性。

margin: 0 auto;
or
margin-left: auto;
margin-right: auto;
or
margin-inline: auto;
登入後複製

文法

Users can follow the syntax below to use logical properties in CSS.

padding-block-start: value;
margin-inline-end: value;
登入後複製

在上述語法中,我們使用邏輯屬性就像使用其他CSS屬性一樣。

範例1(邊距和內邊距邏輯屬性)

在下面的範例中,我們建立了兩個div元素,並在其中新增了文字。在CSS中,我們使用了“padding-block-start”,“padding-inline-start”和“margin-block-end”邏輯CSS屬性來為第一個div設置頂部和左側填充以及底部邊距。

此外,我們使用了‘margin-inline-end’邏輯CSS屬性來為div元素新增右內邊距。

<html>
<head>
   <style>
      .text {
         padding-block-start: 20px;
         padding-inline-start: 30px;
         margin-block-end: 50px;
         color: green;
         background-color: red;
         width: 300px;
         font-size: 2rem;
      }
      .text1 {
         width: 300px;
         font-size: 2rem;
         padding-block-start: 20px;
         padding-inline-start: 10px;
         margin-inline-end: 50px;
         color: blue;
         background-color: yellow;
      }
   </style>
</head>
<body>
   <h3> Using the <i> margins and paddings logical properties </i> in CSS </h3>
   <div class = "text"> This is a text. </div>
   <div class = "text1"> This is another text div element. </div>
</body>
</html>
登入後複製

Example 2

In the example below, we have demonstrated the logical CSS properties related to the border. We used the 'border-block-start' to apply the top border and the 'border-block-end' to apply the bottom border. Furthermore, we used the 'border-inline-start' to apply the left border and 'border-inline-end' to apply the right border.

In the output, users can observe the different borders for the different sides of the div element.

<html>
<head>
   <style>
      .sample {
         border-block-start: 3px dotted blue;
         border-block-end: 5px solid green;
         border-inline-start: 10px double red;
         border-inline-end: 5px groove yellow;
         padding: 10px;
         width: 300px;
         height: 200px;
      }
      .left {color: red;}
      .right {color: yellow;}
      .top {color: blue;}
      .bottom {color: green;}
   </style>
</head>
<body>
   <h2> Using the <i> Logical border </i> properties in CSS </h2>
   <div class = "sample">
      Observe the border of the div.
      <p class = "left"> border inline start </p>
      <p class = "right"> border inline end </p>
      <p class = "top"> border block start </p>
      <p class = "bottom"> border block end </p>
   </div>
</body>
</html>
登入後複製

Example 3

的翻譯為:

範例3

In the example below, we applied the CSS logical properties related to the margin and padding in the flexbox. Here, we have created three div elements inside the container div element. After that, we three div elements inside the container div element. After that, we three divding-inline' apply right and left padding in the container div element.

<html>
<head>
   <style>
      .container {
         display: flex;
         flex-direction: row;
         justify-content: space-between;
         padding-inline: 40px;
         width: 500px;
         background-color: bisque;
         font-size: 2rem;
      }
      .item {flex: 1;}
   </style>
</head>
<body>
   <h3> Using the <i> margin-inline property </i> to set the inline margin </h3>
   <div class = "container">
      <div class = "item"> First </div>
      <div class = "item"> second </div>
      <div class = "item"> Third </div>
   </div>
</body>
</html>
登入後複製

使用者學會了在CSS中使用邏輯屬性。大多數邏輯屬性與邊距、內邊距和邊框有關。然而,與溢出相關的一些邏輯屬性也存在,開發人員可以在網路上查閱。在使用邏輯屬性時,使用者需要專注於「區塊」和「內聯」維度以及「開始」和「結束」方向。

以上是CSS 中的邏輯屬性的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

使用GraphQL緩存 使用GraphQL緩存 Mar 19, 2025 am 09:36 AM

如果您最近開始使用GraphQL或審查了其優點和缺點,那麼您毫無疑問聽到了諸如“ GraphQl不支持緩存”或

使用Redwood.js和Fauna構建以太坊應用 使用Redwood.js和Fauna構建以太坊應用 Mar 28, 2025 am 09:18 AM

隨著最近比特幣價格超過20k美元的攀升,最近打破了3萬美元,我認為值得深入研究創建以太坊

VUE 3 VUE 3 Apr 02, 2025 pm 06:32 PM

它的出局!恭喜Vue團隊完成了完成,我知道這是一項巨大的努力,而且很長時間。所有新文檔也是如此。

用高架創建自己的野蠻人 用高架創建自己的野蠻人 Mar 18, 2025 am 11:23 AM

無論您是開發人員的哪個階段,我們完成的任務(無論大小)都會對我們的個人和專業成長產生巨大影響。

您可以從瀏覽器獲得有效的CSS屬性值嗎? 您可以從瀏覽器獲得有效的CSS屬性值嗎? Apr 02, 2025 pm 06:17 PM

我有人寫了這個非常合法的問題。 Lea只是在博客上介紹瞭如何從瀏覽器中獲得有效的CSS屬性。那樣的是這樣。

在CI/CD上有點 在CI/CD上有點 Apr 02, 2025 pm 06:21 PM

我說的“網站”比“移動應用程序”更合適,但我喜歡Max Lynch的框架:

比較瀏覽器的響應式設計 比較瀏覽器的響應式設計 Apr 02, 2025 pm 06:25 PM

這些桌面應用程序中有許多目標是同時在不同的維度上顯示您的網站。因此,例如,您可以寫作

帶有粘性定位的堆疊卡和一點點的雜物 帶有粘性定位的堆疊卡和一點點的雜物 Apr 03, 2025 am 10:30 AM

前幾天,我發現了科里·金尼文(Corey Ginnivan)網站上的這一點,當您滾動時,彼此之間的卡片堆放集。

See all articles