【问题标题】:How do I keep these elements stacked on top of each other?如何让这些元素堆叠在一起?
【发布时间】:2015-09-10 16:30:59
【问题描述】:

试图让这个 div 充满文本在页面的中心,在一个淡出的图像后面。除了文本 div 的居中之外,我已经解决了所有问题。我尝试做margin-left:auto;margin-right:auto,但无济于事。我做错了吗?

我正在 Chrome 中测试结果,如果这有什么不同的话。

HTML

    <div class="content">
        <div class="info">
            <p class="title">is this like, something i don't know</p>
            <p> </p>
            <p class="size">90 x 145 cm</p>
            <p> </p>
            <p class="year">2012</p>
        </div>
        <img class="work" src="029.jpg"/>
    </div>

CSS

.work {
display: block;
margin-left: auto;
margin-right: auto;
}

img.work:hover {
    opacity:0.5;
}

.content {
    position: relative;
}

.info {
    text-align: center;
    margin-left: auto;
    margin-right: auto;
    margin-top: 100px;
    position: absolute;
    z-index: -1;
}

.title {
    font-size: 20px;
    font-weight: bold;
}

.year {
    font-size: 15px;
    font-weight: bold;
}

.size {
    font-size: 15px;
    font-weight: bold;
}

【问题讨论】:

    标签: html css alignment center


    【解决方案1】:

    看到这个fiddle

    由于您使用的是绝对定位,请使用

    left:0;
    right:0;
    

    使项目集中。

    因此,.info 的新 CSS 就像

    .info {
        text-align: center;
        margin-top: 100px;
        position: absolute;
        z-index: -1;
        right:0;
        left:0;
    }
    

    【讨论】:

    • 非常感谢。已经为此苦苦挣扎了几个小时。很多爱。
    • 太棒了.. :) 很高兴它有帮助.. :)
    猜你喜欢
    • 2013-10-10
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    相关资源
    最近更新 更多