CSS 表格矩阵上的边框样式,无需额外的类

发布于 2024-12-12 03:39:25 字数 1004 浏览 0 评论 0原文

我正在仅使用 CSS 制作类似表格的布局。将所有单元格设置为 float:left 后,容器 .matrix 的宽度足以容纳 4 个水平单元格。

<div class="matrix">
    <!-- first row -->
    <div class="cell">Cell contents</div>
    <div class="cell">Cell contents</div>
    <div class="cell">Cell contents</div>
    <div class="cell right">Cell contents</div>
    <!-- last row -->
    <div class="cell last">Cell contents</div>
    <div class="cell last">Cell contents</div>
    <div class="cell last">Cell contents</div>
    <div class="cell last right">Cell contents</div>
</div>

为了控制边框样式的应用位置,我使用 .last.right 类来省略边缘上的边框样式:

.cell {
    border-bottom:1px solid red;
    border-right:1px solid red;
}
.right {
    border-right:none;
}
.last {
    border-bottom:none;
}

我想知道是否有人有一种技术我实现了相同的结果/逻辑 - 但不需要额外的类。

感谢您的帮助!

I'm making a table-like layout using only CSS. With all the cells set to float:left the container .matrix is wide enough for 4 cells horizontally.

<div class="matrix">
    <!-- first row -->
    <div class="cell">Cell contents</div>
    <div class="cell">Cell contents</div>
    <div class="cell">Cell contents</div>
    <div class="cell right">Cell contents</div>
    <!-- last row -->
    <div class="cell last">Cell contents</div>
    <div class="cell last">Cell contents</div>
    <div class="cell last">Cell contents</div>
    <div class="cell last right">Cell contents</div>
</div>

To control where the border-styles are applied I use .last and .right classes to omit border-styles on the edges:

.cell {
    border-bottom:1px solid red;
    border-right:1px solid red;
}
.right {
    border-right:none;
}
.last {
    border-bottom:none;
}

I'm wondering if anyone has a technique where I achieve the same result/logic - but without the need for the extra classes.

Thanks for your assistance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

强者自强 2024-12-19 03:39:25

有一种方法,但是它使用了一些旧浏览器无法理解的选择器。您的解决方案是最安全的解决方案,因此它是最常见的解决方案。

CSS3中有很多新的选择器,其中一些可以解决您的问题。这是一个有趣的列表 有用的选择器

There is a way, however it uses some selectors which older browsers can not understand. Your solution is the safest one, therefore it is the one most common.

There is a plenty of new selectors in CSS3, some of them can solve your problem. Here is an interesting list of useful selectors

傲世九天 2024-12-19 03:39:25

这是使用 CSS :nth-child 选择器的方法

.matrix{
    width:400px;
}

.cell {
    border-bottom:1px solid red;
    border-right:1px solid red;
    float:left;
    padding:.5em;
}

.cell:nth-child(n+5):nth-child(-n+8){
    border-bottom:none;
}

.cell:nth-child(4), .cell:nth-child(8){
    border-right:none;
}

示例:http://jsfiddle .net/KBMvL/

This is how you would do it using CSS :nth-child selectors

.matrix{
    width:400px;
}

.cell {
    border-bottom:1px solid red;
    border-right:1px solid red;
    float:left;
    padding:.5em;
}

.cell:nth-child(n+5):nth-child(-n+8){
    border-bottom:none;
}

.cell:nth-child(4), .cell:nth-child(8){
    border-right:none;
}

Example: http://jsfiddle.net/KBMvL/

玩心态 2024-12-19 03:39:25

如果您在行周围使用包装元素,则可以使用 CSS :last-child 来处理右侧的单元格和底部的单元格。

我假设您的包装元素也将是

.cell {
    border-bottom:1px solid red;
    border-right:1px solid red;
}

.matrix>div:last-child>.cell, .matrix>div>.cell:last-child {
    border-bottom:none;
}

但请注意,IE8 及更早版本不支持 :last-child

您可能会更幸运地设置相反的边框(即顶部和左侧),并使用 :first-child 来清空最左侧和最顶部的单元格,因为 :first-child > IE7 和 IE8 支持。最终结果将完全相同。

您可以在此处找到有关浏览器对 CSS 选择器支持的更多信息:http://quirksmode.org/css/contents。 html

如果您需要使用 :last-child,并且您需要支持 IE7/8,那么您也许可以通过使用 javascript 库(例如 选择性

然而,毕竟,我还要说,鉴于我为行添加的包装器元素,我们最终得到与 HTML 表相同的基本结构。那么问题是为什么不简单地使用表格呢?然后您可以使用 border-collapse 来实现类似的结果。

关于最后一点,我知道您将“无表”作为问题的标签,所以我假设您正在研究无表设计。但是,如果数据本质上确实是表格形式,那么使用表格就没有问题。无表格设计旨在阻止人们使用表格进行任意页面布局,但在正确的上下文中使用表格仍然完全有效。问题中的描述确实听起来很像您的示例属于此类。

希望有帮助。

If you use a wrapper element around the rows, then you can use CSS :last-child to deal with both the cells on the right and the ones on the bottom.

I'll assume your wrapper element is also going to be a <div>:

.cell {
    border-bottom:1px solid red;
    border-right:1px solid red;
}

.matrix>div:last-child>.cell, .matrix>div>.cell:last-child {
    border-bottom:none;
}

But note that :last-child is not supported by IE8 and earlier.

You might have more luck setting the opposite borders (ie top and left), and using :first-child instead to blank out the leftmost and topmost cells, because :first-child is supported by IE7 and IE8. The end result would be exactly the same.

You can find out more about browser support for CSS selectors here: http://quirksmode.org/css/contents.html

If you need to use :last-child, and you need to support IE7/8, then you might be able to get it working by using a javascript library such as Selectivizr.

However after all that, I would also say that given the wrapper element I've added for the rows, we end up with the same basic structure as an HTML table. So the question is why not simply use a table? Then you can use border-collapse to achieve similar results.

Regarding this last point, I know you put 'tableless' as a tag on the question, so I assume you're working to a table-less design. However, if the data is indeed tabular in nature, then there's nothing wrong with using a table. Table-less design is intended to stop people using tables for arbitrary page layout, but tables are still perfectly valid when used in the right context. The description in the question does make it sound very much as if your example falls into this category.

Hope that helps.

倾其所爱 2024-12-19 03:39:25

我想我找到了一个不使用额外的类、CSS3 选择器或包装元素的解决方案。通过给容器一个 overflow:hidden ,并像这样稍微移动单元格:

.matrix{
    width:400px;
    overflow:hidden;
}

.cell {
    width:100px;
    float:left;
    border-left:1px solid #ccc;
    border-bottom:1px solid #ccc;
    position:relative;
    bottom:-1px;
    padding:5px;
    left:-1px;
}

请参阅此处的示例:http://jsfiddle.net/KBMvL/5/

I think I found a solution without using extra classes, CSS3 selectors or wrapper elements. By giving the container an overflow:hidden, and shifting the cells slightly like so:

.matrix{
    width:400px;
    overflow:hidden;
}

.cell {
    width:100px;
    float:left;
    border-left:1px solid #ccc;
    border-bottom:1px solid #ccc;
    position:relative;
    bottom:-1px;
    padding:5px;
    left:-1px;
}

See example here: http://jsfiddle.net/KBMvL/5/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文