简体   繁体   English

如何强制 div 在 CSS/jQuery 中仅在一个方向上扩展或调整大小

[英]How to force a div to expand or resize only in one direction in CSS/jQuery

I am working on a <div> which is resizable horizontally and text within will be trimmed based on its container width.我正在处理一个<div> ,它可以水平调整大小,其中的文本将根据其容器宽度进行修剪。

 .resizable_div { border: 1px solid red; white-space: nowrap; width: 100px; height: 50px; line-height: 50px; overflow: hidden; text-overflow: ellipsis; margin: 50px auto; padding: 5px; resize: horizontal; cursor: col-resize; min-width: 50px; max-width: 150px; }
 <div class="resizable_div">This is an example</div>

You can see that the div box is resizable horizontally and the text is shortened using text-overflow etc. But the problem is that when you resize this div using resize handle on right hand side, it increases or decreases in opposite direction as well ie left hand side.您可以看到 div 框可以水平调整大小,并且使用text-overflow等缩短了text-overflow 。但问题是,当您使用右侧的调整大小手柄调整此 div 的大小时,它也会在相反的方向上增加或减少,即向左手边。 I don't want this behaviour.我不想要这种行为。 Instead I want the div should resize by going only in right hand direction and left hand side just stays there.相反,我希望 div 应该只在右手方向调整大小,而左手边只是停留在那里。 How it can be achieved?如何实现?

UPDATE更新

There is one more issue.还有一个问题。 The div is resizable only by bottom right corner and not with col-resize handle anywhere on right border. div 只能通过右下角col-resize而不能在右边框的任何地方使用col-resize手柄。 How to fix it?如何解决?

try this, just need add another div and mother div contain margin: 50px auto;试试这个,只需要添加另一个 div 并且母 div 包含 margin: 50px auto;

 .mother { padding: 20px; resize: both; width: 300px; margin: 50px auto; position: relative; } .child{ border: 2px solid; padding: 20px; resize: both; overflow: hidden; position: absolute; left: 0; }
 <div class="mother"> <div class="child"> <p>Let the user resize both the height and the width of this div element.</p> <p>To resize: Click and drag the bottom right corner of this div element.</p> </div> </div>

Please remove the Auto from the margin, and to make it expand only in the right side make the direction as ltr.请从边距中删除自动,并使其仅在右侧扩展,将方向设为 ltr。

Therefore, your CSS will look like so :因此,您的 CSS 将如下所示:

 .resizable_div { border: 1px solid red; white-space: nowrap; width: 100px; height: 50px; line-height: 50px; overflow: hidden; text-overflow: ellipsis; margin: 50px; padding: 5px; resize: horizontal; cursor: col-resize; min-width: 50px; max-width: 150px; direction: ltr; }
 <div class="resizable_div">This is an example</div>

this is because of margin:50px auto;这是因为margin:50px auto; this css property.这个css属性。 your left and right margin is auto so it will make center your div to it's parent (for here parent is body).你的左右边距是自动的,所以它会让你的 div 居中到它的父级(因为这里的父级是 body)。 so while you resize your div it increase you div width and set left and right margin values equally because of it's auto value.所以当你调整你的 div 大小时,它会增加你的 div 宽度,并且因为它是自动值,所以设置左右边距值相等。

if you remove this property your div resize is works perfectly as you required but your box comes to the left side of you body not in center.如果您删除此属性,您的 div 调整大小将完全按照您的要求工作,但您的盒子会出现在您身体的左侧而不是中心。

see the following snippet.请参阅以下代码段。

 .resizable_div { border:1px solid red; white-space:nowrap; width:100px; height:50px; line-height:50px; overflow:hidden; text-overflow:ellipsis; /*margin:50px auto;*/ padding:5px; resize:horizontal; cursor:col-resize; min-width:50px; max-width:150px; }
 <div class="resizable_div">This is an example</div>

I just add comment for your margin css property to the div我只是将您的边距 css 属性的评论添加到 div

I hope this will clear your confusion regarding this problem.我希望这会消除您对这个问题的困惑。

Thank you...谢谢...

I made the css property overflow to auto , so it is working fine.我让 css 属性溢出到 auto ,所以它工作正常。 For the 2nd issue: Using resize property the div will always be resizable only by bottom right corner(horizontal/vertical/both) and it can't be resizable anywhere on right border.对于第二个问题:使用resize属性,div 将始终只能通过右下角(水平/垂直/两者)调整大小,并且不能在右边框的任何地方调整大小。

 .resizable_div { border: 1px solid red; white-space: nowrap; width: 100px; height: 50px; line-height: 50px; **overflow: auto;** text-overflow: ellipsis; margin: 50px auto; padding: 5px; resize: horizontal; cursor: col-resize; min-width: 50px; max-width: 150px; }
 <div class="resizable_div">This is an example</div>

Type resize: both;类型调整大小:两者; not resize:horisontal.不调整大小:水平。

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

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