skip to Main Content

How can I align content in a flexed item?
The title to the top and the "rest"-content centred?

jsFiddle: https://jsfiddle.net/1wc5kv0d/4/

.row {
    display: flex;
}

.col {
    margin-left: 20px;
    border: 1px solid black;
}

.fixed-width-height {
    width: 40px;
    height: 40px;
    display: inline-block;
    background-color: blue;
    vertical-align: middle;
}

.inline-wrapper>span {
    vertical-align: middle;
}

h3 {
    font-size: 1rem;
}
<div class="row">
    <div class="col">
        <h3>
            title
        </h3>
        <div class="should-center">
            <span>Lorem Ispum</span>
            <span>Icon</span>
        </div>
    </div>
    <div class="col">
        <h3>
            another title
        </h3>
        <div class="should-center">
            <div class="inline-wrapper">
                <div class="fixed-width-height">
                    <span>Name</span>
                </div>
            </div>
        </div>
    </div>
</div>

How can I achieve that the titles (h3’s) stay on top and the content below should be equally centred. (Lorem Ipsum should be on the same height as "Name")

Is state:
enter image description here

Should be (Forget that black stroke, photoshop struggled):

enter image description here

2

Answers


  1. use this:

    .inline-wrapper > span{
      vertical-align: middle;
        text-align: center;
          padding: 10px;
    
    
    }
    h3{
      text-align: center;
      font-size: 1rem;
      padding: 10px;
      justify-content: space-around;
    }
    
    Login or Signup to reply.
  2. The .should-center and .col arn’t flex items.

    If you’d make the .should-center display: flex, you can use flex-grow: 1 to let it grow to 100%, then use align-items: center; to vertically align them.

    I’ve also added display: flex; to the .col to ensure all the childs are able to grow the the parent

    .col {
        display: flex;
        flex-direction: column;
    }
    
    .should-center {
        display: flex;
        flex-grow: 1;
        align-items: center;
    }
    
    .row {
      display: flex;
    }
    
    .col{
      margin-left: 20px;
      border: 1px solid black;
      display: flex;
      flex-direction: column;
    }
    
    .should-center {
        display: flex;
        flex-grow: 1;
        align-items: center;
    }
    
    .fixed-width-height{
      width: 40px;
      height: 40px;
      display: inline-block;
      background-color: blue;
      vertical-align: middle;
    }
    .inline-wrapper > span{
      vertical-align: middle;
    }
    h3{
      font-size: 1rem;
    }
    <div class="row">
      <div class="col">
        <h3>
         title
        </h3>
        <div class="should-center">
          <span>Lorem Ispum</span>
          <span>Icon</span>
        </div>
      </div>
      <div class="col">
        <h3>
        another title
        </h3>
        <div class="should-center">
        <div class="inline-wrapper">
            <div class="fixed-width-height">
            
            </div>
            <span>Name</span>
        </div>
        </div>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search