我正在尝试让文本仅出现在页面中央,但我不希望它出现在顶部
我已经试过了
CSS......
.Lum{
font-family: Edwardian Script ITC;
position: fixed;
top: 50%;
left: 50%;
font-size: 50px;
text-align: center;
transform: translate(-50%, -50%);
margin: 0;
letter-spacing: 1px;}
@keyframes pulse {
0% {transform: scale(1);}
50% {transform: scale(1.1);}
100% {transform: scale(1);}
}
.pulse {
animation-name: pulse;
animation-duration: 2s;
}
HTML..............
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
<title>Lumiere</title>
</head>
<body class="pulse">
<h3 class="Lum">Lumiere</h3>
</body>
</html>
我希望文本始终在页面中间,而不是顶部
最佳答案
在当前方法下应如何修复动画:
.Lum{
font-family: Edwardian Script ITC;
position: fixed;
top: 50%;
left: 50%;
font-size: 50px;
text-align: center;
transform: translate(-50%, -50%);
margin: 0;
letter-spacing: 1px;}
@keyframes pulse {
/* Since you're using translate, you shouldn't overwrite it with only scale, just combine them */
0% {transform: translate(-50%, -50%) scale(1);}
50% {transform: translate(-50%, -50%) scale(1.1);}
100% {transform: translate(-50%, -50%) scale(1);}
}
.pulse {
animation-name: pulse;
animation-duration: 2s;
/* If you want the animation keeps going */
animation-iteration-count: infinite;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
<title>Lumiere</title>
</head>
<body>
<!-- You need to put pulse on the element, not the whole page -->
<h3 class="Lum pulse">Lumiere</h3>
</body>
</html>
由于您将
transform
用于动画,因此translate
居中的方法可能不是很理想。也许改为尝试这种方法:body {
/* Make the container flexbox */
display: flex;
/* Center its children */
justify-content: center;
align-items: center;
/* set the width and height as big as the window */
width: 100vw;
height: 100vh;
padding: 0;
margin: 0;
}
.Lum{
font-family: Edwardian Script ITC;
font-size: 50px;
text-align: center;
margin: 0;
letter-spacing: 1px;
}
@keyframes pulse {
0% {transform: scale(1);}
50% {transform: scale(1.1);}
100% {transform: scale(1);}
}
.pulse {
animation-name: pulse;
animation-duration: 2s;
/* If you want the animation keeps going */
animation-iteration-count: infinite;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
<title>Lumiere</title>
</head>
<body>
<h3 class="Lum pulse">Lumiere</h3>
</body>
</html>