简体   繁体   English

使用flexbox垂直对齐内容

[英]Align content vertically with flexbox

I am creating a tile where the image can either be 50% of the tile or 75% of the tile and content will fill the remaining area/ space left. 我正在创建一个图块,其中图像可以是图块的50%或图块的75%,内容将填充剩余的剩余区域/空间。 My issue is I don't want to set a specific height for the image as it will lose it's image/aspect ratio. 我的问题是我不想为图像设置特定的高度,因为它将失去图像的高宽比。 I have something like the following below: 我有以下类似的东西:

const tile= () => (
  <StyledWrapper>
    <StyledImageBlock>
      //get Image here
    </StyledImageBlock>
      <StyledTextContent>
        {getTitleOne()} //this is a span element
        {getTitleTwo()} //this is a span element
      </StyledTextContent>}
  </StyledWrapper>
);

In my StyledWrapper I have the following: 在我的StyledWrapper我具有以下内容:

  text-align: center;

  a {
    text-decoration: none;
  }

In my StyledImageBlock I have the following: 在我的StyledImageBlock我具有以下内容:

  width: 100%;

In my StyledTextContent I have the following: 在我的StyledTextContent我具有以下内容:

align-content: center;
align-items: center;
display: flex;
flex-direction: column;
justify-content: center;

But items refuse to be vertically aligned, I set a height on the StyledWrapper to height: 700px just for demo and still refuses. 但是项目拒绝垂直对齐,我在StyledWrapper height: 700px设置为height: 700px仅用于演示,但仍然拒绝。 Where am I going wrong here? 我在哪里错了? Any suggestions on how to achieve what I have described. 关于如何实现我所描述的任何建议。

Some more information. 一些更多的信息。 I can achieve the desired result when setting a percentage height on the image and content but with this the image aspect ration changes to be worse/ stretches the image which I do not want. 在图像和内容上设置百分比高度时,我可以达到所需的结果,但与此同时,图像的宽高比会变差/拉伸我不需要的图像。 Ultimately this tile will be going into a flexbox grid. 最终,该图块将进入flexbox网格。

You probably need to put 您可能需要放

justify-content: center;
align-items: center;

on StyledWrapper . StyledWrapper上 This will center the StyledImageBlock vertically and horizontally. 这将使StyledImageBlock垂直和水平居中

in the <StyledWrapper> set. <StyledWrapper>设置中。

justify-content: center; to center in x-axis, and, 以x轴为中心,并且,

align-items: center; to center in the. 居中。

The parent element when you define display: flex is a box, where you can set how the childs will interact. 定义display: flex时的父元素display: flex是一个框,您可以在其中设置子元素的交互方式。

flex-basis ( Set width of the first element, affect the element where is defined ) flex-basis (设置第一个元素的宽度,影响定义的元素)

flex-direction ( Set direction of how element will be display , to be use in parent class of childs fleboxes ) flex-direction (设置元素显示方式的方向,用于子级fleboxes的父类中)

flex-grow ( How width it's gonna take an element in the x-axis, affect the element where is defined) flex-grow (元素在x轴上的宽度如何影响定义元素的位置)

flex-flow ( How it's display the childs in the x-axis, to be use in parent class of childs fleboxes ) flex-flow (如何在x轴上显示子项,以在子项fleboxes的父类中使用)

flex-shrink ( How width it's gonna take to shrink, affect the element where is defined) flex-shrink收缩的宽度将如何变化,影响定义的元素)

flex-wrap ( forced onto one line or can wrap onto multiple lines, to be use in parent class of childs fleboxes ) flex-wrap (强制到一行或可以换行,以用于子级fleboxes的父类中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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