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

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

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Ad