代码之家  ›  专栏  ›  技术社区  ›  WoJ

如何强制元素内容的宽度(而不是元素本身)?

  •  0
  • WoJ  · 技术社区  · 5 年前

    8 有时 14 ).

    1 宽度为两位数。我的目标不是完美(数字通常有不同的宽度),而是要说一些沿着 <span class="el" width_of_text_for_this_element="2em;">1</span>

    .clock {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-column-gap: 20px;
      align-items: center;
      width: 400px;
    }
    
    .el {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 200px;
      height: 200px;
      font-size: 120px;
      background-color: black;
      color: white;
      font-family: sans-serif;
      font-weight: bolder;
    }
    <div class="clock">
      <span class="el">1</span>
      <span class="el">18</span>
    </div>

    我相信我正在寻找的是一种将样式应用于元素内容(而不是元素本身)的方法,但我不确定。

    0 回复  |  直到 5 年前
        1
  •  0
  •   Temani Afif    5 年前

    你可以用下面的方法试试 ch 单元

    .clock {
      display: inline-grid;
      grid-template-columns: auto auto;
      grid-column-gap: 20px;
      width: 400px;
    }
    
    .el {
      font-size: 120px;
      background-color: black;
      color: white;
      font-family: sans-serif;
      font-weight: bolder;
      width:2ch;
      height:2ch;
      text-align:right; /* Or left based on your needs */
      border:30px solid black;
    }
    <div class="clock">
      <span class="el">1</span>
      <span class="el">18</span>
    </div>