CSS vh

Created By: chatGPT

The vh unit in CSS stands for viewport height. It is a relative unit that allows you to set elements to a certain proportion of the total height of the viewport, which is the visible area of the web page. One vh is equal to 1% of the viewport height. This is very useful for creating responsive designs that adapt to different screen sizes.
element {
    height: 50vh;  /* Sets the height to 50% of the viewport height */
}
Using vh units can be particularly effective for full-page layouts, hero sections, or any other component that needs to adjust based on the device's height. For example, if you want to create a full-screen hero section that covers the entire viewport, you would use 100vh.
hero {
    height: 100vh;  /* This will make the hero section take the full height of the viewport */
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: lightblue;
}
You can also combine vh units with other CSS properties for creative layouts. For example, you can set margins, padding, or font sizes using vh to ensure proportional scaling as well.
body {
    margin: 0;
    font-size: 2vh;  /* Font size will be responsive based on viewport height */
}

.container {
    padding: 5vh;  /* Padding will scale with the viewport height */
}
While using vh units is great for creating dynamic and responsive designs, be cautious when using it for elements within a scrollable page, as it can sometimes lead to unexpected layout behaviors on certain devices, especially mobile. Always test across different screen sizes to ensure a consistent user experience.

// Example of a responsive layout
.section {
    min-height: 50vh;  /* Use min-height for flexibility */
}

@media (max-width: 768px) {
    .section {
        height: 80vh;  /* Adjusts for smaller screen sizes */
    }
}
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