关于CSS居中(水平/垂直)问题总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lsy1072/article/details/79932112

由于前端代码写的少,很多细节上的东西处理的不够熟练。最近关于CSS居中问题折腾了一下,特此总结。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>DIV内容居中</title>
</head>

<style>

  .div{

    width: 300px;
    height: 300px;
    background-color:yellowgreen;
    font-size: 20px;
  }
  .part{
    width:200px;
    height:100px;
    background-color:red;
    text-align:center;//水平居中
  }
  .part2{
    width:200px;
    height:100px;
    background-color:green;
    line-height:100px;//设置line-height,达到垂直居中的效果
  }
  .part3{
    width:200px;
    height:100px;
    background-color:blue;
    text-align:center;
    line-height:100px;
  }

</style>

<body>
<div class="div">
  <div class="part">水平居中</div>
  <div class="part2">垂直居中</div>
  <div class="part3">水平垂直居中</div>
</div>
</body>
</html>

效果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/lsy1072/article/details/79932112