iT邦幫忙

0

CSS進階選取

css
  • 分享至 

  • twitterImage

如題 例如我要選取最前兩個跟最後兩個P該如何打

<p>第一个段落。</p>
<p>第二个段落。</p>
<p>第三个段落。</p>
<p>第四个段落。</p>
<p>第五个段落。</p>
<p>第六个段落。</p>

目前試過
nth-last-of-type(-n+2)是選倒數兩個
nth-last-of-type(4n+1)在五個的情況下是選第一個跟最後一個
那我要用一行選取最前兩個跟最後兩個要打?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
paicheng0111
iT邦大師 5 級 ‧ 2018-11-22 12:08:10
p:first-of-type, p:first-of-type+p, p:nth-last-of-type(-n+2) {color:red}

這用了三個選擇器 有沒有更簡短一點的?

5
CSScoke
iT邦新手 3 級 ‧ 2018-11-22 14:51:59
p:nth-of-type(-n+2),
p:nth-last-of-type(-n+2){
   color:#f00;
}

我要發表回答

立即登入回答