【问题标题】:Responsive positioning in center响应式中心定位
【发布时间】:2016-10-21 21:40:33
【问题描述】:

我有一个包含一些内容的全宽背景图像。 最后,我想将我的按钮放置在中心(垂直和水平),但是使用position:absolute,这不起作用。你可以在JSFiddle看到它。

我的 CSS 中有一些代码行

.buttons{
    position:relative;
}

.buttons .button-pos{
  width:100%;
  position:absolute;
  margin:auto;
  top:0;
  bottom:0;
  left:0;
  right:0;
}

我想要的几乎没有什么计划。

【问题讨论】:

标签: html css responsive full-width


【解决方案1】:

1.) 你的.buttons div 没有高度,所以首先你需要为它定义一个高度,否则就没有垂直居中的可能性(我在小提琴中设置了 200px)。

2.) 要在.buttons 中居中.button-pos,请使用

.button-pos {
  width: 100%;
  position:absolute;
  top:50%;
  left:50%;
  transform: translate(-50%, -50%);
}

这是小提琴:https://jsfiddle.net/Luo1k7Lt/1/

【讨论】:

    【解决方案2】:

    我自己做了一些解决方案,现在效果很好,我决定将我所有的内容放在中心,即标题中的内容。屏幕尺寸只有一些小的变化,效果很好:)

    #welcome-header .welcome-content{
        width: 80%;
        height: 400px;
        position: absolute;
        margin: auto;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
    }
    
    .buttons{
        margin-top: 40px;
    }
    

    如果有人有更好的解决方案,写在那里:)

    【讨论】:

    • 但这不是居中,这只是将.buttons div 的底部移动到其容器底部上方的 10%。它仍然没有高度。尝试重新考虑我的答案。
    • ?? margin-top: 40px 没有给 .buttons 一个高度(即仍然没有居中)
    • @Johannes 对我来说这个解决方案很合适,但我仍然想只将按钮居中。我让所有内容都居中,看起来不错
    【解决方案3】:

    试试这个:

    .buttons .button-pos { display: inline-block; zoom: 1; }

    一个简单的 IE hack 是在该 CSS 规则中添加 display*: inline;

    【讨论】:

    • 这有帮助吗?
    猜你喜欢
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多