首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >你能制造一个不用固定宽度的右边框包装的流线型头吗?

你能制造一个不用固定宽度的右边框包装的流线型头吗?
EN

Stack Overflow用户
提问于 2013-04-21 00:06:38
回答 2查看 1.7K关注 0票数 4

恐怕比那要复杂一些。

我需要一个流体宽度标题(h2),永远不会包装,与左对齐按钮(.btn1),所有没有重叠的固定宽度右按钮(.btn2)。

如果这听起来太复杂了,那么写得好的小提琴 (http://jsfiddle.net/8ZtU5/)和一些漂亮的ASCII艺术怎么样?

代码语言:javascript
复制
__________________________________________________
|                                                |
| This header is really really... [One]    [Two] |
|                                                |
--------------------------------------------------
__________________________________________________
|                                                |
| This header is short [One]               [Two] |
|                                                |
--------------------------------------------------
_________________________________
|                               |
| This header is... [One] [Two] |
|                               |
---------------------------------

你认为如何?你能帮上忙吗?

HTML:(http://jsfiddle.net/8ZtU5/)

注意:额外的包装只是为了方便。除了呈现的结果之外,什么都不需要。

代码语言:javascript
复制
<div class="container">
    <div class="fluid-width">
        <div class="no-wrapping">
            <h2>This header is really really really really really really really long</h2>
        </div>
        <button class="btn1">One</button>
    </div>
    <div class="fixed-width">
        <button class="btn2">Two</button>
    </div>
</div>
<div class="container">
    <div class="fluid-width">
        <div class="no-wrapping">
            <h2>This header is short</h2>
        </div>
        <button class="btn1">One</button>
    </div>
    <div class="fixed-width">
        <button class="btn2">Two</button>
    </div>
</div>

基本CSS:

代码语言:javascript
复制
.container {
    margin:20px 0;
    width:100%;
    min-width:300px;
    background:#eee;
}

.fluid-width {
    min-width:200px;
    display:inline-block;
}

.fixed-width {
    width:100px;
    max-width:100px;
    min-width:100px;
    display:inline-block;
}

.no-wrapping {
    overflow: hidden; white-space: nowrap; text-overflow: ellipsis; word-break: break-all; word-wrap: break-word;
}
h2 {
    display:inline;
}

不用猜..。如果你想玩的话,那就全在小提琴上了:http://jsfiddle.net/8ZtU5/

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-21 00:41:00

也许这就是你的意思。小提琴编辑了一下html structure

票数 3
EN

Stack Overflow用户

发布于 2013-04-21 01:00:34

这大致是一个解决方案,您可以通过更改h2的最大宽度来改变标题中显示的文本数量。

http://jsfiddle.net/QJg8X/

标记

代码语言:javascript
复制
<div class="container">
    <div class="fluid-width">
        <h2>This header is really really really really really really long</h2>
        <button class="btn1">One</button>
        <button class="btn2">Two</button>
    </div>
</div>
<div class="container">
    <div class="fixed-width">
         <h2>This header is short</h2>
        <button class="btn1">One</button>
        <button class="btn2">Two</button>
    </div>
</div>

css

代码语言:javascript
复制
.container {
    margin:20px 0;
    width:100%;
    min-width:300px;
    background:#eee;
}

.fluid-width {
    display: inline-block;
    overflow: hidden;
    min-width: 200px;
}

.fixed-width {
    display: inline-block;
    overflow: hidden;
    width: 400px;
}

h2 {
    float: left;
    margin: 0;
    padding: 0;
    display: block;
    white-space: nowrap;
    max-width: 80%;
    overflow: hidden;
    text-overflow: ellipsis;
}

button.btn1 { float: left; }
button.btn2 { float: right; }

不过,我只在现代浏览器上测试过上面的内容。

啊.。与AzDesign的答案非常相似(+1),但实际上它使用的是浮点数,而不是不确定的位置。

更新

另一种解决相同问题的方法是为按钮设置一个固定宽度区域,这意味着标记稍有改变,但意味着您不必指定最大宽度:

http://jsfiddle.net/QJg8X/2/

标记

代码语言:javascript
复制
<div class="container">
    <div class="fluid-width">
       <div class="buttons">
            <button class="btn1">One</button>
            <button class="btn2">Two</button>
       </div>
       <h2>This header is really really really really really really long</h2>
    </div>
</div>
<div class="container">
    <div class="fixed-width">
        <div class="buttons">
            <button class="btn1">One</button>
            <button class="btn2">Two</button>
        </div>
        <h2>This header is short</h2>
    </div>
</div>

css

代码语言:javascript
复制
.container {
    margin:20px 0;
    width:100%;
    min-width:300px;
    background:#eee;
}

.fluid-width {
    display: block;
    overflow: hidden;
    min-width: 200px;
}

.fixed-width {
    display: block;
    overflow: hidden;
    width: 400px;
}

h2 {
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.buttons {
    display: block;
    width: 150px;
    float: right;
}

button.btn2 { float: right; }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16126585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档