久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

CSS:僅當(dāng)存在較晚的兄弟時才選擇元素

CSS: Select element only if a later sibling exists(CSS:僅當(dāng)存在較晚的兄弟時才選擇元素)
本文介紹了CSS:僅當(dāng)存在較晚的兄弟時才選擇元素的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在我的 HTML 結(jié)構(gòu)中,我是這樣設(shè)置的:

In my HTML structure, I have it set up like this:

<body>
   <main>
      <section>
      ...
      </section>

      <aside>
      ...
      </aside>
   </main>
</body>

問題是,并不是所有頁面都有<aside>

The problem is, not all pages have <aside>

我需要選擇 <section> 并給它一個 max-width: 500px; 僅當(dāng) <aside> 存在.默認(rèn)為 section { max-width: 1000px;}(當(dāng) <aside> 不存在時)

I need to select <section> and give it a max-width: 500px; ONLY when <aside> is present. The default is section { max-width: 1000px; } (when <aside> is absent)

不同于 一個標(biāo)簽的選擇器,后跟另一個標(biāo)簽;用戶 [提出問題] 想要始終設(shè)置B"樣式.此外,在這個問題中,用戶想要選擇B"(而不是A")

Unlike in Selector for one tag directly followed by another tag; the user [asking the question] wants to style "B" ALL the time. Also, in this question, the user wants to select "B" (not "A")

  • 僅當(dāng) <aside> 存在時,我才需要設(shè)置 <section> 樣式.
  • 我無法更改 HTML 的順序 >_<
  • 只能用 CSS 完成嗎?
  • 我需要什么選擇器或如何設(shè)置它?
  • 如果不能用 CSS 完成(我寧愿它只是 CSS),我該如何完成?
  • I need to style <section> ONLY if <aside> is present.
  • I can't change the order of the HTML >_<
  • Can it be done with CSS only?
  • What selector do I need or how to set it up?
  • If it can't be done with CSS (I rather it be CSS-only), how can I accomplish this?

推薦答案

一個巧妙的小技巧

如果 <section> 元素是 <main>是否達(dá)到您想要的效果>. 如果那里有任何其他元素,這將不起作用.在您的情況下,它應(yīng)該像這樣工作(http://jsfiddle.net/Ljm323qb/2/):

section {
     max-width: 500px;
}
/* The STAR of the show */
section:only-child {
     max-width: 1000px;
}

如本 Codepen 所示:http://codepen.io/omarjuvera/pen/ByXGyK?editors=110

As illustrated in this codepen: http://codepen.io/omarjuvera/pen/ByXGyK?editors=110

+ 選擇器會選擇緊跟在元素之后的兄弟(https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors)

There's the + selector which would select a sibling that comes right after the element (https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors)

還有 ~ 選擇器,它選擇所有以下兄弟姐妹(https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors)

And there's the ~ selector which selects all following siblings (https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors)

您可以通過將 <aside> 元素放在 <section> 元素之前并使用同級選擇器來實現(xiàn).

You could achieve it by putting the <aside> element before the <section> element and using a sibling selector.

這是一個例子:http://jsfiddle.net/Ljm323qb/1/

未來概覽
很快這將成為可能,使用一個新的 :has 偽類 (http://dev.w3.org/csswg/selectors-4/#relational)
您將能夠調(diào)用類似 main:has(>side) >{ ... } 部分,但不幸的是,我們將不得不等待 :(

A quick look in the future
Soon this will be possible, with a new :has pseudo class (http://dev.w3.org/csswg/selectors-4/#relational)
You'll be able to call something like main:has(> aside) > section { ... } but we'll have to wait for that, unfortunately :(

這篇關(guān)于CSS:僅當(dāng)存在較晚的兄弟時才選擇元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

CSS selector when :target empty(:target 為空時的 CSS 選擇器)
Does the CSS direct decendant (gt;) not have any value in selectivity?(CSS 直接后代 (gt;) 在選擇性方面沒有任何價值嗎?)
Using querySelectorAll(). Is the result returned by the method ordered?(使用 querySelectorAll().方法返回的結(jié)果是否有序?)
Safari bug :first-child doesn#39;t update display:block when items are removed with JS(Safari 錯誤:當(dāng)使用 JS 刪除項目時,first-child 不更新 display:block)
nth-Child CSS selectors(nth-子 CSS 選擇器)
Using same ID for multiple HTML tags?(對多個 HTML 標(biāo)簽使用相同的 ID?)
主站蜘蛛池模板: 97国产一区二区精品久久呦 | 国产精品99久久久久 | 中文字幕日韩欧美一区二区三区 | 色综合久久天天综合网 | 久久精品国产v日韩v亚洲 | 91亚洲精华国产 | 亚洲国产欧美在线 | 瑟瑟免费视频 | 狠狠干天天干 | 99精彩视频 | 亚洲精品一区二区 | 日日日视频 | 日韩在线精品视频 | 99久久精品免费看国产四区 | 一级a性色生活片久久毛片 午夜精品在线观看 | 青草久久免费视频 | 久久久久国 | 欧美日韩国产中文 | 久久久av | 91在线精品一区二区 | 紧缚调教一区二区三区视频 | 香蕉一区二区 | 国产成人精品久久久 | 成人三级视频在线观看 | 日日操夜夜操天天操 | 日日摸天天添天天添破 | 久久精品国产一区二区电影 | 日日欧美 | 国产欧美在线 | 国产在线a视频 | 国产高清一区二区三区 | 久久精点视频 | 欧美久久精品一级黑人c片 91免费在线视频 | av毛片 | 国产美女特级嫩嫩嫩bbb片 | 日韩午夜在线播放 | 91av在线免费观看 | 日韩精品四区 | 韩日在线观看视频 | 免费精品久久久久久中文字幕 | 欧美成年网站 |