【问题标题】:Center div horizontally and vertically inside another div在另一个 div 中水平和垂直居中 div
【发布时间】:2013-08-29 02:43:40
【问题描述】:

我有一个关于在另一个 div 内水平和垂直居中元素的问题。

我正在为客户开发一个网站,这是给我带来麻烦的页面:http://staging.slackrmedia.com/halfpast/brand_collection_bremont.html

因此,当您将鼠标悬停在图片上时,您会看到一个显示“查看收藏”的链接。我正在使用 CSS3 过渡和不透明度对其进行样式设置,但我遇到了一个问题:我需要这个“视图集合”链接在它所在的 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 可能只是一个小问题,但我就是想不通。

【问题讨论】:

  • 您可能需要考虑回复您在此处获得的答案
  • 我知道,我真的很抱歉我的延迟回复。我在工作日结束时发布了这个,只是再也没有机会再次查看。感谢您的帮助。

标签: html css layout


【解决方案1】:

为.btn添加宽度和高度,接下来添加位置:绝对;左: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%;
}

还有一个小建议: 而不是 .btn:hover 在这种情况下最好使用 .col-1-3:hover .btn

或者将 img 和 .btn 包裹在另一个 div 中,例如 .img-box,并设置规则 .img-box:hover .btn

【讨论】:

  • 不幸的是,当我将它添加到 jsFiddle (jsfiddle.net/4537r) 时,它似乎将容器 div (.row) 内的每个按钮都居中。
  • 添加 .col-1-3 {position:relative;}
  • 非常好,加上这一切都不同了。现在这完美无缺。谢谢!
【解决方案2】:

使用 jquery 你可以test at 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();
});

确保你的页面有 jQuery 引用 &lt;script src="http://code.jquery.com/jquery-1.10.1.min.js"&gt;&lt;/script&gt;

【讨论】:

  • 非常感谢 cmets,但是,当我将此代码添加到我的网站时,没有任何反应(按钮未显示)(ow.ly/olxck)。我什至相应地更改了 CSS。我敢肯定这只是一个小问题,但你知道它可能是什么吗?
  • 您可能需要将 jQuery 引用添加到您的网站
猜你喜欢
  • 2015-08-29
  • 1970-01-01
  • 2020-09-16
  • 2014-05-10
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 2015-09-16
  • 2012-12-16
相关资源
最近更新 更多