简体   繁体   中英

Prevent overlapping the text in textarea

In Firefox it works correctly:

在此处输入图像描述

But in the Chrome an overlay occurs:

在此处输入图像描述

How to fix it?

FireFox (Developer Edition) 83.0b5

Chrome Version 88.0.4305.3

 div{ position: relative; } label{ position: absolute; color: blue; padding: 1% 2%; } textarea{ padding: 25px; }
 <div> <label>some label</label> <textarea>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</textarea> </div>

As stated in the other answers, this is an inconsistency with Chrome. However, here is a possible workaround, using a white background on the label (covering the text below it) and adjusting the position and padding settings (which requires a bit of trial and error):

 div{ position: relative; } label{ position: absolute; color: blue; padding: 6px 10px 3px; background: #fff; width: 190px; top: 2px; left: 2px; } textarea{ padding: 25px; padding-top: 30px; }
 <div> <label>some label</label> <textarea>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</textarea> </div>

Johannes has the solution if you want the scroll bar alongside the whole container. This is not a fix to the padding inconsistency between Chrome/Firefox just another way so you can avoid it.

Keeping the padding consistent and avoiding "trial and error" positioning.

To note: div max-width: 400px and other max-width / width s can be changed if needed this was a style choice to make it responsive.

textarea:focus This stops Chrome from adding an outline on the textarea - if you need accessibility then I would not do this or make it pretty. */

 div { width: 100%; max-width: 400px; position: relative; } label { display: block; color: blue; background: #fff; border: 1px solid #000; width: 100%; box-sizing: border-box; padding: 5px 20px 10px 10px; border-bottom: none; } textarea { display: block; padding: 0 25px 25px 25px; width: 100%; max-width: 100%; min-width: 100%; min-height: 200px; border: 1px solid #000; border-top: none; box-sizing: border-box; position: relative; top: -1px; } textarea:focus { outline: none; }
 <div class="text-area-container"> <label>Some label</label> <textarea>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</textarea> </div>

Unfortunately, padding in a textarea is an inconsistency with Firefox and Chrome.

I might just wrap the textarea in a div that has the border and padding and place the text area in there with the label at the top of the div.

If you could find a way to tell the difference between Chrome and Firefox, you could write alternate code that works in Chrome. Maybe make the text have a few spaces, and give the header some padding and a thick border that has the same background color as the rest of the div, to make it look like there is a space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM