CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

:Not


The :not() pseudo-class in CSS is a functional pseudo-class that takes a selector list as an argument. It matches elements that are not represented by the selectors in the argument. This is extremely useful for excluding specific elements from a broader set of selected elements, leading to more concise and readable CSS.


Example 1: Basic Exclusion

/* Selects all <p> elements that are not the one with the ID 'special' */
p:not(#special) {
  color: blue;
}

Explanation

This code selects all paragraph (<p>) elements on the page except for the one that has the id="special". This allows you to apply a general style to most paragraphs while easily making an exception for a specific one.


Example 2: Excluding a Class

/* Selects all <div> elements that do not have the class 'exempt' */
div:not(.exempt) {
  border: 1px solid black;
}

Explanation

Here, all <div> elements will receive a solid black border, unless they have the class exempt. This is a common way to prevent certain elements from receiving a widespread style.


Example 3: Excluding an Attribute

/* Selects all <a> elements that do not have an href attribute starting with 'https://' */
a:not([href^="https://"]) {
  color: orange;
}

Explanation

This example targets anchor <a> tags. It changes the color to orange for any <a> tag whose href attribute does not begin with https://. This can be useful for styling internal or non-secure links differently.


Example 4: Excluding the First Child

/* Selects all list items except the first one */
li:not(:first-child) {
  margin-top: 10px;
}

Explanation

This CSS rule applies a top margin to every list item (<li>) except for the very first one in a list. This is useful for adding space between list items without adding unnecessary space before the first item.


Example 5: Excluding Multiple Selectors

/* Selects all <input> elements that are not hidden or disabled */
input:not([type="hidden"]):not([disabled]) {
  padding: 8px;
}

Explanation

You can chain :not() pseudo-classes. This code selects all <input> elements that are neither of type="hidden" nor have the disabled attribute. This is great for applying styles only to interactive input fields.


Example 6: Excluding Descendants

/* Selects all <p> elements that are not inside an <article> element */
body :not(article) > p {
  font-style: italic;
}

Explanation

This selector targets <p> elements that are direct children of any element that is not an <article>. This allows for styling paragraphs differently based on their parent container.


Example 7: Complex Selector Negation

/* Selects elements that are not a <header> or <footer> with the class 'site-section' */
:not(header.site-section, footer.site-section) {
  background-color: #f0f0f0;
}

Explanation

This example demonstrates excluding elements that match a more complex selector. It applies a background color to every element except for <header> and <footer> elements that also have the class site-section.