search
HomeWeb Front-endH5 TutorialUse contentEditable in HTML5 to automatically increase the height of multi-line text_html5 tutorial tips

contentEditable is a global property developed by Microsoft, decompiled and put into use by other browsers. The main function of this attribute is to allow users to edit the content in the element, so the element must be an element that can obtain mouse focus, and a caret must be provided to the user after clicking the mouse to prompt the user that the content in the element is allowed to be edited. The contentEditable property is a Boolean property that can be specified as true or false.

In addition, this attribute also has a hidden inheritance state. When the attribute is true, the element is designated as allowing editing; when the attribute is false, the element is designated as not allowing editing; when true or not is specified When false, it is determined by the inherit state. If the parent element of the element is editable, the element is editable.

In addition, in addition to the contentEditable attribute, the element also has an isContentEditable attribute. When the element is editable, this attribute is true; when the element is not editable, this attribute is false.
The following is an example of using the contentEditable attribute. When the contentEditable attribute is added to a list element, the element becomes editable. Readers can experiment with this example in the browser.

XML/HTML CodeCopy content to clipboard
  1. /span>> 
  2. head> 
  3. meta charset="UTF- 8"> 
  4. title>conentEditalbe attribute example title> 
  5. head> 
  6. h2>Editable list h2> 
  7. ul contentEditable="true" > 
  8. li>List element 1 li> 
  9. li>List element 2 li> 
  10. li>List element 3 li> 
  11. ul> 

The result after running this code is as shown below:
201631113549687.jpg (469×213)

Line text automatically increases in height

When it comes to multi-line text boxes, everyone will immediately think of using textarea. Using textarea is really convenient, but there is one disadvantage. It cannot automatically increase the height. You can only specify the number of words in the corresponding columns and rows or directly use CSS to increase the height. Width.

Automatic heightening is still needed at certain times, such as the input box similar to posting on Weibo, which is a typical requirement: the text box has a default height, and when the input text exceeds this height, it will automatically increase. There must be a maximum limit. After exceeding this limit, a vertical scroll bar will appear.

If you use textarea to complete this requirement, you also need to cooperate with js to monitor changes in text height to dynamically change the height of the text box. This is very inconvenient, especially on the mobile side, which is unscientific. At this time, You can use the attribute contenteditable.

For example:

XML/HTML CodeCopy content to clipboard
  1. div contenteditable="true" class="box" id="box" >
  2.                                                        
  3. div>
  4. style>
  5. .box{width:200px;max-height:100px;border:1px solid #ccc;overflow-y:auto;overflow-x:hidden;}
  6. style>
As long as the value of the contenteditable attribute is specified to be true, the div will become editable and will automatically increase in height as the content increases. Then we give the div a maximum height to achieve the above requirements.
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
What's New in HTML5 Forms? Exploring the New Input TypesWhat's New in HTML5 Forms? Exploring the New Input TypesMay 13, 2025 pm 03:45 PM

HTML5introducesnewinputtypesthatenhanceuserexperience,simplifydevelopment,andimproveaccessibility.1)automaticallyvalidatesemailformat.2)optimizesformobilewithanumerickeypad.3)andsimplifydateandtimeinputs,reducingtheneedforcustomsolutions.

Understanding H5: The Meaning and SignificanceUnderstanding H5: The Meaning and SignificanceMay 11, 2025 am 12:19 AM

H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

H5: Accessibility and Web Standards ComplianceH5: Accessibility and Web Standards ComplianceMay 10, 2025 am 12:21 AM

Accessibility and compliance with network standards are essential to the website. 1) Accessibility ensures that all users have equal access to the website, 2) Network standards follow to improve accessibility and consistency of the website, 3) Accessibility requires the use of semantic HTML, keyboard navigation, color contrast and alternative text, 4) Following these principles is not only a moral and legal requirement, but also amplifying user base.

What is the H5 tag in HTML?What is the H5 tag in HTML?May 09, 2025 am 12:11 AM

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

H5 Code: A Beginner's Guide to Web StructureH5 Code: A Beginner's Guide to Web StructureMay 08, 2025 am 12:15 AM

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

H5 Code Structure: Organizing Content for ReadabilityH5 Code Structure: Organizing Content for ReadabilityMay 07, 2025 am 12:06 AM

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

H5 vs. HTML5: Clarifying the Terminology and RelationshipH5 vs. HTML5: Clarifying the Terminology and RelationshipMay 05, 2025 am 12:02 AM

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools