2014-10-19 53 views
2

如何強制內容可編輯div的行爲像一個文本框?也就是說,它具有固定的水平寬度,但是無限地垂直擴展?這裏有一個的jsfiddle,以下是代碼在撥弄一個副本:http://jsfiddle.net/seoj7cgq/在內容可編輯div上強制尺寸?

<!--How do I force the text to stay in the div horizontally and scroll infinitely vertically?--> 
<title>JS Bin</title> 
<style> 

    .element { 
    max-height: 200px; 
    width:300px; 
    height:300px; 
    max-width: 300px; 
    background: #999; 
    overflow: hidden; 
} 

</style> 
</head> 
<body> 
    <div class="element" contenteditable=true> 
    this is the text 
    </div> 
</body> 
</html> 

回答

1

使用overflow-xoverflow-y屬性:

.element { 
 
    max-height: 200px; 
 
    width: 300px; 
 
    height: 300px; 
 
    max-width: 300px; 
 
    background: #999; 
 
    overflow-x: hidden; 
 
    overflow-y: scroll; 
 
}
<div class="element" contenteditable=true>this is the text</div>