padding absolute

  1. <style>
  2. .box{
  3. position: relative;
  4. width: 100px;
  5. height: 100px;
  6. border: 1px solid green;
  7. }
  8. .big-box{
  9. width: 400px;
  10. height: 200px;
  11. border: 1px solid red;
  12. padding: 20px;
  13. box-sizing: border-box;
  14. }
  15. .box2{
  16. /*float: left;*/
  17. position: absolute;
  18. left: 0;
  19. }
  20. .box3{
  21. float: left;
  22. background-color: #8d7afb;
  23. }
  24. </style>
  25. <div class="box big-box">
  26. <div class="box box2"></div>
  27. <!-- <div class="box box3"></div>-->
  28. </div>

image.png
absolute left不会计算padding
当就一个物体向左移动时,float、绝对定位都可以,但多个物体对齐时,用float结合父元素的padding,或者新建子元素div,配合它的margin、padding

绝对定位实现float效果

  1. <style>
  2. .box{
  3. position: relative;
  4. width: 100px;
  5. height: 100px;
  6. border: 1px solid green;
  7. }
  8. .big-box{
  9. width: 400px;
  10. height: 200px;
  11. border: 1px solid red;
  12. padding: 20px;
  13. box-sizing: border-box;
  14. }
  15. .box2{
  16. /*float: left;*/
  17. position: absolute;
  18. /*left: 0;*/
  19. right: 20px;
  20. background-color: red;
  21. }
  22. .box3{
  23. float: left;
  24. background-color: #8d7afb;
  25. }
  26. </style>
  27. <div class="box big-box">
  28. <div class="box box2"></div>
  29. <div class="box box3"></div>
  30. </div>
  1. <style>
  2. .box{
  3. position: relative;
  4. width: 100px;
  5. height: 100px;
  6. border: 1px solid green;
  7. }
  8. .big-box{
  9. width: 400px;
  10. height: 200px;
  11. border: 1px solid red;
  12. padding: 20px;
  13. box-sizing: border-box;
  14. }
  15. .box2{
  16. /*float: left;*/
  17. position: absolute;
  18. /*left: 0;*/
  19. right: 20px;
  20. background-color: red;
  21. }
  22. .box3{
  23. float: left;
  24. background-color: #8d7afb;
  25. }
  26. .box4{
  27. float: right;
  28. border: 5px solid green;
  29. }
  30. </style>
  31. <div class="box big-box">
  32. <div class="box box2"></div>
  33. <div class="box box3"></div>
  34. <div class="box box4"></div>
  35. </div>

image.png