box model presentation - leigh...

22
First, let’s start with some simple paragraph text… The Box Model Explained

Upload: others

Post on 20-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

First, let’s start with some simple paragraph text…The Box Model Explained

Page 2: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s
Page 3: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

Next we will add a background color to <p> to see how it renders.

Page 4: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s
Page 5: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s
Page 6: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

So let’s set the width to 50%.

Page 7: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s
Page 8: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

Now we can see how a background image works.

Page 9: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s
Page 10: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

To illustrate how these layers stack….

Page 11: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

Visualization of CSS Element Depthtop: “p” element middle: background-image bottom: background-color

Page 12: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

Now that we have explored the layers of a CSS element, we can take a look how it expands outward.

Page 13: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s
Page 14: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s
Page 15: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

We can extend our visual edge of the element out a little bit further by adding a border.

The border is literally the edge of the element’s boundary.

Page 16: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s
Page 17: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

Ideally, an element like this one would also need margins added so that is does not hug the edges of other elements.

In this example, the <p> element is hugging the left edge of its parent element, the <body>.

Page 18: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s
Page 19: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

10%

10%

auto auto

The browser subtracts the cumulative set width of <p> from the parent’s width and then divides it in half to automatically assign left and right margins with the “auto” value.

Page 20: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

Notice, however, that after added padding and borders, we are far off from our original goal of achieving an overall 50% width.

70% + 4px border

~15% ~15%

Page 21: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

It is now safe to design for modern browsers using the CSS3 property, box-sizing.

The example property above indicates that the element’s box model will be resized so that padding and border will be calculated to fit

within the specified “width” of the element.

It basically makes the math a lot easier.

box-sizing: border-box;

Page 22: box model presentation - Leigh Cotnoirlearn.leighcotnoir.com/wp-content/uploads/2016/02/css-box-model... · property, box-sizing. The example property above indicates that the element’s

25%25%

50%