Home

How to Use the Clear Attribute to Control Page Layout for HTML5 and CSS3 Programming

|
Updated:  
2016-03-26 13:16:49
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

There should be some way to make the form work right, regardless of the container's width. CSSS3 provides exactly such a mechanism that is easy to work with when designing your HTML5 pages.

The clear attribute is used on elements with a float attribute. The clear attribute can be set to left, right, or both. Setting the clear attribute to left means you want nothing to the left of this element. In other words, the element should be on the left margin of its container. Each label should begin its own line, so set its clear attribute to left.

To force the button onto its own line, set its clear attribute to both. This means that the button should have no elements to the left or the right. It should occupy a line all its own.

If you want an element to start a new line, set both its float and clear attributes to left. If you want an element to be on a line alone, set float to left and clear to both.

Using the clear attribute allows you to have a flexible-width container and still maintain reasonable control of the form design. The form can be the same width as the page and still work correctly. This version works, no matter the width of the page.

image0.jpg

Here's the final CSS code, including clear attributes in the labels and button:

/* floatForm.css
 CSS file to go with float form
 Demonstrates use of float, width, margin, and clear
*/
fieldset {
 background-color: #AAAAFF;
}
label {
 clear: left;
 float: left;
 width: 5em;
 text-align: right;
 margin-right: .5em;
}
input {
 float: left;
 background-color: #CCCCFF;
}
button {
 float: left;
 clear: both;
 margin-left: 7em;
 margin-top: 1em;
 background-color: #0000CC;
 color: #FFFFFF;
}

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].