Possibly the Most Useful CSS Trick
Tags: webdev, css, frontend
Published: 2019-10-02 20:27:27

---
title: "Possibly the Most Useful CSS Trick"
published: true
description: "A really neat CSS trick using the :not() and :placeholder-shown pseudo classes."
cover_image: "https://xtrp.io/api/content/static_files/not-placeholder-shown-css-trick/photo.jpg"
tags: webdev, css, frontend
---

Imagine you are building a simple search form, where a user types their search into an input box, and then clicks the search button to search.

Here's some sample HTML for the search form:

```html
<input placeholder="Search...">
<button>Search!</button>
```

Note that the search button should only be clickable when something has actually been typed into the search box. You wouldn't want someone searching an empty string, right?

## The JavaScript Way

Ordinarily, some quick JavaScript can be used to do this, for example:

```javascript
inputElement.oninput = function(e) {
    if(inputElement.value != "") {
        activateSearchButton();
    } else {
        deactivateSearchButton();
    }
};
```

## The Cool-CSS-Trick Way

In my opinion, there's a more optimal way to accomplish this, without Javascript, using the **`:not(:placeholder-shown)`** CSS pseudo class. There are two parts of the pseudo class: `:not()` and `:placeholder-shown`.

 - **`:placeholder-shown`** &mdash; is active when the placeholder in an input box is shown. When the input box is empty, the placeholder will be shown, and when the user types something in, the placeholder won't be shown.
 - **`:not()`** &mdash; takes an argument of what selector(s) the style should not apply to.

So, `:not(:placeholder-shown)` simply means applying a style to an input element when the placeholder isn't shown; meaning that styles will only apply when the input box isn't empty.

We can combine this with the `+` CSS operator to make our search button functionality:

```css
button {
    display: none;
}

input:not(:placeholder-shown) + button {
    display: block;
}
```

Here, the button will only display when the input has content, which works exactly like the JavaScript version, just... without JavaScript.

![GIF Video of :not(:placeholder-shown) Demo](https://xtrp.io/api/content/static_files/not-placeholder-shown-css-trick/demo1.gif)

## Another Interesting Way to Use This

We can also combine this with the `:focus` pseudo class so that a style is only applied when the user is typing within an input box, like this:

```html
<input type="text" placeholder="type something">
<p id="otherElement">You are typing</p>
```

```css
#otherElement {
    display: none;
}
input:focus:not(:placeholder-shown) + #otherElement {
    display: block;
}
```

![GIF Video of :not(:placeholder-shown) Demo](https://xtrp.io/api/content/static_files/not-placeholder-shown-css-trick/demo2.gif)

## Conclusions

This is a pretty neat trick in general, and has actually been really useful for me when building search forms, login forms, and the like. I hope you enjoyed this post, and find this CSS trick effective.

Note that the JavaScript code referenced at the beginning of this post will not work without necessary variable definitions, and that the `:not(:placeholder-shown)` pseudo class may not work on some web browsers, especially at the time of reading. I personally recommend checking for browser compatibility before implementing a feature in an app or website.

Thanks for scrolling.

*&mdash; Gabriel Romualdo, October 2, 2019*