Centering Horizontally

It should be easy to center a page horizontally in the browser window.

All you have to do is wrap your content in a div, give this div a width (say 760px so it fits nicely on a screen resolution of 800 x 600) and set the left and right margins to ‘auto’.

#centeringwrapper {
width: 760px;
margin: 0 auto;
}

This works in Firefox and other browsers which display CSS correctly, but of course with Internet Explorer nothing is that straight forward.

IE doesn’t interpret the automatic left and right margins. However we can use another incorrect CSS interpretation to achieve the desired effect in IE. If you apply a centering text alignment to the body tag, IE applies it to all divs within the body, thus centering them. However, this also centers all body text on the page so we need a further fix to reset the text to align to the left.

Finally, the body tag needs to have a minimum width equal to the width of the centering wrapper. This deals with an issue in older Mozilla browsers where reducing the size of the browser window results in half of your centered div hanging off the left of the page.

So the final CSS looks like this:

body {
text-align:center; /* IE centering fix */
min-width: 760px; /* Mozilla resizing fix*/
}
#centeringwrapper {
width: 760px;
text-align:left; /* Negates IE centering fix in other browers*/
margin: 0 auto; /* Ccentering in non-IE browers*/
}

Lorem Ipsum Generator

Lorem Ipsum text has been used for centuries as for filling out page layouts to see how the text will flow and what the design will look like. The spacing and structure is the same as real text, but because it is Latin you are not distracted by the meaning of the words.

www.lipsum.com provides a Lorem Ipsum Generator that outputs however many paragraphs or sentences text you need.

Preventing Spam Form Submissions With CSS and CGI

There are a number of free PHP (and ASP) contact forms and captchas for preventing spam submissions through your contact forms, but what if you don’t have PHP? Or, you just want to stop the spam and not make your human visitors jump through hoops?

The solution I found is to use this anti-spam solution using fake form fields and combined it with the FormMail CGI script from Matt’s Script Archive. (CGI is available on even the most basic hosting package).

In the HTML contact form, add a fake textarea field with an obvious name such as ‘comments’ or ‘message’. Rename the genuine field to something less obvious or completely irrelevant, for example ‘wibble’.

Create a CSS class with the rule ‘display: none;’ and apply it to the fake field. This will make it invisible to human users and they won’t be able to enter text into the fake field. However, the spam bots will spider the code, ignore the styles and fill the fake field with the usual links to crappy websites.

All that remains is to test for content in the fake field in the FormMail CGI script to decide if a submitted form is spam and if so, not send it.

To do this, open the FormMail.cgi file in a text editor and find the send_mail function. The whole function needs to be enclosed in an if statement that checks whether the fake ‘comments’ field is empty. The code to accomplish this is:

sub send_mail {

# Only send email if spam trapping field is empty #
if ($Form{'comments'} eq '') {

# ....rest of send_mail function.... #

# closing bracket for spam if statement #
}

}

Taking it further

It needn’t just be the comments textarea field that is faked, you can fake any other field in a form and add additional tests to the CGI script to see if they are spammed.

One potential problem is what happens if the normal human user has disabled stylesheets, is using an old browser that doesn’t support them or is using an alternative stylesheet for accessibility reasons? They could see the fake fields, fill them in and find their mail is not sent. The answer is to put a note next to the field explaining that it is to there detect spam and that it shouldn’t be filled in. (Or you could set the default text in the field to something similar). Give the text the same class as the fake field and it will disappear when viewed using your default stylesheet.

(You can also make the default size of the fake fields as small as possible to discourage users from filling them in).

Another problem is that the spammers may learn to recognise display: none; and realise that they are filling in hidden fields. However, there is no sign of this yet and if it is found out, there are alternative ways of hiding fields such as large negative indents, which could extend the usefulness of the technique.

Close
E-mail It
Socialized through Gregarious 42