There are a lot of talks how to make an HTML page more flexible to style by using CSS files without touching HTML file.
This sample pages where for a same HTML page we have different kinds of styles from a competition to show the power of CSS: CSS Zen Garden – Beauty of CSS Design in the page you can display all of designs using same HTML file without changing it. Totally different styles which also includes responsive UI and animations.
Is each HTML file always like this? There are some hints of do-s or don’t-s developers need to take care to avoid the rigidity of styling.
Avoid CSS inline style as much as possible
Inline style is not un-useful for simplicity e.g. if we want to style HTML e-mail, or a single static page where we does not have capability to include external styling file.
But once we plan to have dynamic styled page, we need to avoid it as possible (since the internet browser will prioritize inline style (embedded style) higher than CSS file style from external source).
Have HTML with Structural Semantic instead of Content Only
Instead of having unstructured content, in case there are structural concept of the page, structure it using structural semantic. If you are working with HTML5, use the semantic tags (main, sections, navigations, headings, headers, footers, figures, etc.). If you are using previous HTML versions use div and span semantics to structure the page.
Define Class(es) for Each of HTML Structural Section
Define class(es) of each structural section of the HTML pages make it more easier to select it within CSS instead of complex selection using tagging. It will make the code also more readable for the CSS files for the sections to be styled.
If I am talking about structural section, I did not mean only for section tag of HTML5 but also for div, span, table, li, nav, etc, anything related with structural semantic which are not content related (like paragraph, etc.).
Avoid of Using of br-tag for Styling Space
The br-tag is useful to create line break inside content. But br-tag should not be use for styling. Style the spaces using margin and padding feature in CSS instead.
Use Shadow DOM Selector in case of Working with Capsulated Components
In case you are working with components, e.g. using Angular Framework, React, etc. Your CSS inside the component seems to be un-editable. You might think you can’t edit it from parent component. However this is not true.
Components based frameworks use so called Shadow-DOM technology. You can still modify the HTML styles from parent using Shadow-DOM selector (:host etc.) without changing the HTML of the sub-components.
Useful Links
Other references:
- CSS
- HTML5 and new structural semantics
- Shadow DOM