1

使用 Bootstrap,我试图将两个跨度(跨度 4 和 span8)中的文本和图像与同一跨度(span8)中的一个图像和文本垂直对齐。图像当前向左浮动。图像将根据浏览器窗口的宽度隐藏/取消隐藏,因此文本将占据隐藏图像的空间。那部分有效。

但是将文本与图像垂直对齐并没有,我已经查看并尝试了我能找到的每一个“解决方案”,但没有一个对我有用。文本的总高度小于图像的高度。

代码...

<div class="outer">
<div class="row">
<div class="span4">Line 1<br/>Line 2</div>
<div class="span8"><img src="http://lorempixel.com/90/90/abstract/" alt="" />This is a line of text. <br /> This is another line of text.<br /> This is a third line of text.</div>
</div>
</div>

.outer img {display:block;}
.outer .row {padding:4px; background-color:#CC9966}
.span4 {background-color:#555; width:100px; text-align:right}
.span8 {background-color:#777; width:400px}
.row img {float:left; padding-right:5px; width:90px; height:90px;}

/* styles from bootstrap */
.row {margin-left: 0px; *zoom: 1;}
.row:before,.row:after {display: table; content: ""; line-height: 0;}
.row:after {clear: both;}
[class*="span"] {float: left; min-height:1px; margin-left:12px;}

http://jsfiddle.net/profweather/sN9rU/

我的项目中有二十四行,所以这种垂直对齐会重复很多次。

我最接近的是使用在http://phrogz.net/css/vertical-align/index.html上看到的相对和绝对定位的外部 div/内部 div但图像右侧的文本仍然对齐到图像的顶部,未垂直对齐到图像的 90 像素高度。

我什至尝试了表格布局,但无济于事。非常感谢任何帮助/指导。

4

1 回答 1

0

对您的 HTML 进行轻微更改,就好像您有几十行,那么每一行都是外部的(假设您想在每一行上居中文本。

<div class="row outer">
    <div class="span4 inner2">Line 1<br/>Line 2</div>
    <div class="span8">
        <img src="http://lorempixel.com/90/90/abstract/" alt="" />
        <div class="inner3">This is a line of text.
            <br />This is another line of text.
            <br />This is a third line of text.</div>
    </div> 
</div>

还有一个CSS上的frig - 只需根据行数选择每一行,例如将2行居中的文本使用类inner2和3行使用类inner3。这将为您节省一些集中注意力的痛苦。

/* .inner working but positioned relative
    use .inner3 where you have 3 lines of text and
    .inner2 where you have 2 lines, and adjust to suit 

     You will need @media queries if the layout is to be responsive...
*/

.inner3 {
    position:relative;
    top:12px;
}
.inner2 {
    position:relative;
    top:20px;
}

/* Mostly your stuff... */
.outer {
    border:2px solid red
}
.outer img {
    display:block;
}
.outer .row {
    padding:4px;
    background-color:#CC9966
}
.span4 {
    background-color:#555;
    width:100px;
    text-align:right
}
.span8 {
    background-color:#777;
    width:400px
}
.row img {
    float:left;
    padding-right:5px;
    width:90px;
    height:90px;
}

/* styles from bootstrap */
 .row {
    margin-left: 0px;
    *zoom: 1;
}
.row:before, .row:after {
    display: table;
    content:"";
    line-height: 0;
}
.row:after {
    clear: both;
}
[class*="span"] {
    float: left;
    min-height:1px;
    margin-left:12px;
}

ps - 谢谢你的问题,因为我需要自己解决这个问题,并决定继续进行更容易。当然,任何更好的答案都非常感谢......

于 2013-04-24T15:42:31.970 回答