css-垂直居中(高考加油!)

1.通过左上的外边距设定:

box{
                position: absolute;
                top: 50%;
                background-color: gold;
                height: 40px;
                margin-top: -40px; /* negative half of the height */
                width: 100px;
                left: 50%;
                margin-left: -50px;
                text-align: center;
                line-height: 40px;
}

2.通过margin:auto设定:

.box{
                position: absolute;
                top: 0;
                bottom: 0;
                left: 0;
                right: 0;
                margin: auto;
                height: 40px;
                width: 20%;
                font-size: 16px;
 }

3.HTML:

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>高考加油</title>
		<style type="text/css">
			*{
                margin: 0;
                padding: 0;
            }
            
            /*.box{
                position: absolute;
                top: 50%;
                background-color: gold;
                height: 40px;
                margin-top: -40px; /* negative half of the height 
                width: 100px;
                left: 50%;
                margin-left: -50px;
                text-align: center;
                line-height: 40px;
            }*/
            .box{
                position: absolute;
                top: 0;
                bottom: 0;
                left: 0;
                right: 0;
                margin: auto;
                height: 40px;
                width: 20%;
                font-size: 16px;
            }

            
		</style>
	</head>
	<body>
        <div class="box">高考加油</div>
		
	</body>
</html>

4.实现的效果:

 

 

猜你喜欢

转载自blog.csdn.net/qq_42099097/article/details/107192898
今日推荐