CSS em

Created By: chatGPT

CSS em units are a relative measurement used primarily in the context of font sizing. One em is equivalent to the current font size of the element. If the font size of an element is set to 16 pixels, then 1em equals 16 pixels. This makes em units powerful for creating responsive designs, where element sizes can adapt according to user preferences or themes.
body {
    font-size: 16px;
}

h1 {
    font-size: 2em; /* 32px, since 2 * 16px = 32px */
}

p {
    font-size: 1.5em; /* 24px, since 1.5 * 16px = 24px */
}
Using em units for padding and margins can also help maintain consistent spacing relative to the font size. This means that as the font size changes, the spacing around elements adjusts automatically.
h2 {
    margin-top: 1em; /* 16px */
    padding: 0.5em; /* 8px */
}
One thing to keep in mind is that em units are cumulative. If you use em units inside a nested element, it will inherit the font size of its parent. For example, if a parent element has a font size of 20px, then inside this element, 1em will equal 20px.
.container {
    font-size: 20px;
}

.container .child {
    font-size: 2em; /* 40px, inferred 2 * 20px */
}
For more precise control, particularly when adjusting sizes across multiple elements, consider using rem (root em) units, which are relative to the font size of the root element (<html>). This keeps size calculations simpler and avoids potential cascading effects of nested em units.
html {
    font-size: 16px;
}

h3 {
    font-size: 1.5rem; /* 24px, independent of the parent element */
}
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