简体   繁体   English

中心div在另一个div内水平和垂直

[英]Center div horizontally and vertically inside another div

I have a question regarding centering an element horizontally and vertically inside of another div. 我有一个问题,关于在另一个div内水平和垂直对中元素。

I am working on a website for a client, and here is the page that is giving me trouble: http://staging.slackrmedia.com/halfpast/brand_collection_bremont.html 我正在为一个客户的网站工作,这是给我带来麻烦的页面: http//staging.slackrmedia.com/halfpast/brand_collection_bremont.html

So when you hover over an image, you see a link that says "View Collection". 因此,当您将鼠标悬停在图像上时,会看到一个显示“查看集合”的链接。 I'm styling this using CSS3 transitions and opacity, but I'm having one issue: I need this "View Collection" link to center horizontally and vertically inside of the div in which it sits. 我使用CSS3过渡和不透明度来设计这个样式,但是我遇到了一个问题:我需要这个“View Collection”链接来水平和垂直居中于它所在的div内部。

Here is a link to the jsFiddle with my code: http://jsfiddle.net/T2n5b/4/ 这是我的代码jsFiddle的链接: http//jsfiddle.net/T2n5b/4/

HTML 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 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;
}

Any solution with HTML, CSS, or jQuery is perfectly fine. 使用HTML,CSS或jQuery的任何解决方案都非常好。 I think there might just be a small issue with my HTML and CSS that I'm currently using, but I just can't figure it out. 我认为我目前正在使用的HTML和CSS可能只是一个小问题,但我无法弄明白。

Add width and height to .btn, next add position:absolute; 添加宽度和高度到.btn,然后添加位置:绝对; left:50%; 左:50%; top:50%; 顶部:50%; and the last - margin: -50% .btn-height 0 0 -50% .btn-width 和最后一个边距:-50%.btn-height 0 0 -50%.btn-width

Something like that 这样的事情

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

And one more small advice: instead of .btn:hover in this case better use .col-1-3:hover .btn 还有一个小建议:而不是.btn:在这种情况下悬停更好用.col-1-3:悬停.btn

or wrap img and .btn in another div, for example .img-box, and set the rule .img-box:hover .btn 或者将img和.btn包装在另一个div中,例如.img-box,并设置规则.img-box:hover .btn

using jquery you can test at jsFiddle adjust the margin to your preference 使用jquery你可以在jsFiddle测试根据你的喜好调整边距

$(".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();
});

Make sure yo have jQuery reference to your page <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 确保你对你的页面有jQuery引用<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM