The Box Model

The box Model

Introducing The Box Model

It is a concept that every single element in the browser is a box. And we can control different properties of the box.

Three main parts

  • Margin: the distance from another box.
  • Padding: the distance within the box.
  • Border: the boundaries of the box.

Working With Borders

Border Color

  • We use left, right, top and bottom color separately.
				
					Border-color: red;
				
			

Border Width

  • It could be 1em, 2px, etc. We use left, right, top and bottom width separately.
				
					 Border-width: thick;
				
			

Border Style

  • It could be dashed, inset, outset, grown, solid, etc. We use left, right, top and bottom style separately.
				
					Border-style: dotted;
				
			

Border

  • It is shorthand for all others. Color, width, and style.
				
					Border: 3px solid green;
				
			

Width, Height, and Percentages

Width:

  • To adjust the width of the element.
				
					Width: 500px
				
			

Height:

  • To adjust the height of the element.
				
					height:430px;
				
			

Percentage:

  • When we use percent, this change according to parent size
				
					Width: 50%;
				
			

Adding Padding to Elements

  • The distance of content and the border
  • Sequence is top, right, bottom and left. And top, right and left, bottom. And top and bottom, right and left.
				
					Padding: 34px;
				
			

The Alternate Box Model

Regular Box Model:

  • The changing of height and width it’s mean the changing within the box. Only inner content control by height and width. And if we use,
  • With box-sizing set to border-box, the width and height now control the overall dimensions of the entire visible box. If we use this tag, then me add border the border comes within the box and if we don’t add it, then we put border the border come outside the box.
				
					Box-sizing: border-box;
				
			

The Display Property

Block

Inline

Inline-Block

Breaks onto a new line.

Box does not break onto a new line.

Width and height are respected

Width and height are respected

Width and height do not apply.

Box does not break onto a new line

Padding, margin and border cause other elements to be pushed away from the box.

Vertical padding, margin, and borders apply but do not cause other inline elements to move away from the box.

Hybrid of inline and block

The box extends in the inline direction to fill available space in container (unless styled otherwise).

Horizontal padding, margin and border will apply and cause other boxes to move away.

Padding, margin, and border will cause another element to be pushed away from the box

  • Span is inline by default, we add margin, the margin reacts only in left and right. Then we add display property
				
					Display: block;
				
			
  • It could be block, inline, inline-block, flex, grid, etc.

Display None

  • Display none means hides the element.
				
					Display: none;
				
			

Negative Margin & Margin Auto

  • Negative margin is just opposite site of margin in negative values.
				
					Margin: -34px;
				
			
  • Margin auto is just taking margin according to size of box. But it’s only in left and right align not top and bottom.
				
						Margin: 0 -auto;
				
			

Margin Collapsing WTF

  • Margin is collapsing within top and bottom, but not left and right.

A Common Layout Pattern

  • If we have three elements, taking margin of same sizes we add a div element and apply margin of 80px then all three elements have same margin at once.

Min and Max Width

If we fix width as:

				
					max-width: 500px;
min-width: 300px;
				
			
  • Does not shrink larger than 500px and lesser than 300px.
				
					Width: 80px;
				
			
  • We change the unit because of responsiveness.

Rounding Elements with Border Radius

Border Radius:

  • The radius changing of corner of the box.
				
						Border-radius: 20px;
				
			
  • We also use percentage value. One value is for all, two value one for top and bottom and other is left and right, three values, one for top, second for right and left and third for bottom and four values, for all four sides.

Box Shadows

				
						Box-shadow: 2px 1px 3px blue;
				
			
  • First for offset-x, second for offset-y, third for blur, and last is color.

Working With Overflow

  • If we have height and width with 200px both and font inside larger value then the text is overflow that is why we use
				
					Overflow: scroll;
				
			
  • There are three values hidden, visible, and scroll. With scroll we scroll within the box and with hidden value the text cut-off where the box ends.
  • There is also two direction x and y.
				
					Overflow-x: hidden;
				
			
  • You don’t scroll in x direction.
Facebook
Twitter
LinkedIn
Pinterest
WhatsApp

Table of Contents

Ghulam Ahmad is an Excellent Writer, His magical words added value in growth of our life. Highly Recommended

Read more about poetries

Ahmad Shafi Poetry

2 Responses

  1. I found myself deeply moved by this piece. It’s rare to come across writing that doesn’t just inform but transforms. You’ve taken complex ideas and made them feel not just understandable but important. Reading this, I felt both challenged and comforted — a true testament to the power of your words.

Leave a Reply

Your email address will not be published. Required fields are marked *