【问题标题】:Styled components center vertically样式化的组件垂直居中
【发布时间】:2021-07-08 03:20:57
【问题描述】:

“col 1 text”无法垂直居中。这是我所拥有的示例:

export const MyComponent = () => {
  return (
    <div>
        <ColOneContainer>
          col 1 text
        </ColOneContainer>
        <ColTwoContainer>
          <div>text that might run one line or two</div>
          <div>text that might run one line or two</div>
        </ColTwoContainer>
    </div>
  );
};

const ColOneContainer = styled.div`
  float: left;
  width: 25%;
`

const ColTwoContainer = styled.div`
  margin-left: 25%;
`;

const ColTwoContainer = styled.div`
  margin-left: 25%;
`;

给出如下输出:

我有多行“MyComponent”,所以我不能使用position: absolute; 谁能帮我将“col 1 text”垂直居中,使其看起来像这样:

【问题讨论】:

  • 请注意,“ColTwoContainer”中的文本可能很长,所以我不能使用“padding-top: 10rem”

标签: html css styled-components styling


【解决方案1】:

你可以试试 table-cell 来实现这个...

 const ColOneContainer = styled.div`
      float: left;
      width: 25%
      display: table-cell; 
      vertical-align: middle;
   '
 ;

也看看http://howtocenterincss.com/

【讨论】:

  • 这并没有解决问题。但是感谢 howtocenterincss 网站,非常酷的网站
猜你喜欢
  • 1970-01-01
  • 2013-10-15
  • 2014-09-02
  • 2014-03-05
  • 2011-10-02
  • 2021-04-26
  • 2018-03-14
  • 1970-01-01
  • 2018-07-21
相关资源
最近更新 更多