如何限制元素的最小和最大宽度?

发布于 2024-08-19 19:17:11 字数 1106 浏览 4 评论 0原文

我有一行文本,我想在其周围绘制边框。目前,标记如下所示:

<div id="titleBox"><span id="titleName" class="truncate">Really really long long long title name</span></div>

样式如下所示:

    #titleBox{
    margin-top: 10px;

}

#titleName{
    margin: 5px;
    height: 1.6em;
    width: auto;
    max-width: 15.385em;    
    padding: 0.369em;
    margin: 0.369em;
    line-height: 1.7;
    color: rgba(0,0,0,0.4); 
    background-color: rgba(204,204,204,0.4); 
    border: 3px solid rgba(255,255,255,0.14);
    font-size: 153.9%;
    -moz-border-radius: 5px; 
    -webkit-border-radius: 5px;     
}

.truncate {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -ms-text-overflow: ellipsis;        
    -o-text-overflow: ellipsis;
    -moz-binding: url('../templates/ellipsis.xml#ellipsis');
}

我想要的效果是,我希望样式框为文本的宽度,除非宽度超过一定限制。然后我希望宽度受到限制,然后截断类应该启动。

仅使用 div 的问题是框不会换行到文本的宽度。使用跨度会有所帮助,但它不会显示为块。强制它显示为块会出现与将其包装在普通 div 中相同的问题。使用 div/span 组合也不起作用,因为如果文本太长,span 会忽略 div 的边界。

有没有办法限制文本的宽度,强制其截断并在文本周围有一个样式框?

I have a line of text that I want to draw borders around. At the moment, the markup looks like this:

<div id="titleBox"><span id="titleName" class="truncate">Really really long long long title name</span></div>

The style looks like this:

    #titleBox{
    margin-top: 10px;

}

#titleName{
    margin: 5px;
    height: 1.6em;
    width: auto;
    max-width: 15.385em;    
    padding: 0.369em;
    margin: 0.369em;
    line-height: 1.7;
    color: rgba(0,0,0,0.4); 
    background-color: rgba(204,204,204,0.4); 
    border: 3px solid rgba(255,255,255,0.14);
    font-size: 153.9%;
    -moz-border-radius: 5px; 
    -webkit-border-radius: 5px;     
}

.truncate {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -ms-text-overflow: ellipsis;        
    -o-text-overflow: ellipsis;
    -moz-binding: url('../templates/ellipsis.xml#ellipsis');
}

The effect that I'd like is that I'd like the styled box to be the width of the text, unless the width exceeds a certain limit. Then I would want the width to be limited and then the truncate class should kick in.

The problem with only using a div is that the box doesn't wrap to the width of the text. Using a span helps but then it doesn't display as a block. Forcing it to display as a block exhibits the same problem as wrapping it in a plain div. Using a div/span combination doesn't work either because the span ignores the boundaries of the div if the text is too long.

Is there a way to limit the width of the text, forcing it to truncate and have a styled box surrounding the text?

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

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

发布评论

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

评论(1

根据我的评论, display:inline-block 应该能达到预期的效果。但它在

As per my comment, display:inline-block should give the desired effect. it won't work in <IE6 though.

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