Formatting a login message with CSS
In this article I'll explain how to format a login message by using CSS. To view the examples, JavaScript must be enabled in your browser.
Table of contents
The markup
We start with the following markup:
Listing 1. Markup for a login form
<form action="javascript:validate(this)" method="post"><div id="content"><h2>Login</h2><p>To log in, just answer this simple question: Snoopy is a dog, a cat or a horse?</p><input type="text" id="txt" name="txt" /><input type="submit" id="btn" name="btn" value="Log in" /></div></form>
The JavaScript code
We use JavaScript only for a demonstrative purpose. In such cases, a more robust server-side language is strongly recommended. As we've seen in the markup above, we've built an accessible CAPTCHA to prevent robots from logging in. The user should answer a simple question to log in. The JavaScript code is the following:
Listing 2. The JavaScript code
function okLogin() {var content = document.getElementById("content");var result1 = document.createElement("div");result1.id = "ok";result1.innerHTML = "Login successful!";content.appendChild(result1);}function badLogin() {var content2 = document.getElementById("content");var result2 = document.createElement("div");result2.id = "bad";result2.innerHTML = "Login failed!";content2.appendChild(result2);}function validate() {var str = document.getElementById("txt").value.toLowerCase();var re = /dog/if(str.match(re)) {okLogin()}else {badLogin()}}
The first two functions create two different elements, with two different text messages and two unique IDs
(ok and bad) and then add these elements
to the form. The third function, validate(), uses a regular expression to check the input inserted by the user. If
the user's input matches the regular expression, it will be returned the output of the first function, otherwise the output of the
second. To see how this works, let's take a look at the following two images. The first depicts the document tree of the page
before the submission of the form, while the latter shows the page after that submission.
Figure 1. The document tree before the submission of the form
Figure 2. The document tree after the submission of the form
Styles for the login message
The styles for the login message are relatively simple.
Listing 3. Styles for the login message
#ok, #bad {width: 350px;height: 100px;line-height: 100px;text-align: center;color: #fff;font-weight: bold;font-size: 1.4em;text-transform: uppercase;margin-top: 1em;}#ok {background: #0c0 url("../img/ok.png") no-repeat top left;}#bad {background: #c00 url("../img/fail.png") no-repeat top left;}
As you can see, we've used a different background image on each message. Since we want to be sure that the text will be legible also when the background image is not available, we've also specified a background color on each element. To center the text vertically, we've set the line height value to a value that equals the height of each box and then we've added a top margin in order to leave enough room between the message and the other form elements.