【问题标题】:Responsive flex boxes with sass and controlling images overflow [duplicate]具有 sass 和控制图像溢出的响应式弹性框 [重复]
【发布时间】:2023-04-10 12:18:01
【问题描述】:

复制部分

我正在尝试为 React 应用程序中的某个部分构建某种布局,布局类似于下图,忽略右边的行 这是我的 HTML, 盒子暂时是空的,稍后再回到那个..

const projects = () => {
  return (
    <section className="projects">
      <div className="container fcontainer">
        <div className="fcol fcol--1">
          <div className="box-1"></div>
          <div className="box-2"></div>
        </div>
        <div className="fcol fcol--2">
          <div className="box-1"></div>
          <div className="box-1"></div>
          <div className="box-1"></div>
        </div>
        <div className="fcol fcol--3">
          <div className="box-1"></div>
          <div className="box-2"></div>
        </div>
      </div>
    </section>
  )
}
现在这是我的 .scss 代码

.fcol {
    color: #333;
    margin: 1rem;
    text-align: center;
    font-size: 3rem;
    display: flex;
    flex-direction: column;
    height: 60vh;
    flex-grow: 4;

    &--1 {
      .box-1{
        background-color: lightblue;
        flex-grow: 1;
        margin-bottom: 2rem;
      } 
      
      .box-2{
        background-color: lightgreen;
        flex-grow: 2;
      }
    }

    &--2 {
      .box-1{
        background-color: lightblue;
        flex-grow: 1;
        justify-content: space-between;
        &:not(:last-child){
          margin-bottom: 2rem;
        }
      } 
    }

    &--3 {
      .box-1{
        background-color: lightblue;
        flex-grow: 3;
        margin-bottom: 2rem;
      } 
      
      .box-2{
        background-color: lightgreen;
        flex-grow: 2;
      }
    }
  }

这是结果..

这是codepen,如果您有兴趣,可以编辑和修复问题,如果您有更好的方法,也可以随意更改 HTML 和 scss 整个结构。

非重复部分
我有点坚持用图像填充每个框,因为即使我手动将其设置为heightwidth100%,图像总是溢出框外,如果我添加引导类@987654333,它也不起作用@。 在我的 react 应用程序中导入和使用 .scss 文件中的图像时出现另一个错误,请在 link 中查看,就是这样,非常感谢您抽出宝贵时间

【问题讨论】:

  • 你有没有运行过代码sn-ps看看它们是否有效?
  • 好吧,老实说,我没有在这里运行它,但我确实提供了指向 codepen 中工作示例的链接
  • 问题(至少在您的codepen 中)是您对flex-grow 的使用。此属性旨在分配容器中的可用空间。因为中间列有更多的空闲空间(这一​​列有两个间隙,其他列有一个),所以项目会更小。请改用flex-basis。有关详细信息,请参阅副本。
  • 谢谢迈克尔,这有帮助,这个问题实际上由两部分组成,是的,第一部分结果是重复的,但我猜第二部分不是,另外,我实际上提到了另一个问题最后没有人回答,请您考虑检查一下,然后也许您想取消标记它重复,谢谢
  • 你好,迈克尔,你能考虑重读我的问题吗,谢谢

标签: css reactjs sass responsive-design flexbox


【解决方案1】:

这是你想要做的吗?

https://codepen.io/t_sg/pen/NEQayz

HTML:

<section class="projects">
  <div  class="container fcontainer">
    <div class="fcol fcol--1">
      <div class="box box-1"></div>
      <div class="box box-2"></div>
    </div>
    <div class="fcol fcol--2">
      <div class="box box-1"></div>
      <div class="box box-1"></div>
      <div class="box box-1"></div>
    </div>
    <div class="fcol fcol--3">
      <div class="box box-2"></div>
      <div class="box box-1"></div>
    </div>
  </div>
</section>

还有 CSS:

.fcontainer {
  display: flex;
  align-items: stretch;
  background-color: #f1f1f1;
  padding: 10px 30px;

  .fcol {
    color: #333;
    margin: 1rem;
    text-align: center;
    font-size: 3rem;
    display: flex;
    flex-direction: column;
    height: 60vh;
    flex-grow: 4;
  }
}

.box {
  margin-top: 10px;
  margin-bottom: 10px;

  &-1 {
    background: lightblue;
    flex: 0 0 33.33%;
    max-height: calc(33.33% - 20px);
  }

  &-2 {
    background: lightgreen;
    flex: 0 0 66.67%;
    max-height: calc(66.67% - 20px);
  }
}

【讨论】:

  • 是的先生,这正是我想要做的 :),如果您也可以考虑问题的第二部分,我将不胜感激,这与在每个框中放置图像有关,避免图像溢出外部它的。不过还是非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 2021-03-21
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
相关资源
最近更新 更多