简体   繁体   English

如何强制html元素为一行?

[英]How to force html elements into one line?

I have some elements like:我有一些元素,例如:

    <div>
    <div style="overflow: hidden; white-space: nowrap; text-overflow:ellipsis"> 
Texttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
    <div style="float: right">Float text</div>
    </div>

This will display something like:这将显示如下内容:

Texttttttttttttttttttttttttttttttttttttttttttt...
                                       Float text

However I want it to be like:但是我希望它是这样的:

Texttttttttttttttttttttttttttttttttttt...Float text

I try to use the absolute position method, but in this way, the ellipsis will not be shown.我尝试使用绝对位置方法,但是这样,省略号将不会显示。

Is there any way I can do this?有什么办法可以做到这一点吗?

NOTICE:注意:

Not sure what's wrong, but many people suggest with the inline block or span way, both doesn't work.不知道出了什么问题,但很多人建议使用内联块或跨度方式,两者都不起作用。 While putting the float text before 'Textttt' DOES work.将浮动文本放在 'Textttt' 之前确实有效。

Update更新

For either of the below solution to have ellipsis on the left text, it needs a width set, either explicit or by running out of space and have overflow: hidden / text-overflow: ellipsis .对于以下任一解决方案在左侧文本上有省略号,它需要一个宽度集,无论是显式的还是空间不足并有overflow: hidden / text-overflow: ellipsis

You do like this, where the outer div ( wrap ) has white-space: nowrap and the inner displays like inline block你喜欢这样,外面的 div ( wrap ) 有white-space: nowrap和内部显示像内联块

 .wrap { white-space: nowrap; max-width: 400px; } .wrap > * { display: inline-block; vertical-align: top; } .wrap > div:first-child { overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
 <div class="wrap"> <div> Texttttttttttttttttttttttttttttttttttttttttttttttttttttttttt text text text text text text text text text text text text </div> <div>Float text</div> </div>

Or with display: table或带display: table

 .wrap { display: table; table-layout: fixed; width: 500px; } .wrap > * { display: table-cell; } .wrap > div:first-child div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
 <div class="wrap"> <div> <div>Texttttttttttttttttttttttttttttttttttttttttttttttttttttttttt</div> </div> <div>Float text</div> </div>

Put the right-floated block BEFORE the text you want it to be in line with.将右侧浮动块放在您希望它与之一致的文本之前。

<div style="overflow: hidden; white-space: nowrap; text-overflow:ellipsis">
  <div style="float: right">Float text</div>
  Texttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
</div>

https://jsfiddle.net/pkfwnan7/ https://jsfiddle.net/pkfwnan7/

Use display:inline-block to make the div inline使用display:inline-block使 div 内联

  <div>
    <div style="overflow: hidden; white-space: nowrap; text-overflow:ellipsis"> 
Texttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
    <div style="display:inline-block">Float text</div>
    </div>

use span instead改用跨度

<span>Texttttttttttttttttttttttttttttttttttttttttttttttttttttttttt</span>
<span>Float text</span>

Or set display: inline-block;或者设置display: inline-block; on the div elements在 div 元素上

Also make sure the parent element does not have fixed width.还要确保父元素没有固定宽度。 But this will not be an issue since your'e using white-space: nowrap;但这不会成为问题,因为您使用了white-space: nowrap;

Put the text in a span.将文本放在一个跨度中。 No need to add to CSS.无需添加到 CSS。

<span>Text Here</span>

Try wrapping everything inside a span class尝试将所有内容包装在 span 类中

 <span> <div style="overflow: hidden; white-space: nowrap; text-overflow:ellipsis"> </div> </span> <span> <div style="float: right;">Float text</div> </div> </span>

The answers posted are somewhat right but you can even simplify your markup:发布的答案有些正确,但您甚至可以简化您的标记:

<div style="white-space: nowrap;"> 
   Texttttttttttttttttttttttttttttttttttttttttttttttttttttttttt Float text
</div>

Fiddle小提琴

The white-space property is used to describe how whitespace inside the element is handled. white-space 属性用于描述如何处理元素内的空白。

nowrap Collapses whitespace as for normal, but suppresses line breaks (text wrapping) within text. nowrap像正常一样折叠空白,但禁止文本中的换行符(文本换行)。

MDN on white-space空白区域的 MDN

Give both of the elements a display:inline-block property.给这两个元素一个 display:inline-block 属性。 You had several quotation marks and a closing div tag missing.你有几个引号和一个结束 div 标签丢失。 All of this has been fixed and can be seen in this JSFiddle .所有这些都已修复,可以在此JSFiddle 中看到。

The updated html is below:更新后的 html 如下:

<div>
   <div style="overflow: hidden; white-space: nowrap; text-overflow:ellipsis;display:inline-block"> 
       Texttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
   </div>
   <div style="float: right;display:inline-block">
       Float text
   </div>
</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM