Media Queries
Key Concepts
- Media Types
- Media Features
- Logical Operators
- Viewport Meta Tag
- Responsive Design
Media Types
Media types define the category of devices for which the CSS is intended. Common media types include screen
, print
, speech
, and all
. For example, @media screen
applies styles only to screens, while @media print
applies styles only when printing.
Media Features
Media features describe specific characteristics of the user agent or device. Common media features include width
, height
, orientation
, aspect-ratio
, and resolution
. For example, @media (max-width: 600px)
applies styles only when the viewport width is 600 pixels or less.
Logical Operators
Logical operators allow combining multiple media queries. Common logical operators include and
, or
, and not
. For example, @media (min-width: 768px) and (max-width: 1024px)
applies styles only when the viewport width is between 768 and 1024 pixels.
Viewport Meta Tag
The viewport meta tag controls the layout on mobile browsers. It is defined in the HTML head section. For example, <meta name="viewport" content="width=device-width, initial-scale=1.0">
ensures the page width matches the device width and sets the initial zoom level to 1.
Responsive Design
Responsive design uses media queries to create layouts that adapt to different screen sizes. It ensures a consistent user experience across devices. For example, a website might use media queries to adjust font sizes, hide elements, or rearrange content based on the device's screen size.
Examples and Analogies
Media Types Example
Think of media types as different types of paper. A screen is like a glossy magazine, print is like a newspaper, and speech is like an audiobook. Each type of paper requires different formatting.
Media Features Example
Consider media features as the dimensions of a canvas. The width and height determine how much space you have to paint, while the resolution determines the quality of the painting.
Logical Operators Example
Imagine logical operators as filters in a photo editing app. The "and" operator is like applying two filters at once, the "or" operator is like choosing between two filters, and the "not" operator is like removing a filter.
Viewport Meta Tag Example
Think of the viewport meta tag as a lens on a camera. It adjusts the field of view to ensure the subject is in focus, no matter the camera's position.
Responsive Design Example
Consider responsive design as a chameleon. The chameleon changes its appearance based on its environment, just as a responsive website changes its layout based on the device it is viewed on.