简体   繁体   English

最小宽度不起作用

[英]min-width doesn't work

I want to create a div that able to expand the width when user input the text 我想创建一个div,当用户输入文本时能够扩展宽度

I tried to set width:100%; 我试着设置宽度:100%; max-width:600px; 最大宽度:600px的; min-width:300px; 最小宽度:300像素;

But some how this div just stay at 600px, is that any way to keep the width stay at 300px and able to expand to 600px base on the length of the content? 但是这个div如何保持在600px,是否有任何方法可以保持宽度保持在300px并且能够根据内容的长度扩展到600px?

Really appreciate if you can help. 如果你能提供帮助,真的很感激。 Thanks 谢谢

Setting the div to display: inline-block allows it to shift width depending on the contents. 将div设置为display: inline-block允许它根据内容移动宽度。

Otherwise, as a block level element, the div will take up 100% of it's containing element (or however wide you set max-width ). 否则,作为块级元素,div将占用其包含元素的100%(或者设置为max-width )。

Here's a an example: 这是一个例子:

http://cssdesk.com/Bwp8E http://cssdesk.com/Bwp8E

Try: 尝试:

width: auto;
max-width: 600px;
min-width: 300px;

I just update the "min-width" page on MDN with this information: 我只是使用以下信息更新MDN上的“min-width”页面:

In some browsers, on iOS, a <button> element in its native (default) configuration will not respond to min-width . 在某些浏览器中,在iOS上,其本机(默认)配置中的<button>元素不会响应min-width This problem is due to native buttons. 此问题是由本机按钮引起的。 A <span> inside a native button will exhibit the same problem, despite having display:inline-block set. 尽管display:inline-block设置,但本机按钮内的<span>将显示相同的问题。 When changes are made to other style parameters and the browser is forced to abandon the native button, the min-width setting takes affect. 当对其他样式参数进行更改并且强制浏览器放弃本机按钮时, min-width设置会生效。

Make sure that the div or space your working on isn't: position:fixed; 确保你工作的div或空间不是: position:fixed;

It needs to be something like: position: relative; 它需要是这样的: position: relative; or position:absolute; 或者position:absolute;

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

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