Media queries in CSS help make your website look good on different devices. They check things like the screen size or resolution and apply specific styles if those conditions are met.
@media only screen and (min-width: 600px) { /* Apply these styles if the screen width is 600px or more */ }
You can use the `and` keyword to combine multiple conditions in a media query. This lets you apply styles only when several criteria are met, like screen size and resolution.
@media only screen and (max-width: 480px) and (min-resolution: 300dpi) { /* Apply these styles if the screen width is 480px or less and resolution is 300dpi or more */ }
A media query lets you change your website’s design based on the device’s screen size or other features. For example, you can set different styles for phones, tablets, and computers.
/* If the screen width is 480px or less (like on a mobile phone), set the body font size to 12px and make the photo element's width 100% */ @media only screen and (max-width: 480px) { body { font-size: 12px; } #photo { width: 100%; } }
Media features in media queries allow you to target specific aspects of a device, such as its width or orientation. You can combine different features to apply styles based on various conditions.
@media only screen and (min-width: 480px) and (max-width: 600px) { /* Apply these styles if the screen width is between 480px and 600px */ }
You can target a range of screen sizes using media queries. For instance, you can apply styles for screens between 480px and 600px wide by combining `min-width` and `max-width`.
/* This style will apply if the screen width is between 480px and 600px */ @media only screen and (min-width: 480px) and (max-width: 600px) { .nav-container { font-size: 15px; } }
'em' is a unit of measurement in CSS that scales relative to the font size of the current element. It's useful for creating text and elements that scale proportionally.
.news-row { height: 20em; /* Relative to the font size of the .news-row element */ width: 30em; } .news-row .news-column { height: 80%; /* 80% of the height of .news-row */ width: 50%; /* 50% of the width of .news-row */ }
Percentages in CSS can make elements adjust their size relative to their parent element. For example, you can set a child element's width to 50% of its parent's width, so it always scales with the parent.
html { font-size: 18px; } span { font-size: 2rem; /* 2 times the root font size */ }
The `background-size: cover;` property makes sure that a background image covers the entire container. It scales the image to fit the container's size, which can be useful for responsive designs.
.container { background-image: url('background.jpg'); background-size: cover; /* Makes sure the image covers the entire container */ }
'rem' units are similar to 'em' but are relative to the root (html) font size. This helps in setting sizes that scale consistently across the entire page.
html { font-size: 16px; } .container { width: 20rem; /* 20 times the root font size */ }
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!