简体   繁体   English

将Button的高度动态设置为Textarea的高度

[英]Dynamically set the height of a Button to be the height of a Textarea

This is a little thing, but I can't find anything at all in regard on how to go about doing it! 这是一件小事,但就如何去做而我找不到任何东西!

In the picture below, there is a textarea with a styled (and disabled) button purely for aesthetic purposes: 在下图中,有一个带有样式(和禁用) buttontextarea纯粹出于美学目的:

在此输入图像描述

But when you resize the textarea , this happens: 但是当你调整textarea大小时,会发生这种情况:

在此输入图像描述

This doesn't look so great. 这看起来不太好。

I want to tie the dynamic height of the textarea to the height of the button so that they stay together at whatever height, but I cant find anything like this. 我想把textarea的动态高度与button的高度联系起来,这样它们就可以在任何高度保持在一起,但是我找不到这样的东西。

How do you tie an elements styling to another's? 你如何将元素样式与另一个元素联系起来?

Note - Much more confident at PHP than jQuery/javascript, but obviously won't refuse answers in those languages. 注意 - 对PHP的信心比jQuery / javascript更有信心,但显然不会拒绝这些语言的答案。

Flexbox can do that Flexbox可以做到这一点

 div { display: inline-flex; margin: 1em; } textarea, botton { flex: 1; } 
 <div> <textarea id="output">My size will be matched by the button</textarea> <button>Click</button> </div> 

The easiest way would be to wrap the textarea within another element and set the display property of that parent to flex , like so: 最简单的方法是将textarea包装在另一个元素中,并将该父元素的display属性设置为flex ,如下所示:

 div{ display:flex; } textarea{ resize:vertical; } 
 <div> <textarea></textarea> <button>text</button> </div> 

Depending on your requirements and other styles, you may need to adjust some of the other flexbox properties. 根据您的要求和其他样式,您可能需要调整其他一些Flexbox属性。 See caniuse.com as well for details on browser support and prefixing. 有关浏览器支持和前缀的详细信息,请参阅caniuse.com

Here is a way to do it with HTML and CSS, no javascript or php code needed: 这是一种用HTML和CSS做的方法,不需要javascript或php代码:

<div class="container">
<textarea rows="4" cols="50"></textarea>
<button type="button">Click Me!</button>
</div>

.container{
  overflow: auto;
  position: relative;
}

textarea, button{
  float: left;
}

button{
  position:absolute;
  height:100%;
  width:150px;
}

Here is a JsFiddle: https://jsfiddle.net/83uuk6gx/1/ 这是一个JsFiddle: https ://jsfiddle.net/83uuk6gx/1/

EDIT: 编辑:

You can remove the min-height, it will work without it (I removed it from code above and updated jsfiddle). 您可以删除最小高度,它将在没有它的情况下工作(我从上面的代码中删除它并更新了jsfiddle)。 Also I would not use flex as it will impose restrictions on earlier version of the browsers. 此外,我不会使用flex,因为它会对早期版本的浏览器施加限制。 IE9 does not support it I believe, here is more details on it: http://www.w3schools.com/cssref/css3_pr_flex.asp IE9不支持它我相信,这里有更多细节: http//www.w3schools.com/cssref/css3_pr_flex.asp

Either disable the resise of textarea using the following : 使用以下命令禁用textarea的resise:


textarea {
    resize: none;
}

or use this fiddle(I did not create it) by making necessary changes. 或者通过做出必要的改变来使用这个小提琴(我没有创造它)。


http://jsfiddle.net/gbouthenot/D2bZd/ http://jsfiddle.net/gbouthenot/D2bZd/


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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