Skip to content

CSS ::placeholder Pseudo Element

The `::placeholder` pseudo-element is used to style the placeholder text of form elements. The `::placeholder` selector applies only to <input> and <textarea> elements that have a placeholder attribute. By default, the appearance of placeholder text is a semi-transparent or light gray color in most browsers.

::placeholder pseudo-element

The placeholder text is set with the placeholder attribute, which specifies a hint that describes the expected value of an input field.

INFO

Vendor prefixes (-webkit-, -moz-, -ms-) are legacy and unnecessary for modern browsers. The standard `::placeholder` selector is supported everywhere.

Version

Selectors Level 4

Syntax

CSS ::placeholder syntax example

css
::placeholder {
  css declarations;
}

Example of the ::placeholder selector:

CSS ::placeholder example

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      input::placeholder {
        color: #1c87c9;
        font-size: 1.2em;
        font-style: italic;
      }
    </style>
  </head>
  <body>
    <h2>::placeholder selector example</h2>
    <input placeholder="Type here..." />
  </body>
</html>

Example of the ::placeholder selector used in form:

CSS ::placeholder code example

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      * {
        box-sizing: border-box;
      }
      .container {
        margin: 20px auto;
        max-width: 250px;
        background-color: #8ebf42;
        padding: 20px;
      }
      input {
        border: 1px solid #666666;
        background-color: #eeeeee;
        padding: 15px;
        margin-bottom: 20px;
        display: block;
        width: 100%;
      }
      input::-webkit-input-placeholder {
        color: #666666;
      }
      input::-moz-placeholder {
        color: #666666;
      }
      input:-ms-input-placeholder {
        color: #666666;
      }
      input::placeholder {
        color: #666666;
      }
    </style>
  </head>
  <body>
    <h2>::placeholder selector example</h2>
    <div class="container">
      <form>
        <input type="text" placeholder="Lorem ipsum is simply..." />
        <input type="date" placeholder="DD/MM/YYYY" />
      </form>
    </div>
  </body>
</html>

Example of the ::placeholder selector with the HTML <input> autofocus attribute:

HTML example of placeholder

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      label {
        display: block;
        color: #777777;
        margin: 0 0 4px;
      }
      input {
        border: 1px solid transparent;
        padding: 15px;
        font-size: 1.2em;
        outline: 0;
      }
      input::placeholder {
        color: #8ebf42;
      }
      label,
      input {
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h2>::placeholder selector example</h2>
    <form action="#">
      <div>
        <label for="name">Name:</label>
        <input id="name" name="name" type="text" placeholder="Enter your name here" autofocus />
      </div>
    </form>
  </body>
</html>

Practice

What does the ::placeholder pseudo-element in CSS entail?

Dual-run preview — compare with live Symfony routes.