WEBD106 - Introduction to Web Development Technologies

Module 2.3 - Visual Formatting Properties

Throughout this module, you may use the on-screen arrows or the and keys on your keyboard to navigate between sections. When you are ready to begin, click on the "begin" button below.

WEBD106 - Introduction to Web Development Technologies

Module 2.3 - Visual Formatting Properties

Background

So far in our exploration of CSS, we've primarily focused on learning how to recognize and write well-formed CSS properties and apply them to HTML elements. Up until now, we've been practicing using properties that control the Box Model (such as margin, padding, and border).

The Box Model

While this is excellent for sprucing up content in the box that it’s originally contained, our document still reads top-to-bottom in the same order! What if we wanted to position two of our elements next to each other, have them overlap, or have one element stay stuck to the top of the page at all times, like the header?

This is where CSS Visual Formatting Properties come in. These properties can help us perform all the actions listed above, but also builds the foundation for many key scenarios in the future. Being able to position elements where you want, when you want is crucial for responsive web design, in which the formatting of your page adapts to any device regardless of screen size. In addition, we’ll explore dynamically changing these properties to make elements visually respond to the user’s input in the JavaScript section of this course.

 

Learning Objectives

Given an example scenario in which CSS changes are required, you should be able to determine if visual formatting properties are required to produce the desired change(s), as well as differentiate visual formatting properties from other types of CSS properties.

At the end of this module, you will be given an assessment with several of these scenarios in word problem format. You must complete the assessment with a score of at least 80% to proceed to the next module.

What You'll Need

You should have already completed Module 2.1 - Introduction to CSS and Module 2.2 - The CSS Box Model, which cover the relevant information on CSS property syntax and the CSS Box Model that you should have prior to starting this module.

While not required, you may find it helpful to have access to a computer with an Integrated Development Environment (IDE) if you'd like to try the example CSS changes on your own to see how everything works. Microsoft Visual Studio Code, Adobe Brackets, and Komodo Edit are popular, free options for an IDE. For viewing your code, any modern web browser is supported, such as Chrome, Firefox, Edge, or Safari.

Definition

The first step to recognizing and classifying visual formatting properties is to lay down some concrete statements about what they are (and what they aren’t).

A visual formatting property is a type of CSS property. The group of visual formatting properties are any CSS properties that provide instructions to the web browser on how the element they’re applied to should interact with other elements.

By itself, a visual formatting property will never affect the color or size of an element - other CSS properties must be present in conjunction with one in order for those changes to occur.

Visual Formatting Properties in Action

Below are three examples of good, valid usage of visual formatting properties. For each example, slide the toggle switch on and off to see how the example code affects the output on the right.

Display

This is a valid example of a property that instructs the boxes on how they should behave related to one another. Try it for yourself by operating the switch underneath the code chunk.

.box-1, .box-2, .box-3 {
    display: inline-block;
}
.box-1
.box-2
.box-3
Without CSS, these boxes reserve all the space to the right of them.
The CSS property tells the boxes they can allow other elements next to them.

Visibility

In this example, we want to hide the middle box (.box-2) from the user, but we also want to keep the space between Boxes 1 and 3. If we just delete Box 2, then Box 3 would tuck underneath Box 1, so instead we'll need a visual formatting property.

.box-2 {
    visibility: hidden;
}
.box-1
.box-2
.box-3
We want to hide this box, but preserve the space it occupies.
Box 2 is now telling the other boxes: "You can't see me, but I'm still here. Don't step on me!"

Overflow

So far, we've looked at examples on how elements interact with their siblings. However, visual formatting properties are also used to tell elements how they should interact with their parents or children (elements that are inside of other elements).

.box-2 {
    overflow: hidden;
}
.box-1
.box-2

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

.box-3
We need to tell Box 2 that it shouldn't let its contents overflow like this.
Box 2 is now communicating to all its children that they can't display outside the box. Notice how this doesn't change the text wrapping, just chops it off!

Counter-Examples (a.k.a. Non-Examples)

On the surface, these might appear like visual formatting properties. However, we'll take a look at why these examples don't quite fit the definition.

Color

While this CSS does hide the label for Box 2 from view, it doesn't change how any of the elements behave in relation to one another, which is a key part of the definition. (The label is actually still there, and you can click and drag your mouse across it to highlight it).

.box-2 span {
    color: rgba(0,0,0,0);
}
.box-1
.box-2
.box-3
We will make this Box's label transparent to "hide" it.
No formatting has changed. This is also an improper use of the "color" property. Remember: Visual Formatting Properties never alter color or size on their own.

Width and Height

The width and height properties are great for setting fixed dimensions of an object, but not the best choice for altering the visual structure of the page. In this example, we try to "remove" the second box by setting its dimensions to 0x0. Not only is the box's content and border still visible, the other two boxes still allow it space because they haven't been told to ignore it.

.box-2 {
    width: 0;
    height: 0;
}
.box-1
.box-2
.box-3
We can set this box's width and height to 0 to move Box 3 up on the screen.
You can still see the content of Box 2! In addition, Box 3 is still allowing it some space, and won't overlap it (even though it's "technically" taking up 0 by 0 pixels).

Margin

Margin can be used to cause elements to overlap one another like in the example seen here. However, like the other two non-examples, the box that's being adjusted isn't giving instructions to any other elements on what to do. It moves with complete disregard to anything around it, and would even move up off the top of the page if the amount of movement was high enough.

.box-2 {
    margin-top: -30px;
}
.box-1
.box-2
.box-3
We're going to move this box up.
Box 2 doesn't care what's above it, and will move 30 pixels upward regardless. Box 1 also doesn't care that something is on top of it now.

Practice Exercises

Now try deciding whether each example uses a Visual Formatting Property on your own. These function the same way as the examples, but you won't receive an explanation or feedback until you choose an answer.

Exercise 1

A box and a graphical shadow are re-ordered so that the text box properly sits on top of its shadow.

.box-1 {
    z-index: 0;
}
.box-2 {
    z-index: 1;
}

Is a Visual Formatting Property being used here?

That's right — the indexing of the elements is letting the boxes know how they should lay on top of each other. Since Box 1 has an index of '0', it's at the lowest level, and Box 2 having an index of '1' means it would go over top of anything with an index of 0.
Let's take a closer look at what this CSS code is doing. It's saying that Box 1 should have an index of 0, while Box 2 should have an index of 1. Elements will compare their index values and the one with the higher index will display on top. Does this behavior sound like a Visual Formatting Property?
That's right! This is an example of a Visual Formatting Property, because the elements are changing how they behave in relation to one another depending on this property. Without it, they wouldn't know which one should go on the top of the stack.
Since the two boxes occupy the same space on the screen, they need to work out which one should display on top. Let's think back to the definition of a Visual Formatting Property: It's a CSS property that affects how elements react to one another. This z-index property fits that definition, and thus it would be considered a valid example. Consider taking a look at the definition or examples again by using the navigation on the left to re-visit those sections.
.box-1

This is some sample text. We need to have it visible, with the background "shadow" element behind this pane.

Exercise 2

The size of some hard-to-read text inside a box is increased.

.box-1 {
    font-size: 14px;
    line-height: 18px;
}

Is a Visual Formatting Property being used here?

Correct — you may remember from the definition that a Visual Formatting Property never alters the size or color of an element on its own. In addition, simply changing the size of the font doesn't provide any information on how other elements should interact with the text.
This is not an example of a Visual Formatting Property. Recall from the definition that a Visual Formatting Property never changes the size or color of an element on its own. In this example, only the font size changes, which doesn't tell any other elements how to behave in relation to it.
More sample text. In this example, we're making this text bigger and more readable.

Exercise 3

The height of a box is increased so that the white background can cover all of the text inside (which is currently overflowing outside of the box).

.box-1 {
    height: 150px;
}

Is a Visual Formatting Property being used here?

Right! The height of the box is changed so that the background covers all the text, but this happens independently of any other element. In addition, the size of the box changes, which Visual Formatting Properties don't do.
Not exactly. Similar to the last example, simply changing the size of the box doesn't give instructions to any other elements on how they should interact with it. It also changes the size of the box, something that Visual Formatting Properties don't do.

This text should be overflowing the text box by default. Let's write some CSS to change that. Flip the switch!

Exercise 4

Absolute positioning is used to pull an element out of the structure and overlay it on top of all other elements.

.box-1 {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 50%;
    height: 50%;
    transform: translate(-50%, -50%);
}

Is a Visual Formatting Property being used here?

Yes, at least one Visual Formatting Property is used here. The overall effect is that the red box and green box are now interacting differently with one another in that they overlap and the green box is ignoring the position of the red box in the window. By definition, a Visual Formatting Property has been used to make this happen.
Although some of the properties in the CSS code are not Visual Formatting Properties, at least one of them is. Instead of focusing on the properties themselves, try and focus on the effect that all of the code has on the resulting graphic. There is certainly instruction that tells the red and green boxes how they should behave with one another. In this case, the red box is positioned "absolute", and other elements (like the green box) don't need to take its position in the container into account at all.
WOW! I'm a big ol' overlay.

Just another inconspicuous box. Nothing to see here...

Assessment

Fill in your answers according to the directions in each section. When you are finished, click on the "Submit" button at the bottom of the quiz. You will receive your score immediately. Reminder: You need a score of at least 80% to proceed to the next module.

Part 1: True or False (1 pt. each)

Select an option from the dropdowns to mark each statement as True or False.

Part 2: Scenarios (2 pts. each)

For each scenario, select the correct answer from the list of the available options (multiple choice).

3. JoJo wants to hide the error messages on a form unless there's an error. Since the form's background is white, she just sets the color of the error messages to white, and writes code to turn them red when an error happens. Later on, the company gets a call from a customer with a visual impairment, who complains that his screen reader is reading off error messages even though all of his information seems correct. What's happening?



4. The content creation team provides Justin, the developer, with several paragraphs of content to import into the website. After loading all of the content, he notices that one of the sections on the website with a fixed height now has text spilling out of the bottom. It's important for this area's height to remain unchanged, so he writes some CSS to create a scrollbar inside the section. Now, all of the text is contained in the section, and the user can scroll through it to read it all. What's the best statement that describes this situation?



5. Jessica receives feedback from the support team that users are finding it difficult to read the text in the "About" section of the company's homepage. She takes a look and - no wonder! The text is dark red on a black background, which the previous designer had implemented because of the company's black and red brand colors. To fix this, she simply lightens up the shade of the red text slightly, and converts the background to white with a black border instead. However, the text looks a little small, so she increases the font size while she's working on the code. Which statement best describes this solution?