[l56] Tips for a clean[er] website: Part 1

Posted by leemcd56 on March 6, 2011, 11 a.m.

Everyone knows cleanliness is next to Noodliness so why stop at personal hygiene? From the makers of Insta-Kill – when you want YOUR significant other insta-gone – bring you Clean Syntax, the proper way to write a website.

First off, this is the 21st century people. To hell with the now deprecated HTML4 DOCTYPE declaration. It's as simple as THIS now:

<!doctype html>

Oh yes they did. Now you can make a website with code as simple as this:

<!doctype html>
<html>
<head>
<title>Hello world</title>
</head>
<body>
Test
</body>
</html>

It doesn't stop there, however. Now pretty much anything can be controlled with CSS3. Instead of using a DIV container to align every element in the page (<div align="center">) one should now use this:

<div style="margin-left: auto; margin-right: auto"></div>

Oh hell no they didn't! It's also highly recommended that if you have a dynamic-width website to use the max-width and min-width properties. In other words, if you have body content that sizes with the browser at say… 80% and the browser is in fullscreen 1900px, most body content will stretch out pretty terrible if you're not careful. To constrain any element, including IMG tags, use the aforementioned properties:

<div style="max-width: 800px; width: 80%"></div>

If you are like me and prefer to use browser-based default font size, one would like to use the EM modifier to work with all fonts. If the font size for the an element is declared, all child elements can use EM to use its size. Say the font size for the body is 12px and an inner-child element is 1.5em. 12 + (12 * .5) = 18, thus the element's font size would be 18px. It's pretty handy if you're working with OCD.

Oh and here's a little PHP tip. If you like to keep source code for your web site clean on client-side instead of a jumbled mess use line breaks inside of each written display. Say one wants to echo "Hello world!<br />". In reality this would be generated:

Hello world.<br />Futher body content.

By simply adding a line break, you can do this:

<?php
echo "Hello world!<br />\n";
?>

Hello world.<br />
Futher body content.

If you have any questions, ask and I will explain in the next blog. Until next time!

Comments

sirxemic 13 years, 8 months ago

Quote:
To hell with the now deprecated HTML4 DOCTYPE declaration.
Unfortunately a lot of people are still using out-of-date browsers, so doctype declarations are still necessary for them.

Quote:
<div style="margin-left: auto; margin-right: auto"></div>

<div style="max-width: 800px; width: 80%"></div>
EEEWWW

If you are gonna suggest stuff at least do it right. You declare classes in a CSS file and then give divs those classes '>_>

Also, there is a PHP function ln2br which replaces newlines with '<br />\n'.

flashback 13 years, 8 months ago

No, id is useful for one-off instances. Get that CSS out of the html.

KaBob799 13 years, 8 months ago

Well I know 2 reasons people prefer to do it that way, it makes restyling the entire website easier and you can cache the .css so it speeds up loading time.

PY 13 years, 8 months ago

HTML is content, CSS is styling. Keep the two separate and it becomes a lot easier to edit one without affecting the other. For example, if you're hardcoding your CSS you can't use multiple optional stylesheets, you can't cache the data (Which for large amounts of CSS or data could make a not-insignificant difference)

On most scales and applications, though, there's nothing inherently bad about it, it's just a case of always sticking to conventions. It's just something you do, so that when you need to do it it doesn't catch you out because you're not used to it.

KaBob799 13 years, 8 months ago

Quote:
Unfortunately a lot of people are still using out-of-date browsers, so doctype declarations are still necessary for them.

Well actually, I'm pretty sure the HTML5 doctype works just fine in pre-html5 browsers. But out of date browsers (normally IE7 or FF on XP) do have issues with other parts of html5. Luckily the number of people using those browsers is decreasing since old computers like that are falling apart =p

leemcd56 13 years, 8 months ago

Quote:
Also, there is a PHP function ln2br which replaces newlines with '<br />\n'.
Wrong. Simply putting nl2br("Hello world.<br />"); will not generate this:

Hello world.<br />
Futher body content.

nl2br() is for external data to be line broken properly. Get your facts straight. In order to correctly break in PHP from echo and print functions, a manual line break must be used.

Quote:
Well actually, I'm pretty sure the HTML5 doctype works just fine in pre-html5 browsers.

Yes!

eagly 13 years, 8 months ago

What PY said.

PY 13 years, 8 months ago

@leemcd; sirXemic did not say it would, he said it would replace newlines with <br />\n. Which it does. "Hello world.<br />" has no newlines, nobody implied nl2br would do anything to it.

leemcd56 13 years, 8 months ago

Quote:
@leemcd; sirXemic did not say it would, he said it would replace newlines with <br />\n. Which it does. "Hello world.<br />" has no newlines, nobody implied nl2br would do anything to it.
Yes, he did state this. However, it seemed he implied adding "\n" was improper. If he didn't, his comment was still unnecessary.