Published on Apr 7, 2024 10:31
CSS Units: vh, vw,vmin,vmax
CSS units vh, vw, vmin, and vmax are all relative units that are commonly used in web development for specifying sizes and dimensions. Here's a brief explanation of each:
- Viewport Height (vh):
- Description: Represents a percentage of the viewport's height.
- Usage: 1vh is equal to 1% of the viewport height.
- Use Case: Useful for creating elements that should take up a specific percentage of the height of the viewport.
- Example: If you want a container to take up 80% of the viewport height, you can set its height to 80vh.
- Viewport Width (vw):
- Description: Represents a percentage of the viewport's width.
- Usage: 1vw is equal to 1% of the viewport width.
- Use Case: Useful for creating elements that should take up a specific percentage of the width of the viewport.
- Example: If you want a container to take up 50% of the viewport width, you can set its width to 50vw.
- Viewport Minimum (vmin):
- Description: Represents the smaller of the viewport's height or width.
- Usage: 1vmin is equal to 1% of the smaller dimension (height or width).
- Use Case: Useful when you want an element's size to be a percentage of the smaller dimension (height or width).
- Example: If you want a square element that is always 30% of the smaller dimension, you can set its size to 30vmin.
- Viewport Maximum (vmax):
- Description: Represents the larger of the viewport's height or width.
- Usage: 1vmax is equal to 1% of the larger dimension (height or width).
- Use Case: Useful when you want an element's size to be a percentage of the larger dimension (height or width).
- Example: If you want a container to take up 60% of the larger dimension, you can set its size to 60vmax.
In summary, these units are useful for creating designs that are responsive to the size of the viewport. They are particularly handy for situations where you want elements to scale based on the dimensions of the user's screen.