javascript - height is increasing automatically of div with contenteditable="true" - Stack Overflow

I've div with property contenteditable="true".<div contenteditable="true" c

I've div with property contenteditable="true".

<div contenteditable="true" class="textarea"></div>

Requirements
1. Text should be center aligned vertically and horizontally.
2. Height should not increase automatically with increase in content inside that div, if content increases inside div, scrollbar need to display.

Here is js Fiddle

if you want, use jquery too.

I've div with property contenteditable="true".

<div contenteditable="true" class="textarea"></div>

Requirements
1. Text should be center aligned vertically and horizontally.
2. Height should not increase automatically with increase in content inside that div, if content increases inside div, scrollbar need to display.

Here is js Fiddle

if you want, use jquery too.

Share Improve this question edited May 17, 2014 at 6:47 siraj pathan asked May 17, 2014 at 5:53 siraj pathansiraj pathan 1,4951 gold badge15 silver badges31 bronze badges 3
  • 1 i dont think contenteditable will let you do that. instead, you can wrap that contenteditable div inside of another div, and when the contenteditable expands, it will cause its parent to scroll. its why wysiwig editors like tiny mce get wrapped by iframes i think – chiliNUT Commented May 17, 2014 at 5:57
  • i'm trying but still no result – siraj pathan Commented May 17, 2014 at 6:09
  • height problem can be solved by position:absolute; but with this text is not vertically center aligned – siraj pathan Commented May 17, 2014 at 6:14
Add a ment  | 

2 Answers 2

Reset to default 3

You can wrap the contenteditable div in 2 containers, one for the width and height with overflow:auto; and one for the display-table; property so the text is horzontaly centered :

DEMO

HTML :

<div class="container1">
    <div class="container2">
        <div contenteditable="true" class="textarea"></div>
    </div>
</div>

CSS :

.container1{
    height:60px;
    width:273px; 
    overflow:auto;
    border:1px solid green;
}
.container2 {
    min-height:100%;

    display:table;
}
.textarea {
    width:273px;
    font-size: 18px;
    font-weight: normal;
    line-height: 18px;
    outline: none;
    text-align: center;
    vertical-align: middle;
    display: table-cell;
    position: relative;
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    user-select: text;
    word-wrap: break-word;
    overflow:hidden;
}

When u dont specify a height it will automatically grow vertically when more information is added into the div tage. But i think you will have to make sure the content is absolute and/or you can try this simple code to see if it helps

Try this, CSS code.

.scroll {
width:100px;
height:100px;
overflow:scroll;
}

HTML code

<div class="scroll">
SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE. SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.SOME TEXT HERE.
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745371367a4624810.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信