Relative and absolute positioning can be both useful and tricky, as it is easy to make mistakes.
If you are looking to create multiple columns in your layout, the traditional method of using the float
property works well for legacy browsers. However, there are newer properties available that may be more efficient, but they lack support in older browsers. Therefore, it is recommended to use them only if you are certain your audience uses modern browsers.
An Easier Approach
CSS floating can be challenging, so a responsive grid system is highly advised. There are various CSS grid systems available for download, where you simply need to implement the appropriate classes for your columns.
- Bootstrap: http://getbootstrap.com/ (be cautious as it comes with many extras)
- Skeleton:
- Pure.css:
- Responsive Grid System:
And many more options to choose from.
The More Challenging Route
Floating elements remains a popular choice for creating columns. It involves setting block elements (like divs) side by side with specified widths for each column. While this approach can be effective, managing gutters and padding can be cumbersome, leading to the recommendation of utilizing pre-existing stylesheets. Additionally, adding an element with the clear
property may be necessary after several columns to maintain proper layout flow.
Alternatively, depending on your user base, consider exploring CSS features such as columns, flexbox, or grid.
When to Avoid Absolute/Relative Positioning?
Browsers typically handle content flow automatically, adjusting for growing elements. Relative positioning maintains this behavior, while absolute positioning overrides it by placing elements on top of others, necessitating manual adjustments for spacing and layout.
Although relative/absolute positioning can be useful for overlapping elements, like images and text within containers, it is best used sparingly to prevent disrupting the natural flow of content.