Sharing Top Content from the Angular-sphere.

Lesser known CSS quirks & advanced tips – Peedu Tuisk – Medium

  • • Lesser known “content: ‘foo’;” property featuresData attributes – You can use data attributes for dynamic CSS content.
  • For example: – div data-text=”foo”/divdiv:before { – content: attr(data-text); – }This can be useful, for example if you wish to translate the pseudo class content texts (e.g. when using them for tooltips).
  • Currently attr() is supported for content property and almost no browser supports it for other properties, but perhaps they will in the future.
  • Furthermore, the values from attr() are strings, so it really was only meant to be used for content in the first place and can’t be used for units (e.g. font-size) or urls (e.g. content: url() ).
  • div/divdiv:before { – content: url(image.jpg); – }However, what can be used to pass any content from DOM to CSS, is the previously mentioned custom properties: – div style=”–background-image: { – content: ”; – background-image: counter – You can use the property content: counter() feature to incrementally number the pseudo elements:…

Lesser known CSS features, weirdest quirks and oddities. Along with advanced CSS tips and tricks for front-end developers.

Say you have 3 divs, each positioned absolutely, containing another element with increasing z-index number. Each next one will appear on top of the previous one. If you add z-index: 10 to the first one, it will now appear on top of the other two, which remain ordered as before. So far all good. Now add “opacity: 0.99” to the first div and watch it get stacked under the other two. 🙂

Having used SASS or LESS one might figure the CSS custom properties and variables to be the equivalent of features available in those preprocessors, but there are a few key differences worth looking into.

First the basics:

They’re also inheritable, so if you reassign a local variable it’ll have an effect on all children elements and contrary to preprocessors the browser will actually recalculate all variables and expressions where applicable when that happens.

Fallbacks can be used with commas, you can stack multiple fallbacks after comma, even other variables:

color: var(—-my-var, ‘blue’);

This leads us to the main difference from preprocessors: CSS variables are aware of the DOM’s structure and are dynamic.

color: var(–primary-color, –default-color);

Contrary to the first example of inheritability, this example relies on fallback being aware if the custom property was set in the parent DOM element or not.

Furthermore, they can also be easily changed using javascript:

Same goes for “height: 100%”, in many cases…

Lesser known CSS quirks & advanced tips – Peedu Tuisk – Medium