搜索
首页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 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 id="Using-the-i-margins-and-paddings-logical-properties-i-in-CSS"> 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 id="Using-the-i-Logical-border-i-properties-in-CSS"> 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 used the ‘padding-inline’ to 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 id="Using-the-i-margin-inline-property-i-to-set-the-inline-margin"> 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中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
可变字体链接转储!可变字体链接转储!Apr 16, 2025 am 10:22 AM

最近有很多很棒的东西在可变字体上飞来飞去(我们的标签也有很多东西)。我以为我汇集了所有新东西我

链接强调将动画变成块背景链接强调将动画变成块背景Apr 16, 2025 am 10:14 AM

这是一个很酷的效果。默认链接样式具有下划线(这是一个好主意),然后开启:悬停您看到下划线基本上变厚了

在需要之前预加载页面在需要之前预加载页面Apr 16, 2025 am 09:53 AM

浏览网站的人的典型旅程:查看页面,单击链接,浏览器加载新页面。那就是假设没有像一页那样有趣的业务

带有Flexbox的自适应照片布局带有Flexbox的自适应照片布局Apr 16, 2025 am 09:51 AM

让我们看一下一种超轻量级的方式,为一组任意大小的照片创建水平砌体效果。向其中扔任何一套照片,然后

将形状和图像与HTML和CSS联系起来的许多方法将形状和图像与HTML和CSS联系起来的许多方法Apr 16, 2025 am 09:45 AM

不同的网站设计通常需要平方或矩形以外的其他形状来响应点击事件。也许您的网站有某种倾斜或

Web开发人员搜索历史记录Web开发人员搜索历史记录Apr 16, 2025 am 09:41 AM

Sophie Koonin博客“我一周内以专业软件工程师的搜索谷歌搜索的所有内容”,这对Web开发人员和

一个片段,可以看到精灵中的所有SVG一个片段,可以看到精灵中的所有SVGApr 16, 2025 am 09:31 AM

我想到了一个SVG精灵:

当您第一次打开新的浏览器安装时会发生什么?当您第一次打开新的浏览器安装时会发生什么?Apr 16, 2025 am 09:29 AM

乔纳森·桑普森(Jonathan Sampson)的有趣研究,他观看网络请求浏览器,这是您第一次在新的安装中启动它,并且

See all articles

热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.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),