【问题标题】:CSS Grid two rows nested in a single columnCSS Grid 两行嵌套在一个列中
【发布时间】:2019-05-09 03:09:02
【问题描述】:

我正在寻找一种方法,允许单列中有两行,而其右侧的其他两列与这两行的高度相等/灵活。查看所有三列时,宽度应为 100%(因此每列约为 33%)。这是我希望它的外观的示例:

https://i.imgur.com/lLPzXhS.png

我将用可点击的框填充这些框,如下所示:

https://i.imgur.com/uyyDbL7.png

我尝试过使用 display: row, display: cell,但我无法为其添加任何边距,所以这是我得到的产品:

https://i.imgur.com/Ok6EgT0.png

您可以看到我已经设置了一些网格系统,但并没有我想要的那么理想。红色和橙色框之间没有可以设置的边距,即使我可以为绿松石色和蓝色框添加边距。

这是我的代码:

HTML:

<div class='d-table'>
    <div class='t-row'>
        <div class='t-cell one-third'>
            <div class='turqoisebox'>
                Turqoise box here
            </div>
            <div class='bluebox'>
                Blue box here
            </div>
        </div>
        <div class='t-cell one-third redbox'>
            Red box here
        </div>
        <div class='t-cell one-third orangebox'>
            Orange box here
        </div>
    </div>
</div>

CSS:

.d-table {
    display: table;
}

.t-row {
    display: table-row;
}

.t-cell {
    display: table-cell;
    margin-left: unset;
    margin-right: unset;
    /*border: 1px solid tomato;*/
}

.one-third {
    width: 30%;
}

.two-thirds {
    width: 200px;
}

.bluebox {
    background-color: #9dd8dd;
    margin: 25px;
    border-radius: 25px;
    border: solid #7dacb0;
    border-width: 3px;
    box-shadow: 2px 4px 8px 2px rgba(0,0,0,0.4);
    transition: 0.3s;
    text-align: center;
}

    .bluebox:hover {
        box-shadow: 2px 8px 16px 2px rgba(0,0,0,0.8);
    }

关于如何巧妙地复制第二张图像结果有什么想法吗?

【问题讨论】:

  • 为什么不使用 CSS 网格,正如您自己的标题所暗示的那样?

标签: javascript html css


【解决方案1】:

您可以使用flexbox。看看这个简化的例子:

.content {
    background: orange;
    margin: 1rem;
    padding: 1rem;
    flex: 1;
    color: white;
    display: flex;
}

.content > span {
    margin: auto;
}

.row {
    display: flex;
    flex-direction: row;
    background-color: blue;
    flex: 1
}

.col {
    display: flex;
    flex-direction: column;
    flex: 1;
    background-color: red;
}
<div class="row">
    <div class="col">
        <div class="row">
            <div class="content">
                <span>This is centered</span>
            </div>
        </div>
        <div class="row">
            <div class="content">
                <span>This is centered</span>
            </div>
        </div>
    </div>

    <div class="col">
        <div class="content">
            <span>This is centered</span>
        </div>
        <div class="content">
            This is not
        </div>
        <div class="content">
            This is not
        </div>
    </div>

    <div class="col">
        <div class="content">
            This is not
        </div>
        <div class="content">
            This is not
        </div>
        <div class="content">
            This is not
        </div>
        <div class="content">
            <span>This is centered</span>
        </div>
    </div>
</div>

您还可以使用基于 flexbox 的最小网格库,例如 Flexbox Grid

【讨论】:

  • 这是最好的!还有一个问题,我希望垂直和水平对齐文本。我尝试添加css text-align: center; align-items: center; justify-content: center;,但效果不佳。有什么想法吗?
  • 您想在 div 中居中显示文本吗?
  • 是的。我将文本放置在第二张和第三张图片中显示的框中,但它与左上角对齐。当我使用text-align: center; 时,它会将文本水平放置在中心,但我也希望垂直放置。垂直居中是主要问题。再次感谢!
  • 我进行了编辑。我在.content 中添加了display: flex,并添加了一条新规则.content &gt; span { margin: auto; }
  • 成功了,这个问题就解决了。感谢您的帮助!
【解决方案2】:

边距用于设置元素的起始位置,因此请在这两个元素之间使用填充来获得所需的空间。

【讨论】:

  • 你应该至少提供一个你正在解释的例子
  • 是的,我无法理解你的想法。
  • 我在2所大学的路上回答了这个问题,我没有时间举个例子。
猜你喜欢
  • 2017-11-09
  • 1970-01-01
  • 2019-03-10
  • 2023-01-09
  • 2019-09-11
  • 2017-09-25
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
相关资源
最近更新 更多