not:first-child selector
One of the versions you posted actually works for all modern browsers (where CSS selectors level 3 are supported):
div ul:not(:first-child) {
background-color: #900;
}
If you need to support legacy browsers, or if you are hindered by the :not
selector's limitation (it only accepts a simple selector as an argument) then you can use another technique:
Define a rule that has greater scope than what you intend and then "revoke" it conditionally, limiting its scope to what you do intend:
div ul {
background-color: #900; /* applies to every ul */
}
div ul:first-child {
background-color: transparent; /* limits the scope of the previous rule */
}
When limiting the scope use the default value for each CSS attribute that you are setting.
How can I select all children of an element except the last child?
You can use the negation pseudo-class :not()
against the :last-child
pseudo-class. Being introduced CSS Selectors Level 3, it doesn't work in IE8 or below:
:not(:last-child) { /* styles */ }
CSS Selector for not a child of element type?
:not
does not support combinator selectors.
If we're talking about its direct parent:
:not(a) > code
Otherwise there's no way to do this in CSS. You'll have to override it:
code {
/* some styles */
}
a code {
/* override previous styles */
}
Apply css rule on everything but not a selector and its children
This should target any direct child of .parent that doesn't have the class of child as well as its descendants:
.parent> :not(.child),
.parent> :not(.child) * {
background-color: red;
}
<div class="parent">
<div class="child">
<div>inside</div>
<div>inside2</div>
</div>
<div>outside</div>
<div>
<div>outside!</div>
</div>
</div>
How do I apply a style to all children of an element
As commented by David Thomas, descendants of those child elements will (likely) inherit most of the styles assigned to those child elements.
You need to wrap your .myTestClass
inside an element and apply the styles to descendants by adding .wrapper *
descendant selector. Then, add .myTestClass > *
child selector to apply the style to the elements children, not its grand children. For example like this:
JSFiddle - DEMO
.wrapper * { color: blue; margin: 0 100px; /* Only for demo */}.myTestClass > * { color:red; margin: 0 20px;}
<div class="wrapper"> <div class="myTestClass">Text 0 <div>Text 1</div> <span>Text 1</span> <div>Text 1 <p>Text 2</p> <div>Text 2</div> </div> <p>Text 1</p> </div> <div>Text 0</div></div>
how can i use the css .not() on a element and its children
CSS selector for all DIV
elements except #header
:
div:not(#header) { }
CSS selector for its children
div:not(#header) > * { }
or for all its descendants
div:not(#header) * { }
Edit
CSS selector for all DIV
elements that are body
childs except div#header
body > div:not(#header)
Is it possible to select elements that do not have a child of a certain type?
There is a spec, currently in draft, for a :has()
pseudo-class. No browser supports it yet. If the spec is someday approved and implemented, you'd be able to do this:
a:not(:has(img)) {
// Styles
}
The MDN page says that :has
would never work in stylesheets, only in JavaScript; but in saying that, it links to a section of the spec about a "dynamic selector profile" that apparently no longer exists.
I think the browser vendors typically have a problem with implementing CSS features that require knowledge of the DOM that only exists after the selected element is created, so I don't know if we should get our hopes up for this. Someone who follows the mailing lists or is generally smarter than me might offer a better prognosis.
Can I write a CSS selector selecting elements NOT having a certain class or attribute?
Typically you add a class selector to the :not()
pseudo-class like so:
:not(.printable) {
/* Styles */
}
:not([attribute]) {
/* Styles */
}
But if you need better browser support (IE8 and older don't support :not()
), you're probably better off creating style rules for elements that do have the "printable" class. If even that isn't feasible despite what you say about your actual markup, you may have to work your markup around that limitation.
Keep in mind that, depending on the properties you're setting in this rule, some of them may either be inherited by descendants that are .printable
, or otherwise affect them one way or another. For example, although display
is not inherited, setting display: none
on a :not(.printable)
will prevent it and all of its descendants from displaying, since it removes the element and its subtree from layout completely. You can often get around this by using visibility: hidden
instead which will allow visible descendants to show, but the hidden elements will still affect layout as they originally did. In short, just be careful.
Select all child elements recursively in CSS
Use a white space to match all descendants of an element:
div.dropdown * {
color: red;
}
x y
matches every element y that is inside x, however deeply nested it may be - children, grandchildren and so on.
The asterisk *
matches any element.
Official Specification: CSS 2.1: Chapter 5.5: Descendant Selectors
Related Topics
CSS: Is There Any Difference Between These Two Parts
How to Tell CSS: Not() Selector to Affect All Child Nodes
CSS Style Not Recognizing Numbers
Place View on Top of Other View in React Native
Centering a Div Using Flex and Position: Absolute Gives Different Results on Safari
Overlap Notification Badge on Glyph in Bootstrap 3
HTML Formatted Email Not Showing Up at All in Gmail But Is in Other Mail Clients
Twitter Bootstrap 3 Form-Horizontal and Multiple Input Columns on Single Line
Scss/CSS Selector to Select All Input Types
CSS Not Loading in Firefox, Opera and Ie - Chrome and Safari Works Just Fine
>*' Selector Not Working from Parent to Child Component
How to Select The 1St and Then Every 4Th Row in a HTML-Table with Nth-Child()-Selector
How to Replace The Web Font with Svg Icon in CSS(Font Awesome)
CSS: Which Is Faster for The Browser? Color:#Fff; or Color:#Ffffff;
A Mnemonic for The Order of CSS Margin and Padding Shorthand Properties
Browser Developer Tools: What Is The Position of The HTML Element