CSS Design Patterns
- Summary
-
Discussion
- What are the design patterns pertaining to the CSS Box Model?
- What are some traditional CSS patterns for layout design?
- What responsive layout patterns are possible with CSS Flexbox and CSS Grid?
- What CSS design patterns help control spaces?
- Which are the different categories of CSS design elements?
- Is it a good idea to adopt a CSS framework?
- What are some CSS anti-patterns that I should avoid?
- Milestones
- Sample Code
- References
- Further Reading
- Article Stats
- Cite As
To become a good web designer/developer, it's not enough to know CSS properties and their values. We need to know the contexts in which each property-value pair is applicable, how results differ in different contexts, and how various properties interact. Rather than memorize hundreds of rules and their exceptions, it's more efficient to get familiar with contexts of usage and combinations of properties applicable in those contexts. This is what CSS design patterns is all about.
Design patterns are common in programming, such as in object-oriented programming. In HTML/CSS, design patterns are those that work across browsers and devices. They manage complexity, mitigate code bloat, and improve productivity. They're the building blocks that facilitate the creative process: the designer picks known patterns, adjusts them to current context and combines them for a final result.
Discussion
-
What are the design patterns pertaining to the CSS Box Model? Common ways to display an element are
inline
,inline-block
andblock
. Special ways includelist-item
andtable
. We can also take out an element from its normal flow using CSS propertiesposition
andfloat
. More sophisticated ways to layout elements include CSS Flexbox, CSS Grid, and CSS Multi-column.Bowers et al. talk about three patterns that affect box dimensions:
- Sizing: manually assign absolute or relative sizes
- Shrinkwrapping: shrink to fit the content
- Stretching: stretch to fill its parent container
These can be combined with positioning patterns indenting, offsetting, and aligning. Specifically, a stretched element can be indented and this affects its dimensions. A sized or shrinkwrapped element can be offset or aligned without affecting its dimensions.
Much of this is achieved by applying CSS rules to parent and grandparent containers of the element. These rules mainly specify dimensional properties, positional properties and margins.
-
What are some traditional CSS patterns for layout design? Two patterns are not recommended: fixed-width layouts since measurements are absolute and tables since table cells "can't flow" when viewport changes.
One design pattern is to use columns whose widths adjust to viewport width. On smaller screens, columns reflow into rows. This is called fluid layout and is achieved using properties
float
,width
,min-width
andmax-width
.In elastic layout, overall width is set relative to some design element such as font size. A hybrid layout is a combination fixed and fluid/elastic layouts.
Property
width
is actually an element's inner width. Sometimes we wish to fix the outer width in design and then adjust the margin, border and padding without affecting the layout. This is possible by having an element within another element, called outside-in pattern. The inner element includes margin, border and padding but the outer element has none.In fluid layouts, mixing fixed values and percentages is not ideal but can't be avoided. Using percentages isn't possible for borders. It's common to use fixed values for margins and padding, and percentages for width. This problem is solved by the outside-in pattern.
-
What responsive layout patterns are possible with CSS Flexbox and CSS Grid? In 2019, LePage used Flexbox and media queries to implement five layout patterns:
- Mostly Fluid: On small screens, content reflows and columns stack vertically. On bigger screens, margins are adjusted.
- Column Drop: Columns stack vertically on smaller screens.
- Layout Shifter: Content moves around when screen sizes change. Most responsive with many breakpoints but also more complex to maintain.
- Tiny Tweaks: Only small changes involving font sizes, image dimensions, or content offsets.
- Off Canvas: On smaller screens, less frequently used content is kept off-screen but accessible with a single click.
Rachel Andrew has shared a number of patterns based on CSS Grid. Typical layouts contain header, footer, sidebar and main content area. An example of nested grid shows positioning of media along with text and caption. She also gives examples of using named lines and areas that are easer for developers to work with. Bryan Robinson has shared a good example of a responsive layout with CSS Grid.
-
What CSS design patterns help control spaces? CSS gives designers plenty of options to control spaces. Margin adds space on the outside of an element. Padding adds space between the element's border and its content. For both of these, each boundary (top, right, bottom, left) can be individually set. Negative margins can remove spaces and cause elements to overlap.
In the flow of text, spacing can be controlled with
letter-spacing
,word-spacing
,line-height
, andtext-indent
. In addition,text-align:justify
will stretch out inter-word spaces. Propertytext-indent
doesn't work on inline elements. For hanging indent, we can use negativetext-indent
and positivepadding-left
.Whitespace can be preserved with
white-space:pre
, which is useful for displaying code snippets in monospaced font. To preserve whitespace and also wrap long lines, usewhite-space:pre-wrap
,white-space:pre-line
, orwhite-space:break-spaces
. In addition,word-break:break-all
andword-break:break-word
give more control. -
Which are the different categories of CSS design elements? Sometimes it's helpful to think of CSS design patterns in terms of UI components or widgets. A typical webpage has header, footer, menu, buttons, breadcrumbs, forms, tables, pagination, etc. Common ways of using CSS for these purposes can be gathered into CSS patterns.
The page UI Patterns on CodePen documents many such patterns, although some of them are not pure CSS (they include JavaScript as well).
Phuoc's CSS Layout is another place for CSS patterns. Close to 100 patterns are featured here under the categories of layout, navigation, input, display, and feedback.
-
Is it a good idea to adopt a CSS framework? CSS frameworks offer a faster route to benefit from CSS design patterns. Such frameworks often provide many patterns that can be mixed and matched to achieve an intended design. They save us the effort of hand-coding dozens of rules. Well-known frameworks include Bootstrap, Foundation, Bulma, UIkit, Semantic UI, Susy, Materialize, Pure, Skeleton, and Milligram.
Some prefer to choose a small lightweight framework rather than one that tries to do too many things. Smaller frameworks and libraries may also be easier to customize. In any case, be wary of frameworks in which abstractions leak. Some lightweight frameworks include Pure, Milligram, Picnic, Wing, and Chota.
CSS preprocessors are also useful in managing the complexity of CSS styles. Features include variables, nesting, mixins, functions and mathematical operations. Well-known preprocessors include Sass, LESS and Stylus. These can be compiled into CSS. In fact, many CSS frameworks make use of preprocessors.
Even when CSS preprocessors are used, developers can't sacrifice a solid understanding of CSS. Preprocessors complement CSS, not replace it.
-
What are some CSS anti-patterns that I should avoid? We note a few CSS anti-patterns:
- Undoing Styles: Use of
float:none
,padding:0
, orborder-bottom:0
are examples where we're attempting to undo styles set elsewhere. It shows that we added styles too early. - Magic/Hard-coded Numbers: These make the design brittle to changes and confusing for other developers. Examples,
top:37px
orline-height:32px
. The latter could be made relative, such asline-height:1.5em
. - Qualified Selectors: Selector
ul.nav
means that style can't be reused for navigation menus on other element names. Selector.nav
is more reusable. On the flip side, we could have selectors that are too broad, such asul
. Instead, prefer a class selector. - Loose Class Names: Selector
.card
doesn't suggest its intended purpose. Instead,.credit-card-image
is better. Another poorly named selector is.teal
. It describes a visual attribute and has no semantic value. - Repetitive Key Selectors: Same key selector appearing in many CSS rules suggests poor design. There's no single source of truth. Instead, use BEM convention.
- Complex Rules: A rule such as
.slider {height:27px; float:left;}
is trying to do too many things. Instead, we could break it up into two separate rules.slider
and.left
.
- Undoing Styles: Use of
Milestones
A simple three-column layout with a top header that works for all browsers is surprising difficult to design. Owen Briggs builds such a layout and shares his styles online under the title Box Lessons. In time, this becomes a useful reference for many CSS developers. This is an early example of CSS design patterns.
2005
2006
As a CSS preprocessor, Sass v0.1.0 is released to help developers manage complex styles. Subsequently, other preprocessors are released: LESS v1.0 (Apr 2010) and Stylus v0.02 (Jan 2011). Adoption of preprocessors grows through the 2010s. A survey from 2016 reveals that about 86% of respondents use a CSS preprocessor in their development workflow.
2009
2011
Twitter open sources Bootstrap, what's possibly the world's first framework for frontend design. For pragmatic developers who wish to get things done, patterns in Bootstrap speed up development. Subsequently, Bootstrap 2 (Jan 2012) adds responsive functionality, Bootstrap 3 (Aug 2013) becomes responsive by default and mobile first, Bootstrap 4 (Jan 2018) moves from LESS to Sass and uses CSS Flexbox, and Bootstrap 5 Alpha (Jun 2020) replaces jQuery with vanilla JS and card decks with CSS Grid.
2012
2012
W3C publishes the first Candidate Recommendation of CSS Flexible Box Layout Module (first draft in 2009). In May 2017, W3C publishes CSS Grid Layout Module Level 1 as a Candidate Recommendation (first draft in 2012). Both Flexbox and Grid greatly simplify CSS rules for layouts. In time, developers share patterns for these two approaches.
2018
Bob Myers argues that BEM, CSS preprocessors, CSS frameworks, and obsessive separation of content and presentation are all CSS anti-patterns. CSS engines are faster today than when BEM came out. Preprocessors complicate build pipelines and require developers learn their syntax. Frameworks such as Bootstrap are not needed now for layout when we have CSS Flexbox and CSS Grid.
Sample Code
References
- Andrew, Rachel. 2019a. "When And How To Use CSS Multi-Column Layout." Smashing Magazine, January 11. Accessed 2020-12-09.
- Andrew, Rachel. 2019b. "A History of CSS Through Fifteen Years of 24 ways." 24ways, December 16. Accessed 2020-12-11.
- Andrew, Rachel. 2019c. "Editorial Design Patterns With CSS Grid And Named Columns." Smashing Magazine, October 4. Accessed 2020-12-09.
- Andrew, Rachel. 2020. "Grab & Go Patterns." Grid By Example. Accessed 2020-12-09.
- Atkins, Tab, Elika J. Etemad, and Rossen Atanassov (eds). 2018. "CSS Flexible Box Layout Module Level 1." W3C Candidate Recommendation, W3C, November 19. Accessed 2020-12-11.
- Atkins, Tab, Elika J. Etemad, Rossen Atanassov, and Oriol Brufau (eds). 2020. "CSS Grid Layout Module Level 1." W3C Candidate Recommendation, October 21. Accessed 2020-12-11.
- Bootstrap Docs. 2020. "About." Bootstrap Docs, v4.5. Accessed 2020-12-11.
- Bowers, Michael, Dionysios Synodinos, and Victor Sumner. 2011. "Pro HTML5 and CSS3 Design Patterns." Apress. doi: 10.1007/978-1-4302-3781-5. Accessed 2020-12-09.
- Bradley, Steven. 2011. "Pros And Cons Of 6 CSS Layout Patterns: Part 2." Vanseo Design, April 28. Accessed 2020-12-09.
- Briggs, Owen. 2001. "Box Lessons." The Noodle Incident. Updated 2006-05-10. Accessed 2020-12-09.
- CodePen. 2020. "UI Patterns." CodePen. Accessed 2020-12-09.
- Daggett, Mark. 2011. "CSS Anti-patterns." Blog, Innovator & Bricoleur, December 4. Accessed 2020-12-09.
- Holzschlag, Molly E. 2005. "Thinking Outside the Grid." A List Apart, December 19. Accessed 2020-12-11.
- Johnson, Joshua. 2019. "5 Really Useful Responsive Web Design Patterns." Design Shack, July 30. Accessed 2020-12-09.
- LePage, Pete. 2019. "Responsive Web Design Patterns." Web Fundamentals, Google Developers, November 26. Accessed 2020-12-09.
- MDN web docs. 2020a. "white-space." MDN web docs, Mozilla, July 17. Accessed 2020-12-11.
- MDN web docs. 2020b. "word-break." MDN web docs, Mozilla, July 16. Accessed 2020-12-11.
- Malone, Erin. 2017. "A History of Patterns in User Experience Design." Medium, March 31. Accessed 2020-12-11.
- Marcotte, Ethan. 2009. "Fluid Grids." A List Apart, March 3. Accessed 2020-12-11.
- Monus, Anna. 2018. "Popular CSS Preprocessors With Examples: Sass, Less & Stylus." Blog, Raygun, May 2. Accessed 2020-12-09.
- Myers, Bob. 2018. "CSS Anti-patterns." Medium, September 14. Accessed 2020-12-09.
- Norton, Sam. 2020. "Bootstrap 5: What’s New About It and Release Date." Designmodo, June 17. Accessed 2020-12-11.
- Phuoc, Nguyen Huu. 2020. "CSS Layout: Explore." CSS Layout, December 5. Accessed 2020-12-09.
- Queirós, Ricardo. 2017. "A Survey on CSS Preprocessors." 6th Symposium on Languages, Applications and Technologies, pp. 8:1-1:12. Accessed 2020-12-11.
- Roberts, Harry. 2012. "Code smells in CSS." CSS Wizardry, November 20. Accessed 2020-12-10.
- Roberts, Harry. 2017. "Code Smells in CSS Revisited." CSS Wizardry, February 8. Accessed 2020-12-09.
- Robinson, Bryan. 2018. "How To: Use CSS Grid to Mix and Match Design Patterns." Blog, June 13. Accessed 2020-12-09.
- Schuster, Werner. 2011. "Interview and Book Review: Pro HTML5 and CSS3 Design Patterns." InfoQ, December 2. Accessed 2020-12-09.
- Thakur, Ankush. 2020. "10 Best CSS Frameworks for Front-End Developers." Geekflare, May 15. Accessed 2020-12-09.
- Tidwell, Jenifer. 1999. "Common Ground: A Pattern Language for Human-Computer Interface Design." May 17. Accessed 2020-12-11.
- Wikipedia. 2020. "Bootstrap (front-end framework)." Wikipedia, November 29. Accessed 2020-12-11.
- Wroblewski, Luke. 2012. "Multi-Device Layout Patterns." LukeW Ideation + Design, March 14. Accessed 2020-12-12.
- troxler. 2020. "Awesome CSS Frameworks." troxler/awesome-css-frameworks, on GitHub, October 19. Accessed 2020-12-09.
Further Reading
- Bowers, Michael, Dionysios Synodinos, and Victor Sumner. 2011. "Pro HTML5 and CSS3 Design Patterns." Apress. doi: 10.1007/978-1-4302-3781-5. Accessed 2020-12-09.
- Lamping, Kevin. 2013. "Stop Hitting Yourself: CSS Design Patterns That Scale." Code PaLOUsa 2013, on InfoQ, December 1. Accessed 2020-12-09.
- Roberts, Harry. 2012. "Code smells in CSS." CSS Wizardry, November 20. Accessed 2020-12-10.
- LePage, Pete. 2019. "Responsive Web Design Patterns." Web Fundamentals, Google Developers, November 26. Accessed 2020-12-09.
- Phuoc, Nguyen Huu. 2020. "CSS Layout: Explore." CSS Layout, December 5. Accessed 2020-12-09.
- Wroblewski, Luke. 2012. "Multi-Device Layout Patterns." LukeW Ideation + Design, March 14. Accessed 2020-12-12.
Article Stats
Cite As
See Also
- CSS Architecture
- Web Components
- CSS Selectors
- BEM Methodology
- Cascading Style Sheets
- CSS Grid Layout