你可以在这个jsfiddle http://jsfiddle.net/L9k5c/上看到我的"你好"文本位于我的红色圆圈之外,而不是居中于其中(这是问题1)。问题2是这个"你好"文字使" 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;
}
答案 0 :(得分:2)
您可以使用absolute position
和transform
轻松实现您的目标。
.ch-item p {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
margin: 0;
}
transform
不适用于IE 8及以下版本,但您已经在其他地方使用它了,所以我想这没关系。答案 1 :(得分:1)
<强> HERE IS THE UPDATED FIDDLE 强>
因为您在包装器中使用position:relative;
,因此您可以使用position:absolute;
内容来定位它
.ch-item p{
position:absolute;
top:35%;
left:40%;
}
也会在你的问题文本悬停时使opacity:0
成为问题。因为在你的例子中,你的问候文本在悬停后仍然可见
.ch-item:hover p{
opacity:0;
}
<强> second time modified fiddle 强>