【问题标题】:3 divs in row with auto width adjust具有自动宽度调整功能的 3 个 div
【发布时间】:2012-06-09 02:00:27
【问题描述】:

我有 3 个 div:

  • 需要并排(连续)
  • 都有背景图片
  • center div需要自动调整宽度,center div的宽度依赖于已经插入div的内容(长文本=长宽度
  • center div 也需要位于页面的中心。
  • 两侧的 div 具有不同的背景 img。

HTML 代码:

<div class="spanl"></div>
<div class="center">Headline</div>
<div class="spanr"></div>

注意侧边的 div 是空的。

类似这样的: http://jsfiddle.net/fsnuh/134/

这可能与 css 或 css3 有关吗?如果不是,我怎么能用 jquery 或 raw javascript 来做呢?

【问题讨论】:

  • 您的自动宽度 div(居中的)可以扩展多少?

标签: jquery css


【解决方案1】:

为胜利而战! fiddle

<!DOCTYPE html>
<html>
    <head>
        <style type="text/css">
            #container {  display: table; width: 100% }
            #left, #right { display: table-cell; width: 50%; background: #ffd }
            #content { display: table-cell }
        </style>
    </head>
    <body>
        <div id="container">
            <div id="left"></div>
            <div id="content">
                <div style="white-space: nowrap" onclick="this.firstChild.nodeValue += ' blah'">blah</div>
            </div>
            <div id="right"></div>
        </div>
    </body>
</html>

【讨论】:

  • 我喜欢这个解决方案,但是当我看到它时,我问你是否真正观察到现在可以使用表格进行布局。 (再次)
【解决方案2】:

如果可以的话,给标题一个固定的宽度。然后使用 jQuery 计算宽度并相应地设置它们。

这是一个工作演示:http://jsfiddle.net/rniestroj/fsnuh/135/

html:

<div>
     <h3 class="headline2">
     <div class="spanl"></div>
     <div class="center">Kontakt 11111111111 22222</div>
     <div class="spanr"></div>
     </h3>
</div>

css:

*{
    padding: 0;
    margin: 0;    
}

h3{
    width: 600px;
    overflow: auto;
}

.spanr, .spanl {
    background: url("../img/headline2.png") no-repeat scroll right center gold;
    float: left;
    height: 33px;
    position: relative;
    width: 33%;
}
.center {
    background: none repeat scroll 0 0 #E6B043;
    float: left;
    height: 33px;
    line-height: 33px;
    padding: 0 10px;
    position: relative;
    text-transform: uppercase;
    width: auto;
}

js:

var h3Width = $("h3").width();
var centerWidth = $(".center").width();
var asideWidth = 0;
asideWidth = ((h3Width - centerWidth) / 2) - 12;

$(".spanl").width(asideWidth );
$(".spanr").width(asideWidth );

【讨论】:

    【解决方案3】:

    试试这个纯 CSS 的解决方案:

    jsFiddle Example.

    HTML:

    <div id="headlines">
        <h3 class="headline2">
            <span>Kontakt</span>
        </h3>
    </div>
    

    CSS:

    #headlines {
        background-image: url('../img/headline2.png'), url('../img/headline2.png');
        background-repeat: no-repeat;
        background-position: left center, right center
    }
    .headline2, .headline2 > span {
        height: 33px;
        text-align: center;
        text-transform: uppercase;
        line-height: 33px
    }
    .headline2 > span {
        background-color: #E6B043;
        display: inline-block;
        padding: 0 10px
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 2015-09-05
      • 2023-04-03
      相关资源
      最近更新 更多