【问题标题】:How to style textarea-element in grid column?如何在网格列中设置文本区域元素的样式?
【发布时间】:2019-08-28 13:56:14
【问题描述】:

我正在使用对 styledcomponents 的反应,并在 css-grid 周围转转,在对齐 textarea 时遇到问题,无法获得正确的高度(下一行溢出)和宽度(必须与 'col3' 对齐在右侧):

const Wrapper = styled.div`
    display:grid
    grid-template-columns: 1fr 1fr 1fr 30px;
    grid-template-rows:25% 25% auto;
    justify-items: start;
    border: solid 1px   #000000
    `

const ColumnSpan2 = styled.div`
    grid-column: 2/4;
    grid-row: row 2;
`;

组件如下所示:

<Wrapper>
            <Column1>
                <select>
                    <option>een</option>
                    <option>twee</option>
                </select>
            </Column1>
            <Column2>
                <input type="text" value="col2" />{' '}
            </Column2>
            <Column3>
                <input type="text" value="col3" />{' '}
            </Column3>
            <Column4>todo iceon </Column4>
            <ColumnSpan2>
                {' '}
                <textarea>Hello comments here</textarea>
            </ColumnSpan2>
</Wrapper>

css:

body {
    margin: 0 auto;
    max-width: 60%;
}

textarea{
    width: 200%;
    height: 100%
  }

如何为textarea 获得正确的样式(我尝试更改grid-template-rows 属性但没有给出解决方案)

【问题讨论】:

    标签: reactjs css css-grid styled-components


    【解决方案1】:

    我为你的反应代码创建了一个普通的 CSS 表示 - 这里有一些指针:

    • justify-items: start 将对齐网格项目内部的内容 - 考虑将其删除(还有width: 200% 等我猜你试图解决它保持justify-items

    • 现在将inputselecttextarea 的宽度设置为使用width: 100%填充它们的网格单元格

    • grid-row: row 2 无效,因为您在 grid-template-rows 定义中没有 named grid-line - 将其更改为 grid-row: 2

    • 您没有为 grid 容器定义高度 - 所以您可以将 grid-template-rows 更改为 grid-template-rows: auto auto

    • 还要注意使用grid-gappadding 来调整网格项之间的空格

    请看下面的演示:

    body {
      margin: 0 auto;
      max-width: 60%;
    }
    
    textarea, input, select {
      width: 100%; /* extend the width of the grid item */
      box-sizing: border-box; /* including padding / border in width */
    }
    
    .wrapper {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr 30px;
      grid-template-rows: auto auto; /* changed */
      border: solid 1px #000000;
      grid-gap: 10px; /* grid gap to handle spaces between items if needed */
      padding: 5px; /* space between wrapper and grid items */
    }
    
    .column1 {
      grid-column: 1/2;
    }
    
    .column2 {
      grid-column: 2/3;
    }
    
    .column3 {
      grid-column: 3/4;
    }
    
    .column4 {
      grid-column: 4/5;
    }
    
    .colspan2 {
      grid-column: 2/4;
      grid-row: 2; /* changed */
    }
    <div class="wrapper">
    
      <div class="column1">
        <select>
          <option>een</option>
          <option>twee</option>
        </select>
      </div>
      <div class="column2">
        <input type="text" value="col2" />
      </div>
      <div class="column3">
        <input type="text" value="col3" />
      </div>
      <div class="column4">(i)</div>
      <div class="colspan2">
        <textarea>Hello comments here</textarea>
      </div>
    </div>

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2022-11-17
    • 2011-04-27
    • 1970-01-01
    • 2015-01-31
    • 2015-08-13
    • 2012-01-16
    相关资源
    最近更新 更多