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

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

Linda Hamilton
Release: 2024-12-02 15:43:14
Original
489 people have browsed it

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

Styling Editable DIVs as Text Fields

When creating a DIV with editable content using contentEditable=true, users may not immediately recognize that it can be edited due to its default appearance. This can be addressed by styling the DIV to resemble a standard text input field.

To achieve this visual similarity, you can leverage the following CSS styles:

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

These styles provide a border, adjustable height through overflow, and set the appropriate font for a text field.

Additionally, you can include custom styling in the input {} section to further enhance the text field-like design:

input {
    margin-top: 5px;
    width: 400px;
}

#inputted-div {
    -moz-appearance: textfield;
    -webkit-appearance: textfield;
    background-color: white;
    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

Here, we have added a margin to prevent the input from appearing too closely to other elements, and customized the background color, border, and shadow to achieve a consistent look and feel.

By implementing these CSS styles, you can transform an editable DIV into a visually appealing text field, making it clear to users that they can modify its contents.

The above is the detailed content of How Can I Style a ContentEditable 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