I’m coding a dropdown, but don’t want an element with the class .dropdown-content
, nor the tag <div>
with :hover
, but instead, all tags (excluding <div>
s and .dropdown-content
).
I have no idea what to put there as I don’t have any knowledge about this situation.
2
Answers
In the
:not()
function, we can specify values that should not apply to the given element, in our case, they should not be hovered over.Another concept is that every element belongs under a common parent. If the parent is hovered over, we modify all children except the one specifically being hovered over. In CSS, the last strongest modification applied to a specific element always prevails. Therefore, if you modify all your children and then override the exceptions with an equally strong or stronger rule, you achieve the same result: you select everything except the exceptions.
Or… Since I don’t know the goal, for example, it’s can align the style with the container’s hover.
More information
:not()
function – MDN Docs:hover
– MDN DocsIt sounds like you want to apply some CSS styling to all elements except for those with the class
.dropdown-content
and all<div>
elements. You can achieve this by using the:not()
pseudo-class selector in CSS.Here’s a sample CSS code snippet that applies the style to all elements except those with the class
.dropdown-content
and all<div>
elements:In this CSS rule, the
*
selects all elements,:not(.dropdown-content)
excludes elements with the class.dropdown-content
, and:not(div)
excludes all<div>
elements.Please replace
/* your styles here */
with the actual CSS properties and values you’d like to apply.This method can be very powerful but needs to be used with care, as it can easily override other styles you may have in your CSS. Always test thoroughly to ensure it behaves as expected across all elements and layouts.