【问题标题】:Keep a button centered over an fluid image保持按钮居中在流畅的图像上
【发布时间】:2013-03-12 10:13:32
【问题描述】:

最近几天,我尝试将按钮置于流畅图像的中心。到目前为止,位置是固定的和静态的。相关的 HTML 部分如下所示:

<ul class="moodlegrid sectionwrap">
  <li>
    <a class="ajax1" href="project1.html">
      <img title="Project 1" src="img/projectblur.jpg" alt="Project 1" />
      <img title="Project 1" src="img/project.jpg" alt="Project 1" />
      <span class="openoverlay">Click</span>
    </a>
  </li>
</ul>

CSS 看起来像这样:

.moodlegrid{
    li{
        a{
            position: relative;
            display:block;
            width: 100%;
            img:nth-child(1){
                display:block;
            }
            img:nth-child(2){
                display: none;
            }
            span{
                display:none;
            }
        }
        a:hover{    
            img:nth-child(2){
                display: block;
                position: absolute;
                z-index: 100;
                top:0;
                left:0;
            }
            span{
                display: block;
                position: absolute;
                text-align:center;
                top: 37%;
                left: 28%;
                z-index:101;
                @include border-radius(5px, 5px);
                @include box-shadow(black 2px 2px 10px);
            }
        }
    }
}

img{
    width:100%;
    max-width: 100%;
    height:auto !important;
}

当鼠标悬停在第二张图片上时,一个按钮应该在第一张图片上方居中浮动。问题是,如果视口更改,最新版本不再起作用并且按钮未居中。以下关于 CSS 技巧的文章有一个很有前途的解决方案:

Centering in the unknown

演示运行良好:

Centering in the unknown demo

但它使用了 inline-block 元素,这使得分层图像并在最后显示居中按钮变得困难 - 当设置 display:inline-block 属性时,元素将依次显示。还有一个问题,与我的 HTML 相比,演示将子对象与父对象对齐。

有没有办法将上述技术应用于我的问题,或者是否有更好的适合方法?最好的问候拉尔夫

【问题讨论】:

    标签: html css compass-sass


    【解决方案1】:

    如果我理解正确,我想你就差不多了。

    &lt;a&gt; 必须是position: relative;

    &lt;span&gt; 必须是position: absolute;

    &lt;span&gt;上如果你设置了特定的宽度,然后应用:

    width: 100px;
    height: 100px;
    top: 50%;
    left: 50%;
    margin-top: -50px /* Half of the height */
    margin-left: -50px; /* Half of the width */
    

    http://codepen.io/anon/pen/xjcCf

    这能解决你想要做的事情吗?

    【讨论】:

    • 您可能希望将宽度和高度调整为对Click 文本更合理的值。这是一个使用50px 作为宽度并使用20px 作为该文本的示例。它还解析LESS。如果您愿意,请随意利用其中的任何一个来回答您的问题。 jsfiddle.net/kCVzr/1
    • 感谢工作!我刚刚用 rem 交换了 px 值,但除此之外一切正常。您的解决方案的优势在于它利用了一般属性,而没有像垂直对齐这样可能导致跨浏览器问题的东西。所以我唯一要做的就是调整媒体查询中的按钮大小和边距值。再次感谢您的快速帮助!
    【解决方案2】:

    你好,是这个样子的

    html

    <div>
        <h1><span>button</span></h1>
        <img src="imageSample.jpg" alt="" />    
    </div>
    

    css

    div {
        position:relative;
        width:100%;
    }
    div img {
        max-width:100%;
        width:100%;
    }
    div h1 {
        width:100px;
        margin:auto;
    }
    div h1 span {
        position:absolute;
        z-index:999;
        top:200px;
        padding:5px;
        background-color:#fff;
    }
    

    工作demo

    注意:滚动小提琴,你可以看到效果

    【讨论】:

    • hm 如果我在你的小提琴中缩小窗口并且图像调整大小按钮没有保持其居中位置。在您的示例中,跨度放置在顶部边框下方 200 像素处,因此对于较小的图像尺寸,按钮会向下移动。
    猜你喜欢
    • 2014-11-08
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2018-03-04
    • 2015-09-11
    • 1970-01-01
    • 2013-07-25
    相关资源
    最近更新 更多