04. Boxes

You may find the need to create a surrounding box around an element for better manipulation.

Creating a Box

To create a box in HTML, we used either <div> or <span> tags.

Recall that that <div> is a block-level element, while span is an inline-element. This means that <div> will take up the entire line, pushing its surrounding content above or below it, while span inserts itself within the line.

We can set the dimensions of this box in CSS with the height and width properties.

Choosing the Appropriate Units

To set the length and heigh values, we must specify a unit. The most common units include pixels, percentages or ems.

Just in time to review our CSS lengths!

  • Pixels are popular because they allow developers to accurately control their size.
  • Percentages are relative to the size of the browser window, or the box's containing box. This can be useful especially if you want a fluid layout, and want your page size to be relative to how stretched out the browser window is.
  • If you use ems, the size is based on the text within the box. This is useful for scaling up in case the user increases the text size of his or her browser.
Ems are getting more popular, as developers need to account for different sized screens.

Setting a Maximum Dimension

You can limit the width or height with themin-width, andmax-width properties. These properties are useful in case your element contains text. You want to avoid your lines of text being too wide, as you having the user move from screen to screen can tire out his or her eyes!

Build modern and responsive webpages

Responsive Typography

Build modern and responsive webpages Try Front-end

Responsive web design helps your site maintain its design integrity on a variety of screen sizes, but how does it affect your typography? With this practical book, graphic designers, web designers, and front-end developers alike will learn the nuts and bolts of how to get the best appearance from type without sacrificing performance on any device.

$ Check price
24.9924.99Amazon 4.5 logo(3+ reviews)

More Front-end resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources