DEV Community

Cover image for Mastering CSS Sizing: A Guide to Responsive Design
Oluwasegun Olatunji
Oluwasegun Olatunji

Posted on

Mastering CSS Sizing: A Guide to Responsive Design

The presentation and layout of web pages are managed using the powerful styling language known as Cascading Style Sheets, or CSS.

Size is one of the most important parts of responsive and adaptive designs, among others. In this article, we will dive deep into the world of CSS sizing, exploring the different units, techniques, and best practices for responsive design.

Here's a list of what we will cover in this article:

  • CSS Units

  • CSS Sizing Properties

  • Responsive Design Techniques

CSS Units

To change the size of elements in CSS, you need to know how to use the different units, and the three different types of CSS units are viewport units, relative units, and absolute

Absolute

Absolute units have a set size that is independent of the parent element's and the user's device sizes. Common absolute units are as follows:

  1. Pixel (px) on a user's screen corresponds to a single dot. Although this is the most often used absolute unit, responsive design cannot effectively use it because it does not scale for different screen sizes.

  2. Points (pt): One point, which was formerly employed in print media, is equivalent to 1/72 of an inch. Because it does not scale well across different devices, it is not advised for web design.

Relative Units

Relative units are perfect for flexible design since they can change in size according to the size of the parent element or other characteristics. Common related units are as follows:

  1. Em: This measurement unit is based on the parent element's font size. For instance, 1em is equivalent to 16px if the parent's font size is 16px. Scalable typography can be made with the use of em units.

  2. Rem: By using rem, which is unaffected by parent components' font sizes, you may guarantee uniform sizing across all of your elements.

  3. Percentages (%): This measurement is based on how big the parent element is. For instance, a child element with a width of 50% would be 100px wide if the parent element had a width of 200px.

Viewport Units

The size of the user's device viewport determines the viewport units. These components are especially helpful for producing responsive designs that change according to the size of the screen. Common viewport units are as follows:

  • Viewport Width (vw): One unit is equal to one per cent of the viewport's width. For instance, 1vw is equal to 10px on a device with a viewport width of 1000px.
  • Viewport Height (vh): One unit equals one per cent of the viewport's height (vh).
  • Viewport Minimum (vmin): The smaller of vw or vh is equal to one unit in the Viewport Minimum (vmin) system.
  • Viewport Maximum (vmax): The larger vw or vh is equal to one unit in the Viewport Maximum (vmax) system.

Choosing the Right Units

Choosing the appropriate unit for sizing is crucial for creating responsive designs. In general, it is recommended to use relative units like em, rem, and percentages for most sizing tasks. Viewport units can be used when you want the size of an element to adapt directly to the viewport dimensions.

CSS Sizing Properties

Now that we are familiar with the various units, let's look at some of the most popular CSS properties for element sizing.

Width and Height

The width and height properties define the size of an element's content area. They can be set using any of the units discussed above.

The parameters of an element's width and height determine the size of its content area. To set them, any of the previously mentioned units can be used.

div {
  width: 50%;
  height: 200px;
}

Enter fullscreen mode Exit fullscreen mode

Padding, Margin, and Border

Controlling the area around an element's content is made easier with padding, margin, and border attributes. In contrast to margin, which adds space outside the element, padding adds space inside the element, and the border attribute controls the element's border width.

div { padding: 1em;
    margin: 2rem;
    border: 1px solid black; }
Enter fullscreen mode Exit fullscreen mode

Box-sizing

The box-sizing property controls how to calculate an element's dimensions. The default value, content-box subtracts padding and borders from the content area to determine the width and height.

It is simpler to control element sizing using the border-box value because padding and border are taken into account when calculating width nd height.

div {
  box-sizing: border-box;
  width: 50%;
  padding: 1em;
  border: 1px solid black;
}
Enter fullscreen mode Exit fullscreen mode

Background-size and Background-position

The background image of an element can be positioned and scaled using these properties. You can change the background-size attribute to values like contain, cover, or particular measurements. The background-position attribute determines where the background image is located within the element.

div {
  background-image: url('image.jpg');
  background-size: cover;
  background-position: center;
}
Enter fullscreen mode Exit fullscreen mode

Responsive Design Techniques

To make responsive designs, the layout must be changed to fit different screen sizes and devices. Here are a few well-liked methods for doing this:

Media Queries:

Based on the user's device parameters, such as screen width or device orientation, media queries let you apply several styles and also construct breakpoints that modify the layout at particular screen sizes.


@media (max-width: 768px) {
  div {
    width: 100%;
  }
}
Enter fullscreen mode Exit fullscreen mode

Flexbox

Flexbox is a CSS layout module that offers a more effective method of spacing out and aligning elements inside a container. By utilizing Flexbox, you can design responsive layouts that change to fit the available space.

.container {
  display: flex;
  flex-wrap: wrap;
}

.item {
  flex: 1;
  min-width: 200px;
}
Enter fullscreen mode Exit fullscreen mode

Grid Layout

Another layout module that uses a two-dimensional grid structure to enable the creation of intricate and responsive designs is CSS Grid. Using CSS Grid, you may specify rows and columns and insert objects inside grid cells.

.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

.item {
  grid-column: span 1;
}

Enter fullscreen mode Exit fullscreen mode

Transitions and Transforms in CSS

Responsive animations and effects can be made using CSS transformations and transitions. While transitions manage the timing of these changes, transforms let you alter the position, scale, and rotation of an element.

div:hover {
  transform: scale(1.2);
  transition: transform 0.3s ease-in-out;
}

Enter fullscreen mode Exit fullscreen mode

Conclusion

The ability to understand CSS sizing is crucial for web designers and developers. You can only make responsive designs that look great on any device if you know about the different devices, features, and especially CSS sizing.

For the majority of sizing activities, remember to use relative units like em, rem, and percentages, use viewport units for items that must adjust to the size of the viewport and to adjust element proportions, use properties like box-sizing, padding, margin, and border. For responsive layouts, look into new layout modules like Flexbox and Grid.

Last but not least, remember to combine CSS transforms and transitions for responsive animations and effects and utilise media queries to establish breakpoints for various screen sizes. You'll be well on your way to producing stunning, responsive designs with CSS if you adhere to these best practices.

See you next time!

Photo by Mohammad Rahmani on Unsplash

Top comments (2)

Collapse
 
detzam profile image
webstuff

nice one.
add this from us old guys.
if u using rems or ems add also the pixel value as a fallback for older browsers, that is if you re not sure the uers will use some chromium based browser.

also group your media queryes at the end of your stylesheet or create a responsive.css file and add all of them there

its late so that;s what came form the top of my head

Collapse
 
segunolatunji profile image
Oluwasegun Olatunji

Thank you for reading and pointing out these facts.
Much appreciated!