简体   繁体   English

如何让文本右对齐,但 div 居中对齐?

[英]How can I have text aligned right, but the div aligned center?

How can I have text aligned to the right, but at the same time the text is in the center?如何让文本向右对齐,但同时文本位于中心?

The longest line will be centered, shorter lines will be aligned to right until their most right character is at the same spot the longest line's character is?最长的行将居中,较短的行将向右对齐,直到它们最右边的字符与最长行的字符位于同一位置?

Simplified version of current code:当前代码的简化版本:

<div style="text-align:right;content-align:center">
<label><b>Longer item:</b>&nbsp;&nbsp;</label><kbd id="item1">Equal amount of stuff</kbd>&nbsp;&nbsp;<button class="copyButton btn btn-default" id="copyButtonId"  
 data-clipboard-action="copy" data-clipboard-target="kbd#item1">
         Copy
     </button>
    <br><br>
    <label><b>Shorter:</b>&nbsp;&nbsp;</label><kbd id="item2">Equal amount of stuff</kbd>&nbsp;&nbsp;<button class="copyButton btn btn-default" id="copyButtonId"  
 data-clipboard-action="copy" data-clipboard-target="kbd#item2">
         Copy
      </button>
</div>

Better version at codepen .更好的版本在 codepen

If I'm understanding your question correctly, the best way to achieve what you're asking is to put your text inside another div , set it's text-align to end , and set its parent to display: flex; justify-content: center;如果我正确理解你的问题,实现你所要求的最好方法是将你的文本放在另一个div ,将它的text-align设置为end ,并将其父级设置为display: flex; justify-content: center; display: flex; justify-content: center; Here is your code with those changes:这是带有这些更改的代码:

<div style="display: flex; justify-content: center;">
  <div style="text-align: end;">
    <label><b>Longer item:</b>&nbsp;&nbsp;</label><kbd id="item1">Equal amount of stuff</kbd>&nbsp;&nbsp;<button class="copyButton btn btn-default" id="copyButtonId"  
 data-clipboard-action="copy" data-clipboard-target="kbd#item1">
         Copy
    </button>
    <br><br>
    <label><b>Shorter:</b>&nbsp;&nbsp;</label><kbd id="item2">Equal amount of stuff</kbd>&nbsp;&nbsp;<button class="copyButton btn btn-default" id="copyButtonId"  
 data-clipboard-action="copy" data-clipboard-target="kbd#item2">
         Copy
    </button>
  </div>
</div>

I recommend that you don't use inline styles, as it is difficult to read and maintain.我建议您不要使用内联样式,因为它很难阅读和维护。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在div的中心向左对齐文本? - How do I have left-aligned text in the center of a div? 如何使文本居中并正确对齐,同时保持响应速度 - How to center text and have it right aligned while keeping it responsive 如何在同一行上有两个DIV,一个不带HTML表格,一个左对齐,一个右对齐? - How can I have two DIV s on the same line , one aligned to the left and one aligned to the right without an HTML table? 如何使两个对象水平对齐(一个在另一个的右侧)并居中放置一个? - How can I have two objects aligned horizontally (one to the right of the other) and center the left one? 如何在同一元素中居中放置文本和右对齐图像? - How can I have centered text and right-aligned image in the same element? 我该如何布置两个 <div> 在一行上,并且其中一个居中对齐(相对于整行),而另一个居右对齐? - How can I lay out two <div>s on one line, and have one centre-aligned (relative to the entire line) and the other right-aligned? 将text-div对齐到图像顶部的右下方 - Have text-div aligned right bottom on top of image 在右对齐div中垂直居中放置文本框 - Vertically center a textbox in a right-aligned div 如果表格中的单元格中有文本,如何将文本添加到与单元格最右侧对齐的同一个单元格? - If I have text in a cell within a table, how can I add text to the same cell just aligned to the far right of the cell? 如何在仍然与左/右对齐的同时居中文本? - How to center a text while still being aligned to the left/right?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM