- 水平居中

1.行内元素:text-align:center

  1. <p class="cen">居中</p> //html代码
  2. .cen{ //css代码
  3. text-align:center;
  4. border:1px solid red;
  5. }

CSS水平与居中 - 图1

2.块级元素

让块级元素居中的方法就是设置 margin-leftmargin-rightauto
margin:0 auto

  1. <div class="first"></div> //html
  2. .first{ //css
  3. width:20px;
  4. height:20px;
  5. margin:0 auto;
  6. border:1px solid red;
  7. }

CSS水平与居中 - 图2

- 垂直居中

1.设置padding-top与padding-bottom相等

  1. <div class="first">first</div> //html
  2. .first{ //css
  3. width:200px;
  4. padding-top:20px;
  5. padding-bottom:20px;
  6. border:1px solid red;
  7. }

CSS水平与居中 - 图3
2.设置height与line-height相等

  1. <div class="first">first</div> //html
  2. .first{ //css
  3. width:100px;
  4. height: 50px;
  5. line-height: 50px;
  6. border:1px solid red;
  7. }

CSS水平与居中 - 图4

- 左右布局

通过float浮动实现,float:left与float:right

  1. <div class="first">leftcontent</div>
  2. <div class="second">rightcontent</div>
  3. .first{
  4. width:100px;
  5. height: 100px;
  6. float:left;
  7. border:1px solid red;
  8. }
  9. .second{
  10. width:100px;
  11. height: 100px;
  12. float:right;
  13. border:1px solid green;
  14. }

CSS水平与居中 - 图5

- 左中右布局

思路:使用div将左中左浮,右侧右浮

  1. <div class="out">
  2. <div class="clearfix inner" style="float:left">
  3. <div class="first">left</div>
  4. <div class="first">middle</div>
  5. </div>
  6. <div class="third">right</div>
  7. </div>
  8. .out{
  9. width:354px;
  10. height:100px;
  11. border:1px solid red;
  12. }
  13. .clearfix::after{
  14. content:'';
  15. display:block;
  16. clear:both;
  17. }
  18. .first{
  19. width:100px;
  20. height: 100px;
  21. float:left;
  22. border:1px solid red;
  23. }
  24. .second{
  25. width:100px;
  26. height: 100px;
  27. float:left;
  28. border:1px solid green;
  29. margin-left:25px;
  30. }
  31. .third{
  32. width:100px;
  33. height: 100px;
  34. float:right;
  35. border:1px solid green;
  36. }

CSS水平与居中 - 图6
其他技巧

google关键字:

  • css shadow generator 生成阴影
  • css gradient generator 渐变背景
  • webpage free psd 免费psd文件
  • dribbble 网站
  • css tricks shape css形状代码
  • iconfont.cn 图标库
  • wallhaven 高清壁纸