Bootstrap 如何使内容在节的中间居中?

tjvv9vkg  于 2023-02-06  发布在  Bootstrap
关注(0)|答案(2)|浏览(192)

我正在尝试使每个部分的内容居中。我该怎么做呢?我使用的容器/行是否正确?我是在部分、容器还是行中放置对齐方式?我不知道,尝试时两者都不起作用

  1. .content {
  2. height: 100vh;
  3. }
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Test</title>
  7. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
  8. <link rel="stylesheet" href="{{ url_for('static', filename='css/index.css') }}" />
  9. </head>
  10. <body>
  11. <section class="content" style="background-color: lightgreen;">
  12. <div class="container">
  13. <div class="row">
  14. <div class="col-lg-6 text-center text-lg-start">
  15. <h1>heading</h1>
  16. <p>test</p>
  17. </div>
  18. <div class="col-lg-6">
  19. <img src="https://via.placeholder.com/500/09f/fff.png" />
  20. </div>
  21. </div>
  22. </div>
  23. </section>
  24. <section class="content" style="background-color: moccasin;">
  25. <div class="container">
  26. <div class="row">
  27. <div class="col-lg-10 mx-auto col-lg-5">
  28. <h1>heading</h1>
  29. <p>test</p>
  30. </div>
  31. <div class="col-lg-6">
  32. <img src="https://via.placeholder.com/500/09f/fff.png" />
  33. </div>
  34. </div>
  35. </div>
  36. </section>
  37. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
  38. </body>
  39. </html>

使用 Bootstrap v5.3.0-alpha1
编辑:澄清问题

cbeh67ev

cbeh67ev1#

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Test</title>
  7. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
  8. <link rel="stylesheet" href="{{ url_for('static', filename='css/index.css') }}" />
  9. </head>
  10. <body>
  11. <section class="content" style="background-color: lightgreen;">
  12. <div class="container">
  13. <div class="row align-items-center">
  14. <div class="col-lg-6 text-center text-lg-start">
  15. <h1>heading</h1>
  16. <p>test</p>
  17. </div>
  18. <div class="col-lg-6">
  19. <img src="https://via.placeholder.com/500/09f/fff.png" />
  20. </div>
  21. </div>
  22. </div>
  23. </section>
  24. <section class="content" style="background-color: moccasin;">
  25. <div class="container">
  26. <div class="row justify-center text-center">
  27. <div class="col-lg-10 mx-auto col-lg-5">
  28. <h1>heading</h1>
  29. <p>test</p>
  30. </div>
  31. <div class="col-lg-6 m-auto">
  32. <img src="https://via.placeholder.com/500/09f/fff.png" />
  33. </div>
  34. </div>
  35. </div>
  36. </section>
  37. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
  38. </body>
  39. </html>
展开查看全部
bvpmtnay

bvpmtnay2#

当我检查了你的代码片段,我发现你已经设置了部分高度100vh和你的图像高度大于它,所以图像是重叠到其他部分.要删除重叠,您需要将节的高度设置为大于图像元素内部的高度,或者您可以删除应用于节的内容类的内联高度。并将节内容居中对齐。将类text-center与内容类或容器类一起使用,否则您将不得不使用每个元素编写它

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Test</title>
  7. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
  8. <link rel="stylesheet" href="{{ url_for('static', filename='css/index.css') }}" />
  9. </head>
  10. <body>
  11. <section class="content" style="background-color: lightgreen;">
  12. <div class="container text-center">
  13. <div class="row">
  14. <div class="col-lg-6 text-lg-start">
  15. <h1>heading</h1>
  16. <p>test</p>
  17. </div>
  18. <div class="col-lg-6">
  19. <img src="https://via.placeholder.com/500/09f/fff.png" />
  20. </div>
  21. </div>
  22. </div>
  23. </section>
  24. <section class="content" style="background-color: moccasin;">
  25. <div class="container text-center">
  26. <div class="row">
  27. <div class="col-lg-10 mx-auto col-lg-5">
  28. <h1>heading</h1>
  29. <p>test</p>
  30. </div>
  31. <div class="col-lg-6">
  32. <img src="https://via.placeholder.com/500/09f/fff.png" />
  33. </div>
  34. </div>
  35. </div>
  36. </section>
  37. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
  38. </body>
  39. </html>
展开查看全部

相关问题