Reputation: 5318
What would be the best way to implement a design that want div-containers be provides with a border, if the content of the div causes vertical scroll of the div?
CSS
div {
max-height: 100px;
overflow-y: auto;
// if content exceeds 100 px show border
border: solid 1px;
}
HTML
<div>
// Text ..
</div>
Upvotes: 0
Views: 44
Reputation: 12641
Set border if overflow.
var div = document.querySelector('div');
if (div.scrollHeight > div.clientHeight) {
div.style.border = '1px solid';
}
div {
max-height: 100px;
overflow-y: auto;
}
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
Upvotes: 2
Reputation: 2366
I am assuming there is some kind of event that changes or determines the content of that div. In my humble opinion, you can not achieve that using html/css alone. You would have to use JavaScript. https://jsfiddle.net/4u2Lurt3/
Here is a simple example of how to do that. You may need to "re-wire" a bit differently to get the exact behavior you wanted
<div id="myDiv">
Hello
</div>
<button onclick="addText()">
Click to change div
</button>
And
function checkWidth() {
var div = document.getElementById('myDiv');
if(div.offsetWidth > 100) {
div.style.border = "1px solid black"
}
}
function addText() {
var div = document.getElementById('myDiv');
div.style.width="250px";
div.innerText = "AAAAAAAAAAAAA VVVVVVEERRRRY LOOOONNNNGGG TEEEEEXXXXXTT";
checkWidth();
}
Upvotes: 0