Home > Web Front-end > CSS Tutorial > How Can I Style an Editable DIV to Look Like a Text Field?

How Can I Style an Editable DIV to Look Like a Text Field?

Mary-Kate Olsen
Release: 2024-12-09 05:56:09
Original
235 people have browsed it

How Can I Style an Editable DIV to Look Like a Text Field?

Making an Editable DIV Resemble a Text Field

Oftentimes, it may be desirable to allow users to edit specific sections of a web page using editable DIVs. However, DIV elements naturally lack the visual cues that indicate editability, leaving users uncertain about whether they can interact with them.

To address this issue, CSS styling can be employed to transform an editable DIV into a semblance of a standard text input field. Fortunately, modern browsers provide specific CSS properties that mimic the appearance of native text input controls.

CSS Styling for a Text Field-Like DIV

The following CSS code can be applied to a DIV to give it a text field-like appearance:

#textarea {
  -moz-appearance: textfield-multiline;
  -webkit-appearance: textarea;
  border: 1px solid gray;
  font: medium -moz-fixed;
  font: -webkit-small-control;
  height: 28px;
  overflow: auto;
  padding: 2px;
  resize: both;
  width: 400px;
}
Copy after login

In addition, specific styles can be applied to the textarea element for added realism:

textarea {
  height: 28px;
  width: 400px;
}
Copy after login

CSS Styling for a Text Field-Like Input

Similarly, an input can be styled to resemble a text field using the following CSS:

#input {
  -moz-appearance: textfield;
  -webkit-appearance: textfield;
  background-color: white;
  background-color: -moz-field;
  border: 1px solid darkgray;
  box-shadow: 1px 1px 1px 0 lightgray inset;  
  font: -moz-field;
  font: -webkit-small-control;
  margin-top: 5px;
  padding: 2px 3px;
  width: 398px;    
}
Copy after login
input {
  margin-top: 5px;
  width: 400px;
}
Copy after login

Output

The resulting HTML and CSS produce the following output:

[Image of a textarea and an input, both styled to look like text fields]

This styling is compatible with Safari, Chrome, and Firefox, degrading gracefully for Opera and IE9.

The above is the detailed content of How Can I Style an Editable DIV to Look Like a Text Field?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template