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

JavaScript & JQuery

Build modern and responsive webpages Try Front-end

This book was written for anyone who wants to use JavaScript to make their websites a little more interesting, engaging, interactive, or usable. In particular, it is aimed at people who do not have a degree in computer science (well, not yet anyway).

$ Check price
39.9939.99Amazon 4.5 logo(385+ reviews)

More Front-end resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Ad