How to combine class and ID in CSS selector?
In your stylesheet:
div#content.myClass
Edit: These might help, too:
div#content.myClass.aSecondClass.aThirdClass /* Won't work in IE6, but valid */
div.firstClass.secondClass /* ditto */
and, per your example:
div#content.sectionA
Edit, 4 years later: Since this is super old and people keep finding it: don't use the tagNames in your selectors. #content.myClass
is faster than div#content.myClass
because the tagName adds a filtering step that you don't need. Use tagNames in selectors only where you must!
Combining a Class selector with an ID
quirksmode have a table of which selector (among other things) browser supports. unfortunaltly, there is not test for combining selector.
but as ie6 fail multiple classes it's not that surprising.
w3c css specification explains how css selector should works, there is a DIV.warning
and E#myid
which are not exactly like yours but suggest it should work (maybe it's explain in the page I didn't read it all ;) )
How do I combine ID, and Class together with an active?
.dashboard-wrapper
isn't a child of #sidebar
, it's sibling. Use +
selector.
#sidebar.active + .dashboard-wrapper {
margin-left: 89px;
}
Or use your current styles and change HTML markup and put .dashboard-wrapper
into #sidebar
.
<div class="nav-left-sidebar sidebar-dark" id="sidebar">
SIDEBAR CONTENTS
<div class="dashboard-wrapper">
MAIN CONTENT
</div>
</div>
How do I in CSS combine a plus selector with parent and class?
This SO post should answer your specific question about selector associativity.
If you have control over the HTML and you find yourself needing highly elaborate selectors, it's probably better to slightly reorganize the HTML instead (yes, this goes against the ideal of separating semantics and presentation, but HTML and CSS have always fallen far short in that respect)
Can I use DIV class and ID together in CSS?
Yes, yes you can.
#y.x {
/* will select element of id="y" that also has class="x" */
}
Similarly:
.x#y {
/* will select elements of class="x" that also have an id="y" */
}
Incidentally this might be useful in some use cases (wherein classes are used to represent some form of event or interaction), but for the most part it's not necessarily that useful, since id
s are unique in the document anyway. But if you're using classes for user-interaction then it can be useful to know.
Combining CSS Selectors with :first-child
You're missing a space. You want the span that's the first child, not the div that's the first child.
Finds the first span:
#messages > div :first-child
Finds the first div:
#messages > div:first-child
Related Topics
How to Center an Absolutely Positioned Element in a Div
Chrome/Safari Not Filling 100% Height of Flex Parent
How to Apply a CSS Filter to a Background Image
Difference Between Visibility:Hidden and Display:None
Align Child Elements of Different Blocks
What Are the Differences Between Flex-Basis and Width
I Do Not Want to Inherit the Child Opacity from the Parent in Css
Bootstrap Align Navbar Items to the Right
What Does the CSS Rule "Clear: Both" Do
How to Combine a Background-Image and Css3 Gradient on the Same Element
How to Interpolate CSS Variables With Url()
Font Awesome 5, Why Is CSS Content Not Showing
Prevent Flicker on Webkit-Transition of Webkit-Transform
How to Apply Opacity to a CSS Color Variable