3

私は次のようにコードを設定しています: http://jsfiddle.net/LtzhN/

コンテナーの中央にある青いボタンを垂直方向に揃えたいのですが、コンテナーの高さが固定されていません。

できればCSSだけで、どうすればこれを行うことができますか?

私は次のようなjqueryでそれを行うことができることを知っています:

var halfOuterHeight = $('.jbe-result').height()/2,
    halfButtonHeight = $('.jbe-run').height()/2;

$('.jbe-run').css('margin-top', halfOuterHeight - halfButtonHeight)

しかし、そうではありません!

4

5 に答える 5

1

私はあなたのフィドルを更新しました:

JSFiddleデモ

.jbe-result{
    ...
    display:table;
}
.jbe-run-container{
    display:table-cell;
    vertical-align:middle;
    border-left:1px solid silver;
    width: 30%;
}

意図しない結果をもたらす可能性のある float を回避することもできます。

于 2013-09-13T14:52:29.367 に答える
0

このアプローチには少し手間がかかりますがjbe-content-container、 と同じレベルで呼び出される div コンテナでテキストをラップし、クラスjbe-run-containerの を削除し、マークアップを並べ替えて の前に置き、 withを使用することで、これを修正できます。また、いくつかのマージンとパディングを移動して調整する必要がありました。float: rightjbe-run'jbe-content-containerjbe-run containerdisplay: inline-blockvertical-align: middle

HTML

<div class="jbe-result">
    <div class="jbe-content-container">        
        <h3>small amount of text</h3>
        <p>Date saved: 13-09-2013, 12:14 am</p>
    </div>
    <div class="jbe-run-container">
        <div class="jbe-run">Run</div>
    </div>
</div>

<div class="jbe-result">
    <div class="jbe-content-container">
        <h3>text goes here which could be quite long winded which will push the height of the div higher</h3>
        <p>Date saved: 13-09-2013, 12:14 am</p>
    </div>
    <div class="jbe-run-container">
        <div class="jbe-run">Run</div>
    </div>
</div>

CSS

.jbe-result{
    float: left;
    width: 96%;
    padding: 5px 2%;
    border-radius: 5px;
    border: 1px solid #c9cfdd;
    background-color: #ffffff;
    margin-bottom: 6px;
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    -webkit-transition-duration: .25s;
    transition-duration: .25s;
    opacity:1;
    height:auto;
    position: relative;
    display: table-row;
}

.jbe-content-container {
    width: 64%;
    border-right:1px solid #c9cfdd;
    padding-right:2%;
    margin-right:1%;
}

.jbe-content-container,
.jbe-run-container {
    display: inline-block;
    vertical-align: middle;
    zoom: 1;
}

.jbe-result h3{
    font-size: 14px;
    color: #003777;
    margin: 0 0 2px 0;
}

.jbe-result p{
    color: #888888;
    font-size: 12px;
    margin: 0;
}

.jbe-run{
    margin:0;
    float:right;
    padding:4px 10px;
    width:70px;
    color:#ffffff;
    font-size:12px;
    border-radius:4px;
    -webkit-border-radius:4px;
    text-align: center;
}

.jbe-run-container{
    width:32%;
}

.jbe-run, .jbe-edit, .add-div{
    background-image: linear-gradient(bottom, #003777 52%, #005DA4 85%);
    background-image: -o-linear-gradient(bottom, #003777 52%, #005DA4 85%);
    background-image: -moz-linear-gradient(bottom, #003777 52%, #005DA4 85%);
    background-image: -webkit-linear-gradient(bottom, #003777 52%, #005DA4 85%);
    background-image: -ms-linear-gradient(bottom, #003777 52%, #005DA4 85%);
    background-image: -webkit-gradient(
        linear,
        left bottom,
        left top,
        color-stop(0.52, #003777),
        color-stop(0.85, #005DA4)
    );
}

ワーキングjsFiddle

于 2013-09-13T14:51:36.680 に答える