Search code examples
htmlcss

Text above cards


I'm trying to align the text "Zarząd" above my cards but, everything I have tried doesn't work. I've tried every way that I could think of, I'm a beginner so I'm still learning. I tried adding new styles, columns, displays, etc.

This is the most recent attempt at the fix:

#zarzad {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.container section {
  margin-top: 20px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.card {
  width: 200px;
  margin-right: 10px;
  margin-bottom: 20px;
  border: 2px solid #eee;
  background-color: #333;
  color: #eee;
  box-sizing: border-box;
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

#zarzad h2 {
  width: 100%;
  text-align: center;
  margin-bottom: 20px;
}

.card img {
  width: 100%;
  border-radius: 10px 10px 0 0;
}

.card .info {
  padding: 15px;
}

.card h3,
.card p {
  margin: 0;
  color: #eee;
}
<section id="zarzad">
  <div class="container">
    <h2>Zarząd</h2>
    <div class="card">
      <img src="https://cdn.discordapp.com/avatars/637347195623833630/71891a4d1cd9795017d08953adb04cdb.png" alt="Zarząd 1">
      <div class="info">
        <h3>Bakłażan</h3>
        <p>Właściciel</p>
      </div>
    </div>
    <div class="card">
      <img src="https://cdn.discordapp.com/avatars/314066973879304203/4207e8071ce2f216fac7d188e17a8502.png" alt="Zarząd 1">
      <div class="info">
        <h3>Templariusz</h3>
        <p>Zarząd</p>
      </div>
    </div>
  </div>
</section>

This is how it should look


Solution

  • You would need to put the cards in another container separate from the <h2> text. Then make it a flexbox.

    See this this example:

    #zarzad {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    
    .container section {
      margin-top: 20px;
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
    }
    
    .card-container {
      display: flex;
    }
    
    .card {
      width: 200px;
      margin-right: 10px;
      margin-bottom: 20px;
      border: 2px solid #eee;
      background-color: #333;
      color: #eee;
      box-sizing: border-box;
      border-radius: 10px;
      overflow: hidden;
      display: flex;
      flex-direction: column;
    }
    
    #zarzad h2 {
      width: 100%;
      text-align: center;
      margin-bottom: 20px;
    }
    
    .card img {
      width: 100%;
      border-radius: 10px 10px 0 0;
    }
    
    .card .info {
      padding: 15px;
    }
    
    .card h3,
    .card p {
      margin: 0;
      color: #eee;
    }
    <section id="zarzad">
      <div class="container">
        <h2>Zarząd</h2>
    
        <div class="card-container">
          <div class="card">
            <img src="https://cdn.discordapp.com/avatars/637347195623833630/71891a4d1cd9795017d08953adb04cdb.png" alt="Zarząd 1">
            <div class="info">
              <h3>Bakłażan</h3>
              <p>Właściciel</p>
            </div>
          </div>
          <div class="card">
            <img src="https://cdn.discordapp.com/avatars/314066973879304203/4207e8071ce2f216fac7d188e17a8502.png" alt="Zarząd 1">
            <div class="info">
              <h3>Templariusz</h3>
              <p>Zarząd</p>
            </div>
          </div>
        </div>
      </div>
    </section>