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

Logical Operators


In modern CSS, logical operations are primarily handled through specific pseudo-classes and within the context of media queries, allowing for more dynamic and responsive styling. While there aren't direct and, or, and not keywords in the same way as in some programming languages for general styling, their concepts are fundamental to writing advanced and efficient CSS.

The and logical operator

In CSS, the and logic is most prominently used in media queries to apply styles only when multiple conditions are met. This is crucial for creating responsive designs that adapt to various device characteristics. It is also implicitly used when chaining selectors.

Example 1: Chaining Selectors

/* Selects an element that is both a 'p' and has the class '.highlight' */
p.highlight {
  color: blue;
}

Explanation

This code targets <p> elements that also have the class highlight. The absence of a space between p and .highlight acts as an "and" condition, meaning both must be true for the style to apply.


Example 2: Media Query for screen size and orientation

/* Applies styles when the viewport is at least 600px wide AND in landscape orientation */
@media screen and (min-width: 600px) and (orientation: landscape) {
  body {
    background-color: lightblue;
  }
}

Explanation

The and keyword in this media query ensures that the enclosed styles are applied only when the device screen has a minimum width of 600 pixels and is in landscape mode.


Example 3: Combining class selectors

/* Selects elements with both 'button' and 'primary' classes */
.button.primary {
  background-color: #007bff;
  color: white;
}

Explanation

This selector targets elements that have been assigned both the button class and the primary class. This is a common pattern for creating component variations.


Example 4: Attribute and class selector

/* Selects 'a' elements with a 'target="_blank"' attribute AND the class '.external-link' */
a[target="_blank"].external-link {
  text-decoration: underline dotted;
}

Explanation

This rule applies a dotted underline to an <a> tag only if it has the target="_blank" attribute and the .external-link class, combining attribute and class selectors.

The or logical operator

The "or" logic in CSS is achieved using a comma-separated list of selectors or with the :is() and :where() pseudo-classes. This allows you to apply the same styles to multiple, different elements, classes, or IDs in a single rule.

Example 1: Grouping Selectors with a Comma

/* Applies the same color to h1, h2, and h3 elements */
h1, h2, h3 {
  color: #333;
}

Explanation

The comma acts as an "or" operator, applying the color style to <h1> elements, OR <h2> elements, OR <h3> elements. This is more efficient than writing a separate rule for each.


Example 2: The :is() Pseudo-class

/* Applies styles to any 'p', 'li', or 'span' element that is a child of an 'article' */
article :is(p, li, span) {
  line-height: 1.6;
}

Explanation

The :is() pseudo-class takes a selector list as its argument and selects any element that matches any of the selectors in that list. It simplifies complex selector lists.


Example 3: The :where() Pseudo-class

/* Applies margin to any 'header' or 'footer' within a 'div' or 'section' */
:where(div, section) :where(header, footer) {
  margin-bottom: 20px;
}

Explanation

Similar to :is(), :where() also matches one of the selectors in the list. The key difference is that :where() always has a specificity of 0, making it easier to override its styles later.


Example 4: Media Query with comma-separated conditions

/* Applies styles if the device is a screen OR if it is for printing */
@media screen, print {
  body {
    font-family: sans-serif;
  }
}

Explanation

In this media query, the comma acts as an "or". The styles will be applied if the media type is screen or if the page is being printed.

The not logical operator

The "not" logic is implemented using the :not() pseudo-class. It allows you to select elements that do not match a specific selector, providing a way to exclude elements from a selection.

Example 1: Basic :not() Usage

/* Selects all 'p' elements that DO NOT have the class '.intro' */
p:not(.intro) {
  font-size: 14px;
}

Explanation

This rule applies the font-size to all paragraph elements except for those that have the class intro.


Example 2: Chaining :not()

/* Selects 'div' elements that DO NOT have the id '#header' AND DO NOT have the class '.footer' */
div:not(#header):not(.footer) {
  padding: 10px;
}

Explanation

You can chain :not() pseudo-classes to exclude elements based on multiple conditions. This example selects <div>s that are not the header and not the footer.


Example 3: :not() with child elements

/* Selects all elements that are NOT a 'span' inside a 'p' */
p *:not(span) {
  font-weight: bold;
}

Explanation

This targets any element (*) inside a <p> tag and makes it bold, as long as that element is not a <span>.


Example 4: Using :not() in a media query

/* Applies styles for all media types that are NOT 'print' */
@media not print {
  body {
    color: #444;
  }
}

Explanation

The not operator in a media query inverts the condition. In this case, the styles are applied to all media types except for print.