Bootstrap 4 - Column breaking onto a fresh line

I am facing a challenging issue with bootstrap 4.

Here is the problem: I have a row with two columns in the code snippet below. The first column contains an extremely long text without any spaces, while the second one is a small column specified as "col-auto".

<html>

    <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    </head>

    <body>
        <div style="background-color: red; width: 500px;" class="container">
            <div class="row">
                <div class="col">
                    THIS_IS_A_SUPER_LONG_TEXT_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                </div>
                <div class="col-auto">
                    RIGHT
                </div>
            </div>
        </div>
    </body>

</html>

The issue arises when my columns are displayed on two separate lines, rather than wrapping around as desired.

Please refer to the example image here:

https://i.sstatic.net/iMaP8.png

What I want is for the text to wrap properly, and look like this (without additional spaces):

https://i.sstatic.net/mtlDE.png

Can anyone advise if this is achievable? If so, which CSS property should be applied to which tag? I've tried using the width:auto attribute but haven't been successful.

Thank you

Answer №1

Utilize break-word feature:

Given that col delegates grid management to bootstrap, achieving this outcome with col alone is not feasible. The solution involves adding the break-word property to the col class to prevent text overflow within the parent div.

.col {
  overflow-wrap: break-word;
}
<html>

<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>

<body>
  <div style="background-color: red; width: 500px;" class="container">
    <div class="row">
      <div class="col">
        THIS_IS_A_SUPER_LONG_TEXT_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      </div>
      <div class="col-auto">
        RIGHT
      </div>
    </div>
  </div>
</body>

</html>

Implementing bootstrap col-x-x:

Alternatively, you can accomplish this using col-6 and applying it to both child div elements in order to signify two columns of equal height within the same row. However, due to the lengthy text, including break-word in the respective div is still necessary to avoid layout issues. For a more thorough understanding of Bootstrap's grid system and discovering tailored solutions, I recommend consulting the official Bootstrap documentation from here or exploring the W3Schools guide on Bootstrap grids at here. Here is the code snippet to achieve the desired result:

div.row>div:first-of-type.col-6 {
  overflow-wrap: break-word;
}
<html>

<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>

<body>
  <div style="background-color: red;" class="container">
    <div class="row">
      <div class="col-6">
        THIS_IS_A_SUPER_LONG_TEXT_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      </div>
      <div class="col-6">
        RIGHT
      </div>
    </div>
  </div>
</body>

</html>

NOTE: To prevent further complications, removing width: 500px; from the parent element is advisable.

Answer №2

To ensure text breaks correctly, utilize bootstrap's .text-break when dealing with plain text.

.text-break {
  word-break: break-word !important; // IE & < Edge 18
  overflow-wrap: break-word !important;
}

<html>

<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>

<body>
  <div style="background-color: red; width: 500px;" class="container">
    <div class="row">
      <div class="col text-break">
        THIS_IS_A_SUPER_LONG_TEXT_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      </div>
      <div class="col-auto">
        RIGHT
      </div>
    </div>
  </div>
</body>

</html>

If the text is preformatted, using text-break won't apply. In such cases, where bootstrap lacks a preformatted text wrapping class, custom CSS is necessary.

.pre-text-wrap  {
  white-space: normal !important;
  word-break: break-all;
}

.pre-text-wrap {
  white-space: normal !important;
  word-break: break-all;
}
<html>

<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>

<body>
  <div style="background-color: red; width: 500px;" class="container">
    <div class="row">
      <div class="col">
        <pre class="pre-text-wrap">
                     THIS_IS_A_SUPER_LONG_TEXT_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
</pre>
      </div>
      <div class="col-auto">
        RIGHT
      </div>
    </div>
  </div>
</body>

</html>

Alternatively, you can make use of bootstrap's .text-wrap along with word-break: break-all; to facilitate text wrapping.

.text-break-all {
   word-break: break-all;
}

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

Guide on triggering a bootstrap modal by clicking on the image within an AngularJS application

In my angularjs project, I am currently working on a UI feature that involves cards. I have successfully implemented a modal box that opens when a button below the card image is clicked. However, I am looking to enhance the user experience by allowing th ...

Discover the current style being applied using JavaScript

After taking a look at this particular post on Stack Overflow about How to retrieve the background color of an element using javascript? I discovered that by using div.style.color You can access the color applied to the div element. However, it seems ...

Transferring a selection from a dropdown menu

I am currently working on a dropdown menu where each entry has an ID and a value. The issue I am facing is that when I click on an item on the list, the click-handler is called and passes the div. While the click handler can access the id without any probl ...

Stylish date picker in CSS

Here is a design mockup along with my current solution. The issue arises when two numbers are adjacent to each other (either horizontally or vertically) as they should merge, illustrated in the mockup below. For example, when selecting numbers 48-49-50, t ...

A rightward sliding submenu that appears upon clicking a button

I have a vision of creating an admin control panel for my website, featuring a set of buttons. I envision that when one of the buttons is clicked, the corresponding sub-menu will appear alongside it. For instance, if "My Account" is selected, its sub-menu ...

How to Style Bootstrap 4 Tables with Rounded Borders

Currently, I am in the process of enhancing the design of a Bootstrap 4 table within a Node application using handlebars (.hbs). While I have been able to adjust the width of the table with CSS, I am encountering challenges in creating the desired borders ...

Customizing the Mui Theme in a Unique Way

After generating an MUI component from Mui DatePicker, I encountered the following code: <span class="my-theme-MuiTypography-root my-theme-MuiTypography-caption my-theme-MuiDayCalendar-weekdayLabel css-1mln09i-MuiTypography-root-MuiDayCalendar-week ...

The font face feature does not seem to be functioning properly in the IE browser

I'm facing an issue with font face in a jQuery popup. It seems to be working fine on Chrome and Firefox, but it's not rendering properly on IE browsers. Font Face Code in css ---------------------- @font-face { font-family: "pt-sans"; sr ...

Firefox is having trouble keeping <select> tags within their designated borders

I am currently developing a mobile app that requires the use of 3 <select> elements stacked on top of each other. It is crucial for these tags to align perfectly with each other and not extend beyond the boundaries of the background div. I have manag ...

Conceal a text field depending on the value within a hidden field that was populated automatically by a JavaScript function

I encountered a peculiar issue recently. I have a form with a field for entering card numbers. There is a JavaScript code that automatically detects the type of card (such as Maestro, Visa, etc.) based on the entered number. If the detected card type is &a ...

What are the steps for enlarging the display containing components with the 'position: absolute' style?

How can I magnify the screen with the following code? .drawing-board { width: 25%; height: 25%; background-color: black; position: relative; /* transform: scale(2, 2); */ } .drawing-board .box-1 { width: 20px; height: 20px; background-c ...

jQuery neglecting events of child elements

I am facing an issue with the following list structure: <ul id="menu"> <li style="background-image:url('Images/X-AFH-Company-Office-Background.png')" id="office"> <div >Company / Office</div> ...

Creating personalized labels with Vue.js and Element UI radio button components

When using Element UI radio buttons on a form, I am attempting to personalize the label for each selection. Each radio button includes a prop called "label" where I can input my label text successfully. The problem arises when I need to apply a different s ...

Guide to adding an icon within an HTML `<input>` element

Is anyone able to help me successfully place my icon font inside the search input field? I've tried adjusting the margin and padding, but nothing seems to be working! If you have any insights or suggestions, please let me know. Your help is greatly a ...

What could be causing my animation element to not be aligned in the center?

I'm having trouble figuring out why the car in my animation isn't swerving around the middle line. It seems to be veering a bit off to the right? .car { width: 40px; height: 60px; background: #f00; left: 50%; transform: translate(-50 ...

Entering text into the input field with the string "Foo"<[email protected]> is not functioning correctly

The text in my input is initially filled with this string "foo foo"<<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f2920200f29202061263b">[email protected]</a>>. However, after an insert, the string now l ...

Preserve text input from a multi-line textarea

Having an issue with a form that includes a <textarea>....</textarea> field. When saving the text, it displays the result in another form but as one continuous line within a paragraph <p>...</p>. The problem arises when trying to e ...

List of prices with a dotted line separating the price and product, adjusting its width based on

I am attempting to create a price list with a dotted underline between the price and product that adjusts dynamically in width. Here is my code snippet: https://jsfiddle.net/romariokbn/2gm27rv6/ <ul class="how-can-i-do"> <li> <span ...

Need inspiration for testing web content with Protractor?

Exploring new possibilities for testing web content with Protractor. Any fresh ideas on what else can be tested? So far, I've checked links to ensure they redirect correctly. I've also verified text color and decoration changes when hovering ove ...