当前位置:   article > 正文

最全CSS垂直水平居中的方法(十二种)_css文字水平垂直居中怎么设置

css文字水平垂直居中怎么设置

 1. 第一种方案 子元素高度可以设定也可以不设定

  1. .app{
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. display: flex;
  6. align-items: center;
  7. justify-content: center;
  8. }
  9. .app>div{
  10. width: 100px;
  11. height: 100px;
  12. font-size: 20px;
  13. background: blueviolet;
  14. }


2.第二种方案  绝对定位+margin auto

  1. .app{
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. position: relative;
  6. }
  7. .app>div{
  8. width: 100px;
  9. height: 100px;
  10. font-size: 20px;
  11. background: blueviolet;
  12. position: absolute;
  13. top: 0;
  14. left: 0;
  15. right: 0;
  16. bottom:0;
  17. margin: auto;
  18. }


3.第三种方案 绝对定位+  translate 子元素高度可以设定也可以不设定

  1. .app{
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. position: relative;
  6. }
  7. .app>div{
  8. width: 100px;
  9. height: 100px;
  10. font-size: 20px;
  11. background: blueviolet;
  12. position: absolute;
  13. top: 50%;
  14. left: 50%;
  15. transform: translate(-50%,-50%);
  16. }

4.第四种方案   绝对定位+计算属性(CSS3  calc)感谢css3带来了计算属性,既然top的百分比是基于元素的左上角,那么在减去宽度的一半就好了,代码如下

  1. .app {
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. position: relative;
  6. }
  7. .app>div{
  8. position: absolute;
  9. width: 100px;
  10. height: 100px;
  11. top: calc(50% - 50px);
  12. left: calc(50% - 50px);
  13. background-color: aliceblue;
  14. }

5.第五种方案  grid网格布局  考虑兼容性问题,不推荐

  1. .app {
  2. display: grid;
  3. width: 500px;
  4. height: 500px;
  5. background: greenyellow;
  6. }
  7. .app>div {
  8. width: 100px;
  9. height: 100px;
  10. align-self: center;
  11. justify-self: center;
  12. background-color: aliceblue;
  13. }

 6.第六种方案 inline 高度就是文字加line-height的高度 局限性在于必须里边的元素是inline否则不生效

  1. .app{
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. line-height: 500px;
  6. text-align: center;
  7. }
  8. .app>div{
  9. font-size: 20px;
  10. background: blueviolet;
  11. display: inline;
  12. }

7.第七种方案 混合 横向利用margin auto,垂直还是利用定位和平移 也不用管里边元素的高度

  1. .app{
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. }
  6. .app>div{
  7. width: 100px;
  8. font-size: 20px;
  9. background: blueviolet;
  10. position: relative;
  11. top: 50%;
  12. transform: translateY(-50%);
  13. margin-left: auto;
  14. margin-right:auto ;
  15. }

8.第八中方案 table  原来盒子的背景颜色被子元素覆盖了,这个方法也是解决多行文字垂直居中的方法

  1. .app{
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. display: table;
  6. text-align: center;
  7. }
  8. .app>div{
  9. width: 100px;
  10. height: 100px;
  11. font-size: 20px;
  12. background: blueviolet;
  13. display: table-cell;
  14. vertical-align: middle;
  15. }


9.第九种方案 单行文本好居中 就是让文本的父元素的line-height和高度相同

  1. .app{
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. text-align: center;
  6. line-height: 500px;
  7. }
  8. .app>div{
  9. width: 100px;
  10. height: 100px;
  11. font-size: 20px;
  12. background: blueviolet;
  13. display: inline;
  14. }

10.第十种方案  绝对定位+margin   通过计算宽高,一旦内部元素改变了高度或者宽度将失效

  1. .app {
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. position: relative;
  6. }
  7. .app>div {
  8. position: absolute;;
  9. width: 100px;
  10. height: 100px;
  11. top: 50%;
  12. left: 50%;
  13. margin-left: -50px;
  14. margin-top: -50px;
  15. }
  16. \

 11. 第十一种方案 最不推荐的方案  通过计算padding 一旦内部元素改变了高度或者宽度,将失效

  1. .app{
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. box-sizing: border-box;
  6. padding-left: 200px;
  7. padding-top: 200px;
  8. }
  9. .app>div{
  10. width: 100px;
  11. height: 100px;
  12. font-size: 20px;
  13. background: blueviolet;
  14. }

12.第十二种方案 多行文本垂直居中 这里就不水平居中了
除了歌词水平居中之外,其他什么的都不好看。
两种方法:一:

  1. .app{
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. display: table;
  6. }
  7. .app>div{
  8. width: 100px;
  9. height: 100px;
  10. font-size: 20px;
  11. background: blueviolet;
  12. display: table-cell;
  13. vertical-align: middle;
  14. }

二:

  1. .app{
  2. width: 500px;
  3. height: 500px;
  4. background: greenyellow;
  5. display: flex;
  6. align-items: center;
  7. justify-content: center;
  8. }
  9. .app>div{
  10. width: 100px;
  11. font-size: 20px;
  12. background: blueviolet;
  13. /* overflow: hidden;
  14. text-overflow: ellipsis;
  15. word-break: break-all;
  16. white-space: pre-line; */
  17. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/853426
推荐阅读
相关标签
  

闽ICP备14008679号