html-左右容器未放置在内容容器旁边

前端之家收集整理的这篇文章主要介绍了html-左右容器未放置在内容容器旁边 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在努力实现这样的目标

Image: How I would like it to look (disregard the white spaces in the image)

但是,左右容器不会粘在我的内容容器上.它们也没有正确排列在顶部.我很困惑为什么.我已经设定好花车和最高位置,但仍然无法使用.我必须将主容器保持在相对位置.

  1. html,body {
  2. height: 100%;
  3. min-height: 100%;
  4. margin: 0;
  5. padding: 0;
  6. }
  7. .main-container {
  8. position: relative;
  9. top: 0;
  10. width: 100%;
  11. height: 100%;
  12. background: green;
  13. padding: 0;
  14. margin: 0;
  15. }
  16. .left-container {
  17. position: relative;
  18. top: 0;
  19. left: 0%;
  20. float: right;
  21. width: 10%;
  22. min-width: 100px;
  23. max-width: 100px;
  24. background-color: blue;
  25. display: block-inline;
  26. }
  27. .content-container {
  28. position: relative;
  29. top: 0;
  30. left: 50%;
  31. transform: translateX(-50%);
  32. width: 80%;
  33. min-width: 800px;
  34. max-width: 800px;
  35. height: auto;
  36. background: red;
  37. display: block-inline;
  38. }
  39. .right-container {
  40. position: relative;
  41. top: 0;
  42. right: 0%;
  43. float: left;
  44. width: 10%;
  45. min-width: 100px;
  46. max-width: 100px;
  47. background-color: purple;
  48. display: block-inline;
  49. }
  1. <html>
  2. <body>
  3. <div class="main-container">
  4. <div class="left-container">
  5. <p>This is the left container</p>
  6. </div>
  7. <div class="content-container">
  8. <p>This is the content container</p>
  9. </div>
  10. <div class="right-container">
  11. <p>This is the right container</p>
  12. </div>
  13. </div>
  14. </body>
  15. </html>
最佳答案
一世
例如在这种情况下使用flexBox

  1. h1,h2 {
  2. font-family: Lato;
  3. }
  4. html,body {
  5. margin: 0;
  6. padding: 0;
  7. width: 100vw;
  8. height: 100%;
  9. }
  10. .main-container {
  11. top: 0;
  12. left:0;
  13. width: 100%;
  14. height: 100%;
  15. background: green;
  16. padding: 0;
  17. display:flex;
  18. margin:0 auto;
  19. justify-content: center;
  20. align-content: flex-start;
  21. }
  22. .right-container {
  23. top:0;
  24. min-width: 10% ;
  25. background-color: purple;
  26. height:100%;
  27. }
  28. .left-container {
  29. top: 0;
  30. min-width: 10%;
  31. background-color: blue;
  32. height:100%;
  33. }
  34. .content-container {
  35. top:0;
  36. background: red;
  37. width:800px;
  38. height:100%;
  39. max-width:80%;
  40. }
  1. <html>
  2. <body>
  3. <div class="main-container">
  4. <div class="left-container">
  5. <p>This is the left container</p>
  6. </div>
  7. <div class="content-container">
  8. <p>This is the content container</p>
  9. </div>
  10. <div class="right-container">
  11. <p>This is the right container</p>
  12. </div>
  13. </div>
  14. </body>
  15. </html>

猜你在找的HTML相关文章