2

I have a problem with the alignment of my child divs. Is it possible to align them if the child elements were separated? I tried display: flex; and grids but nothing happened. What I want is to make the "title box" height equally. Sorry for my bad explanation, I am not a native English speaker.

this is the output of my problem

body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.box-container {
  display: flex;
  gap: 10px;
}

.box {
  height: 100%;
  height: inherit;
  border: 2px solid green;
  width: 300px;
  padding: 5px;
}

.box-title {
  text-align: center;
  border: 1px solid red;
}

.text {
  color: blue;
  text-align: center;
}

button {
  width: 100%;
  margin: 0 auto;
}
<body>
  <div class="box-container">
    <div class="box">
      <div class="box-title">
        <h1>This is a loooooonnnnggggg title</h1>
      </div>
      <div>
        <p class="text">this should be aligned with the other div</p>
        <button>this should be aligned with the other div</button>
      </div>
    </div>
    <div class="box">
      <div class="box-title">
        <h1>Title</h1>
      </div>
      <div>
        <p class="text">this should be aligned with the other div</p>
        <button>this should be aligned with the other div</button>
      </div>
    </div>
    <div class="box">
      <div class="box-title">
        <h1>Title</h1>
      </div>
      <div>
        <p class="text">this should be aligned with the other div</p>
        <button>this should be aligned with the other div</button>
      </div>
    </div>
  </div>
</body>
Kirby Tan
  • 103
  • 11

2 Answers2

4

Go to your .box class and add these:

.box {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

enter image description here

Suggestion: you should learn more about flexbox

Sonny49
  • 425
  • 3
  • 18
0

Add this to the .box:

display: flex;
flex-direction: column;

Add this to the description:

margin-top: auto;

body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.box-container {
  display: flex;
  gap: 10px;
}

.box {
  height: 100%;
  height: inherit;
  border: 2px solid green;
  width: 300px;
  padding: 5px;
  display: flex;
  flex-direction: column;
}

.box-title {
  text-align: center;
  border: 1px solid red;
}

.box-description {
  margin-top: auto;
}

.text {
  color: blue;
  text-align: center;
}

button {
  width: 100%;
  margin: 0 auto;
}
<body>
  <div class="box-container">
    <div class="box">
      <div class="box-title">
        <h1>This is a loooooonnnnggggg title</h1>
      </div>
      <div class="box-description">
        <p class="text">this should be aligned with the other div</p>
        <button>this should be aligned with the other div</button>
      </div>
    </div>
    <div class="box">
      <div class="box-title">
        <h1>Title</h1>
      </div>
      <div class="box-description">
        <p class="text">this should be aligned with the other div</p>
        <button>this should be aligned with the other div</button>
      </div>
    </div>
    <div class="box">
      <div class="box-title">
        <h1>Title</h1>
      </div>
      <div class="box-description">
        <p class="text">this should be aligned with the other div</p>
        <button>this should be aligned with the other div</button>
      </div>
    </div>
  </div>
</body>
James Whiteley
  • 3,363
  • 1
  • 19
  • 46