Search code examples
htmlcssflexboxcentering

How to make text start on newline instead of exceeding div when using display: flex


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


Solution

  • 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>