1

I am trying to make the front end for a very simple chat box using javascript, css, and html.

I have a element overflow issue when user resizes the window, if the chat is greater than 12 messages, the messages overflow from the container.

I made a jsfiddle so you guys can try for yourself: https://jsfiddle.net/fb72uwwq/6/

In the fiddle, click the chat button and spam messages, resize your browser and you can see they overflow. enter image description here

I tried some stack overflow answers but overflow: hidden; did not work. How can I solve this?

2
  • the simple answer is that you shouldn't be adding margins to the bottom to create the desired effect. If you can, consider looking into using flexbox instead (though it will not work on old browsers). Also, javascript does not use static types, so Number(Key) and key.toString() are not necessary. Commented Aug 13, 2017 at 0:15
  • Setting height to 100% should resolve the issue. c.style.height = "100%"; Commented Aug 13, 2017 at 0:23

3 Answers 3

1

css

#chat{
  height: 100% !important;
  overflow-y: hidden; 
   /* if you want to scroll on overflow, you can use overflow-y: scroll; or overflow-y: auto; */
}
Sign up to request clarification or add additional context in comments.

Comments

1

Setting height to 100% should resolve the issue.

c.style.height = "100%";

Comments

0

overflow: hidden will not work because you're not placing the messages inside the container at all. Instead, you're stacking them with absolute positioning and calculated bottom-margin as its sibling. With your current markup, there's no way to cut off the messages at top, unless you also want to calculate that by hand.

Also, your question is not very clear: do you want to stop adding messages once it's outside the container or you just want to crop them off the screen (but they'd still be visible in the DOM).

Either way, your current structure can be much improved if you create it the following way:

<div id=chat>
  <input type=text id=chat-message>
  <div class=message>First message</div>
  <div class=message>Second message</div>
  <div class=message>Third message</div>
</div>

This way, all you have to do to make them appear like you want it to use Flexbox:

#chat {
  display: flex;
  flex-direction: column-reverse; /* from bottom to top */
}

Your JavaScript will also be much simpler, since you'll just be creating a new element as the last child of #chat, meaning no additional calculations. (You can still remove the oldest messages after a certain amount of messages have appeared on the screen).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.