html - 无法将我的文本定位在用 CSS 制作的圆圈中

标签 html css

正如您在这个 jsfiddle 上看到的 http://jsfiddle.net/L9k5c/我的“你好”文本位于我的红色圆圈之外,而不是居中(这是问题 1)。问题 2 是这个“Hello”文本使“onmouse over”圆圈不再与红色圆圈对齐。 我该如何解决这两个问题? (对于问题 1,我知道我可以使用填充和边距,但我想这不是一个干净的解决方案 + 它不会解决问题 2。 非常感谢

<div class="ch-item ch-img-1"><p>hello</p>
    <div class="ch-info">
        <h3>Use what you have</h3>
        <p>by Angela</p>
    </div>
</div>

CSS:

.ch-item {
    width: 200px;
    height: 200px;
    border-radius: 50%;
    position: relative;
    cursor: default;
    box-shadow: 
        inset 0 0 0 16px rgba(255,255,255,0.6),
        0 1px 2px rgba(0,0,0,0.1);

    -webkit-transition: all 0.4s ease-in-out;
    -moz-transition: all 0.4s ease-in-out;
    -o-transition: all 0.4s ease-in-out;
    -ms-transition: all 0.4s ease-in-out;
    transition: all 0.4s ease-in-out;
}

.ch-img-1 { 
    background: red;
}

.ch-info {
    position: absolute;
    background: rgba(63,147,147, 0.8);
    width: inherit;
    height: inherit;
    border-radius: 50%;
    opacity: 0;

    -webkit-transition: all 0.4s ease-in-out;
    -moz-transition: all 0.4s ease-in-out;
    -o-transition: all 0.4s ease-in-out;
    -ms-transition: all 0.4s ease-in-out;
    transition: all 0.4s ease-in-out;

    -webkit-transform: scale(0);
    -moz-transform: scale(0);
    -o-transform: scale(0);
    -ms-transform: scale(0);
    transform: scale(0);

    -webkit-backface-visibility: hidden;

}

.ch-info h3 {
    color: #fff;
    font-size: 18px;
    margin: 0 30px;
    padding: 45px 0 0 0;
    height: 100px;
    font-family: 'Open Sans', Arial, sans-serif;
}

.ch-info p {
    color: #fff;
    font-style: italic;
    padding-left:80px;
    font-size: 12px;
    border-top: 1px solid rgba(255,255,255,0.5);
    opacity: 0;
    -webkit-transition: all 1s ease-in-out 0.4s;
    -moz-transition: all 1s ease-in-out 0.4s;
    -o-transition: all 1s ease-in-out 0.4s;
    -ms-transition: all 1s ease-in-out 0.4s;
    transition: all 1s ease-in-out 0.4s;
}


.ch-item:hover {
    box-shadow: 
        inset 0 0 0 1px rgba(255,255,255,0.1),
        0 1px 2px rgba(0,0,0,0.1);
}
.ch-item:hover .ch-info {
    -webkit-transform: scale(1);
    -moz-transform: scale(1);
    -o-transform: scale(1);
    -ms-transform: scale(1);
    transform: scale(1);
    opacity: 1;
}

.ch-item:hover .ch-info p {
    opacity: 1;
}

最佳答案

您可以使用绝对位置转换轻松实现您想要的。

jsFiddle Demo

.ch-item p {
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    margin: 0;
}
  • P.S - 使用 transform 不适用于 IE 8 及以下版本,但您已经在其他地方使用它,所以我想没问题。

关于html - 无法将我的文本定位在用 CSS 制作的圆圈中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18673185/

相关文章:

javascript - jqplot转换为图像不显示y轴html

html - 背景模式和下拉菜单冲突

html - WordPress : Handle CSS priority between Child & Parent Theme

css - 液体 - 固定 - 液体布局

html - angularjs 路由 - 跳转到路由链接上的特定页面部分

javascript - html js onclick切换不起作用

javascript - 将自定义 CSS 添加到 Vuetify 组件时出现问题

jquery - ListView 选择的元素背景颜色没有改变

javascript - 通过仅使用 html 和/或 javascript 的表单发送电子邮件?

html - 隐藏有边框半径的溢出在图像上显示一个奇怪的灰色边框