【问题标题】:How to fit a flex container inside a grid container如何在网格容器内安装弹性容器
【发布时间】:2018-10-18 01:44:05
【问题描述】:

我正在尝试在网格容器内的两侧都有边距的 flex 容器中进行调整,但是在最小的视口宽度处,flex 容器会泄漏出来。我为各个 div 添加了边框,以显示其泄漏的位置。

我还添加了一个演示来展示我的问题:https://codesandbox.io/s/9z49m3ny0p

如果演示不清楚,请查看以下屏幕截图:

正如您从上面的屏幕截图中看到的那样,红色边框始终保持在视口内,但蓝色边框的容器溢出。

Home.js

<div id="rightContainer">
    <div id="editorContainer">
        <Notepad />
    </div>
    <div id="notesCardContainer">
        <NoteCard />
        <NoteCard />
    </div>
</div>

Home.css

#editorContainer {
  display: grid;
  width: 100%;
  position: relative;
  border: 1px solid red;
}
#notesCardContainer {
  display: grid;
  width: 100%;
  grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
  flex-direction: row;
  justify-content: space-evenly;
  padding-bottom: 60px;
}

还有 Notepad.js

<div className="editorInnerContainer">
    <div className="editorTop">
        <input
         type="text"
         id="noteTitle"
         value="Some Title"
         disabled={true}
        />
        <div id="tools">
            <a href="" title="Edit this note">
                <i className="fas fa-pencil-alt" />
            </a>
            <a href="" title="Delete this note">
                <i className="fas fa-trash" />
            </a>
            <a href="" title="Download this note">
                <i className="fas fa-download" />
            </a>
            <a href="" title="Share this note">
                <i className="fas fa-share-square" />
            </a>
        </div>
    </div>
</div>

来自 Notepad.css 的片段

.editorInnerContainer {
  display: flex;
  flex-direction: column;
  background-color: var(--white-100);
  padding: 20px;
  margin-left: 20px;
  margin-right: 20px;
  margin-top: 50px;
  border-radius: 5px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.14);
  border: 1px solid blue;
}

【问题讨论】:

    标签: reactjs css flexbox css-grid


    【解决方案1】:

    看看这个: https://codesandbox.io/s/2v0o67w4kn

    #noteTitle {
        flex: 1;
        height: 60px;
        font-size: 18px;
        line-height: 17px;
        font-weight: 700;
        border: none;
        color: var(--black);
        border: 2px solid sandybrown;
    
        /* add width */
        width: 100%;
        max-width: 100%;
    }
    

    如果您还想调整边框问题,请将宽度设置为 100% - 边框大小通过计算相加:https://codesandbox.io/s/l7rk0zop69

    #noteTitle {
        width: calc(100% - 6px);
        max-width: calc(100% - 6px);
    }
    

    【讨论】:

    • 太棒了,第一个在 FF 上正常工作,但在 Chrome 中一直存在问题。我必须在调整大小后手动刷新页面才能正确获取它。
    • 奇怪,你的意思是当你调整编辑器预览窗口的大小时?我在chrome中没有这个问题。但是,当我刚刚检查时,它似乎用 4px 而不是 6 计算边框(这也更有意义,因为两者看起来都是 2px 宽)。
    • 是的,我在我的浏览器上测试它,而不是在沙箱上。仅在 chrome 上,当我加载页面时,一切都很好,我调整了它的大小,红色边框及其内容向右移动了一点。我刷新了它,它又回到了视口中。
    • 多么奇怪的问题。您是否尝试过将 box-sizing:border-box 添加到盒子中?
    • 我可以使用 chrome 上的最大化按钮重现您的问题,但不能用于手动调整大小。我建议为此特定问题打开一个新问题,也许其他人可能对此问题有解决方案或一些意见:)
    猜你喜欢
    • 2022-08-15
    • 2012-11-04
    • 2023-02-10
    • 2021-10-21
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多