【CSS】fixed绝对定位水平垂直居中不随滚动条而移动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .fixed{
            width: 300px;
            height: 150px;
            background-color: red;
            position: fixed;
            top: 50%;
            margin-top: -75px;
            left: 0; right: 0;
            margin:0 auto;
        }
        li{
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="fixed">fixed绝对居中</div>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>8</li>
        <li>8</li>
        <li>8</li>
        <li>8</li>
        <li>8</li>
        <li>8</li>
        <li>8</li>
        <li>8</li>
        <li>8</li>
        <li>8</li>
        <li>8</li>
    </ul>
</body>
</html>

动图演示:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013451157/article/details/80188887