2-14-编程练习的水平居中问题

来源:2-14 编程练习

我学习太差被关起来了

2019-06-09 17:15:24

我使用以下CSS代码也实现水平居中效果,这样有什么问题吗?

left: 0;
right: 0;
margin: auto;

完整代码如下;

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
 *
 {
                margin: 0;
 padding: 0;
 }

            .out
 {
                width: 100%;
 height: 2000px;
 background-color: #ABCDEF;
 }

            .test
 {
                width: 800px;
 height: 100px;
 background: red;

 position: fixed;
 top: 0;
 left: 0;
 right: 0;
 margin: auto;
 text-align: center;
 font: 40px "微软雅黑";
 color: #FFF;
 }
        </style>
    </head>

    <body>
        <div class="out">
            <div class="test">顶部文字</div>
        </div>
    </body>

</html>


写回答

1回答

好帮手慕夭夭

2019-06-09

你好同学,实现的可以哦。这个方法属于“拔河效应”。盒子设置left:0 ;想要让它显示在左边 ,设置了right:0,又想要让它又显示在右边 。两边都在拉它 ,这个时候出现margin:auto; 我直接在中间好了。而top:0和bottom:0是为了设置垂直居中的。所以代码中本身就不用设置top和bottom哦

祝学习愉快,望采纳。

0

0 学习 · 40143 问题

查看课程