Stop text from overflowing when it reaches the maximum width in flexbox

A basic flexbox layout is displayed with an image next to text, aiming for this layout:

#container {
  align-items: center;
  background: black;
  display: flex;
  justify-content: center;
  padding: 10px;
  width: 500px; /* Dynamic */
}

#image {
  background: white;
  height: 50px; /* Dynamic */
  margin-right: 10px;
  width: 50px; /* Dynamic */
}

span {
  color: white;
  font-size: 40px;
  overflow-wrap: anywhere;
}
<div id="container">
  <div id="image"></div>
  <span>long_text_should_be_<br />allowed_to_break</span>
</div>

However, in my scenario I need to use <wbr> instead of <br> which disrupts the centered alignment.

#container {
  align-items: center;
  background: black;
  display: flex;
  justify-content: center;
  padding: 10px;
  width: 500px;
}

#image {
  background: white;
  height: 50px;
  margin-right: 10px;
  width: 50px;
}

span {
  color: white;
  font-size: 40px;
  overflow-wrap: anywhere;
}
<div id="container">
  <div id="image"></div>
  <span>long_text_should_be_<wbr />allowed_to_break</span>
</div>

I realize that this behavior is expected as line breaks do not adjust to text width within block elements. Despite working well with <br>, I am curious if it can be optimized to achieve similar results with <wbr> or other line break hints.

Objectives:

  • Should function in a dynamic sizing environment, accommodating varying container and image dimensions
  • Should default to break-all behavior when no line break hints are provided
  • Should accommodate text of any length, from 1 to n characters
  • Only needs to support recent versions of Chrome with left-to-right Latin text

Answer №1

If you haven't already, consider using the <p> tag along with CSS margins to adjust top and bottom spacing for your paragraphs.

#container {
  align-items: center;
  background-color: red;
  display: flex;
  justify-content: center;
  width: 500px;
}

#image {
  background: green;
  height: 50px;
  margin-right: 10px;
  width: 50px;
}
#custombr{
margin-top:5px;
margin-bottom:5px
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<div id="container">
  <div id="image"></div>
  <h1>long_text_should_be_<p id="custombr">allowed_to_break</p></h1>
</div>

</body>
</html>

Answer №2

Avoid using <br> or <wbr>. It is not recommended especially if your website will support multiple languages.

An easy solution can be implemented using CSS:

h1 {
  word-wrap: break-word;
  width: 400px;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can HTML be utilized to create a brief description?

I want to insert a link into the brief description that says "Read More..." which will direct the buyer to the main description further down on the product page when clicked. Can someone please provide guidance on how to achieve this? The URL for the link ...

Save hyperlinks in a storage system

I need to store an article in a MongoDB database, and some words within the article should be clickable links leading to other pages. However, when I display the article using an EJS template, the anchor tags I added while writing the article appear as pl ...

Enhance Vuetify pagination with personalized styles

I'm attempting to create a toolbar with pagination control aligned to the right, featuring smaller pagination control buttons. To achieve this, I took the following steps: Wrapped v-pagination in span (since v-spacer did not work without this) Adde ...

Arrange divs on the screen in a specific formation

I am exploring ways to position specific divs in the middle of the screen to create a pearl necklace-like shape. The desired shape is as follows: 0 0 0 0 0 0 0 0 0 0 My aim is to achieve this using jQuery on a mobile ...

What is the best way to ensure consistent spacing between columns in a Bootstrap 3 grid layout, similar to the spacing on the left and right sides?

Is there a way to achieve equal spacing between all blocks in a row, similar to the left and right padding that Bootstrap automatically applies? Currently, Bootstrap adds padding to all columns on both sides, but does not render extra spacing on the left s ...

Accessing CSS content from a bundle within the code of an ASP.NET MVC website

I have a website that utilizes bootstrap along with a customized bootstrap theme. The website is designed to send notification emails to its users upon certain actions being triggered. For these notification emails, I want the content to be in well-format ...

Connect PHP code to an HTML document

Recently, I created an entry form login page saved as a .php file which displays content in html and css. The page showcases various elements followed by a login form with two fields: Username, Password, and a 'Log in' button. Additionally, I ha ...

Modifying .vue files within Laravel seems to unexpectedly impact unrelated CSS styles

I've been working on a Laravel project that involves building Vue components. Strangely, every time I update a .vue file and add it for a commit, modifications are also made to the app.css and app.js files. These changes seem to be undoing a particula ...

Displaying and hiding the top menu when the div is scrolled

I have developed a script that is designed to display the menu in a shaking motion and hide it as you scroll down. It functions properly when scrolling within the body of the webpage, but I am facing issues when attempting to do so with a div that has an o ...

Choose the initial offspring from a shared category and assign a specific class identifier

I am trying to figure out how to apply the "active" class to the first tab-pane within each tab-content section in my HTML code. Here is an example of what I'm working with: <div class="tab-content"> <div class='tab-pane'>< ...

Exploring the bond between siblings from a child's perspective

Is there a way to apply styling to the .item class when the corresponding input field is checked? <input> <span class="item"> I have discovered that I can achieve this using input:checked ~ .item {} However, my challenge lies in obtaining th ...

How can I prevent an image from moving in relation to other objects on a webpage using HTML?

I'm currently working on my HTML website and facing a layout issue. I have an image in the center of the screen, but I want to add text beside it on the right side. Every time I try to do this, the image moves down, which is frustrating. I'm not ...

Tips for limiting a website to load exclusively within an iframe

I am managing two different websites: https://exampleiframe.com (a third-party website), https://example.com (my own website) My goal is to limit the loading of https://example.com so that it only opens within an iframe on https://exampleiframe.com To a ...

Could you provide me with some information regarding the relationship between screen resolution and screen size?

When checking my website on different screen resolutions, I rely on a tool called Screenfly from quirktools.com. While using this tool, I came across an interesting feature - the display icon in the top left corner with a dropdown menu showing resolution ...

What steps are involved in creating a table using Bootstrap framework?

Hey there! I need your expertise on how to create a specific layout using bootstrap: codepen.io/anon/pen/bqJdQJ <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <div cl ...

Flexbox divs that adapt to different screen sizes

Hello everyone, I need help with a school project where I have to make blocks responsive and change their layout based on screen width. Specifically, above 1024px they should be in 2 horizontal rows, and below 1024px in 2 vertical rows, always spelling out ...

Navigating to two separate webpages concurrently in separate frames

I am working on creating a website with frames, where I want to set up a link that opens different pages in two separate frames. Essentially, when you click the link, one page (such as the home page in a .html file) will open in frame 1 on the left side, ...

Updating Navigation Bar Font

I am a novice in the process of constructing my own navigation bar for my website, and I am encountering difficulties when trying to customize the font (color, font-family, etc). Currently, the links in my navigation bar (such as home, contact, store, etc ...

"Placing an image at the bottom within a DIV container in HTML

My goal is to neatly align a group of images at the bottom of a fixed-height div. The images all have the same height and width, making the alignment easier. Here is the current structure of my code: <div id="randomContainer"> <div id="image ...

A webpage styled with w3.css featuring text without any underline

I am currently using w3.css but I'm facing an issue where some of my hyperlinks have underlines while others do not. My goal is to remove all underlines from the hyperlinks in the content below: <ul class="w3-ul w3-white"> <a class="" href=" ...