By default,<input>
and <textarea></textarea>
元素不會根據(jù)其內(nèi)容大小自動調(diào)整尺寸。事實上,并沒有簡單易用的HTML 或CSS 方法能夠?qū)崿F(xiàn)此功能。這有點奇怪,因為這似乎是一個合理的用例。但當(dāng)然,總有辦法,我的朋友。總有辦法。
我最近在思考這個問題,起因是Remy Sharp 在其博客中討論了內(nèi)聯(lián)<input>
元素的這個問題。
非輸入元素自然擴展
對我來說,奇怪的是沒有辦法強制<input>
元素模仿這種行為,但事實就是這樣。
我們可以使用contenteditable
屬性將任何元素設(shè)為可編輯的、類似輸入的元素:
<span contenteditable="true" role="textbox">99</span>
該<span></span>
元素會自然地擴展到其內(nèi)容所需寬度。如果它是一個<div> 或任何其他塊級元素,它也會根據(jù)需要垂直擴展。 <h3 id="但是-非輸入元素是否具有可訪問性">但是,非輸入元素是否具有可訪問性?</h3> <p>我不太確定。請注意,我在元素上設(shè)置了<code>role="textbox"
。這只是根據(jù)一些文檔做出的最佳猜測。
即使這有幫助……
- 回車鍵提交表單的情況如何?
- 表單數(shù)據(jù)經(jīng)常被序列化并發(fā)送,而執(zhí)行此操作的代碼可能不會查找
<span></span>
元素,這種情況如何? - 它在屏幕閱讀器中的讀取方式是否真的與
<input>
元素相同? -
<input>
元素還有哪些1 我沒有想到的自然行為?
雖然我很喜歡通過使用非輸入元素從瀏覽器免費獲得自動調(diào)整大小的想法,但我同時也擔(dān)心(我)未知的可用性和可訪問性風(fēng)險。
調(diào)整實際輸入元素的大小
因此,假設(shè)我們堅持使用<input>
和<textarea></textarea>
。即使它不是特別自然,我們能否使它們可調(diào)整大?。?/p>
我想到的一個想法是用一個相對內(nèi)聯(lián)的父元素包裝輸入元素,并將其絕對定位在父元素內(nèi)部。然后,使用JavaScript,我們可以將輸入值與包裝器內(nèi)的隱藏<span></span>
元素同步,根據(jù)需要將寬度擴展。
對于<textarea></textarea>
元素,一種經(jīng)典技術(shù)是計算換行符的數(shù)量,用它來設(shè)置高度,然后乘以行高。這對于預(yù)格式化的文本(如代碼)非常有效,但對于長篇幅的段落式內(nèi)容則完全無效。
以下是所有這些想法的組合。
其他想法
Shaw 有一個非常巧妙的JavaScript 單行代碼。JavaScript 在元素上設(shè)置一個data-*
屬性,該屬性等于輸入的值。輸入元素設(shè)置在一個CSS 網(wǎng)格中,該網(wǎng)格是一個偽元素,它使用該data-*
屬性作為其內(nèi)容。該內(nèi)容是根據(jù)輸入值將網(wǎng)格拉伸到適當(dāng)大小的內(nèi)容。
你的想法
我絕對知道你們這些網(wǎng)絡(luò)極客已經(jīng)用六種方法解決了這個問題。讓我們在評論中看看它們。
- Eric Bailey 給了我一些即興的想法:(1)沒有可訪問的名稱。(2)它可能不適用于語音控制。(3)它將在高對比度模式下被忽略。
The above is the detailed content of Auto-Growing Inputs & Textareas. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

No,CSSdoesnothavetobeinlowercase.However,usinglowercaseisrecommendedfor:1)Consistencyandreadability,2)Avoidingerrorsinrelatedtechnologies,3)Potentialperformancebenefits,and4)Improvedcollaborationwithinteams.

CSSismostlycase-insensitive,butURLsandfontfamilynamesarecase-sensitive.1)Propertiesandvalueslikecolor:red;arenotcase-sensitive.2)URLsmustmatchtheserver'scase,e.g.,/images/Logo.png.3)Fontfamilynameslike'OpenSans'mustbeexact.

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

CSScounterscanautomaticallynumbersectionsandlists.1)Usecounter-resettoinitialize,counter-incrementtoincrease,andcounter()orcounters()todisplayvalues.2)CombinewithJavaScriptfordynamiccontenttoensureaccurateupdates.

In CSS, selector and attribute names are case-sensitive, while values, named colors, URLs, and custom attributes are case-sensitive. 1. The selector and attribute names are case-insensitive, such as background-color and background-Color are the same. 2. The hexadecimal color in the value is case-sensitive, but the named color is case-sensitive, such as red and Red is invalid. 3. URLs are case sensitive and may cause file loading problems. 4. Custom properties (variables) are case sensitive, and you need to pay attention to the consistency of case when using them.

Theconic-gradient()functioninCSScreatescirculargradientsthatrotatecolorstopsaroundacentralpoint.1.Itisidealforpiecharts,progressindicators,colorwheels,anddecorativebackgrounds.2.Itworksbydefiningcolorstopsatspecificangles,optionallystartingfromadefin
