CSS中的对角堆栈效果

job*_*208 7 css

我正在尝试在CSS中创建一堆扑克牌,其中每张牌都与前一张牌对角略微偏移.这是它的样子:

.card {
    float: left;
    width: 100px;
    height: 140px;
    background-color: #fff;
    border: 1px solid #000;
    border-radius: 5px;
}
.card:nth-child(2) {
    margin-left: -98px;
    margin-top: -2px;
}
.card:nth-child(3) {
    margin-left: -98px;
    margin-top: -4px;
}
.card:nth-child(4) {
    margin-left: -98px;
    margin-top: -6px;
}

/* and so on... */
Run Code Online (Sandbox Code Playgroud)

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

我知道我可以通过为每张卡指定不同的边距来实现,但我想知道是否有更好的方法.

创建纯粹的水平偏移很容易:

.card {
    float: left;
    width: 100px;
    height: 140px;
    background-color: #fff;
    border: 1px solid #000;
    border-radius: 5px;
}
.card:not(:first-child) {
    margin-left: -98px;
}
Run Code Online (Sandbox Code Playgroud)

纯粹垂直也很容易.但有没有办法只用几条CSS规则来获得对角线偏移?

mop*_*922 4

这有点像黑客,但如果您使用提供的第二个选项,您最终会得到这种效果:

.card:not(:first-child)
Run Code Online (Sandbox Code Playgroud)

并在每张卡后面加上<br>

<div>
    <div class=card></div><br>
    <div class=card></div><br>
    <div class=card></div><br>
    <div class=card></div><br>
</div>
Run Code Online (Sandbox Code Playgroud)

JSFiddle: http://jsfiddle.net/e4o0k2o5/

line-height如果您使用 a或 s 以外的其他东西,您可能可以对其进行微调<br>