Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section>
  <h1>How to implement a CSS placeholder for <q>contenteditable</q> elements</h1>
  <em>2016/06/22</em>
  <p>If you want to use smilies or images in text areas oder input fields you will need to use <strong>contenteditable="true"</strong> elements instead. <br>But the placeholder attribute will not work there. Therefore I present a way to do it with CSS only.</p>

  <article>
  <h2>Native version (textarea)</h2>
    <textarea placeholder="Native version"></textarea>
  </article> 

  <article>
  <h2>Contenteditable version 1</h2>
    <div id="v1" contenteditable="true" placeholder="Contenteditable version 1"></div>
  </article>

  <article>
  <h2>Contenteditable version 2</h2>
    <div id="v2" contenteditable="true" placeholder="Contenteditable version 2"></div> 
  </article> 

  <article>
  <h2>Viewing the CSS code (version 2)</h2>
    <code>
      <pre>
  div[contenteditable][placeholder]:empty:not(:focus):before {
    content: attr(placeholder);
    color: #aaa;
  }
    </pre>
    </code>
    <p>This CSS code takes DIVs with contenteditable and placeholder attributes and prepends the placeholder's content (<code>:before</code>) to the content, <br>if the DIV is empty (<code>:empty</code>) and not focused (<code>:not(:focus)</code>). <br>If you do not want to undisplay the placeholder on focus, just leave the <code>:not(:focus)</code> part out.</p>
    <h3>Browser Support</h3>
    <ul>
      <li><a href="http://caniuse.com/#feat=css-gencontent" target="caniuse">:before</a></li>
      <li><a href="http://caniuse.com/#feat=css-sel3" target="caniuse">:empty and :not()</a></li>
      <li><a href="http://caniuse.com/#feat=css-sel2" target="caniuse">:focus</a></li>
      <li><a href="http://caniuse.com/#feat=css3-attr" target="caniuse">content: attr()</a> - seems not to be supported, but works in all major browsers (Chrome, Firefox, IE, Edge, Opera) with <code>:before</code> and <code>:after</code> selector.</li>
      <li>Firefox uses to show the placeholder, but once you typed something and empty the field again, it will not show the placeholder again.</li>
    </ul> 
  </article>
  <p style="padding-bottom: 50px">This article can also be read in German on <a href="http://www.itholics.de/css-placeholder-fuer-contenteditable-elemente" target="itholics">my blog</a></p>
</section>
              
            
!

CSS

              
                /* designing the container */
div[contenteditable], textarea {
  display: inline-block;
  min-width: 300px;
  min-height: 100px;
  border: 1px solid black;
  padding: 5px;
}

/* 
if contenteditable and placeholder attribute is available,
it uses :before to place the text of the placeholder attribute into the container if the container is empty (:empty) 
*/
div#v1[contenteditable][placeholder]:empty:before {
  content: attr(placeholder);
  color: #aaa;
}

/*
like the first version, but undisplaying the placeholder on focus (:not(:focus))
*/
div#v2[contenteditable][placeholder]:empty:not(:focus):before {
  content: attr(placeholder);
  color: #aaa;
}

/* after here just for presentation purpose */
a {
  color: #393;
  font-weight: bold;
  text-decoration: none;
}

body {
  display: flex;
  justify-content: center;
  padding: 0;
  margin: 0;
  background-image: url('http://gdj.gdj.netdna-cdn.com/wp-content/uploads/2011/02/background-pattern-design-87.jpg');
  background-attachment: fixed;
}

section {
  display: flex;
  flex-direction: column;
  min-width: 400px !important;
  max-width: 70%;
  padding: 0;
}
section > *:not(h1) {
  background: white;
  margin: 0;
  padding: 20px 30px 0 30px;
}

h1 {
  background: rgba(55,55,55,0.7);
  padding: 20px;
  margin: 0;
  color: white;
}

h2 {
  border-top: 1px ridge #eee;
  padding-top: 10px;
}

              
            
!

JS

              
                
              
            
!
999px

Console