CSS architecturesOOCSS: Object Orientated CSSBEM: Block, Element, ModifierSMACSS: Scalable and Modular CSSITCSS: Inverted Triangle ACSSInline stylesCSS in JSCSS ModulesSpecificityCSS attributesUsage casesDefine variablesProgramming to change CSS attributesProgramming to get CSS attributesDark images when in Dark ModeGet the size of a scaled element:ImageLayoutPadding/marginFlexGridNamed area and seperate linesInternationalisation(i18n)Avoid Hardcoding of Labels in a Certain LanguageRight-to-Left LanguagesAnimationEventsReduce the animation timeScrollMedia query@container Css properties’s prefixReset css propertyCSS shapes(clip-path)Cos/sin Organzing your cssToolsPrefix CSS attributesPitfallUseful code SnapsPost-processingReadingsReference
css-protips
AllThingsSmitty • Updated Sep 20, 2024
CSS architectures
OOCSS: Object Orientated CSS
The idea is to identify common visual patterns and extract duplicated code blocks into reusable classes.
- Separation of structure from skin
- Separation of containers and content
Bootstrap is one of the most widely used CSS frameworks that leverages this design principle.
BEM: Block, Element, Modifier
BEM is a model for breaking down components, their sub-elements, and their states into discrete parts. It provides a systematic naming convention that keeps all selectors flat (no descendant selectors). Each element that gets styled has its own class name.
BEM works well with popular CSS pre-processors like Sass, which use nested rules that compile down to flat CSS selectors.
Block
Standalone entity that is meaningful on its own.
Examples
header
, container
, menu
, checkbox
, input
Element
A part of a block that has no standalone meaning and is semantically tied to its block.
Examples
menu item
, list item
, checkbox caption
, header title
Modifier
A flag on a block or element. Use them to change appearance or behavior.
Examples
disabled
, highlighted
, checked
, fixed
, size big
, color yellow
SMACSS: Scalable and Modular CSS
In practice, large single-file CSS files quickly become unmanageable and hard to debug. SMACSS was a guide to categorizing the different types of CSS and was compatible with approaches like OOCSS. The main idea was to take all these class names and organize them into separate buckets, providing much-needed structure to our CSS files. Additionally, it introduced conventions around naming classes.
Base, Layout, Modules, State, Theme
ITCSS: Inverted Triangle
ITCSS is a "meta-framework" for CSS, compatible with other systems, designed to help tame the chaos of CSS overriding each other unpredictably.
It is based on the concept of layering, with each progressive layer forming an inverted pyramid shape, hence the name "inverted triangle".
This methodology is influential for managing CSS files on large-scale projects. To learn more, watch a talk given by its creator.
ACSS
Inline styles
The shift to component-based frameworks often saw styles applied inline within components. In frameworks like React, we pass a Javascript object to the
style
prop, which is then converted to inline styles.This can be a cause of concern for many, as it feels like we're reverting back to the days before external stylesheets, disregarding existing best practices.
However, when it comes to components, inline styles don't suffer from the same issue of massive duplication, as they are encapsulated within the component. In addition, the fact that styles only affect the element they are applied to provides a safe way to add and modify CSS within components.
The main issue with inline styles is the lack of access to more powerful CSS features, such as pseudo selectors and media queries. This also makes it difficult to leverage shared design tokens, caching, static analysis, and pre-processing.
CSS in JS
This resembled inline styles, but with the ability to leverage the capabilities of stylesheets.
The first wave of CSS-in-JS libraries, such as Styled Components, Emotion, and many more, gained traction in the React ecosystem. However, there was a performance tax end users were paying due to server-side rendering inefficiencies, caching issues, and client runtime costs.
The second wave of CSS-in-JS tools, like Vanilla Extract, Linaria, and Compiled, extract stylesheets out from components in a compile step. This moves much of the runtime processing that happens on users' browsers to compile time, reducing the performance tax.
CSS Modules
It depends on a bundler like Webpack to generate and ensure selectors are scoped, which keeps CSS localised within a component directory.
Specificity
The :is(), :not() and :has() will use the max specificity in its parameters list
So the p ‘s background color will be green
The :where() exception always has its specificity replaced with zero
CSS attributes
currentColor
Often referred to as "the first CSS variable",
currentColor
is a value equal to the element's color
property. It can be used to assign a value equal to the value of the color
property to any CSS property which accepts a color value. This forces the CSS property to inherit the value of the color
property.Usage cases
- SVG
border-color
,background
, andbox-shadow
Define variables
In CSS
In JS
Programming to change CSS attributes
Programming to get CSS attributes
Dark images when in Dark Mode
Get the size of a scaled element:
we can only use
getBoundingClientRect
and the getComputedStyle
will not workImage
background-size
for background-image
(cover
/contain
/ values
)object-fit
for image
(contain
/cover
/fill
/scale-down
)Layout
Padding/margin
Spacing between elements is crucial for improving the aesthetics and readability of a website. There are several ways to set spacing between elements in CSS:
margin
andgap
can be used to set the outer spacing between elements.gap
is commonly used in Flexbox and Grid layouts to set equal spacing between items. But there is one prerequisite, which is that the spacing between all the items is equal.
padding
is used to set the spacing between the container edge and its content.
- Alignment in Flexbox and Grid layouts can also be used to set spacing between items, but it can easily cause the layout to be unattractive due to changes in the number of items. It's best to avoid alignment whenever possible to control spacing between items.
- In some unique layout scenarios, CSS positioning can also be used to set spacing between elements.
These are just some of the common ways to set spacing in CSS. In actual use, you can also combine other CSS features, such as sibling adjacent selectors (
E + F
), negation selector :not()
, parent selector :has()
, pseudo-class selectors :last-child
or :first-child
, to set spacing between elements based on specific conditions. This allows the code to be more adaptable to different scenarios and environments, without having to manually modify the CSS code due to data changes.Flex
flexbugs
philipwalton • Updated Oct 3, 2024
Grid
Each cell in the same column must have the same width, and each cell in the same row must have the same height. Additionally, rows and columns cannot end prematurely; if a grid has two columns and three rows, each column must have three cells.
when center the menu at the center, the grid is a better choice.
Named area and seperate lines
Seperate lines
Names
Internationalisation(i18n)
Avoid Hardcoding of Labels in a Certain Language
When creating UI components, it is important to allow for customisation of label strings by making them part of the component props/options. This will allow for more flexibility in the components. For example, an image carousel should have customisable labels for the prev/next buttons.
Right-to-Left Languages
For languages read from right-to-left, such as Arabic and Hebrew, the UI should be flipped horizontally. This can be done by passing in a
direction
prop/option. This will ensure that elements are rendered in the correct order, such as having the prev and next buttons on the right and left respectively in RTL languages.- offset.. is relative to it’s parent element
Animation
Events
animationstart
An
animationstart
event will happen for each valid keyframe animation that is applied. Typically, there will be one animationstart
event for each valid animation-name
identifier that is present. If there is a animation-delay, the event will fire once the delay period has expired.animationend
Like
animationstart
, this event will be fire once each applied animation finish.(
animation-duration
* animation-iteration-count
) + animation-delay
= event fired required timeanimationiteration
The
animationiteration
event happens at the end of each animation iteration, before the next one starts. If animation-iteration-count
is an integer, the number of animationiteration
events is typically one less than the value of the animation-iteration-count
property. This is true as long as the absolute value of any negative delay is less than the duration.Reduce the animation time
Trigger re-layout:
- clientHeight (Left, Top, Width)
- focus()
- getBoundingClientRect()
- getClientRects()
- innerText
- offsetHeight (Left, Parent, Top, With)
- outerText
- scrollByLInes() .....
Scroll
Here is a useful code snippet for creating a horizontally-scrolling list of items that snap to a specific position. This can be achieved using the
scroll-snap-type
and scroll-snap-align
properties.Media query
@container
The
@container
rule is a new media query that allows you to style elements based on their container size. You can use this to change the styling of an element based on the size of the container it is in, instead of the size of the viewport. For example, you could use this to style a card differently when it is placed in a narrow panel on a larger screen.Css properties’s prefix
Private Prefix | Compatibility |
-webkit- | WebKit engine's Safari, Blink engine's Chrome, Opera, Edge; partially supported by EdgeHTML engine's Edge |
-moz- | Gecko engine's Firefox |
-ms- | Trident engine's IE and EdgeHTML engine's Edge |
-o- | Presto engine's Opera |
Reset css property
The property value
initial
sets an element's property to the initial value specified by the W3C specification.The property
all
resets all of an element's properties.Browsers may set default styles for some elements, such as form elements, outside of the specification.Property values can come from developer-defined, user-configured, and browser-default sources.
Using
all:initial
is equivalent to clearing user configurations and browser-default styles.In practice, it is preferable to reset to default styles rather than clear them.
The
all:revert
property restores an element's properties. Sub-element properties are reset according to the following rules:- Inherited properties: reset to the parent element's property value.
- Non-inherited properties or parent element inherited properties that are not set: reset to user-configured and browser-default properties.
CSS shapes(clip-path)
Cos/sin
Organzing your css
Create logical sections in your stylesheet. By organizing your stylesheet in this way, you can easily locate and modify different parts of the stylesheet.
Common styling
utility classes
Then we can add everything that is used sitewide. That might be things like the basic page layout, the header, navigation styling, and so on.
specific things
Tools
Prefix CSS attributes
PostCSS is a plugin that parses CSS and adds vendor prefixes to CSS rules based on values from Can I Use.
autoprefixer
postcss • Updated Oct 3, 2024
Online review
cubic bézier curves
Pitfall
- For line elements, the height they contribute to their parent is determined by their line height, not their padding or content.
justify-content
default isflex-start
, andalign-items
’s initial value isstretch
.
transition
has effect at the different time
translate
100% percentage value is based on its own width and height,left
,top
,bottom
,right
,margin-*
using 100% percentage value is based on the parent’s width and height.
fixed
The element is removed from the normal document flow. It is positioned relative to the initial containing block established by the viewport, except when one of its ancestors has atransform
,perspective
, orfilter
property set to something other thannone
, or thewill-change
property is set totransform
, in which case that ancestor behaves as the containing block.
Useful code Snaps
Determine if the browser is compatible with the CSS feature.
GPU acceleration
Want to add outline or change background when focusing a child element, but without adding unnecessary effect when clicking using mouse (Not work on firefox)
Truncate text if it is longer than three lines.
Mobile-first media queries
Post-processing
Readings
This detailed post from the Chrome teams brings together all of the headline features that landed in Chrome and across the web platform this year, including nesting, new color functions, the
:has
selector, linear-easing, and much much more.