【问题标题】:My images are stacking one over the other (CSS) and taking more space outside the designed website我的图像堆叠在一起(CSS)并在设计的网站之外占用更多空间
【发布时间】:2026-01-24 08:55:01
【问题描述】:

我的内容图像相互堆叠,我设置了最大宽度,但它们占用的空间比预期的要多。我在标题下的另一张图片也占用了比网站设计更多的空间。我尝试在 css 中删除 .image 选择器,但没有任何改变。

* {
    font-family: -apple-system, BlinkMacSystemFont, Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

a {
    font-size: 16px;
    text-decoration: none;
    color: #e5e7eb;
}

li {
    list-style-type: none;
    margin: 8px;
}

ul {
    display: flex;
    flex-direction: row;
}

.header {
    background-color: #1f2937;
    margin: 0;
    padding: 0;
}

.header2 {
    display: flex;
    justify-content: space-around;
}

.heromain {
    font-weight: bolder;
    font-size: 48px;
    color: #f9faf8;
}

.herotwo {
    font-size: 18px;
    color: #e5e7eb;
}

.section-left {
    display: flex;
    flex-direction: column;
}
.logo {
    font-size: 24px;
    color: #f9faf8;
    font-weight: bold;
    padding-top: 10px;
}

.content {
   font-size: 36px;
   font-weight: bolder;
   color: #1F2937; 
   text-align: center;
}

.quote {
    background-color: #e5e7eb;
    font-size: 36px;
    font-style: italic;
    color: #1f2937;
    padding: 100px 300px;
}

.action, button {
    background-color: #3882f6;
    color: white;
}

.action {
    width: 650px;
    height: 80px;
    margin: 0 auto;
    border-radius: 15px;
    padding: 30px 100px;
}

button {
    border-radius: 5px;
    border: 2px solid white;
    padding: 4px 17px;
    width: 50px;
    margin: 10px;
    display: inline;
}

footer {
    background-color: #1F2937;
    color: #e5e7eb;
    text-align: center;
    padding: 30px;
}

body {
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 30px;
}

.container {
    display: flex;
    justify-content: space-around;
    padding: 100px;
}

.info {
    border-radius: 15px;
    border-color: #3882f6;
    border-width: 4px;
    max-width: 200px;
}

.information {
    display: flex;
    gap: 16px;
    align-items: center;
    justify-content: center;
    flex: 1 1 auto;
}

.text {
    text-align: center;
}

.image {
    max-width: 200px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My website</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="header">
        <div class="header2">
          <div class="logo">Header Logo</div>
          <ul>
              <li><a href="https://www.google.com" target="_blank" rel="noreferrer noopener">header link one</a></li>
              <li><a href="https://www.twitter.com" target="_blank" rel="noreferrer noopener">header link two</a></li>
              <li><a href="https://www.pinterest.com" target="_blank" rel="noreferrer noopener">header link three</a></li>
          </ul>
        </div>
        <div class="container">
          <div class="section-left">
            <div class="heromain">This website is<br>awesome</div>
            <div class="herotwo">This website has some subtext that goes here under the <br>main title. It is a smaller font and colors contrast.</div>
            <button>Sign up</button>
          </div>
          <div class="image">
            <img src="image.png">
          </div>
        </div>
    </div>
    <div class="content">Some random information.</div>
    <div class="information">
        <div class="info">
            <img src="image.png" alt="img">
            <div class="text">this is some subtext under an illustration or image</div>
        </div>
        <div class="info">
            <img src="image.png" alt="img">
            <div class="text">this is some subtext under an illustration or image</div>
        </div>
        <div class="info">
            <img src="image.png" alt="img">
            <div class="text">this is some subtext under an illustration or image</div>
        </div>
        <div class="info">
            <img src="image.png" alt="img">
            <div class="text">this is some subtext under an illustration or image</div>
        </div>  
    </div>
    <div class="quote">This is an inspiring quote, or a testimonial from a customer.
        Maybe its just filling up space, or maybe people will actually read it. Who knows? All I know is that it looks nice.
        <div class="attribution">-Thor, God of Thunder</div>
    </div>
    <div class="action">
        <p><strong>Call to action! It's time!</strong><br>
        Sign up for our product by clicking that button right over there!
        </p>
        <button>Sign up</button>
    </div>
    <footer>Copyright © The Odin Project 2021</footer>
</body>
</html>

The desired outcome for my images

我得到的结果: [2]:https://imgur.com/O4EFYVo

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    解决方案一

    您可以通过将引导程序添加到您的项目来解决此问题,例如,您希望 3 张图片彼此相邻,试试这个并通过您的图片 src 进行编辑

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    
        <title>*</title>
    
    </head>
    <body>
    
    
        <div class="row row-cols-1 row-cols-md-3 g-4">
            <div class="col">
              <div class="card h-100">
                <img src="..." class="card-img-top" alt="...">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                </div>
              </div>
            </div>
            <div class="col">
              <div class="card h-100">
                <img src="..." class="card-img-top" alt="...">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">This is a short card.</p>
                </div>
              </div>
            </div>
            <div class="col">
              <div class="card h-100">
                <img src="..." class="card-img-top" alt="...">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content.</p>
                </div>
              </div>
            </div>
    
    
    </body>
    </html>
    

    或者如果您希望 4 张图片彼此相邻,请更改第 17 行中的 md 和 g ...

    解决方案二

    或者您可以使用带有类的 HTML 和 CSS 来解决这个问题......看看这个:

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    * {
      box-sizing: border-box;
    }
    
    .column {
      float: left;
      width: 33.33%;
      padding: 5px;
    }
    
    /* Clearfix (clear floats) */
    .row::after {
      content: "";
      clear: both;
      display: table;
    }
    </style>
    </head>
    <body>
    
    <h2>Images Side by Side</h2>
    <p>How to create side-by-side images with the CSS float property:</p>
    
    <div class="row">
      <div class="column">
        <img src="img_snow.jpg" alt="Snow" style="width:100%">
      </div>
      <div class="column">
        <img src="img_forest.jpg" alt="Forest" style="width:100%">
      </div>
      <div class="column">
        <img src="img_mountains.jpg" alt="Mountains" style="width:100%">
      </div>
    </div>
    
    </body>
    </html>
    

    请记住,您的图片大小也很重要,您的网站最好是响应式的,因为我建议您使用第一,但最后它们都很有用,您可以使用他们

    【讨论】:

      【解决方案2】:

      将此添加到您的 CSS:

      .info img {
          max-width: inherit;
        }
      

      【讨论】:

        最近更新 更多