DEV Community

Cover image for Inline pseudo: A Weekend Experiment in Inline Pseudo-Class Styling
Glenn
Glenn

Posted on • Edited on

Inline pseudo: A Weekend Experiment in Inline Pseudo-Class Styling

The web development landscape is a constant dance of innovation, fueled by the relentless pursuit of better, faster, and more expressive ways to build. As I delved into the world of Tailwind CSS, its utility-first approach sparked a question: why not simply use inline styles?

While inline styles offer undeniable convenience, their limitations become apparent when dealing with dynamic states like hover or focus. Tailwind’s state variants elegantly solve this issue, but could there be another way?

This question became a challenge, a weekend experiment to explore the possibilities of inline pseudo-classes. The result? inline_pseudo, a Javascript library (clocking in at around just 1KB minified) that empowers developers to target dynamic states directly within their inline styles.

View the Project on GitHub

Key Features:

  1. Inline Styling: Directly apply pseudo-class styles, such as :hover, :focus, or :disabled, within an element’s attributes using a style attribute with a colon-separated syntax.
  2. Modifier Stacking: Combine multiple pseudo-class styles within the same style attribute using semicolons as separators, providing enhanced customization possibilities.
  3. Conciseness: Simplify your styling workflow by eliminating the need for external stylesheets when dealing with basic pseudo-class customization.
  4. Exploration: inline_pseudo is designed for experimentation and learning, offering a playground for rapid prototyping without the constraints of traditional CSS.

Examples:

Let’s take a look at how inline_pseudo can be used:

<button style:hover=”color: blue;”>Click me</button>
<a style:hover=”text-decoration: underline; color: green;”>Visit my site</a>
<input type=”text” style:disabled:hover=”opacity: 0.5;”>
Enter fullscreen mode Exit fullscreen mode

Disclaimer:

Before diving into the excitement of inline_pseudo, it’s crucial to acknowledge its experimental nature and limitations:

  1. Experimental: This library is not intended for production use. As with any experiment, it may have performance or compatibility issues.
  2. Browser Support: Inline pseudo-class styling may not be universally supported across all browsers, potentially leading to inconsistent behavior.
  3. Specificity: Inline styles can possess higher specificity than external styles, potentially overriding intended styles.

Installation:

Getting started with inline_pseudo is a breeze. Follow these steps:

Clone the repository:

git clone https://github.com/GlennKnight/inline_pseudo.git
Enter fullscreen mode Exit fullscreen mode

Install dependencies:

npm install
Enter fullscreen mode Exit fullscreen mode

Usage:

Incorporate the minified script into your HTML, and start applying inline pseudo-classes to your elements:

<body>
 <script src=”dist/inline_pseudo.min.js”></script>
</body>
Enter fullscreen mode Exit fullscreen mode

Apply pseudo-class styles to your elements:

<button style:hover=”color: blue;”>Click me</button>
<a style:hover=”text-decoration: underline; color: green;”>Visit my site</a>
<input type=”text” style:disabled:hover=”opacity: 0.5;”>
Enter fullscreen mode Exit fullscreen mode

This journey wasn’t just about finding an alternative to Tailwind; it was about pushing the boundaries of inline styling and exploring its potential. While inline_pseudo is only an experiment, it serves as a testament to the power of experimentation in web development.

Top comments (0)