老师,固定定位居中,这样写正确吗

来源:2-18 编程练习

dy12369

2022-03-11 20:27:03

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>过渡与动画</title>

    <style>

        *{

            margin: 0;

            padding: 0;

        }

        .box{

            font-family: Arial;

            font-size: 72px;

            font-weight: bold;

            position: fixed;

            bottom: 0;

            margin: auto;

            width: 30px;

            height: 30px;

            left: 50%;

            margin-left: -15px;

            cursor: pointer;

            transform: rotate(90deg);

            animation: r1 1s linear 0s alternate infinite;

        }

        @keyframes r1{

            from{

                bottom: 30px;

            }

            to{

                bottom: 0;

            }

        }

    </style>

</head>

<body>

    <div class="box">></div>

</body>

</html>


写回答

1回答

好帮手慕久久

2022-03-12

同学你好,居中代码是正确的。由于父元素比字体小,字体超出了,所以字体居中的实际效果有误差:

https://img.mukewang.com/climg/622bfb2a099a2b0903700106.jpg

建议增大父元素的宽高,并让字体在父元素中水平垂直居中:

https://img.mukewang.com/climg/622bfb580941eab307640573.jpg

祝学习愉快!

0

0 学习 · 15276 问题

查看课程