html - 如何在不导致换行符的情况下将元素的宽度指定为最大?

标签 html css width overflow

我对 CSS 很陌生。我想显示一长行充满省略号的文本。 This jsfiddle演示了一个工作示例。

但是,请注意,如果您扩展窗口,文本仍然限制在 100 像素(由于 max-width: 100px)属性。但是,如果我删除 max-width 属性,文本 never shrinks :

enter image description here

我希望文本在不引起换行的情况下尽可能地延长。即这是它应该如何寻找各种窗口宽度:

enter image description here

enter image description here

enter image description here

我该怎么做?

最佳答案

这样就可以了。您可以使用 display: block; 而不是 display: inline-block; 然后添加 margin-right: 75px; 这样文本就不会与按钮重叠。

.one-line-only {
    font-family: monospace;
    display: block;
    vertical-align: middle;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-right: 75px; /*(you can set the value depends on your needs)*/
}

然后将此 css 添加到您的按钮上,然后添加 margin-top: -20px; 以内联按钮。

.b{
    margin-top: -20px; /*(you can set the value depends on your needs)*/
    float:right;
}

<span class="one-line-only"> Stuff to the left ----  Thisisareallylongthingwhichshouldbestoppedatsomepointforsure</span>
<button class='b'>Stuff right</button>

JSFIDDLE DEMO

关于html - 如何在不导致换行符的情况下将元素的宽度指定为最大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33048613/

相关文章:

css - 为什么这些元素不重叠?

html - 我想全屏显示背景图片

css - 图像中心,而不是背景 CSS 或负边距

html - CSS 计数器 - 递增深度嵌套列表

html - 限制 HTML5 Facebook 评论框的大小

HTML 5 Doctype 导致 quirksmode?

css - 为什么这 4 个 div 不彼此相邻? (宽度 25%)

html - 从 iOS Swift 中的摄像头扫描仪 SDK 返回时,WKwebview 是空白屏幕吗?

css 转换不适用于 android 股票浏览器

css - DOM 中没有 CSS 属性的不可见文本节点?