3

別の div 内で要素を水平方向および垂直方向の中央に配置することについて質問があります。

私はクライアント向けの Web サイトに取り組んでいますが、ここにあるページで問題が発生しています: http://staging.slackrmedia.com/halfpast/brand_collection_bremont.html

そのため、画像にカーソルを合わせると、「コレクションを表示」というリンクが表示されます。CSS3 トランジションと不透明度を使用してこれをスタイリングしていますが、問題が 1 つあります。この「ビュー コレクション」リンクを、それが配置されている div の内側で水平方向および垂直方向の中央に配置する必要があります。

ここに私のコードを含む jsFiddle へのリンクがあります: http://jsfiddle.net/T2n5b/4/

HTML

<h2>Bremont Collections</h2>

<div class="row text-center uppercase">
    <div class="col-1-3">
        <img src="http://staging.slackrmedia.com/halfpast/images/watches/alt1-p.png" alt="ALT1-P Pilot" />  <a href="" class="btn small">View Collection</a>
        <a href="">ALT1-P Pilot</a>
    </div><!-- .col-1-3 -->

    <div class="col-1-3">
        <img src="http://staging.slackrmedia.com/halfpast/images/watches/solo-37.png" alt="Supermarine" />  <a href="" class="btn small">View Collection</a>
    <a href="">Solo-37</a>

    </div><!-- .col-1-3 -->

    <div class="col-1-3">
        <img src="http://staging.slackrmedia.com/halfpast/images/watches/bc-f1.png" alt="BC-F1" />  <a href="" class="btn small">View Collection</a>
        <a href="">BC-F1</a>
    </div><!-- .col-1-3 -->
</div><!-- .row -->

CSS

/* Buttons
   ============================= */
 .btn {
    background: #c60202;
    border-bottom: 1 px solid rgba(0, 0, 0, 0.1);
    border: 0;
    color: #fff;
    cursor: pointer;
    display: inline-block;
    font-size: 1em;
    height: 45px;
    line-height: 2.9;
    margin: 0px;
    padding: 0 40px;
    position: relative;
    text-align: center;
    text-decoration: none;
    vertical-align: bottom;
    width: auto;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    font-smoothing: antialiased;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-transition: all .2s ease;
    -moz-transition: all .2s ease;
    -ms-transition: all .2s ease;
    -o-transition: all .2s ease;
    transition: all .2s ease;
}
.btn:hover {
    background: #d31d1c;
    background-image: -webkit-gradient(linear, 0 0, 0 100%, from(rgba(0, 0, 0, 0.05)), to(rgba(0, 0, 0, 0.05)));
    background-image: -moz-linear-gradient(top, rgba(0, 0, 0, 0.05), rgba(0, 0, 0, 0.05));
    background-image: -webkit-linear-gradient(top, rgba(0, 0, 0, 0.05), rgba(0, 0, 0, 0.05));
    background-image: -o-linear-gradient(top, rgba(0, 0, 0, 0.05), rgba(0, 0, 0, 0.05));
    background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.05), rgba(0, 0, 0, 0.05));
    background-repeat: repeat-x;
}
.btn.small {
    font-size: 0.9em;
    line-height: 2.4;
    height: 35px;
    padding: 0 15px;
}
a.btn {
    color: #fff;
    text-decoration: none;
}
/*
 * Type Styles
 */
 .text-center {
    text-align: center;
}
.uppercase {
    text-transform: uppercase;
}
/* Grid
   ============================= */
 *, *:after, *:before {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
}
.row {
    margin-left: -20px;
}
.row:after {
    clear: both;
    content:"";
    display: table;
}
/*
 * Grid Gutters
 */
[class*='col-'] {
    float: left;
    padding-left: 20px;
}
.row-pad {
    padding: 20px 0 20px 20px;
}
.row-pad[class*='col-']:last-of-type {
    padding-right: 20px;
}
/* 
 * Grid Columns
 */
 .col-1-3 {
    width: 300px;
}
/*
 * Button Hover
 */
 .row .col-1-3 img {
    display: block;
    width: 163px;
    height: 276px;
    margin: 0 auto 10px;
}
.row .col-1-3 a.btn.small {
    font-size: 0.75em;
    margin: -165px 0 0 -80px;
    opacity: 0;
    position: absolute;
}
.row .col-1-3 a.btn.small:hover {
    opacity: 1;
}

HTML、CSS、または jQuery を使用したソリューションはまったく問題ありません。現在使用している HTML と CSS に小さな問題があるのではないかと思いますが、それを理解することはできません。

4

2 に答える 2

1

jsFiddle でテストできる jquery を使用 して、好みに合わせて余白を調整します

$(".col-1-3 img").hover(function(){
    var $viewCollection  = $(this).next();
    $viewCollection.attr("id","selectedVC");
    $viewCollection.css({"margin-top": "-165px","margin-left": "68px"});
    $viewCollection.show();
});

$(".col-1-3 img").mouseleave(function(){
    var $viewCollection  = $(this).next();
    if(idIsHovered("selectedVC") == false){
    $viewCollection.removeAttr("id");
    $viewCollection.hide();
    }
});

function idIsHovered(id){
    return $("#" + id + ":hover").length > 0;
}

$(function(){
   $(".btn.small").hide();
});

あなたのページへのjQuery参照があることを確認してください <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

于 2013-08-28T03:15:37.013 に答える
1

幅と高さを .btn に追加し、次に position:absolute; を追加します。左:50%; 上:50%; そして最後 - マージン: -50% .btn-height 0 0 -50% .btn-width

そんな感じ

.btn.small {
    width:140px;
    height:36px;
    margin:-18px 0 0 -70px;
    position:absolute;
    left:50%;
    top:50%;
}

もう 1 つの小さなアドバイス: .btn:hover の代わりに、この場合は .col-1-3:hover .btn を使用することをお勧めします。

または、img と .btn を別の div (.img-box など) でラップし、ルール .img-box:hover .btn を設定します。

于 2013-08-28T02:10:54.563 に答える