Box-Sizing Reset in CSS

CSSCSSBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the concept of Box-Sizing Reset in CSS programming. The lab will cover how to reset the box-model and prevent the width and height of an element from being affected by border or padding. By the end of the lab, you will have a thorough understanding of the box-sizing property and how it can be used to enhance the design of your web pages.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL css(("`CSS`")) -.-> css/BasicConceptsGroup(["`Basic Concepts`"]) css(("`CSS`")) -.-> css/BasicStylingGroup(["`Basic Styling`"]) css(("`CSS`")) -.-> css/CoreLayoutGroup(["`Core Layout`"]) css(("`CSS`")) -.-> css/IntermediateStylingGroup(["`Intermediate Styling`"]) css/BasicConceptsGroup -.-> css/selectors("`Selectors`") css/BasicStylingGroup -.-> css/colors("`Colors`") css/CoreLayoutGroup -.-> css/box_model("`Box Model`") css/CoreLayoutGroup -.-> css/margin_and_padding("`Margin and Padding`") css/CoreLayoutGroup -.-> css/borders("`Borders`") css/CoreLayoutGroup -.-> css/width_and_height("`Width and Height`") css/CoreLayoutGroup -.-> css/display_property("`Display Property`") css/IntermediateStylingGroup -.-> css/backgrounds("`Backgrounds`") css/IntermediateStylingGroup -.-> css/pseudo_elements("`Pseudo-elements`") subgraph Lab Skills css/selectors -.-> lab-35172{{"`Box-Sizing Reset in CSS`"}} css/colors -.-> lab-35172{{"`Box-Sizing Reset in CSS`"}} css/box_model -.-> lab-35172{{"`Box-Sizing Reset in CSS`"}} css/margin_and_padding -.-> lab-35172{{"`Box-Sizing Reset in CSS`"}} css/borders -.-> lab-35172{{"`Box-Sizing Reset in CSS`"}} css/width_and_height -.-> lab-35172{{"`Box-Sizing Reset in CSS`"}} css/display_property -.-> lab-35172{{"`Box-Sizing Reset in CSS`"}} css/backgrounds -.-> lab-35172{{"`Box-Sizing Reset in CSS`"}} css/pseudo_elements -.-> lab-35172{{"`Box-Sizing Reset in CSS`"}} end

Box-Sizing Reset

index.html and style.css have already been provided in the VM.

To ensure that the width and height of an element are not affected by border or padding, use the box-sizing: border-box CSS property. This includes the padding and border in the calculation of the element's width and height. If you want to inherit the box-sizing property from a parent element, use box-sizing: inherit.

Here's an example of using box-sizing property with two div elements:

<div class="box">border-box</div>
<div class="box content-box">content-box</div>
*,
*::before,
*::after {
  box-sizing: inherit;
}

.box {
  display: inline-block;
  width: 120px;
  height: 120px;
  padding: 8px;
  margin: 8px;
  background: #f24333;
  color: white;
  border: 1px solid #ba1b1d;
  border-radius: 4px;
  box-sizing: border-box;
}

.content-box {
  box-sizing: content-box;
}

In this example, the first div element has box-sizing: border-box, and the second div element has box-sizing: content-box.

Please click on 'Go Live' in the bottom right corner to run the web service on port 8080. Then, you can refresh the Web 8080 Tab to preview the web page.

Summary

Congratulations! You have completed the Box-Sizing Reset lab. You can practice more labs in LabEx to improve your skills.

Other CSS Tutorials you may like