html - 居中居中文本的增长效果

标签 html css centering

由于 transform: translateZ(0); h1 的位置改变了。如果我删除它,效果将无法正常工作。

我怎样才能更改下面的代码并使 h1 居中,并且在文本中心出现增长效果?

我从 here 中发现了增长效应但是代码有相互冲突的转换

h1{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

}
.hvr-grow {
    transform-origin:center center;
    display: inline-block;
    vertical-align: middle;
    /*transform: translateZ(0);*/
    box-shadow: 0 0 1px rgba(0, 0, 0, 0);
    backface-visibility: hidden;
    -moz-osx-font-smoothing: grayscale;
    transition-duration: 0.3s;
    transition-property: transform;
}

.hvr-grow:hover,
.hvr-grow:focus,
.hvr-grow:active {
    transform: scale(1.8);
}
<div style="position:relative">
<h1 class="hvr-grow">Hello I'm centered</h1>
</div>

最佳答案

那是因为你对同一个元素应用了两种不同的 CSS 变换:首先,你应用了 translate(-50%, -50%),然后你应用了 scale( 1.8)。 CSS 转换的烦人之处在于它们不堆叠,所以基本上您的缩放会覆盖转换...换句话说,使用您的原始代码,以下样式将应用于悬停状态:

transform: translate(-50%, -50%);  /* This will be overwritten! */
transform: scale(1.8);             /* Overwrites all preceding transforms */

你可以做的只是将它们组合在 :hover 选择器中,即

transform: translate(-50%, -50%) scale(1.8);

p/s: 附带说明一下,由于您正在使用 CSS 变换技巧来垂直居中您的元素,因此您不需要使用 display: inline-blockvertical-对齐:中间技巧。

这是一个工作示例:

h1{
    position: absolute;
    top: 50%;
    left: 50%;
}
.hvr-grow {
    box-shadow: 0 0 1px rgba(0, 0, 0, 0);
    backface-visibility: hidden;
    -moz-osx-font-smoothing: grayscale;
    transition-duration: 0.3s;
    transition-property: transform;
    transform: translate(-50%, -50%);
}

.hvr-grow:hover,
.hvr-grow:focus,
.hvr-grow:active {
    transform: translate(-50%, -50%) scale(1.8);
}
<div style="position:relative">
<h1 class="hvr-grow">Hello I'm centered</h1>
</div>

关于html - 居中居中文本的增长效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44907183/

相关文章:

css - 确定 iphone 与 ipad CSS 渲染问题的技术?

javascript - 单击功能后在 jquery 中禁用悬停效果时出错

css - div 不会以 normalize.css 为中心

javascript - jQuery Waterfall 元素不按其 HTML 布局顺序排列

javascript - 通过html传递对象变量

html - 灵活改变 child 的 child 的位置?

javascript - 适用于 Firefox 和 Chrome 的刷新率按钮 PHP

html - 当父级将所有子元素居中对齐时,将一个子元素左对齐

html - 无法在 html/css 中居中

html - 如何使元素水平居中?