1. 利用text-align实现行内元素居中

行内元素居中就很简单,只需要设置一下元素的text-align和行高

  1. .box {
  2. height: 300px;
  3. width: 300px;
  4. background-color: pink;
  5. margin: 100px auto;
  6. }
  7. .box p {
  8. text-align: center;
  9. line-height: 300px;
  10. }

2.利用定位再结合margin:auto实现

块元素的水平居中利用margin就可以实现,但垂直居中显然不太好实现。

  1. .box {
  2. height: 300px;
  3. width: 300px;
  4. background-color: pink;
  5. margin: 100px auto;
  6. position: relative;
  7. }
  8. .box div {
  9. width: 30px;
  10. height: 30px;
  11. background-color: red;
  12. position: absolute;
  13. top: 0;
  14. bottom: 0;
  15. left: 0;
  16. right: 0;
  17. margin: auto;
  18. }

3.已知宽高利用负margin和定位配合

  1. .box {
  2. height: 300px;
  3. width: 300px;
  4. background-color: pink;
  5. margin: 100px auto;
  6. position: relative;
  7. }
  8. .box div {
  9. width: 30px;
  10. height: 30px;
  11. background-color: red;
  12. position: absolute;
  13. left: 50%;
  14. top: 50%;
  15. margin-left: -15px;
  16. margin-top: -15px;
  17. }

4. 定位通过计算元素宽高实现居中

  1. <!DOCTYPE html>
  2. <head>
  3. <title>absolute+margin计算元素宽高判断居中</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <style>
  6. .content {
  7. width: 300px;
  8. height: 300px;
  9. border: 1px solid #109D71;
  10. position: relative;
  11. }
  12. .box {
  13. position: absolute;
  14. width: 100px;
  15. height: 100px;
  16. /* top: 50%;
  17. left: 50%;
  18. margin-top: -50px;
  19. margin-left: -50px; */
  20. top: calc(50% - 50px);
  21. left: calc(50% - 50px);
  22. background: #109D71;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="content">
  28. <div class="box">
  29. </div>
  30. </div>
  31. </body>
  32. </html>

5.通过translate将元素移动自身的50%

translate(-50%,-50%) 属性:向上(x轴)和左(y轴),移动自身长宽的 50%,使其居于中心位置。
top: 50%;left: 50%;:是以窗口左上角为原点,需要减掉自身宽高的一半,才能居中。
与使用margin实现居中不同的是,margin必须知道自身的宽高,而translate可以在不知道宽高的情况下进行居中,tranlate函数中的百分比是相对于自身宽高的百分比。

  1. <!DOCTYPE html>
  2. <head>
  3. <title>absolute+translate</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <style>
  6. .content {
  7. width: 300px;
  8. height: 300px;
  9. border: 1px solid #109D71;
  10. position: relative;
  11. }
  12. .box {
  13. position: absolute;
  14. width: 100px;
  15. height: 100px;
  16. top: 50%;
  17. left: 50%;
  18. transform: translate(-50%, -50%);
  19. background: #109D71;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="content">
  25. <div class="box">
  26. </div>
  27. </div>
  28. </body>
  29. </html>

6.flex 弹性布局实现

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>flex布局</title>
  6. <style>
  7. .content {
  8. width: 300px;
  9. height: 300px;
  10. margin: 50px;
  11. border: 1px solid #109D71;
  12. display: flex;
  13. align-items: center;
  14. justify-content: center;
  15. }
  16. .box{
  17. width: 150px;
  18. height: 150px;
  19. background-color: #109D71;
  20. text-align: center;
  21. margin: 0 auto;
  22. display: flex;
  23. align-items: center;
  24. justify-content: center;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="content">
  30. <div class="box">
  31. 水平垂直居中
  32. </div>
  33. </div>
  34. </body>
  35. </html>

7.grid网格布局实现

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>flex布局</title>
  6. <style>
  7. .content {
  8. width: 300px;
  9. height: 300px;
  10. margin: 50px;
  11. border: 1px solid #109D71;
  12. display: grid;
  13. place-items: center;
  14. place-content: center;
  15. }
  16. .box {
  17. width: 150px;
  18. height: 150px;
  19. background-color: #109D71;
  20. text-align: center;
  21. margin: 0 auto;
  22. display: grid;
  23. place-items: center;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="content">
  29. <div class="box">
  30. 水平垂直居中
  31. </div>
  32. </div>
  33. </body>
  34. </html>

display:table-cell属性来实现" class="reference-link">8.利用display:table-cell属性来实现

display:table-cell;结合vertical-align: middle;使用实现垂直居中,margin:0 atuo;可以实现子元素的水平居中。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>table-cell居中</title>
  6. <style type="text/css">
  7. .content{
  8. border: 1px solid blue;
  9. display: table;
  10. margin: 50px auto;
  11. }
  12. .table{
  13. height: 300px;
  14. width: 300px;
  15. display: table-cell;
  16. vertical-align: middle;
  17. border: 1px solid red;
  18. }
  19. .box{
  20. height: 150px;
  21. width: 150px;
  22. background: #109D71;
  23. margin: 0 auto;
  24. display: table;
  25. }
  26. .word{
  27. display: table-cell;
  28. vertical-align: middle;
  29. text-align: center;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="content">
  35. <div class="table">
  36. <div class="box">
  37. <div class="word">
  38. 垂直水平居中
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </body>
  44. </html>

更多相关文章

  1. 0323作业-css基础知识3
  2. Playwright自动化测试工具之元素定位实战
  3. 0322作业-CSS基础1
  4. 堆实战(动态数据流求top k大元素,动态数据流求中位数)
  5. 0322作业(选择器的优先级,id,class,tag); (前端组件样式模块化的
  6. 0319作业-常用html元素(下)
  7. 0319作业-常用html元素(上)
  8. python+opencv实现连通区域分离
  9. 前端vue面试题大全

随机推荐

  1. linux基本操作(2)
  2. Linux-HA 入门指南(引用)
  3. Centos 7 利用LVM实现动态扩容
  4. linux下vim配置以及一些常用的快捷键
  5. ubuntu中ssh-server的安装与开机启动
  6. linux修改文件所属用户和组
  7. Linux系统监控命令详解
  8. Shell脚本更改带变量的目录
  9. 初探Linux kernel之进程相关二
  10. 【Linux】CentOS7无法使用tab补全功能