前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CSS 元素从右到左排列

CSS 元素从右到左排列

作者头像
草帽lufei
发布2022-07-29 09:34:26
1.1K0
发布2022-07-29 09:34:26
举报
文章被收录于专栏:程序语言交流

代码示例效果如上图

默认元素排列: 从左到右
代码语言:javascript
复制
 <h3>默认:从左到右</h3>
  <div style="border:4px solid #ccc;display:flex;">
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;">1</div>
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;">2</div>
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;">3</div>
  </div>
设置元素从右到左
1. 从右到左 float:right
代码语言:javascript
复制
  <h3>1. 从右到左 float:right</h3>
  <div style="border:4px solid #ccc;">
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;float:right;">1</div>
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;float:right;">2</div>
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;float:right;">3</div>
  </div>

使用float布局在实际场景中需要手动清理,否则会影响布局

2.1 从右到左 derection:rtl ,设置 css
代码语言:javascript
复制
  <h3>2.1 从右到左 derection:rtl</h3>
  <div style="border:4px solid #ccc;direction: rtl;display:flex;">
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;direction:ltr;">1</div>
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;">2</div>
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;">3</div>
  </div>
2.2 从右到左 dir="rtl" 设置元素属性 (推荐)
代码语言:javascript
复制
  <h3>2.2 从右到左 dir="rtl"</h3>
  <div style="border:4px solid #ccc;display:flex;" dir="rtl">
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;direction:ltr;">1</div>
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;">2</div>
    <div style="border:2px solid red;backgroun-color: #eee;width:60px;height:40px;margin-right:20px;">3</div>
  </div>

官方资料 - MDN:

https://developer.mozilla.org/en-US/docs/Web/CSS/float

https://developer.mozilla.org/en-US/docs/Web/CSS/direction

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/dir

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-09-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 参谋总长萨博 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 默认元素排列: 从左到右
  • 设置元素从右到左
    • 1. 从右到左 float:right
      • 2.1 从右到左 derection:rtl ,设置 css
        • 2.2 从右到左 dir="rtl" 设置元素属性 (推荐)
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档