Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

absolute positioned text area does not respect right and bottom properties

Tags:

css

textarea

I am trying to absolute position a text area that will stretch to cover it's parent. Unfortunately Chrome seems to ignore right property and Firefox ignores both right and bottom properties.

Here is the CSS I am using :

#container {
  position: relative;
}
#my-text-area {
  position: absolute;
  top: 0px;
  bottom: 0px;
  left: 10px;
  right: 10px;
}

Here is a JsFiddle example :enter link description here

Is there a textarea default property that i have to disable or are these just bugs? There are workarounds to achieve the full width and height of the parent by inheriting these properties from the parent but i am looking for a way to make the absolute positioning for the scenario you would in the future have custom right and bottom not only 0.

like image 739
coanda mihai Avatar asked Jan 09 '16 13:01

coanda mihai


People also ask

Why is position Absolute not working CSS?

If you are placing an element with absolute position, you need the base element to have a position value other than the default value. In your case if you change the position value of the parent div to 'relative' you can fix the issue.

How do you fix a position absolute?

Absolutely positioned elements are positioned with respect to a containing block, which is the nearest postioned ancestor. If there is no positioned ancestor, the viewport will be the containing block. Elements with fixed positioning are fixed with respect to the viewport—the viewport is always their containing block.

What is absolute position property?

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.


1 Answers

Wrap the textarea in an element and use same css on that element. Demo: https://jsfiddle.net/lotusgodkk/csub6b96/2/

HTML:

<div id="container">
    <div id="my-text-area">

        <textarea></textarea>
    </div>

</div>

CSS:

body,
html,
#container {
    width: 100%;
    height: 100%;
}

body {
    margin: 0px;
}

#container {
    position: relative;
}

#my-text-area {
    position: absolute;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
}

textarea {
    height: 100%;
    width: 100%;
}

Reference: http://snook.ca/archives/html_and_css/absolute-position-textarea

If you're interested in why this is required, it's because the browsers affected are those which render textarea as a replaced element. Rendering this control is delegated to the OS, and not handled by the browser itself, and it appears that certain CSS properties aren't respected when rendering a replaced element with position: absolute. The relevant (and complicated) section of the CSS spec is https://www.w3.org/TR/CSS21/visudet.html#abs-replaced-width

like image 51
K K Avatar answered Sep 19 '22 15:09

K K