css justify-content:元素之间的空间不在边缘

zbdgwd5y  于 2023-04-01  发布在  其他
关注(0)|答案(4)|浏览(175)

我有一个问题,项目没有使用完整的width。在codepen中,他们使用的空间比我原来的代码更好,但他们也没有使用完整的width,我真的不知道为什么。
space-betweenspace-around之间的差异极小,不应如此。(本代码中未测试)
http://codepen.io/notyetnamed/pen/KdMqJV
HTML:

  1. <div class="wrapper">
  2. <h2>Lorem Ipsum</h2>
  3. <ul class="cf">
  4. <li>
  5. <a href="#">
  6. <img src="http://lorempixel.com/180/180/" alt="">
  7. <h3>lorem</h3>
  8. </a>
  9. </li>
  10. <li>
  11. <a href="#">
  12. <img src="http://lorempixel.com/180/180/" alt="">
  13. <h3>lorem</h3>
  14. </a>
  15. </li>
  16. <li>
  17. <a href="#">
  18. <img src="http://lorempixel.com/180/180/" alt="">
  19. <h3>lorem</h3>
  20. </a>
  21. </li>
  22. </ul>
  23. </div>
  24. <div class="wrapper">
  25. <h2>Lorem Ipsum</h2>
  26. <ul class="cf">
  27. <li>
  28. <a href="#">
  29. <img src="http://lorempixel.com/180/180/" alt="">
  30. <h3>lorem</h3>
  31. </a>
  32. </li>
  33. <li>
  34. <a href="#">
  35. <img src="http://lorempixel.com/180/180/" alt="">
  36. <h3>lorem</h3>
  37. </a>
  38. </li>
  39. <li>
  40. <a href="#">
  41. <img src="http://lorempixel.com/180/180/" alt="">
  42. <h3>lorem</h3>
  43. </a>
  44. </li>
  45. <li>
  46. <a href="#">
  47. <img src="http://lorempixel.com/180/180/" alt="">
  48. <h3>Lorem</h3>
  49. </a>
  50. </li>
  51. </ul>
  52. </div>

CSS:

  1. .wrapper {
  2. margin: 0 auto;
  3. max-width: 1256px;
  4. padding-left: 20px;
  5. padding-right: 20px;
  6. position: relative;
  7. width: 100%;
  8. border: 1px solid black;
  9. }
  10. .wrapper ul {
  11. border: 1px solid red;
  12. -webkit-box-pack: space-between;
  13. -ms-flex-pack: space-between;
  14. -ms-justify-content: space-between;
  15. -webkit-justify-content: space-between;
  16. justify-content: space-between;
  17. display: -webkit-box;
  18. display: -moz-box;
  19. display: -webkit-flexbox;
  20. display: -ms-flexbox;
  21. display: -webkit-flex;
  22. display: flex;
  23. &:before {
  24. content: " ";
  25. display: table;
  26. }
  27. &:after {
  28. content: " ";
  29. display: table;
  30. clear: both;
  31. }
  32. }
  33. .wrapper ul li {
  34. border: 1px solid green;
  35. max-width: 186px;
  36. width: 15.30%;
  37. list-style: none;
  38. }
tvz2xvvm

tvz2xvvm1#

padding: 0;和删除clear fix(我忽略了...)解决了它。
感谢所有人!

4zcjmb1e

4zcjmb1e2#

您可以使用box-sizing属性到border-box来调整大小的计算方式。
为了方便使用,我将它添加到所有元素中,但为了优化,我强烈建议你只将它添加到你需要添加它的元素中。
至于你的宽度问题,也许删除width: 15.30%;可以解决这个问题,如下所示。

  1. * {
  2. box-sizing: border-box;
  3. }
  4. .wrapper {
  5. margin: 0 auto;
  6. max-width: 1256px;
  7. padding-left: 20px;
  8. padding-right: 20px;
  9. position: relative;
  10. width: 100%;
  11. border: 1px solid black;
  12. }
  13. .wrapper ul {
  14. padding: 0;
  15. border: 1px solid red;
  16. -webkit-box-pack: space-between;
  17. -ms-flex-pack: space-between;
  18. -ms-justify-content: space-between;
  19. -webkit-justify-content: space-between;
  20. justify-content: space-between;
  21. display: -webkit-box;
  22. display: -moz-box;
  23. display: -webkit-flexbox;
  24. display: -ms-flexbox;
  25. display: -webkit-flex;
  26. display: flex;
  27. }
  28. .wrapper ul li {
  29. border: 1px solid green;
  30. list-style: none;
  31. }
  1. <div class="wrapper">
  2. <h2>Lorem Ipsum</h2>
  3. <ul class="cf">
  4. <li>
  5. <a href="#">
  6. <img src="http://lorempixel.com/180/180/" alt="">
  7. <h3>lorem</h3>
  8. </a>
  9. </li>
  10. <li>
  11. <a href="#">
  12. <img src="http://lorempixel.com/180/180/" alt="">
  13. <h3>lorem</h3>
  14. </a>
  15. </li>
  16. <li>
  17. <a href="#">
  18. <img src="http://lorempixel.com/180/180/" alt="">
  19. <h3>lorem</h3>
  20. </a>
  21. </li>
  22. </ul>
  23. </div>
  24. <div class="wrapper">
  25. <h2>Lorem Ipsum</h2>
  26. <ul class="cf">
  27. <li>
  28. <a href="#">
  29. <img src="http://lorempixel.com/180/180/" alt="">
  30. <h3>lorem</h3>
  31. </a>
  32. </li>
  33. <li>
  34. <a href="#">
  35. <img src="http://lorempixel.com/180/180/" alt="">
  36. <h3>lorem</h3>
  37. </a>
  38. </li>
  39. <li>
  40. <a href="#">
  41. <img src="http://lorempixel.com/180/180/" alt="">
  42. <h3>lorem</h3>
  43. </a>
  44. </li>
  45. <li>
  46. <a href="#">
  47. <img src="http://lorempixel.com/180/180/" alt="">
  48. <h3>Lorem</h3>
  49. </a>
  50. </li>
  51. </ul>
  52. </div>

http://www.w3schools.com/cssref/css3_pr_box-sizing.asp

展开查看全部
xv8emn3q

xv8emn3q3#

引导用户,请注意.container-fluid引入的::before::after
修复:

  1. .container-fluid:before,
  2. .container-fluid:after {
  3. content: none;
  4. display: none;
  5. }

贷方:https://stackoverflow.com/a/48648382

jw5wzhpr

jw5wzhpr4#

你应该在顶层设置margin和padding为零。ul元素带有默认的padding。只需将此添加到wrapper ul:

  1. .wrapper ul {
  2. padding: 0;
  3. ...

或者更好的,开始你的css

  1. *{
  2. margin:0;
  3. padding: 0;
  4. }

您将摆脱任何不需要的默认填充和边距。

相关问题