CSS 我可以使用 display:inline with text-align: 对吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3401749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:31:21  来源:igfitidea点击:

Can I use the display:inline with text-align: right?

cssxhtml

提问by Lenard

Example:

例子:

<td>
  <img src="..." />
  <img src="..." />
  <div style="text-align:right display:inline;">
    hello world!
  </div>
</td>

回答by Danny Nimmo

Well technically you can but it will have no effect. Display: inline will display the div as an inline element (like an a or span) and therefore not have a width - it will shrink to fit the text.

那么技术上你可以,但它不会有任何影响。Display: inline 会将 div 显示为内联元素(如 a 或 span),因此没有宽度 - 它会缩小以适应文本。

If you are trying to display inline text on the right try using float: right;

如果您尝试在右侧显示内联文本,请尝试使用 float: right;

Also, in your code you missed out an ";" after the text-align: right.

此外,在您的代码中,您错过了一个“;” 在 text-align: right 之后。

回答by You

The spec is somewhat unclear on this, but it shouldn't work. The text-alignproperty doesn't really make sense on inline elements, since it applies to blocks of text (which inline elements are not). In your example, <p>(a block element) would be a better choice anyway.

规范对此有些不清楚,但它不应该起作用。该text-align属性对内联元素没有真正意义,因为它适用于文本块(内联元素不是)。在您的示例中,<p>(块元素)无论如何都是更好的选择。

回答by iHeartIT

No, but try using display:inline-block; See snippet

不,但尝试使用 display:inline-block; 见片段

.img{
display:inline-block;
}

.text{
display:inline-block;
color:white;
font-size: 15px;
font-family: tahoma;
text-align:right;
}

.wrapper{
display:block;
background-color:black;
padding:1em;
width:23em;
}
<td>
<div class="wrapper">
  <img src="..." class="img"/>
  <img src="..." class="img"/>
  <div class="text">
    hello world! Lorem ipsum lorem ipsum lorem ipsum loren ipsum
  </div>
</div>
</td>