CSS Defaults... Mere Suggestions
Join the DZone community and get the full member experience.Join For Free
html has no style
probably the most important key element that prevents a better understanding of the separation of style and content concept is css defaults. while these defaults are extremely useful and make our everyday job a lot more enjoyable, they eclipse a small yet important part of our job that leads to a lot of confusion for people not aware of these defaults actually kicking in.
we all know that html is not meant for styling, yet there is a big different between the behavior of a standard div, span or p element in a browser. a div will behave like a block element while a span will behave like an inline element. and a p looks a lot like a div but comes with stardard margins. so even though you might be writing simple html and haven't even started your first line of css, a minimum of styling is already applied to your html document.
most people go on to assume that this basic styling comes from the html elements themselves. this might sound plausible at first but goes right against our base axiom that demands separation of style and content. even then you could argue that front-end developers usually prefer breaking rules rather than following them, but in this particular case things are a little different.
what happens is that each browser has its own default css file it applies to a html document. these default css files might map quite well between browsers but you cannot assume that each browser will render the same default styling. hence part of the reason why most people prefer to use some kind of reset css, leveling the standard styling between all (or most) browsers.
if you check firebug (or most other similar tools) you can see traces of these default css declarations. just highlight a simple div and see how the browser css applies a display:block style. if you want to be a true adventurer you can even go and change these css files, though there's quite little to be gained there. this should also explain why brand new tags (html5) don't come with standard styling in a browser (if you were wondering about that).
conquer your fears
some people seem a little scared to start tinkering with these css defaults, especially when it concerns the display property. but even the most basic css rules often changes common css defaults. simply adapting the color of a link means overwriting the default browser css. overwriting a display property isn't any different from that and shouldn't be considered a hack at all (which some people seem to suggest).
you might remember the little table-row/table-css hype from a little while ago. use divs to create your own table layout. this never really took off (for various reasons, but mainly because tables have very limited positioning potential) but the essence of this little trick is still quite important. you might be using simple divs, but you're creating a table layout in css. the perfect example that styling and layout is not inherently connected to html.
you can also go the other way around. working on some crappy project with table-layouts? simply set the table, tbody, thead, tfoot, tr and td tags to display:block and they will behave like normal block elements. you can start floating tr elements to turn them into columns, or you can use absolute positioning to allow for more flexibility in your layout changes. and once again, remember this is not a hack, just plain and normal use of css as it was intended.
while css defaults are a useful necessity guaranteeing the separation of content and style, you should not be afraid to change them around to better match the layout you need to create. it all starts by realizing that the basic browser styling is just some simple default css hidden away in a separate file. if you get to that point, overwriting it is just the next logical step.
Published at DZone with permission of Niels Matthijs, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.