简体   繁体   English

CSS Flexbox 子项未占用全部可用宽度

[英]CSS Flexbox children items not taking up full available width

I'm trying to position the title of a card component to the left most side and some other data to the right most, as shown in the picture below:我正在尝试 position 最左边的一个卡片组件的标题和最右边的一些其他数据,如下图所示:

在此处输入图像描述

the component I've written for this looks like the following:我为此编写的组件如下所示:

    <div className="card">
      <div className="card-title">Some Title</div>
      <div className="card-data">
        <span>{`Some data: 07/22/2020`}</span>
        <span>TEST</span>
      </div>
    </div>

For the CSS, I've seen from several previous posts that a solution is to assign flex-grow:1 or flex: 1 1 auto to the children items.对于 CSS,我从之前的几篇文章中看到,解决方案是将flex-grow:1flex: 1 1 auto分配给子项。 For that I've done the following:为此,我做了以下事情:

.card {
  display: flex;
  justify-content: space-between;
  .card-title {
    flex: 1;
  }
  .card-data {
     flex: 1;
  }
}

Despite specifying the flex-grow to be 1, it's not taking up the full space of the content, as highlighted here.尽管将flex-grow指定为 1,但它并没有占用内容的全部空间,如此处突出显示的。

在此处输入图像描述

The card div is literally at the top of the component so nothing else is messing with the styles here. card div 实际上位于组件的顶部,因此这里没有其他东西与 styles 混淆。 Is there any other property/specification needed here to make the two divs take up the full width?这里是否需要任何其他属性/规范来使两个 div 占据整个宽度?

If you need to make the .card-title and .card-data elements take up half of the card width, as well as being positioned to the left and right side, try this:如果您需要使.card-title.card-data元素占据卡片宽度的一半,以及位于左侧和右侧,请尝试以下操作:

.card {
  display: flex;
  justify-content: space-between;

  .card-title {
    flex-grow: 1;
  }

  .card-data {
    display: flex;
    flex-grow: 1;
    justify-content: flex-end;
  }
}

Don't use flex: 1 in card-title and card-data just use flex property on card.不要在卡片标题和卡片数据中使用flex: 1只需在卡片上使用 flex 属性。

.card {
  display: flex;
  justify-content: space-between;
}

Can't see anything wrong, should just work using what you wrote and applying flex-grow: 1 or flex: 1 to the children elements of the card.看不出有什么问题,应该只使用您编写的内容并将flex-grow: 1flex: 1应用于卡片的子元素。 You got something else in your CSS/DOM that interferes.您的 CSS/DOM 中有其他干扰因素。

https://jsfiddle.net/cg9y3mv6/ https://jsfiddle.net/cg9y3mv6/

Actually, you need to define class on element as class attribute not className.实际上,您需要在元素上将 class 定义为class 属性而不是 className。 Than everything will work比一切都会奏效

 <div class="card"> <div class="card-title">Some Title</div> <div class="card-data"> <span>{`Some data: 07/22/2020`}</span> <span>TEST</span> </div> </div>

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

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