CSS水平居中+垂直居中+水平/垂直居中的方法总结

男娘i 2023-10-09 15:03 121阅读 0赞

转载一位大佬的

CSS水平居中+垂直居中+水平/垂直居中的方法总结_一只前端小菜鸟~的博客-CSDN博客_垂直居中的方法

分享一篇我的博客 (详细记载工作中遇到的问题)

前端工作四个月经验总结以及技术分享__揽的博客-CSDN博客

水平居中

  • 行内元素

首先看它的父元素是不是块级元素,如果是,则直接给父元素设置 text-align: center;

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. text-align: center;
  7. }
  8. </style>
  9. <div id="father">
  10. <span id="son">我是行内元素</span>
  11. </div>

如果不是,则先将其父元素设置为块级元素,再给父元素设置 text-align: center;

  1. <style>
  2. #father {
  3. display: block;
  4. width: 500px;
  5. height: 300px;
  6. background-color: skyblue;
  7. text-align: center;
  8. }
  9. </style>
  10. <span id="father">
  11. <span id="son">我是行内元素</span>
  12. </span>

效果:

a9e2ba2f7c014ea3afaaecf4432f5976.png

  • 块级元素

方案一:(分宽度定不定两种情况)

定宽度:需要谁居中,给其设置 margin: 0 auto; (作用:使盒子自己居中)

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. }
  7. #son {
  8. width: 100px;
  9. height: 100px;
  10. background-color: green;
  11. margin: 0 auto;
  12. }
  13. </style>
  14. <div id="father">
  15. <div id="son">我是块级元素</div>
  16. </div>

效果:

8edac1374be3497c9c0f12e8ba5260ac.png 不定宽度:默认子元素的宽度和父元素一样,这时需要设置子元素为display: inline-block;display: inline;即将其转换成行内块级/行内元素,给父元素设置 text-align: center;

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. text-align: center;
  7. }
  8. #son {
  9. background-color: green;
  10. display: inline;
  11. }
  12. </style>
  13. <div id="father">
  14. <div id="son">我是块级元素</div>
  15. </div>

效果:(将#son转换成行内元素,内容的高度撑起了#son的高度,设置高度无用)

a1aabf1e3db041d5a0fcee65320cb043.png

方案二:使用定位属性

首先设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的left:50%,即让子元素的左上角水平居中;

定宽度:设置绝对子元素的 margin-left: -元素宽度的一半px; 或者设置transform: translateX(-50%);

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. position: relative;
  7. }
  8. #son {
  9. width: 100px;
  10. height: 100px;
  11. background-color: green;
  12. position: absolute;
  13. left: 50%;
  14. margin-left: -50px;
  15. }
  16. </style>
  17. <div id="father">
  18. <div id="son">我是块级元素</div>
  19. </div>

不定宽度:利用css3新增属性transform: translateX(-50%);

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. position: relative;
  7. }
  8. #son {
  9. height: 100px;
  10. background-color: green;
  11. position: absolute;
  12. left: 50%;
  13. transform: translateX(-50%);
  14. }
  15. </style>
  16. <div id="father">
  17. <div id="son">我是块级元素</div>
  18. </div>

效果:2638fc6a161d4d5c9fe0fd5f71daa90c.png

方案三:使用flexbox布局实现(宽度定不定都可以)

使用flexbox布局,只需要给待处理的块状元素的父元素添加属性 display: flex; justify-content: center;

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. display: flex;
  7. justify-content: center;
  8. }
  9. #son {
  10. width: 100px;
  11. height: 100px;
  12. background-color: green;
  13. }
  14. </style>
  15. <div id="father">
  16. <div id="son">我是块级元素</div>
  17. </div>

效果:

196799062c8b48ec9e02e4ceda5c6b10.png

垂直居中

  • 单行的行内元素

只需要设置单行行内元素的”行高等于盒子的高”即可;

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. }
  7. #son {
  8. background-color: green;
  9. height: 300px;
  10. }
  11. </style>
  12. <div id="father">
  13. <span id="son">我是单行的行内元素</span>
  14. </div>

效果:

3cd48c9e27ed4745a9ad922e64f67579.png

  • 多行的行内元素

使用给父元素设置display:table-cell;****vertical-align: middle;属即可;

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. display: table-cell;
  7. vertical-align:middle;
  8. }
  9. #son {
  10. background-color: green;
  11. }
  12. </style>
  13. <div id="father">
  14. <span id="son">我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素</span>
  15. </div>

258ce17335fa491b8b7f6728809ee2d4.png

块级元素 方案一:使用定位 首先设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的top: 50%,即让子元素的左上角垂直居中;

定高度:设置绝对子元素的 margin-top: -元素高度的一半px; 或者设置transform: translateY(-50%);

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. position: relative;
  7. }
  8. #son {
  9. height: 100px;
  10. background-color: green;
  11. position: absolute;
  12. top: 50%;
  13. margin-top: -50px;
  14. }
  15. </style>
  16. <div id="father">
  17. <div id="son">我是块级元素</div>
  18. </div>

不定高度:利用css3新增属性transform: translateY(-50%);

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. position: relative;
  7. }
  8. #son {
  9. width: 100px;
  10. background-color: green;
  11. position: absolute;
  12. left: 50%;
  13. transform: translateY(-50%);
  14. }
  15. </style>
  16. <div id="father">
  17. <div id="son">我是块级元素</div>
  18. </div>

效果:

cfc51ac38e44441688645d4dda776ca0.png

方案二:使用flexbox布局实现(高度定不定都可以)

使用flexbox布局,只需要给待处理的块状元素的父元素添加属性 display: flex; align-items: center;

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. display: flex;
  7. align-items: center;
  8. }
  9. #son {
  10. width: 100px;
  11. height: 100px;
  12. background-color: green;
  13. }
  14. </style>
  15. <div id="father">
  16. <div id="son">我是块级元素</div>
  17. </div>

效果:

b0772c5a280f4f998979435aa7a8c603.png

水平垂直居中

  • 已知高度和宽度的元素

方案一:设置父元素为相对定位,给子元素设置绝对定位,top: 0; right: 0; bottom: 0; left: 0; margin: auto;

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. position: relative;
  7. }
  8. #son {
  9. width: 100px;
  10. height: 100px;
  11. background-color: green;
  12. position: absolute;
  13. top: 0;
  14. right: 0;
  15. bottom: 0;
  16. left: 0;
  17. margin: auto;
  18. }
  19. </style>
  20. <div id="father">
  21. <div id="son">我是块级元素</div>
  22. </div>

效果:

244da79c79154db7b8d7db50974114ac.png

方案二:设置父元素为相对定位,给子元素设置绝对定位,left: 50%; top: 50%; margin-left: -**-元素宽度的一半px; margin-top: --元素高度的一半px**;

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. position: relative;
  7. }
  8. #son {
  9. width: 100px;
  10. height: 100px;
  11. background-color: green;
  12. position: absolute;
  13. left: 50%;
  14. top: 50%;
  15. margin-left: -50px;
  16. margin-top: -50px;
  17. }
  18. </style>
  19. <div id="father">
  20. <div id="son">我是块级元素</div>
  21. </div>

效果:

2611c7aa24b04c6cae2c68b7d459cb0e.png

  • 未知高度和宽度的元素

方案一:使用定位属性

设置父元素为相对定位,给子元素设置绝对定位,left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%);

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. position: relative;
  7. }
  8. #son {
  9. background-color: green;
  10. position: absolute;
  11. left: 50%;
  12. top: 50%;
  13. transform: translateX(-50%) translateY(-50%);
  14. }
  15. </style>
  16. <div id="father">
  17. <div id="son">我是块级元素</div>
  18. </div>

效果:

f484ff2b46894ffc9590831281496071.png

方案二:使用flex布局实现

设置父元素为flex定位,justify-content: center; align-items: center;

  1. <style>
  2. #father {
  3. width: 500px;
  4. height: 300px;
  5. background-color: skyblue;
  6. display: flex;
  7. justify-content: center;
  8. align-items: center;
  9. }
  10. #son {
  11. background-color: green;
  12. }
  13. </style>
  14. <div id="father">
  15. <div id="son">我是块级元素</div>
  16. </div>

效果:

2e7e34130b6f47e78fb615b01b46bb6d.png

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CSS水平垂直居中

            在实际前端开发中,我们会遇到让一些元素居中的情况,如水平居中、垂直居中或者水平垂直居中等等。CSS的表示方法有很多,现在小编在这里总结一下,以便开发的时候可以快