I'm trying to get a different width for each li
element inside ul
, but as soon as I add a larger element all the li
get the same width. I want the li
s have the same width as the text length.
My HTML:
<div>
<ul>
</ul>
</div>
<textarea></textarea>
<button>Send</button>
My CSS:
div{
overflow-y: scroll;
overflow-x: hidden;
height: 400px;
width:400px;
border:1px solid black;
}
div ul {
display: table;
list-style: none;
}
div ul li{
display: block;
background-color: #b70505;
color: white;
font-weight: 600;
font-size: 0.8em;
border-radius: 25px;
margin-top: 5px;
padding: 10px;
max-width:200px;
word-wrap:break-word;
}
My JS:
var box = document.getElementsByTagName('div')[0];
var newText = document.getElementsByTagName('textarea')[0];
var button = document.getElementsByTagName('button')[0];
button.addEventListener('click', function(e){
e.preventDefault();
if(newText.value !== ''){
var Newitem = document.createElement('li');
Newitem.innerHTML = newText.value;
box.getElementsByTagName('ul')[0].appendChild(Newitem);
box.scrollTop = box.scrollHeight;
}
});
https://jsfiddle.net/hdr60zmb/
What am I doing wrong?