可变大小裁剪字段的纯 CSS 格式

发布于 2024-11-03 14:19:40 字数 1841 浏览 10 评论 0原文

我有一个文本字符串和一个数字 ID,我想将它们并排显示:

This is a String [123]

看起来很简单,但我无法控制字符串或字符串的长度我要放入的容器的大小。第一个挑战是让它们彼此相邻排列,我使用了我在 SO 上找到的一个技巧:

<div>
    <div class="display_field">
        <div class="label">This is a long string that should be cropped.</div>
        <div class="id">[123]</div>
    </div>
</div>

应用此 CSS,我得到两个彼此相邻的字符串:

* { font-size: 12px; }  /* Just for testing. */

.label, .id {
    display: table-cell;
}

现在我可以显示这是一个应该被裁剪的长字符串。 [123]

问题是如果最外面的 DIV 是一个太小的固定宽度(通常是这样):

<div style="width:125px;"> <!-- Just testing, I have no control over this guy. -->
    <div class="cropped_field">
        <div class="label">This is a long string that should be cropped.</div>
        <div class="id">[123]</div>
    </div>
</div>

输出现在看起来像这样:

This is a long string  [123]
that should be
cropped.

我想要的是 This is a long string [123]

我已经尝试了我能想到的一切,将 DIV 的高度设置为 12px(因此它与字体高度匹配)并将溢出设置为隐藏,使用成熟的表格,我什至考虑使用 Flash 。我已经尝试了很多选择 - 到目前为止我已经花了三四个小时。

我通过谷歌搜索并找到了有用的帖子,得到了很多有用的答案,我决定加入并尝试问我自己的问题。

有几件事需要注意(不确定这些是否会伤害或帮助情况):

  • 我无法控制 ID 的值,它可能是 [1] 或 [9999999] - 我不想硬 -编码该宽度(或“display_field”或“标签”)。即使是百分比也会导致问题。

  • 此环境可能未启用 JavaScript(我希望,我会使用 jQuery 插件!)

  • 容器可能是任何类型的标签,但通常是DIV。我实际上无法控制“显示”字段”之外的标记。

  • 我控制“显示字段”的标记,并且可以使用 SPAN 标记或任何其他完成工作的 HTML。

  • 我只是为 FireFox(需要 3)和 Chrome 编写此内容,不需要与其他蹩脚浏览器兼容(是的,我正在看你的 IE)。

  • “id”字段始终是方括号中的数字,因此无需担心空格。

  • 无需在裁剪文本结束处添加省略号。这是一个很好的功能,但我没有看到任何方法让它在 FireFox 上工作(3.x 有一个 hack,但版本 4 没有)。

I have a text string and a numeric ID that I want to display next to each other:

This is a String [123]

Seems simple enough, but I can't control the length of the string or the size of the container that I'm placing this in. The first challenge is getting these to line up next to each other and I used a trick I found on SO:

<div>
    <div class="display_field">
        <div class="label">This is a long string that should be cropped.</div>
        <div class="id">[123]</div>
    </div>
</div>

Applying this CSS, I get the two strings next to each other:

* { font-size: 12px; }  /* Just for testing. */

.label, .id {
    display: table-cell;
}

Now I can display This is a long string that should be cropped. [123]

The problem is if the outermost DIV is a fixed width that is too small (which it often is):

<div style="width:125px;"> <!-- Just testing, I have no control over this guy. -->
    <div class="cropped_field">
        <div class="label">This is a long string that should be cropped.</div>
        <div class="id">[123]</div>
    </div>
</div>

The output will look now like this:

This is a long string  [123]
that should be
cropped.

And what I want is This is a long string [123]

I have tried everything I could think of, setting the heights of the DIVs to 12px (so it matches the font height) and setting overflow to hidden, using a full-fledged table, I even considered using Flash. I've tried a ton of options - so far I've been at this for three or four hours.

I've been getting so many helpful answers by googling and finding helpful posts on SO, I decided to join and try asking my own question.

A few things to note (not sure if these hurt or help the situation):

  • I can't control the value of the ID, it might be [1] or [9999999] - I don't want to hard-code the width of that (or the "display_field" or the "label" for that matter. Even percentages will cause issues.

  • This environment might not have JavaScript enabled (I wish, I'd use a jQuery plugin!)

  • The container might be any type of tag, but will usually be a DIV. I don't really have control of the markup outside of my "display"field".

  • I control the markup of the "display_field" and can use SPAN tags or any other HTML that gets the job done.

  • I'm only writing this for FireFox (3 is required) and Chrome, other compatibility with crappy browsers isn't necessary (yes, I'm looking at you IE).

  • The "id" field will always be a number in square brackets, so there's no white space to worry about.

  • There's no need to put ellipsis in where the cropped text ends. It's a nice feature, but I don't see any way to get it working on FireFox (there's a hack for 3.x, but nothing for version 4).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像极了他 2024-11-10 14:19:41

下面的怎么样?它主要基于您提供的摘录。

.cropped_field
{
    height:1.4em;
    overflow:hidden;
}

看一下演示小提琴

How about the following? It is mostly based on the extracts you provided.

.cropped_field
{
    height:1.4em;
    overflow:hidden;
}

Take a look at the demo fiddle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文