css nth-child或nth-of-type问题

时间:2016-03-07 14:55:22

标签: html css css-selectors

问题在于news4 div的宽度为30%。它必须是50%。看起来nth-of-type也会计算.advertisement div和news4 div成为第五个子元素。 nth-of-typenth-child也是如此。 我的html示例是:

<div class="container">
    <div class="news">news1</div>
    <div class="news">news2</div>
    <div class="news">news3</div>
    <div class="advertisement">Advertisement</div>
    <div class="news">news4</div>
    <div class="news">news5</div>
    <div class="news">news6</div>
</div>

和css:

.container { width: 100%; }
.news:nth-of-type(3n+1) { width: 50%; }
.news:nth-of-type(3n+2) { width: 30%; }
.news:nth-of-type(3n+3) { width: 20%; }

1 个答案:

答案 0 :(得分:1)

ERROR_REPORTING(E_ALL); require_once('../MailQueue.class.php'); $queue = new MailQueue(); $currentTime = date('YmdHis'); $try = 50; $data = Array(); for($i=1; $i<=3; $i++) { $data[$i] = Array(); $data[$i]['sendAt'] = date('Y-m-d H:i:s'); $data[$i]['fromName'] = 'John Doe '.$i; $data[$i]['fromAddress'] = 'john@doe.com'; $data[$i]['toName'] = 'Jane Doe'; $data[$i]['toAddress'] = 'jane@doe.com'; $data[$i]['subject'] = 'Test message MailQueue '.$currentTime; $data[$i]['body'] = 'Test message MailQueue '.$currentTime; } // Directly process the added messages echo "Trying to send ".$try." emails.<br />"; $counter = $queue->processQueue($try, $queue->add($data)); echo "Added 3 new emails to be sent. ".$counter." emails were actually sent."; 是指元素标记(nth-of-type),而不是类名(div)。

如果您想在.news之后定位.news项目,可以考虑以下选择器:

.advertisement