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

CSS:僅當存在較晚的兄弟時才選擇元素

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

問題描述

在我的 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; 僅當 <aside> 存在.默認為 section { max-width: 1000px;}(當 <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)

不同于 一個標簽的選擇器,后跟另一個標簽;用戶 [提出問題] 想要始終設(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")

  • 僅當 <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>是否達到您想要的效果>. 如果那里有任何其他元素,這將不起作用.在您的情況下,它應(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:僅當存在較晚的兄弟時才選擇元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 錯誤:當使用 JS 刪除項目時,first-child 不更新 display:block)
nth-Child CSS selectors(nth-子 CSS 選擇器)
Using same ID for multiple HTML tags?(對多個 HTML 標簽使用相同的 ID?)
主站蜘蛛池模板: 亚洲精品免费看 | 91精品免费 | 热久久免费视频 | 亚洲免费专区 | 97色综合 | 午夜美女福利视频 | 亚洲成人精品在线观看 | 亚洲超碰在线 | 日本天堂网 | 亚洲一区自拍 | 精品少妇一区二区三区免费观 | av黄色在线观看 | 国产在线麻豆精品观看 | 亚洲精品区 | 欧美日韩四区 | 亚洲精品1 | 亚洲视频一区二区三区 | 香蕉一区二区 | 国产三级在线免费观看 | 国产香蕉av | 日韩av免费看 | 国产日韩久久 | 谁有毛片网址 | 国产高清视频一区 | 久久av一区二区三区亚洲 | 综合99 | 人人爽爽人人 | 日本伊人 | h片在线免费观看 | 国产精品原创 | 永久免费看mv网站入口亚洲 | 成人在线免费视频 | 亚洲国产精品久久久久 | 亚洲精品福利视频 | 国产欧美日韩在线 | 国产精品一区在线播放 | 97超碰资源 | 欧美亚洲一区二区三区 | 欧美一级黄 | 日韩欧美国产精品 | 国产精品一区二区三区四区五区 |