0

Pretty much the title. How do I stop the text in the div start on a newline if it's larger than the div width?

I'm also using display: flex because it's the only way I found I could center the text in the middle of the div both vertically and horizontally Here's my Code:

#front {
  position: relative;
  background-color: #121212;
  color: #e6e600;
  border: 3px solid #e6e600;
  border-radius: 10px;
  width: 500px;
  height: auto;
  min-height: 400px;
  margin: 0 auto;
  padding: 30px;
  font-weight: bold;
  overflow: auto;
  display: flex;
  justify-content: center;
  align-items: center;
}
<div id="front"> 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
</div>

Here's what I get in my chrome browser:

enter image description here

Edit: Turns out that the problem was because I was using a single word. Please ignore this question

Udo-N
  • 43
  • 8
  • 2
    Well having such a big single word(containing digits language) will creates overflow. As browser tend to start into new line at breaks(spaces). Try to have some break. – Rana Nov 21 '21 at 19:26
  • @Rana My bad, I didn't even consider that it was a single word. Thanks – Udo-N Nov 21 '21 at 19:28
  • 1
    But your solution is in `word-break: break-all;` property, go ahead and try to use it, if it solves your problem with such a biggy word – Rana Nov 21 '21 at 19:32
  • 1
    Does this answer your question? [How to force a line break in a long word in a DIV?](https://stackoverflow.com/questions/3058866/how-to-force-a-line-break-in-a-long-word-in-a-div) – Rana Nov 21 '21 at 19:34

1 Answers1

1

i made a code snippet for the problem. I put your box into a container and your text into the flex. When in the flexbox if it overflows (i turned overflow:hidden) the command "overfolw-wrap:break-word" breaks the sentence.

Hope this works for you. :)

-Noel

#front {
  font-weight: bold;
  overflow: hidden;
  overflow-wrap: break-word;
}

container {
  position: relative;
  background-color: #121212;
  color: #e6e600;
  border: 3px solid #e6e600;
  border-radius: 10px;
  width: 500px;
  height: auto;
  min-height: 400px;
  margin: 0 auto;
  padding: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}
<container>
  <div id="front">
    123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
  </div>
</container>
Noel Huibers
  • 109
  • 12