html 为什么justify-content:center不会将h2和#experience_content项移动到.experience框的中心?

ubbxdtey  于 2023-11-15  发布在  其他
关注(0)|答案(1)|浏览(150)

此问题在此处已有答案

Flexbox: center horizontally and vertically(14个回答)
6天前关闭.

  1. <html>
  2. <head>
  3. <style>
  4. .experience {
  5. display: flex;
  6. flex-direction: column;
  7. justify-content: center;
  8. }
  9. .experience h2 {
  10. }
  11. #experience_content {
  12. display: flex;
  13. gap: 10px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <section class="experience">
  19. <h2>Work Experience</h2>
  20. <div id="experience_content">
  21. <img src=./images/supermarket.jpg width="260" height="195" alt=" shopping center">
  22. <p> Please help me solve this stupid problem. I beg you.</p>
  23. </div>
  24. </section>
  25. </body>
  26. </html>

我试着从.experience元素中删除h2元素。
我在#experience_content元素上尝试了justify-content:center,但对img和p元素也不起作用。

wvyml7n5

wvyml7n51#

你所面临的问题是因为你将justify-content: center应用到一个带有flex-direction: column的flex容器。justify-content属性沿着flex容器的 * main-axis * 工作,在本例中,由于flex-direction: column,它是垂直轴。如果你想水平居中,你需要使用在横轴上工作的属性align-items

  1. <html>
  2. <head>
  3. <style>
  4. .experience {
  5. display: flex;
  6. flex-direction: column;
  7. align-items: center;
  8. /* <-- Here */
  9. }
  10. .experience h2 {}
  11. #experience_content {
  12. gap: 10px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <section class="experience">
  18. <h2>Work Experience</h2>
  19. <div id="experience_content">
  20. <img src=./images/supermarket.jpg width="260" height="195" alt=" shopping center">
  21. <p> Please help me solve this stupid problem. I beg you.</p>
  22. </div>
  23. </section>
  24. </body>
  25. </html>

字符串

展开查看全部

相关问题