4

Here's what I've tried:

<textarea name='text' spellcheck = "true" style="height: 4em; width: 80%;" id="wider"></textarea>

I've also tried setting height with px or with %, both of which don't work. I also tried the following in my css file (as well as setting height as %, px, or em in the css, which also didn't work):

#wider{
    width: 80%;
    height: auto;
}

I can adjust the width either with the inline style command or with the css no problem. Why should the height be different, and what else might I try?

Thanks for any suggestions.

update: I have also tried setting the rows property in both the in-line and the CSS style. I understand that inline will overrule CSS. I just want one to work. Right now width attribute is always adjusted to reflect my change, but the height always stays the same. Is there something else I should be looking at? Thanks again!

4
  • typo: width = 80% should be width: 80% Commented Dec 18, 2014 at 21:09
  • @Lashane thanks for pointing that out, but that typo was not in the code so unfortunately does not explain the bug. Commented Dec 18, 2014 at 21:12
  • The question is not very clear. You can set the height of the textarea with height: 4em. Is that not working, or not what you want? If it's not working, in what browser? Commented Dec 18, 2014 at 21:14
  • @ralph.m I have tried that command. In my question I was trying to explain that I tried all of the following: height: 4em, height: 10px, height: 5%, but none of these worked. So far I am using Chrome and Firefox. No matter what values I put in or what sizing units I use, the text box height remains the same even though the text box width is responsive to the same styling attempts. Commented Dec 18, 2014 at 21:16

5 Answers 5

6

In this case it turned out there was a "min-height" property tucked away for textareas in forms. Apparently the inline styling setting of height does not override the min-height property from the CSS file.

Sign up to request clarification or add additional context in comments.

Comments

2

If you're using an inline style tag, it will take priority over your CSS styling.

The height property works as expected: JSfiddle here.

You can also set the height by using the rows attribute on the textarea if you have an expected number of lines to be used.

<textarea name='text' spellcheck="true" id="wider" rows="5"></textarea>

2 Comments

thanks for checking it. But the fact is that it's not working for me, so I suppose my question is then how to trouble-shoot. If it were a matter of server lag (I'm running on GoDaddy), I'd expect not to see the height adjusting along with the width. I can see the width changing in response to changing code but not the height.
Have you removed your inline code on the style="" attribute? Perhaps you are running into browser caching issues? Try using Ctrl+F5 when viewing your page, or inspect with the browser developer tools (F12) to inspect your code.
0

You can use javascript to resize dimentions of textarea as soon as some parent element changes its own, for instance, height. You are going to call the functios (ES6 Javascript version):

const makeDimensionsEqual = (someTextareaSelector, someParentElementSelector) => {

  document.querySelectorAll(someTextareaSelector)[0].style.height = 
    document.querySelectorAll(someParentElementSelector)[0]
    .getBoundingClientRect().height + "px"

}

const someTextareaSelector = '...' 
const someParentElementSelector = '...'
makeDimensionsEqual(someTextareaSelector, someParentElementSelector)

Comments

0

Disclaimer: I can only offer a guess as to why this works, but it works in my wordpress ... I pulled my hair out trying to get the textarea to respond to the css "height: auto" adjustment. By accident, while trying to put in an "oninput=" javascript suggestion from "Moussawi7" [see question https://stackoverflow.com/questions/17772260/textarea-auto-height] which I couldn't get to work, I added a js listener tied to a <textarea id="auto-open"[id name not important]> to wait for the DOM to load.

Long story short, with the DOM Listener keyed to an id for a textarea, apparantly the browser waits for the DOM to finish loading before the browser finishes setting all the attributes for the text area (this is my 100% guesswork). But presto! suddenly the textarea sized itself to the content withOUT a css "height: " setting at all. This is just what I wanted.

Here was that first js code, css, and html

js 
function auto_grow() {
  var element = document.getElementById('auto-grow');
// note: function does NOTHING!!!
console.log("inside auto_grow") // msg shows in cons after page loads
}
document.addEventListener('DOMContentLoaded', function () {
  auto_grow(); 
});

css
.gw-input-content {
  resize: none;
  width: 100%;
  box-sizing:border-box;
  margin-top: .5rem;
  white-space: pre-line; /* removes 1st line text indent in firefox */
  text-wrap: pretty;  /* removes 1st line text indent in chrome */
  overflow: hidden;
}

html
<textarea id="auto-grow" 
class="gw-input-content">
  <?php echo esc_textarea(wp_strip_all_tags(get_the_content()));

?>

Credit where credit is due, chatGPT3.5 then suggested I add another listener inside the DOM listener for textarea input and add Moussawi7's suggestion for expanding the height with a js style.height setting when the user types text which wraps to a new line or hits enter to add a new line. (I had asked chat to help me get Moussawi7's function working in my page.) No changes to the above css or html were needed. Now the textarea expands one line at a time as the user's typing adds new lines.

function auto_grow() {
  var element = document.getElementById('auto-grow');
  element.style.height = "5px";  // Initial height
  element.style.height = (element.scrollHeight) + "px";
}
document.addEventListener('DOMContentLoaded', function () {
  auto_grow();    
  var textarea = document.getElementById('auto-grow');
  textarea.addEventListener('input', auto_grow);  // Call the function on input
});

Comments

-1

It's an old one, but if someone still looks for this, try display: inline-table;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.