Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underline single letter in an input placeholder

One can apply CSS styling to a placeholder, such as for Firefox:

::-moz-placeholder { text-decoration: underline; }

However, what I would like to do is underline a single letter in a placeholder, for the purpose of hinting at a hotkey for the user to press (similar to Windows in file menus), such as making the F in First Name underlined below:

<input type='text' placeholder='First Name' />

Is there any way to do this?

like image 355
dthree Avatar asked Oct 17 '25 17:10

dthree


2 Answers

I think you can achieve this with CSS only in google chrome. For example:

You can select the first letter of placeholder

::-webkit-input-placeholder::first-letter {
  color: red;
  text-decoration:underline; 
}

Result:

enter image description here

The text-decoration does not render when set with :first-letter in Chrome (Version 39.0.2171.71). So we can achieve the underline with border-bottom.

::-webkit-input-placeholder::first-letter {
  color: red;
  border-bottom: 1px solid red;
}

Result:

enter image description here

UPDATE: text-decoration works fine on Chrome 41.0.2235.0 Canary.

enter image description here

Here is the DEMO: http://codepen.io/MizR/pen/myeJZe

Unfortunately, this solution doesn't work on Firefox. :(

Update 2: No longer works. :(

like image 94
Mizanur Rahman Avatar answered Oct 20 '25 08:10

Mizanur Rahman


You can use an absolute-positioned u tag, being careful to use the same font and padding as the input.

You'll need to hide the u when the input has content:

document.getElementById('iFirstName').onkeyup= function() {
  var u= document.getElementById('uFirstName');
  u.style.display= this.value>'' ? 'none':'inline';
};
u {
  position: absolute;
  font: 10pt verdana;
  padding: 4px;
}

input {
  padding: 3px;
  font: 10pt verdana;
  border: 1px solid #ddd;
}
<u id="uFirstName">F</u>
<input id="iFirstName" type='text' placeholder='First Name' />
like image 26
Rick Hitchcock Avatar answered Oct 20 '25 07:10

Rick Hitchcock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!