Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a ::before selector be used with a <textarea>?

I'm experimenting with some styles on <textarea>s and I tried doing some stuff with ::before and ::after selectors and I couldn't to anything to get them to work. So the question is: is this possible? I know the CSS surrounding forms is arcane beyond mention but it seems like this should work.

like image 594
xj9 Avatar asked Jul 20 '10 04:07

xj9


People also ask

What is :: before for?

::before (:before) In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

What attribute are valid for textarea input?

The <textarea> element also accepts several attributes common to form <input> s, such as autocomplete , autofocus , disabled , placeholder , readonly , and required .

What are the two attributes used for textarea tag?

The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the text area will be submitted). The id attribute is needed to associate the text area with a label.

Can I use before in input?

As Robert Koritnik's answer points out, :before and :after can only be applied to container elements and input elements are not containers.


1 Answers

The :before and :after will not work on a text-area (nor any element that cannot contain another element, such as img or input), because the generated content of the pseudo-element gets placed within the element but before or after that element's content, and acts itself as an element. The pseudo-element does not get placed before or after the parent element itself (contrary to some information one may find on the internet). To illustrate:

If you have this css:

p:before {content: 'before--'} p:after {content: '--after'} 

Then html like this:

<p>Original Content</p> 

Effectively renders to the screen as if the source code were:

<p>before--Original Content--after</p> 

Not as if the source code were:

before--<p>Original Content</p>--after 

Which is why tags that cannot contain any html element "content" (like those mentioned above) do not recognize the pseudo-elements, as there is no "place" for that content to be generated to. The textarea can contain "content," but only pure text content.

like image 184
ScottS Avatar answered Oct 27 '22 10:10

ScottS