Home  >  Article  >  Web Front-end  >  How can I Style a Text Input to Look Like a Password Field Using CSS?

How can I Style a Text Input to Look Like a Password Field Using CSS?

Susan Sarandon
Susan SarandonOriginal
2024-10-26 06:53:02412browse

How can I Style a Text Input to Look Like a Password Field Using CSS?

Style Input Text to Resemble Password Field Using CSS

Challenge:

Many web forms include input fields with the "type=text" attribute. While this attribute allows users to enter visible text, it may be desirable to conceal sensitive information by displaying asterisks instead, as in an input field with the "type=password" attribute.

Solution with CSS:

CSS provides a solution to address this challenge. The -webkit-text-security and text-security properties offer a way to conceal text input in browsers:

Code:

<code class="css">input.pw {
  -webkit-text-security: disc;
  text-security: disc;
}</code>

Implementation:

Apply the class "pw" to the input field:

<code class="html"><input type="text" class="pw" value="abcd"></code>

Additional Features:

For browsers that do not support these properties, consider providing a fallback option or using a JavaScript-based solution.

Browser Compatibility:

The -webkit-text-security property is supported in WebKit-based browsers, while the text-security property is supported in modern browsers. For IE8 compatibility, a CSS fallback or JavaScript solution is recommended.

The above is the detailed content of How can I Style a Text Input to Look Like a Password Field Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn