简体   繁体   中英

Vertically aligning image and span inside div

I have an image and a span in a button, and I've tried using:

  1. flex with align-items: center
  2. setting span line-height to same height as container
  3. inline-block and vertical-align

but if I don't add a 1px padding-top to the image, it doesn't appear to be truly vertically aligned in the div, while the span text looks fine, and I can't figure out why.

HTML

   <button class="metabolite-btn">
      <img src="/images/metabolite/icon_question.svg" class="metabolite-btn__image">
      <span class="metabolite-btn__text">代謝物質とは</span>
   </button>

CSS

  .main-wrapper {
    .metabolite-btn {
      width: $vw-size-115-width-375;
      border-radius: 14px;
      border: none;
      height: 28px;
      background-color: $main-color;
      padding: 0 0 0 5px;
      text-align: left;
      cursor: pointer;

      &__image {
        width: $vw-size-20-width-375;
        padding: 1px 0 0 0;
      }

      &__text {
        font-size: $vw-size-12-width-375;
        font-weight: bold;
        color: $white;
        line-height: 28px;
      }
    }

To align both the elements vertically, you need to add display:flex and align-items: center to parent which is your button (Shown below)

//CSS
button {
  display: flex;
  align-items: center;
}

Working Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM