Lädt...


🔧 Understanding CSS Box Sizing: A Guide to 'content-box' and 'border-box'


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

CSS Box Sizing is a fundamental concept in web development that influences how the dimensions of an element are calculated. The box-sizing property allows developers to specify whether an element's declared width and height include its padding and border. There are two main values for box-sizing: 'content-box' and 'border-box'. In this blog post, we'll delve into these values, their implications, and how to use them effectively in your web projects.

The Default: 'content-box'

By default, when you set a width or height for an element in CSS, you are using the 'content-box' model. This means that the specified width and height apply only to the content area of the element, excluding padding and border. Let's illustrate this with an example:

div {
  width: 100px;
  height: 50px;
  padding: 10px;
  border: 2px solid black;
}

In this scenario, the total width of the div would be 100px (content width) + 20px (padding) + 4px (border) = 124px. This default behavior can sometimes lead to unexpected results, especially when dealing with responsive designs or precise layout requirements.

Taking Control with 'border-box'

To simplify sizing calculations and achieve more predictable layouts, the 'border-box' value comes into play. When you set an element's box-sizing to 'border-box', the declared width and height now include the content, padding, and border. Here's an example:

div {
  box-sizing: border-box;
  width: 100px;
  height: 50px;
  padding: 10px;
  border: 2px solid black;
}

With 'border-box', the total width of the div is now strictly 100px, encompassing both the content and the space taken by padding and border. This can be particularly advantageous in scenarios where you want a more intuitive way to control the dimensions of your elements.

Practical Use Cases

  1. Responsive Design: When working on responsive layouts, 'border-box' can simplify calculations. Elements will respond to changes in width or height without unexpected variations caused by padding or border adjustments.

  2. Consistent Sizing: Ensuring a consistent sizing model across different elements can be crucial. 'border-box' makes it easier to maintain a uniform appearance without constant adjustments for padding and border differences.

  3. Easier Grids and Flexboxes: When creating grids or using flexbox layouts, 'border-box' can streamline the process. The specified dimensions directly represent the space an element occupies, simplifying alignment and spacing considerations.

Conclusion

Understanding and leveraging the box-sizing property in CSS can significantly contribute to a smoother development process and more maintainable code. By choosing between 'content-box' and 'border-box' based on your specific needs, you gain greater control over how elements are sized and how they interact within your layout. Whether you're aiming for responsive designs or consistent spacing, box-sizing is a powerful tool in your CSS toolkit.

...

🔧 CSS tutorial series: CSS Units and Sizing


📈 35.43 Punkte
🔧 Programmierung

🔧 Introduction of CSS, What is CSS, Why we use CSS and How CSS describe the HTML elements


📈 29.38 Punkte
🔧 Programmierung

📰 Transforming immigration and border crossing in Colombia with Automated Border Control


📈 27.94 Punkte
📰 IT Security Nachrichten

📰 Transforming immigration and border crossing in Colombia with Automated Border Control


📈 27.94 Punkte
📰 IT Security Nachrichten

🔧 An Overview of CSS Sizing Units


📈 27.15 Punkte
🔧 Programmierung

🎥 You Should Know These 7 CSS Responsive Sizing Features


📈 27.15 Punkte
🎥 Video | Youtube

🔧 An Ultimate Guide On Sizing, Spacing, Grids And Layout In Web And UI/UX Design


📈 27.13 Punkte
🔧 Programmierung

🔧 Mastering CSS Border Style: A Comprehensive Guide


📈 24.86 Punkte
🔧 Programmierung

🐧 How large should you make the UEFI System Partition? An ESP sizing guide.


📈 24.61 Punkte
🐧 Linux Tipps

⚠️ VB2015 paper: Sizing cybercrime: incidents and accidents, hints and allegations


📈 22.63 Punkte
⚠️ Malware / Trojaner / Viren

⚠️ VB2015 paper: Sizing cybercrime: incidents and accidents, hints and allegations


📈 22.63 Punkte
⚠️ Malware / Trojaner / Viren

🔧 Right-Sizing GPU and CPU Resources For Training and Inferencing Using Kubernetes


📈 22.63 Punkte
🔧 Programmierung

🔧 CSS Grid: Moving From CSS Frameworks To CSS Grid (2018 and beyond)


📈 22.35 Punkte
🔧 Programmierung

🔧 A BRIEF REVIEW OF CSS CASCADING, CSS SELECTORS and CSS SPECIFICITY.


📈 22.35 Punkte
🔧 Programmierung

⚠️ Sizing Business Opportunities in Middle East and Africa


📈 21.37 Punkte
⚠️ Malware / Trojaner / Viren

⚠️ Sizing Business Opportunities in Middle East and Africa


📈 21.37 Punkte
⚠️ Malware / Trojaner / Viren

🐧 Why are almost all the linux distros, have shitty sizing of icons, taskbar and fonts?


📈 21.37 Punkte
🐧 Linux Tipps

📰 Home Security: Market Sizing and Forecasts 2019


📈 21.37 Punkte
📰 IT Security Nachrichten

🔧 What Is Kubernetes Right-Sizing and Should I Care?


📈 21.37 Punkte
🔧 Programmierung

📰 How to buy the Samsung Galaxy Ring - and what the sizing process looks like


📈 21.37 Punkte
📰 IT Nachrichten

🔧 Bootstrap Utilities for Spacing and Sizing


📈 21.37 Punkte
🔧 Programmierung

🍏 Circular's second-gen Ring adds ECG and digital ring-sizing


📈 21.37 Punkte
🍏 iOS / Mac OS

🔧 Understanding SEPA and Cross-Border Payments


📈 21.13 Punkte
🔧 Programmierung

🔧 Stylify CSS: Automagic CSS bundles splitting into CSS layers in Astro.build


📈 21.09 Punkte
🔧 Programmierung

🔧 Choosing the Right CSS Approach: Tailwind CSS vs Bootstrap vs Vanilla CSS


📈 21.09 Punkte
🔧 Programmierung

🕵️ Mozilla Firefox up to 1.5.0.1 CSS border resource management


📈 20.37 Punkte
🕵️ Sicherheitslücken

🔧 Tailwind CSS tutorial #33: Border Radius


📈 20.37 Punkte
🔧 Programmierung

🐧 How Can I Set a CSS Border on One Side Only?


📈 20.37 Punkte
🐧 Linux Tipps

🔧 CSS Border Radius Tricks


📈 20.37 Punkte
🔧 Programmierung

🔧 The Complex But Awesome CSS border-image Property


📈 20.37 Punkte
🔧 Programmierung

🔧 Awesome Border Animation Effects using CSS


📈 20.37 Punkte
🔧 Programmierung

matomo