经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » XHTML » 查看文章
详解html的几种水平垂直居中的方式(基础)_HTML/Xhtml
来源:jb51  时间:2019/8/21 9:18:22  对本文有异议

前言

我们在编写马过程中,想必大家对水平垂直居中的方法了解并不多。所以我给大家总结式的列出几种常用的水平垂直居中的方法。

第一种方法

  1. <!--html盒子代码-->
  2. <!--水平垂直居中-->
  3.  
  4. <div class="Centered1">
  5. <p>d第一种</p>
  6. </div>
  7.  
  8. <!-css样式部分-->
  9. .Centered1{
  10. background-color: #800070;
  11. width: 100%;
  12. height:500px;
  13. position: relative;
  14. }
  15. .Centered1 p{
  16. width: 200px;
  17. height: 200px;
  18. background-color: deeppink;
  19. line-height: 200px;
  20. text-align: center;
  21. position: absolute;
  22. left: 0;
  23. bottom: 0;
  24. right:0;
  25. top: 0;
  26. margin: auto;
  27. }
  28.  

第二种方法

  1. <!--html盒子代码-->
  2. <!--水平垂直居中-->
  3.  
  4. <div class="Centered2">
  5. <p>d第二种</p>
  6. </div>
  7.  
  8. <!-css样式部分-->
  9. /*第二种方法水平垂直居中*/
  10. .Centered2{
  11. background-color: #ef8518;
  12. width: 100%;
  13. height: 500px;
  14. position: relative;
  15. }
  16. .Centered2 p {
  17. position: absolute;
  18. width: 200px;
  19. height: 200px;
  20. background-color:red;
  21. line-height: 200px;
  22. text-align: center;
  23. left: 50%;
  24. top:50%;
  25. margin-left:-100px;
  26. margin-top: -100px;
  27. }
  28.  

第三种方法

  1. <!--html盒子代码-->
  2. <!--水平垂直居中-->
  3.  
  4. <div class="Centered3">
  5. <p>d第三种</p>
  6. </div>
  7.  
  8. <!-css样式部分-->
  9. /*第三种方法水平垂直居中*/
  10. .Centered3{
  11. background-color: dimgrey;
  12. width: 100%;
  13. height: 500px;
  14. position: relative;
  15. }
  16. .Centered3 p {
  17. position: absolute;
  18. width: 200px;
  19. height: 200px;
  20. background-color:darkorange;
  21. line-height: 200px;
  22. text-align: center;
  23. left: 50%;
  24. top: 50%;
  25. transform:translate(-50%,-50%);
  26. }
  27.  

第四种方法

  1. <!--html盒子代码-->
  2. <!--水平垂直居中-->
  3.  
  4. <div class="Centered4">
  5. <p>d第四种</p>
  6. </div>
  7.  
  8. <!-css样式部分-->
  9. /*第四种方法水平垂直居中,老版本flex布局*/
  10. .Centered4{
  11. background-color: #FF4444;
  12. width: 100%;
  13. height: 500px;
  14. display: -webkit-box;
  15. -webkit-box-pack:center;
  16. -webkit-box-align: center;
  17. }
  18. .Centered4 p {
  19. width: 200px;
  20. height: 200px;
  21. background-color:cadetblue;
  22. line-height: 200px;
  23. text-align: center;
  24. }
  25.  

第五种方法

  1. <!--html盒子代码-->
  2. <!--水平垂直居中-->
  3.  
  4. <div class="Centered5">
  5. <p>d第五种</p>
  6. </div>
  7.  
  8. <!-css样式部分-->
  9. /*第五种方法水平垂直居中,新版本的flex水平垂直居中*/
  10. .Centered5{
  11. background-color: darkslateblue;
  12. width: 100%;
  13. height: 500px;
  14. display: flex;
  15. justify-content:center;
  16. align-items: center;
  17. }
  18. .Centered5 p {
  19. width: 200px;
  20. height: 200px;
  21. background-color:fuchsia;
  22. line-height: 200px;
  23. text-align: center;
  24. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号