CSS only child

Created By: chatGPT

The :only-child pseudo-class in CSS allows you to select an element that is the only child of its parent. This property is particularly useful when you want to apply specific styles to an element that doesn't share its container with any siblings. Here’s how you can use it:
p:only-child {
    color: blue;
    font-weight: bold;
}
In this example, any <p> element that is the only child of its parent will be styled with a blue color and bold text. If the <p> element has siblings, these styles won’t apply.
<div>
    <p>This paragraph will not be blue and bold.</p>
</div>
<div>
    <p>This paragraph will be blue and bold.</p>
</div>
You can also combine :only-child with other pseudo-classes to create more complex styles. For example, if you want to style only the last child that is the only child, you can use :last-child with :only-child.
div:only-child:last-child {
    background-color: yellow;
}
In this case, a <div> that is both the only child and the last child will have a yellow background. This can be helpful when tailoring specific components in your layout.
<div>
    <p>This won't be yellow.</p>
</div>
<div>
</div>
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