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
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).
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.
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.
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.
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.
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.
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;
}
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;
}
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;
}
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.
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.
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);
}
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;
}
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;
}
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.
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?
This is some sample text. We need to have it visible, with the background "shadow" element behind this pane.
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?
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?
This text should be overflowing the text box by default. Let's write some CSS to change that. Flip the switch!
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?
Just another inconspicuous box. Nothing to see here...
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.