I've been struggling with CSS for a couple of hours in order to make a perfect spaced dotted line for my table. I've tried the border property, creating a image and put it as background, repeating it in Y axis, and some other stuff (even in CSS3), but my case is a little more specific than the ones I found out by searching in Google: I have alternated columns and rows classes in my table, and it seems that I can't define the whole row with a continuous border.
为了为我的表创建一个完美的间隔虚线,我花了几个小时与CSS做斗争。我试过边界属性,创建一个图像作为背景,在Y轴重复它,和其他一些东西(即使在CSS3),但是我的情况是一个更具体的我发现通过搜索在谷歌:我有交替行和列类在我的桌子,我似乎不能定义整个行用一个连续的边界。
My CSS:
我的CSS:
th { height: 42px; font-weight: bold; font-size: 15px; vertical-align:bottom; padding: 8px 16px; margin-bottom: 5px; border-bottom: 2px dotted #999; }
th.odd { background-color: #e6e6e6; }
th.even { background-color: #ddd; }
td { padding: 16px; }
tr{ border-bottom: 2px dotted #999; }
tr.even { background-color: #eee; }
tr.even td.even { background-color: #e2e2e2; }
tr.odd td.even { background-color: #eaeaea; }
I've tried also to change the "border-bottom" ones to a background-image with dots and spaces, as I mentioned before.
就像我之前提到的,我也尝试过把“边底”改成带有点和空格的背景图像。
What I have today is this:
今天我要讲的是:
And I want the the dots to be continuous as this: . . . . . . . . . . . .
我希望这些点是连续的:……
4
Disclaimer: This is not a simple solution and is rather complex to understand but if you really want to produce continuous dots then this is the only approach that I can think of. I wouldn't recommend adding so much complexity for what is a small aberration with the borders but I'd leave it to you.
声明:这不是一个简单的解决方案,它是相当复杂的,但是如果你真的想要产生连续的点,那么这是我能想到的唯一方法。我不建议为边界上的一个小异常增加这么多的复杂性,但是我把它留给你们。
The approach for creating the continuous border is very simple:
创建连续边界的方法非常简单:
table
itself using radial-gradient
images. Since table
is the parent container, the dots stretch continuously in a seamless manner.td
+ the padding
on either side. This is critical to the working.background-repeat
is set to round
so that it kind of always produces full dots. All these are critical to the solution.td
or the tr
as solid colors because they would hide the table
background and so the solution is to add them via linear-gradient
(except that the color does not change). This is done because the size of gradients can be controlled whereas that of solid color cannot be.table { /* to produce the dots via radial gradient */
background-image: radial-gradient(circle at 50% 50%, black 1px, transparent 2px);
background-size: 8px 50px; /* size in y-axis is equal to td height + padding top + padding bottom */
background-position: 2px -27px; /* position in y-axis is -1 * size/2 - 2px */
background-repeat: round; /* makes dots repeat in round manner */
border-collapse: collapse;
}
td {
vertical-align: bottom;
height: 46px;
padding: 2px;
}
tr:nth-child(even) {
background-image: linear-gradient(#eee, #eee);
background-size: 100% 46px; /* size in y-axis is height of td */
background-repeat: no-repeat;
}
tr:nth-child(even) td:nth-child(even) {
background-image: linear-gradient(#e2e2e2, #e2e2e2);
background-size: 100% 46px;
background-repeat: no-repeat;
}
tr:nth-child(odd) td:nth-child(even) {
background-image: linear-gradient(#eaeaea, #eaeaea);
background-size: 100% 46px;
background-repeat: no-repeat;
}
Hello World
Hello World
Hello World
Hello World!!!
Hello World!!!
Hello World!!!
Hello World
Hello World
Hello World
Hi There!!!
Hi There!!!
Hi There!!!
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor
Lorem Ipsum Dolor
Lorem Ipsum Dolor
Lorem Ipsum Dolor Sit
Lorem Ipsum Dolor Sit
Lorem Ipsum Dolor Sit
Lorem Ipsum
Lorem Ipsum
Lorem Ipsum
As shown in the fiddle that you had provided in comments, the whole thing becomes a lot simpler if all the td
will have some solid color as their background (either white or other shades of gray). In such cases, we don't need the extra linear-gradient
backgrounds for the td
or the other background-*
properties. This approach would work even when the dimensions of the tr
and td
are not fixed.
如您在评论中提供的小提琴所示,如果所有的td都有一些纯色作为背景(白色或其他灰色),那么整个事情就会变得简单得多。在这种情况下,我们不需要额外的线性梯度背景为td或其他背景-*属性。即使tr和td的尺寸不是固定的,这种方法也可以工作。
table {
border-collapse: collapse;
border-spacing: 0;
margin: 12px;
font-family: Arial;
color: #333;
font-size: 13px;
background-image: radial-gradient(circle at 50% 50%, #999 1px, transparent 1px);
background-size: 4px 2px;
background-repeat: round;
}
td {
padding: 16px;
border-bottom: 2px solid transparent;
}
tr.odd td.odd {
background: #fff padding-box; /* the padding-box property is to prevent the background from being present under the 2px transparent border area */
}
tr.even td.odd {
background: #eee padding-box;
}
tr.even td.even {
background: #e2e2e2 padding-box;
}
tr.odd td.even {
background: #eaeaea padding-box;
}
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum
Dolor Sit Amet
Lorem Ipsum
Dolor
Sit
Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem
Ipsum
Dolor
Sit
Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
4
Try setting 尝试设置边框-左:1px固体白色,以分离两个点。但是使用border-left : 1px solid white
to separate double dots. But create the table using .row {
border-bottom-style: dotted;
border-width: 1px;
height: 20px;
}
.columnG {
background-color: gray;
float: left;
width: 50%;
height: 20px;
}
.columnW {
background-color: white;
float: left;
width: 50%;
height: 20px;
}
2
Is the demo I'm using radial gradient
to make the dots. You can "play" with the range
control at the bottom do find the exact result.
是我用径向梯度绘制点的demo。您可以“播放”与范围控制在底部的确定结果。
$('input').on('input', function(){
$('tr').css('background-size', $(this).val() + 'px 4px');
$('#num').html($(this).val());
});
table {
width: 400px;
border-spacing: 0;
}
th {
text-align: left;
}
tr {
background: radial-gradient(circle at bottom, black 1px, transparent 1.5px) repeat-x bottom;
background-size: 5px 4px;
}
tr:first-child {
font-weight: bold;
}
tr:nth-child(odd) {
background-color: #eee;
}
td {
padding: 5px;
/*border-bottom:1px dotted #aaa;*/
/*background: radial-gradient(circle at bottom, black 1px, transparent 1.5px) repeat-x bottom;
background-size: 5px 4px;*/
}
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
background-size: 5px 4px;