-1

I need an advice how to center text on line vertically. I have basically a data grid that can have rows and cells and within the cell you can put text on multiple lines which have fixed height of 34px. So I need to center text on its own line, but not within the row because then if on the same row you had one cell which has 2 line and the other has just one line the latter one would be centered in the middle of 64px.

.container {
  width: 100%;
  border: 1px solid black;
  display: flex;
  flex-wrap: wrap;
}

.text {
  height: 34px;
  border: 1px solid red
}

.cell {
  padding: 3px;
  border: 1px solid black;
}

.line1 {
  display: flex;
  width: 100%;
}
<div class="container">
  <div class="line1">
    <div class="cell">
      <div class="text">
        Text 1
      </div>
      <div class="text">
        Text 2
      </div>
    </div>
    <div class="cell">
      <div class="text">
        Text 2
      </div>
    </div>
  </div>
</div>

https://jsfiddle.net/ve4qauz0/34/

Diego D
  • 6,156
  • 2
  • 17
  • 30
  • Add these styles to your `.cell` class `display: flex; justify-content: center; flex-direction: column;` – Sergej Nov 24 '22 at 12:40

2 Answers2

2

I think this is what you need:

.text{
  display:flex;
  align-items: center;
}
Coopero
  • 296
  • 2
  • 10
1

You can give height of the containing div to the line-height and it should work as you expect.

In your case to .text add line-height: 34px;

.container {
  width: 100%;
  border: 1px solid black;
  display: flex;
  flex-wrap: wrap;
}

.text {
  height: 34px;
  border: 1px solid red;
  line-height: 34px;
}

.cell {
  padding: 3px;
  border: 1px solid black;
}

.line1 {
  display: flex;
  width: 100%;
}
<div class="container">
  <div class="line1">
    <div class="cell">
      <div class="text">
        Text 1
      </div>
      <div class="text">
        Text 2
      </div>
    </div>
    <div class="cell">
      <div class="text">
        Text 2
      </div>
    </div>
  </div>
</div>
Abhishek Kokate
  • 450
  • 2
  • 11