html - 将元素放在中心

标签 html css slider

我有一个非常简单的问题,但我不知道如何解决。

我有一个 slider ,我想在中间显示一些文本,如下所示: enter image description here

问题是,我无法强制它与图像垂直居中对齐。这是我的 CSS:

.caption {
    position: absolute;
    height: auto;
    text-align: center;
    zoom: 1;
    color: white;
    display:table;
    left:20%;
    width: 60%;
    margin-top: auto;
    margin-bottom: auto;
    padding: 10px 0;
    background:rgba(138, 138, 138, 0.55);    
}

我做错了什么?

最佳答案

因为您已经使用了 position 标签,这非常好,只需像这样使用 top 属性和 transform 属性

.caption {
    position: absolute;
    height: auto;
    text-align: center;
    zoom: 1;
    color: white;
    display:table;
    left:20%;
    top:50%;
    transform:translate(0,-50%);
    width: 60%;
    margin-top: auto;
    margin-bottom: auto;
    padding: 10px 0;
    background:rgba(138, 138, 138, 0.55);    
}

With both properties div will be vertically center no matter whatever is the height

示例:https://jsfiddle.net/y2z94ez3/

关于html - 将元素放在中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36434212/

相关文章:

jQuery-ui 问题 : . slider ("value")返回 slider 本身而不是 slider 的值

javascript - Owl Carousel 100% 高度

javascript - 多个元素的 jQuery 隐藏元素问题

html - 对齐盒子和调整 child 的大小

html - CSS子清除父 float

javascript - 如何在 Angular Material 表中获取 <tr> 之间的间隙

html - 尝试 Img 适合 100% 的 div 但阻止调整大小

javascript - 选择元素 onChange 事件未触发?

javascript - AJAX : Applying effect to CSS class

javascript - 如何检索和显示 slider 范围值?