【问题标题】:Use CSS grid to align an ALT SYMBOL and regular text?使用 CSS 网格对齐 ALT SYMBOL 和常规文本?
【发布时间】:2019-08-22 21:18:40
【问题描述】:

任何人都知道将“alt 符号”与“常规文本”对齐的最佳方法 - 如果没有一些 hacky 操作,似乎无法让这两个都居中(或任何东西)。

我目前正在使用 CSS 网格,但如果它们易于实施且可持续,我愿意接受其他解决方案。

display.jsx

  <div onClick={props.toggle} className='data__item__header'>
    <span className='data__item__header__bullet'>&#9679;</span>
    <p className='data__item__header__title'>Clashes {props.index}</p>
  </div>

display.scss

.data__item__header {
  display: grid;
  grid-template-columns: 20px 1fr;
  line-height: 20px;
  align-content: center;
  grid-gap: 10px;
}

.data__item__header__bullet {
  font-size: 36px;
  color: $tomato;
  border: 1px solid blue;
  display:grid;
  text-align: center;
}

.data__item__header__title {
  border: 1px solid green;
}

目前是:

应该是:

【问题讨论】:

    标签: html css css-grid centering


    【解决方案1】:

    align-self: centerjustify-self: center 添加到.data__item__header__bullet 元素,同时调整行高 - 参见下面的演示:

    .data__item__header {
      display: grid;
      grid-template-columns: 20px 1fr;
      line-height: 36px; /*changed*/
      /* align-content: center; */ /* not needed */
      grid-gap: 10px;
    }
    
    .data__item__header__bullet {
      font-size: 36px;
      color: $tomato;
      border: 1px solid blue;
      display:grid;
      text-align: center;
      align-self: center; /* added */
      justify-self: center; /* added */
    }
    
    .data__item__header__title {
      border: 1px solid green;
    }
    <div  class='data__item__header'>
        <span class='data__item__header__bullet'>&#9679;</span>
        <p class='data__item__header__title'>Clashes {props.index}</p>
      </div>

    您可以使用border-radius 来提供更好的子弹样式,而不是使用可能会给您带来对齐问题的&amp;#9679; 字符 - 请参阅下面的演示:

    .data__item__header {
      display: grid;
      grid-template-columns: 20px 1fr;
      grid-gap: 10px;
    }
    
    .data__item__header__bullet {
      background: blue;
      border-radius: 100%; /* added */
      height: 10px; /* added */
      width: 10px; /* added */
      display:grid;
      text-align: center;
      align-self: center; 
      justify-self: center; 
    }
    
    .data__item__header__title {
      border: 1px solid green;
    }
    <div  class='data__item__header'>
        <span class='data__item__header__bullet'></span>
        <p class='data__item__header__title'>Clashes {props.index}</p>
      </div>

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多