Using REM for Responsive Design
Understanding REM Units
The REM (Root EM) unit is a relative measurement in CSS. While EM units are relative to the font size of their immediate parent, REM units are relative to the font size of the root <html> element. This makes them predictable for scaling layouts.
/* Setting the root font-size */
html {
font-size: 16px;
}
/* An element using rem */
.component {
width: 2rem; /* Calculates to 2 * 16px = 32px */
padding: 1.5rem; /* Calculates to 1.5 * 16px = 24px */
}
Implementing Media Queries
Media queries allow CSS rules to be applied conditionally based on device characteristics like screen width, enabling responsive designs.
Syntax and Structure
The basic syntax for a media query is:
@media media-type and (media-feature) {
/* CSS rules applied when condition is met */
}
Common media types include screen for devices and print for printers. The most frequently used media features are:
min-width: Applies styles when the viewport is at least this wide.max-width: Applies styles when the viewport is at most this wide.
/* Example: Styles for tablets and larger */
@media screen and (min-width: 768px) {
.navigation {
display: flex;
}
}
/* Example: Styles for mobile phones */
@media screen and (max-width: 767px) {
.navigation {
display: block;
}
}
For organization, you can link separate stylesheets for different breakpoints:
<link rel="stylesheet" media="screen and (min-width: 992px)" href="desktop-styles.css">
Enhancing CSS with Less
Less is a CSS preprocessor that extends CSS with programming features like variables, nesting, and operations, making stylesheets more maintainable.
Variables in Less
Variables store reusable values like colors or font sizes.
// Defining variables
@primary-color: #3498db;
@base-spacing: 1rem;
// Using variables
.button {
background-color: @primary-color;
margin: @base-spacing;
}
Variable names must start with an @ symbol and cannot begin with a number.
Nesting Selectors
Less allows CSS rules to be nested, reflecting the HTML structure and improving readability.
// Traditional CSS
.card { }
.card .title { }
.card .title:hover { }
// Less nesting
.card {
.title {
&:hover { } // The & refers to the parent selector '.card .title'
}
}
Mathematical Operations
Less can perform calculatinos directly within stylesheets.
@container-width: 960px;
@column-count: 4;
.column {
width: (@container-width / @column-count) - 20px; // Results in 220px
height: 100px + 50; // Results in 150px
}
Note: For division, operations inside parentheses are evaluated first. When mixing units, the result typical takes the unit of the first value.
Less code must be compiled into standard CSS, often done automatically by tools or editor plugins.
Creating REM-Based Adaptive Layouts
The core strategy uses a dynamic root font-size (set via media queries or JavaScript) and REM units for element dimensions, enabling proportional scaling across devices.
Strategy 1: Media Queries with Less
This method defines specific root font-sizes for different screen width ranges.
// Define base design width (commonly 750px for mobile-first)
@design-width: 750;
@base-font-size: 50px; // Hypothetical base for the design width
// Media query for a 320px wide device
@media screen and (max-width: 320px) {
html {
font-size: 21.33px; // (320 / @design-width) * @base-font-size
}
}
// A component sized relative to the root
.box-element {
width: 2rem; // Scales based on the current html font-size
height: 2rem;
}
Strategy 2: Using Flexible.js Library
Libraries like flexible.js automate the calculation of the root font-size by dividing the viewport into 10 units, removing the need for manual media queries.
// With flexible.js included, for a 750px design:
html {
font-size: 75px; // 750px / 10
}
// Element sizes are then calculated as:
.component-width {
width: (100px / 75); /* Result is approximately 1.3333rem */
}
Utilizing the Bootstrap Framework
Bootstrap is a comprehensive front-end framework providing a responsive grid system and pre-built components.
Core Structure and Grid
Bootstrap layouts require a container, typically using the .container (fixed-width) or .container-fluid (full-width) class. The grid system divides the container into 12 columns.
<div class="container">
<div class="row">
<div class="col-md-8">Main Content (8 columns on medium screens)</div>
<div class="col-md-4">Sidebar (4 columns on medium screens)</div>
</div>
</div>
Grid classes like .col-sm-, .col-md-, .col-lg- define column behavior at different breakpoints (small, medium, large).
Advanced Grid Features
Column Nesting: Rows and columns can be nested within a parent column.
<div class="col-sm-8">
<div class="row">
<div class="col-sm-6">Nested Column A</div>
<div class="col-sm-6">Nested Column B</div>
</div>
</div>
Column Offsetting: The .col-md-offset-* classes add left margin to push columns rightward.
<div class="col-md-4 col-md-offset-4">This column is centered with a 4-column offset</div>
Responsive Utilities: Classes like .hidden-xs or .visible-md control the visibility of elements across different screen sizes, complementing the core grid behavior.