CSS:如何针对居中文本对文本进行右对齐?(CSS: How can I right

编程入门 行业动态 更新时间:2024-10-26 23:26:12
CSS:如何针对居中文本对文本进行右对齐?(CSS: How can I right-justify text against centered text?)

给出以下HTML:

<div class="css_class_1"> <div class="css_class_2"> A 1 A 1 A 1 </div> <div class="css_class_3"> B 2 B 2 </div> </div>

我想将文本A 1 A 1 A 1水平居中。 我想使文本B 2 B 2右对齐,使其右边界与A 1 A 1 A 1的右边界对齐。

我该怎么做? 请显示我应该为上述三个类中的每一个定义的css。

Given the following HTML:

<div class="css_class_1"> <div class="css_class_2"> A 1 A 1 A 1 </div> <div class="css_class_3"> B 2 B 2 </div> </div>

I would like to make the text A 1 A 1 A 1 horizontally-centered. And I would like to make the text B 2 B 2 right-justified so that it's right border is aligned with the right border of A 1 A 1 A 1.

How can I do it? Please show the css I should define for each of the three mentioned classes.

最满意答案

将右边的文本置于中心位置:

.css_class_1 {
  background: green;
  padding: 10px;
}

.css_class_2 {
  background: yellow;
  text-align: center;
  display: inline-block;
}

.css_class_3 {
  background: red;
  text-align: right;
} 
  
<div class="css_class_1">
    <div class="css_class_2">
        A 1 A 1 A 1
      
      <div class="css_class_3">
          B 2 B 2
      </div>
    </div>
</div> 
  
 

Put your right justify text inside the center one:

.css_class_1 {
  background: green;
  padding: 10px;
}

.css_class_2 {
  background: yellow;
  text-align: center;
  display: inline-block;
}

.css_class_3 {
  background: red;
  text-align: right;
} 
  
<div class="css_class_1">
    <div class="css_class_2">
        A 1 A 1 A 1
      
      <div class="css_class_3">
          B 2 B 2
      </div>
    </div>
</div> 
  
 

更多推荐

本文发布于:2023-07-28 12:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1305195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   右对齐   CSS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!