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!

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Build modern and responsive webpages

The Modern Web

Build modern and responsive webpages Try Front-end

The Modern Web will guide you through the most important tools of device-agnostic web development, including HTML5, CSS3, and JavaScript. The plain-english explanations and practical examples emphasize the techniques, principles, and practices that you'll need to easily transcend individual browser quirks and stay relevant as these technologies are updated.

$ Check price
34.9534.95Amazon 4.5 logo(30+ reviews)

More Front-end resources