How to center the box horizontally and vertically in HTML

for example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
        }
        ul{
            width: 400px;
            height: 300px;
            background-color: #ccc;
        }
        ul>li{
            width: 100px;
            height: 100px;
            background-color: red;
            list-style: none;
        }
    </style>
</head>
<body>
    <ul>
        <li></li>
    </ul>
</body>
</html>

results as shown in the figure below,

method:

makes ul relatively positioned and li absolutely positioned, in two ways

        ul{
            position: relative; 
        }
        ul>li{
            position: absolute;
            /*1*/
            margin: auto;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0; 
            /*2、 或者直接使用如下top和left */
            top:100px;/*父元素高度减子元素的高度的一半*/
            left: 150px;/*父元素宽度减子元素的宽度的一半*/
        }

results as shown below

Set ul display:flex

        ul{
            display: flex;
            justify-content: center;
            align-items: center;
        }

Read More: