javascript - How to resize textarea depending on the parent element -


so need resize textarea in form take whole available space in box depending on parent element (#wrapper). how do that?

i put negative number calculation more precise when sure how text go there.

the calculation code wrote in, calculates height of textarea 0px.

var boxheight = ($("#wrapper").height()-200);    $( window ).ready(function(){      $("#box").height( boxheight );  });
<!doctype html>  <html>  <head>  <meta charset="utf-8">  <title>xxx</title>  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  </head>  <body>  	<div id="wrapper">  		<h1 class="aligncenter"> hello </h1>  		<p>lorem ipsum dolor sit amet, consectetur adipiscing elit. mauris porta egestas urna ac lobortis. nam bibendum purus sagittis odio fermentum dapibus. praesent venenatis justo eget hendrerit finibus. sed rutrum eget lacus pulvinar. nulla sit amet faucibus ligula. curabitur eget nunc finibus, pharetra libero vitae, eleifend sapien. ut porttitor vehicula arcu, non porttitor tellus faucibus ut.</p>  		<form class="form" id="form">  			<p class="text">  				<textarea name="text" id="box" placeholder="xyz"></textarea>  			</p>  			<div class="g-recaptcha" data-sitekey="6lec2wytaaaaaol3dsuojasko8fk-6lm-vbscwox"></div>  			<div class="aligncenter">  				<button class="brown" type="submit">save</button>  				<button class="purple" type="submit">publish</button>  			</div>  		</form>  	</div>  </body>  </html>

if know #wrapper have specified height, use css reasonable fit textarea.

first, specify value height in #wrapper rule.

then, set height #form using absolute positioning , css calc function set form height of parent minus 200px.

you need similar trick p element wrapping textarea, leaving 50px height buttons.

finally, set height , width 100% #box, , set box-sizing: border-box take account border of textarea element.

comment: although can done, approach depends on 2 hard-coded values height of text before form , height of buttons. javascript/jquery may better approach. either way, #wrapper needs height value.

#wrapper {      border: 1px dotted blue;      height: 500px;      position: relative;  }  #form {      border: 1px dotted blue;      height: calc(100% - 200px);      position: absolute;      width: 100%;      bottom: 0;  }  #form p {      border: 1px dashed blue;      height: calc(100% - 50px);      margin: 0;  }  #box {      height: 100%;      width: 100%;      box-sizing: border-box;  }
<div id="wrapper">      	<h1 class="aligncenter"> hello </h1>        <p>lorem ipsum dolor sit amet, consectetur adipiscing elit. mauris porta egestas urna ac lobortis. nam bibendum purus sagittis odio fermentum dapibus. praesent venenatis justo eget hendrerit finibus. sed rutrum eget lacus pulvinar. nulla sit amet faucibus ligula. curabitur eget nunc finibus, pharetra libero vitae, eleifend sapien. ut porttitor vehicula arcu, non porttitor tellus faucibus ut.</p>      <form class="form" id="form">          <p class="text">              <textarea name="text" id="box" placeholder="xyz"></textarea>          </p>          <div class="g-recaptcha" data-sitekey="6lec2wytaaaaaol3dsuojasko8fk-6lm-vbscwox"></div>          <div class="aligncenter">              <button class="brown" type="submit">save</button>              <button class="purple" type="submit">publish</button>          </div>      </form>  </div>


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -