网页布局的居中问题,如何让文本居中,如何让div居中,怎样让div居中?居中都有哪几种情况?

关于网页设计制作(布局)过程中,如果让内容居中,分为以下几种情况:

1、文本,图片等内联元素的水平居中。text-align:center

2、定宽的块状元素在浏览器窗口或父容器中水平居中。margin:0 auto

3、不定宽块状元素水平居中

    方案一:给父元素添加display:table;将box转换为表格形式,将不定宽转换为定宽,然后设置左右margin为auto

   方案二:将子元素转换为内联块状元素,给父元素设置text-align:center;

4、不定宽高的元素在屏幕窗口水平垂直都居中

5、不定宽高元素在父元素中水平垂直都居中

-----------------------------以下为详细内容-------------------------------------------------

1、文本,图片等内联元素的水平居中。

       如果被设置元素为文本、图片等行内元素时,水平居中是通过给父元素设置text-align:center;来实现的。

    如:

  1. <style type="text/css">
  2. .imgBox{
  3. border: 1px solid black;
  4. text-align: center;
  5. }
  6. h1{
  7. border: 1px solid black;
  8. text-align: center;
  9. }
  10. .menu{
  11. border: 1px solid black;
  12. text-align: center;
  13. }
  14. form{
  15. border: 1px solid black;
  16. text-align: center;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <!-- 一张图片在父容器中水平居中 -->
  22. <div class="imgBox">
  23. <img src="img/pic2.png"/>
  24. <span style="background-color: red;">我是span </span>
  25. </div>
  26. <!-- 文本内容在父容器中水平居中 -->
  27. <h1>我是标题 </h1>
  28. <!-- 内联元素在父容器中水平居中 -->
  29. <div class="menu">
  30. <a href="#">新闻资讯 </a>
  31. <a href="#">课程介绍 </a>
  32. <a href="#">关于我们 </a>
  33. <a href="#">在线留言 </a>
  34. <a href="#">师资力量 </a>
  35. <a href="#">联系我们 </a>
  36. </div>
  37. <form action="">
  38. <input type="text"/>
  39. <input type="submit" value="百度一下"/>
  40. </form>
  41. </body>
  42. </html>

2、定宽的块状元素在浏览器窗口或父容器中水平居中。margin:0 auto

  1. <style type="text/css">
  2. body, div, p, ol, ul, li, dl, dt, dd, form, table, tr, td, h1, h2, h3, h4, h5, h6, hr, figure, input, textarea{
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .header{
  7. width: 100%;
  8. height: 100px;
  9. background: black;
  10. }
  11. .header_con{
  12. width: 1000px;
  13. height: 100px;
  14. background: red;
  15. margin: 0 auto;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <!-- 定宽块状元素在浏览器窗口或父容器中水平居中 -->
  21. <div class="header">
  22. <div class="header_con"> </div>
  23. </div>
  24. <!-- 总结:满足定宽和块状两个条件的元素是可以通过设置“左右margin”值为“auto”来实现居中的 -->
  25. </body>

3、不定宽块状元素水平居中

    方案一:给父元素添加display:table;将box转换为表格形式,将不定宽转换为定宽,然后设置左右margin为auto

  1. <style type="text/css">
  2. .box1{
  3. display: table;
  4. margin: 0 auto;
  5. }
  6. .box1 a{
  7. display: block;
  8. float: left;
  9. padding: 3px 5px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <!-- 网页中的分页效果,分页的页数不定,但是始终是居中的效果,如何实现? -->
  15. <!-- 方案一:给父元素添加display:table;将box转换为表格形式,将不定宽转换为定宽,然后设置左右margin为auto -->
  16. <!-- 当我们随意改变a的数量时,会发现他们始终是居中的 -->
  17. <div class="box box1">
  18. <a href="#">首页1 </a>
  19. <a href="#">上一页 </a>
  20. <a href="#">1 </a>
  21. <a href="#">2 </a>
  22. <a href="#">3 </a>
  23. <a href="#">4 </a>
  24. <a href="#">5 </a>
  25. <a href="#">下一页 </a>
  26. <a href="#">尾页 </a>
  27. </div>
  28. </body>
  29. </html>

方案二:

将子元素转换为内联块状元素,给父元素设置text-align:center;

  1. <style type="text/css">
  2. .box2{
  3. margin-top: 50px;
  4. text-align: center;
  5. }
  6. .box2 a{
  7. padding: 3px 5px;
  8. border: 1px solid blue;
  9. margin: 0 3px;
  10. text-decoration: none;
  11. color: #333;
  12. font-size: 12px;
  13. display: inline-block;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <!-- 网页中的分页效果,分页的页数不定,但是始终是居中的效果,如何实现? -->
  19. <!-- 方案二:将a转换为内联块状元素,给父元素设置text-align:center; -->
  20. <div class="box box2">
  21. <a href="#">首页 </a> <a href="#">上一页 </a> <a href="#">1 </a> <a href="#">2 </a> <a href="#">3 </a> <a href="#">4 </a> <a href="#">5 </a> <a href="#">下一页 </a> <a href="#">尾页 </a>
  22. </div>
  23. </body>
  24. </html>

4、不定宽高的元素在屏幕窗口水平垂直都居中

  1. <style type="text/css">
  2. /*body,div,p,ol,ul,li,dl,dt,dd,form,table,tr,td,h1,h2,h3,h4,h5,h6,hr,figure,input,textarea{
  3. margin: 0;
  4. padding: 0;
  5. }*/
  6. /*方案一:屏幕窗口的居中,使用固定定位,将四个方向的值全部设置为0,然后设置margin为auto,自动计算上下左右的margin值*/
  7. .imgs{
  8. position: fixed;
  9. left: 0;
  10. right: 0;
  11. top: 0;
  12. bottom: 0;
  13. margin:auto;
  14. }
  15. /*方案二*/
  16. /*使用弹性盒的方式实现,首先给html,body设置宽度为100%,然后给body设置主轴,交叉轴对齐方式为center*/
  17. /*html,body{
  18. height: 100%;
  19. }
  20. body{
  21. display: flex;
  22. justify-content: center;
  23. align-items: center;
  24. }*/
  25. /*方案三*/
  26. /*使用css3中transform变形中的translate来实现水平垂直都居中*/
  27. /*.imgs{
  28. position: fixed;
  29. left: 50%;
  30. top: 50%;
  31. transform: translate(-50%,-50%);
  32. }*/
  33. </style>
  34. </head>
  35. <body>
  36. <img class="imgs" src="img/pic2.png"/>
  37. </body>
  38. </html>


5、不定宽高元素在父元素中水平垂直都居中

  1. <style type="text/css">
  2. /*.father{
  3. width: 800px;
  4. height: 500px;
  5. background: gray;
  6. position: relative;
  7. }*/
  8. /*方案一:给父元素相对定位,给子元素绝对定位*/
  9. /*.imgs{
  10. position: absolute;
  11. left: 0;
  12. top: 0;
  13. right: 0;
  14. bottom: 0;
  15. margin: auto;
  16. }*/
  17. /*方案二:使用css3的translate实现*/
  18. /*.imgs{
  19. position: absolute;
  20. left: 50%;
  21. top: 50%;
  22. transform:translate(-50%,-50%);
  23. }*/
  24. /*方案三:非父元素设置table-cell,将父元素转换为表格单元格形式*/
  25. /*.father{
  26. width: 800px;
  27. height: 500px;
  28. background: gray;
  29. display: table-cell;
  30. text-align: center;
  31. vertical-align: middle;
  32. }
  33. .imgs{
  34. vertical-align: bottom;
  35. }*/
  36. /*方案四:给img添加一个参照物span,并设置高度为100%*/
  37. /*.father{
  38. width: 800px;
  39. height: 500px;
  40. background: gray;
  41. text-align: center;
  42. }
  43. .father span{
  44. display: inline-block;
  45. width: 0;
  46. height: 100%;
  47. vertical-align: middle;
  48. }
  49. .imgs{
  50. vertical-align: middle;
  51. }*/
  52. /*方案五:使用弹性盒的方式实现*/
  53. .father{
  54. width: 800px;
  55. height: 500px;
  56. background: gray;
  57. display: flex;
  58. justify-content: center;
  59. align-items: center;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <div class="father">
  65. <img class="imgs" src="img/pic2.png"/>
  66. <!-- <span></span> -->
  67. </div>
  68. </body>
  69. </html>

猜你喜欢

转载自blog.csdn.net/Lydia11111/article/details/80946118