CSS attribute selectors

Created By: chatGPT

CSS attribute selectors are used to select elements based on their attributes and attribute values. They allow for more specific targeting of HTML elements compared to class or ID selectors. Attribute selectors can be very handy in various scenarios. Here's a breakdown of how you can use them effectively.
/* Select all input elements with a type attribute of 'text' */
input[type='text'] {
    background-color: lightblue;
}

/* Select all anchor elements that have a title attribute */
a[title] {
    text-decoration: underline;
}

/* Select all elements with a data attribute starting with 'data-' */
[data-*] {
    border: 1px solid gray;
}

/* Select all elements with a class that includes 'example' in it */
[class*='example'] {
    color: red;
}

There are four main types of attribute selectors:

  • Exact Match: [attribute='value']
  • Starts With: [attribute^='value']
  • Ends With: [attribute$='value']
  • Contains: [attribute*='value']

These selectors can be combined with other selectors, including class and type selectors, to apply styles based on attribute values.

/* Select all image elements with a src attribute ending with '.png' */
img[src$='.png'] {
    border: 2px solid green;
}

/* Select all elements with a class that starts with 'prefix-' */
[class^='prefix-'] {
    background-color: yellow;
}

/* Select all link elements with an href attribute containing 'http' */
a[href*='http'] {
    color: blue;
}
Using attribute selectors promotes cleaner code by avoiding unnecessary classes and ensuring that styles are closely tied to the element’s intended use.
/* Example of combining selectors */
input[type='checkbox']:checked {
    accent-color: green;
}

/* Styling based on a combination of class and attribute */
button.primary[data-active='true'] {
    background-color: blue;
    color: white;
}
Introduction And SetupSelectorsType SelectorClass SelectorId SelectorAttribute SelectorsUniversal SelectorGrouping SelectorsCombinatorsDescendant CombinatorChild CombinatorAdjacent Sibling CombinatorGeneral Sibling CombinatorPseudo ClassesHoverNth ChildNth Of TypeFocusVisitedActiveFirst ChildLast ChildOnly ChildPseudo ElementsBeforeAfterFirst LetterFirst LineBox ModelMarginPaddingBorderWidthHeightBox SizingPositioningStaticRelativeAbsoluteFixedStickyZ IndexDisplayInlineBlockInline BlockFlexGridNoneFloatClearTypographyFont FamilyFont SizeFont WeightFont StyleLine HeightLetter SpacingText AlignText DecorationText TransformVertical AlignOverflowOverflow XOverflow YVisibilityClip PathBackgroundsBackground ColorBackground ImageBackground SizeBackground PositionBackground RepeatBackground AttachmentGradientsBordersBorder WidthBorder ColorBorder StyleBorder RadiusShadowsBox ShadowText ShadowFlexboxFlex DirectionJustify ContentAlign ItemsAlign SelfFlex GrowFlex ShrinkFlex BasisOrderGridGrid Template ColumnsGrid Template RowsGrid GapGrid Auto FlowAlign ContentCss VariablesCustom PropertiesAnimationKeyframesTransitionTransformRotateScaleTranslateSkewMedia QueriesMin WidthMax WidthOrientationAspect RatioUnitsPxEmRemVhVwPercentages