选择CSS中的前三个元素并重复

时间:2016-02-01 10:16:01

标签: html css css3 css-selectors css-transitions

我有一张N卡的列表,浮动,width:33.3333%。当用户点击卡片时,它会旋转并展开。

但是,我希望每个3:rd卡从1:st开始有一个margin,每3:rd卡从2开始:另一个margin和每3: rd从3:rd开始了另一个margin

有没有办法用 CSS 选择器,例如子选择器?

Plunker demo

示例:

<!DOCTYPE html>
<html>

  <head>
    <script data-require="jquery@*" data-semver="2.1.4" src="https://code.jquery.com/jquery-2.1.4.js"></script>
    <link href="style.css" rel="stylesheet" />
    <script src="script.js"></script>
  </head>

  <body class="landing">
    <section id="intro">
      <div class="inner">
        <h2>Title</h2>
        <!-- Cards -->
        <div class="cards">
          <div class="riga">
            <div class="r2">
              <div class="r4">
                <div class="card-container manual-flip">
                  <div onclick="rotateCard(this)" class="card">
                    <div class="front">
                      <div class="cover">
                        <img src="img/rotating_card_thumb2.png" />
                      </div>
                      <div class="user">
                        <img src="img/img1.png" class="img-circle" />
                      </div>
                      <div class="content">
                        <div class="main">
                          <h3 class="name">Text</h3>
                          <p class="profession">Text</p>
                          <a onclick="rotateCard(this).disable();" class="actions button rimmed" href="#">Text</a>
                          <a onclick="rotateCard(this).disable();" class="actions button rimmed" href="#">Text</a>
                        </div>
                        <div class="footer">
                          <button class="btn btn-simple">
                                                    Info
                                                </button>
                        </div>
                      </div>
                    </div>
                    <div class="back">
                      <div class="header">
                        <h5 class="motto">Text</h5>
                      </div>
                      <div class="content">
                        <div class="main">
                          <h4 class="text-center">Info</h4>
                        </div>
                      </div>
                      <div class="footer">
                        <button class="btn btn-simple">
                                                Back  
                                            </button>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="r4">
                <div class="card-container manual-flip">
                  <div onclick="rotateCard(this)" class="card">
                    <div class="front">
                      <div class="cover">
                        <img src="img/rotating_card_thumb.png" />
                      </div>
                      <div class="user">
                        <img src="img/img2.png" class="img-circle" />
                      </div>
                      <div class="content">
                        <div class="main">
                          <h3 class="name">Text</h3>
                          <p class="profession">Text</p>
                          <a onclick="rotateCard(this).disable();" class="actions button rimmed" href="#">Text</a>
                          <a onclick="rotateCard(this).disable();" class="actions button rimmed" href="#">Text</a>
                        </div>
                        <div class="footer">
                          <button class="btn btn-simple">
                                                   Info
                                                </button>
                        </div>
                      </div>
                    </div>
                    <div class="back">
                      <div class="header">
                        <h5 class="motto">Text</h5>
                      </div>
                      <div class="content">
                        <div class="main">
                          <h4 class="text-center">Info</h4>
                        </div>
                      </div>
                      <div class="footer">
                        <button class="btn btn-simple">
                                                Back  
                                            </button>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="r4">
                <div class="card-container manual-flip">
                  <div onclick="rotateCard(this)" class="card">
                    <div class="front">
                      <div class="cover">
                        <img src="img/rotating_card_thumb2.png" />
                      </div>
                      <div class="user">
                        <img src="img/img3.png" class="img-circle" />
                      </div>
                      <div class="content">
                        <div class="main">
                          <h3 class="name">Text</h3>
                          <p class="profession">Text</p>
                          <a onclick="rotateCard(this).disable();" class="actions button rimmed" href="#">Text</a>
                          <a onclick="rotateCard(this).disable();" class="actions button rimmed" href="#">Text</a>
                        </div>
                        <div class="footer">
                          <button class="btn btn-simple">
                                                    Info
                                                </button>
                        </div>
                      </div>
                    </div>
                    <div class="back">
                      <div class="header">
                        <h5 class="motto">Text</h5>
                      </div>
                      <div class="content">
                        <div class="main">
                          <h4 class="text-center">Info</h4>
                        </div>
                      </div>
                      <div class="footer">
                        <button class="btn btn-simple">
                                                Back  
                                            </button>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- end inner -->
    </section>
  </body>

</html>

3 个答案:

答案 0 :(得分:1)

您可以使用CSS :nth-child()

&#13;
&#13;
span {
  float: left;
  color: white;
  text-align: center;
  margin-top: 5px;
  width: calc(33% - 60px);
}

span:nth-child(3n+1) {
  background-color: green;
  margin-left: 20px;
}
span:nth-child(3n+2) {
  background-color: red;
  margin-left: 40px;
}
span:nth-child(3n+3) {
  background-color: blue;
  margin-left: 60px;
}
&#13;
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

是的,你可能可以。

试试这个:

.yourclass:nth-child(3n+1){
  //child 1, 4, 7....
  margin-left:10px;
}
.yourclass:nth-child(3n+2){
   //child 2,5,8....
   margin-left:20px;
 }
.yourclass:nth-child(3n+3){
    //child 3,6,9....
    margin-left:30px;
 }

答案 2 :(得分:0)

假设您拥有以下HTML和CSS,您将获得所需内容:

&#13;
&#13;
/* Global Setting for columns */
.col {
    float: left;
    text-align: center;
}

/* Different margin for columns. */
.col:nth-child(3n-2) {
	background-color: red;
    clear: left;

    border: 1px solid #000;
    margin: 0 7.5px;
    width: calc(33.33333333% - 2px - 15px); /* size + border + margin */
}
.col:nth-child(3n-1) {
	background-color: green;

    border: 1px solid #000;
    margin: 0 15px;
    width: calc(33.33333333% - 2px - 30px); /* size + border + margin */
}
.col:nth-child(3n) {
	background-color: blue;

    border: 1px solid #000;
    border-left: 0;
    border-right: 0;
    width: 33.33333333%; /* Only size */
}
&#13;
<div class="container">
	<div class="row">
		<div class="col">1</div>
		<div class="col">2</div>
		<div class="col">3<br>3</div>
		<div class="col">1</div>
		<div class="col">2<br>2</div>
		<div class="col">3</div>
		<div class="col">1<br>1</div>
		<div class="col">2</div>
		<div class="col">3</div>
		<div class="col">1</div>
		<div class="col">2</div>
		<div class="col">3</div>
	</div>
</div>
&#13;
&#13;
&#13;