css 如何设置Flex Box容器的特定大小?

ffscu2ro  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(133)

正如标题所述,我希望flexbox的容器具有特定的宽度和高度。然而,我不知道我可以使用什么方法。这是我的代码:

  1. <div class="container-gallery">
  2. <img src="gallery/img.png" alt="">
  3. <img src="gallery/img1.jpg" alt="">
  4. <img src="gallery/img2.jpg" alt="">
  5. <img src="gallery/img3.jpg" alt="">
  6. <img src="gallery/img4.jpg" alt="">
  7. <img src="gallery/img5.jpg" alt="">
  8. </div>

个字符
我尝试创建容器库的父div,以使子div居中,并在container-gallery中放置特定的宽度。它确实有效;但是,当您尝试重新设置它时,它不会将其内容居中,因为它设置了宽度。类似于以下内容:

  1. <div class="container-gallery">
  2. <img src="gallery/img.png" alt="">
  3. <img src="gallery/img1.jpg" alt="">
  4. <img src="gallery/img2.jpg" alt="">
  5. <img src="gallery/img3.jpg" alt="">
  6. <img src="gallery/img4.jpg" alt="">
  7. <img src="gallery/img5.jpg" alt="">
  8. </div>
  1. .container-gallery {
  2. display: flex;
  3. flex-wrap: wrap;
  4. width: 67.5%;
  5. }
  6. .container-gallery img{
  7. width: 15%;
  8. height: 30vh;
  9. border-radius: 8%;
  10. margin: 1%;
  11. }
  12. .main-gallery {
  13. display: flex;
  14. justify-content: center;
  15. }

的字符串
我该怎么办?
我对Flexbox很陌生,所以把这个当作新手问题。
它应该是这样的!宽度和高度. x1c 0d1x

hmtdttj4

hmtdttj41#

下面的CSS解决了你的问题!
请在全屏查看正确的结果!
flex-grow: 0
flex-shrink: 0
flex-wrap: 30.33%
之所以不是33.33%或1/3,是因为要调整保证金!

  1. html,
  2. body {
  3. background-color: #191D2B;
  4. margin: 0px auto;
  5. padding: 0px;
  6. display: flex;
  7. align-items: center;
  8. justify-content: center;
  9. flex-direction: column;
  10. }
  11. .title {
  12. font-size: 50px;
  13. color: white;
  14. }
  15. .container-gallery {
  16. display: flex;
  17. flex-wrap: wrap;
  18. justify-content: center;
  19. }
  20. .container-gallery img {
  21. flex: 0 0 30.33%;
  22. border-radius: 8%;
  23. margin: 5px;
  24. }
  25. .main-gallery {
  26. display: flex;
  27. justify-content: center;
  28. }

个字符

展开查看全部

相关问题