代码之家  ›  专栏  ›  技术社区  ›  software_writer

嵌套文本在HTML中对齐

  •  1
  • software_writer  · 技术社区  · 7 年前

    我有一个 div 我希望与页面右侧对齐的元素。例如,

    <div class="meta">
         Answered May 12 '20 at 14:32 <br />
         <div class="author">Person A</div>
    </div>
    
    .meta {
        text-align: right;
    }
    

    看起来是这样的:

                                                    Answered May 12 '20 at 14:32
                                                                        Person A
    


    现在,我想要 .author DIV左对齐 .meta DIV,如下所示:

                                                    Answered May 12 '20 at 14:32
                                                    Person A
    

    所以我添加了:

    .author {
        text-align: left;
    }
    

    但结果是:

                                                    Answered May 12 '20 at 14:32
    Person A
    

    我应该用什么风格来达到上述效果?

    7 回复  |  直到 7 年前
        1
  •  3
  •   Bhuwan    7 年前

    这是因为你 .author DIV是100%宽度,因为它的父元素是块元素。

    所以要解决这个包裹你的 .meta 另一个分区内的分区,即 .wrap 然后应用 text-align:right . 你还需要申请 display:inline-block DIV,这样它只能获取其内容宽度,而不能 100% .

    .wrap {
      text-align: right;
    }
    
    .meta {
      display: inline-block;
    }
    
    .author {
      text-align: left;
    }
    <div class="wrap">
      <div class="meta">
        Answered May 12 '20 at 14:32 <br />
        <div class="author">Person A</div>
      </div>
    </div>
        2
  •  1
  •   Thanveer Shah    7 年前

    是的,简单的解决方法就是这样。

    CSS

    .meta {
        text-align: right;     
    }
    .title{
        display: inline-block;
        text-align: left;
    }
    

    HTML

    <div class="meta">
        <div class="title">
         Answered May 12 '20 at 14:32 <br>
         <div class="author">Person A</div>
        </div>
    </div>
    

    祝你好运。

        3
  •  0
  •   Partho63 BASANT KUMAR    7 年前

    您也可以这样做:

    .meta {
      position: absolute;
      right: 0;
      text-align: left;
    }
    <div class="meta">
      Answered May 12 '20 at 14:32 <br />
      <div class="author">Person A</div>
    </div>
        4
  •  0
  •   bilashism    7 年前
    <div class="meta" style="float: right;">
        Answered May 12 '20 at 14:32
        <div class="author">Person A</div>
    </div>
    <div style="clear: both;"></div>
    
        5
  •  0
  •   Sumit Ridhal    7 年前

    解1。使用Flex。

    .meta {
      display: flex;
        justify-content: flex-end;
    }
    <div class="meta">
        <div class="title">
         Answered May 12 '20 at 14:32 <br>
         <div class="author">Person A</div>
        </div>
    </div>

    解2。将文本左对齐(默认),并添加左边距以使DIV右对齐。

    .meta {
        margin-left: 75%;
    }
    <div class="meta">
         Answered May 12 '20 at 14:32 <br />
         <div class="author">Person A</div>
    </div>
        6
  •  -2
  •   Oussama Nh    7 年前

    我想你可以这样做

    <div align="right">

    在此链接中搜索 https://www.w3schools.com/tags/att_p_align.asp

        7
  •  -2
  •   Oussama Nh    7 年前

    我想你可以这样做 <div align="right"> 这个链接中的searsh https://www.w3schools.com/tags/att_p_align.asp