CSS: Shifting flexbox child to a new row when overflowing

I have a flex row with 2 divs (.box-1 and .box-1) both containing some content. I want them to remain in the same row until the content in .box-1 becomes long enough to push .box-2 to the next line.

.box-2

  • Should have a fixed width on desktop screens.
  • Should be 50% width on tablets.
  • Should be full width on mobile devices.

Currently, the contents in .box-2 overflow but stay on the same line. I would like .box-2 to move to a separate line when the content overflows, or have a minimum width wide enough to hold all its contents.

Note: Using the col-lg-3 class sometimes makes .box-2 too small for its contents.

I am using CSS grid and sticking with Bootstrap classes for this design. Trying to avoid custom styling as much as possible. The .wrappable class is part of the library I'm working with and cannot be modified.

.container {
  max-width: 300px; 
}

.wrappable {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: row wrap;
    flex-flow: row wrap;
    -webkit-justify-content: space-between;
    justify-content: space-between;
    -webkit-align-items: center;
    align-items: center;
      margin-right: -0.625rem;
    margin-bottom: -0.625rem;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
<div class="wrappable">
  <div class="box-1 wrappable-flex border rounded item-1">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, incididunt  ut labore et 
  </div>
  <div class="box-2 col-sm-12 col-md-6 col-lg-3 border rounded text-nowrap">
  some other text I want to put in the same row 
  </div>
</div>
</div>

Answer №1

You might consider including min-width: fit-content; to the .box-2 class.

.container {
  max-width: 300px; 
}

.wrappable {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: row wrap;
    flex-flow: row wrap;
    -webkit-justify-content: space-between;
    justify-content: space-between;
    -webkit-align-items: center;
    align-items: center;
      margin-right: -0.625rem;
    margin-bottom: -0.625rem;
}

.box-2 {
  min-width: fit-content;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
<div class="wrappable">
  <div class="box-1 wrappable-flex border rounded item-1">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, incididunt  ut labore et 
  </div>
  <div class="box-2 col-sm-12 col-md-6 col-lg-3 border rounded text-nowrap">
  some other text I want to put in the same row 
  </div>
</div>
</div>

Answer №2

My approach was to stick with using only bootstrap classes, and a simple adjustment to the width classes made all the difference.

Instead of sticking with col-lg-3, I switched it to col-lg-auto. It seems obvious now. I found this helpful information on the official bootstrap site.

.container {
  max-width: 300px; 
}

.wrappable {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: row wrap;
    flex-flow: row wrap;
    -webkit-justify-content: space-between;
    justify-content: space-between;
    -webkit-align-items: center;
    align-items: center;
      margin-right: -0.625rem;
    margin-bottom: -0.625rem;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
<div class="wrappable">
  <div class="box-1 wrappable-flex border rounded item-1">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, incididunt  ut labore et 
  </div>
  <div class="box-2 col-sm-12 col-md-6 col-lg-auto border rounded text-nowrap">
  some other text I want to put in the same row 
  </div>
</div>
</div>

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

Title Frame and Interactive Sidebar Content

Hello, I am a newcomer in the world of WordPress website design and I have been struggling with a minor CSS issue on my site (www.dimjaa.org) for the past couple of days. Despite my efforts, I have been unable to find a solution on my own. I am reaching ou ...

Looking to adjust the background-image size of a table cell (td) upon clicking a specific

I have a website where I would like to display logos of different games with links attached to them. I have managed to adjust the size of the image on hover, but now I am looking for a way to keep the size bigger after clicking on the link. Is there a simp ...

Javascript: Harnessing Textbox Arrays for Improved Functionality

Check out the form displayed below. <form id="upload_form" enctype="multipart/form-data" method="post"> <input type="text" name="name[]" id="name"><br> <input type="text" name="name[]" id="name"><br> <input type="fil ...

Add content or HTML to a page without changing the structure of the document

UPDATE #2 I found the solution to my question through Google Support, feel free to read my answer below. UPDATE #1 This question leans more towards SEO rather than technical aspects. I will search for an answer elsewhere and share it here once I have th ...

What is the best way to place a dropdown menu in front of buttons?

Here is an example snippet of HTML code that showcases a SweetAlert2 date picker popup for a button titled "Click me!": <!DOCTYPE html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf ...

Struggling to display a table on my website using HTML and CSS

Struggling to display a table in an HTML/CSS popup used with openlayers 3 when clicking on the map. I'm new to HTML and CSS, and despite searching for a solution all afternoon, I can't seem to figure it out. Apologies if I'm missing a basic ...

Guide on solving the issue of a bootstrap 4 carousel loop within WordPress posts

Is it possible to create a dynamic Bootstrap 4 carousel within Wordpress custom post types? Here is the code snippet: <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> < ...

trouble displaying strength of passwords in AngularJS

Recently, I've delved into the world of angular js and have been working on a new directive to showcase the strength of passwords. If you'd like to see what I've done so far, check out this fiddle - https://jsfiddle.net/user_123/3hruj8ce/12 ...

Align pictures in the middle of two divisions within a segment

This is the current code: HTML: <section class="sponsorSection"> <div class="sponsorImageRow"> <div class="sponsorImageColumn"> <img src="img/kvadrat_logo.png" class="sponsorpicture1"/> </div& ...

Adjusting the CSS for a specific element while hovering over another

I'm currently facing the challenge of creating a fixed position navbar with white links that should change to black when hovering over another element with a white background. Here is the CSS for the navbar links: nav ul li a {color:#ffffff;} CSS f ...

Incorporate gulp-clean-css into the current gulpfile.js

I was recently provided with a gulpfile.js file by a colleague, which contains the code below. It keeps an eye on my scss files and compiles them when I save. var gulp = require('gulp'), gutil = require('gulp-util'), browser ...

The Bootstrap form validation is preventing the Ajax request from functioning properly

Having successfully implemented a form with Bootstrap validation, I encountered an issue where the AJAX Post method fails to execute after validation. The catch clause does not capture any errors and only the ajax portion of the code doesn't run. Belo ...

Is the container in semantic ui being breached by a segment overflow?

I've recently started learning Semantic UI. One issue I'm facing is that the width of the "segment" overflows the "container." Check this JSFiddle for more clarity. Are there any alternative solutions? In addition to the grid system, I'm ...

Display "Temporary Text" in an HTML drop-down list if no selection is made

Is there a way to ensure that the placeholder in an existing select element always remains visible? I noticed that when I select an option and then go back to the blank item, the placeholder disappears. How can I make sure it is always displayed when no ...

My element's padding being overlooked by Tailwind CSS

In the process of developing a comment/response mechanism through MPTT, I am utilizing indentation to clearly designate replies and their respective levels. The comment.level attribute is being employed to establish the padding value. Consequently, a comm ...

JQuery fails to retrieve accurate width measurements

Utilizing this code snippet, I have been able to obtain the width of an element and then set it as its height: $(document).ready(function(){ $(".equal-height").each(function(){ var itemSize = $(this).outerWidth(); cons ...

Delete an entry from the localStorage

I am attempting to create a basic To-Do list using jQuery, but I have encountered an issue. This is my first time utilizing localStorage. After setting up the structure for my To-Do list, I wanted the items to remain visible when I refresh the page. Initia ...

Aligning the stars with CSS

One of the components I have deals with a star rating system, and I thought it would be cool to use Font Awesome icons for half stars. Everything is working well except for the CSS styling aspect. While I managed to position some of the stars correctly by ...

The width property is ineffective when used in conjunction with the d3 styling method

My goal is to create bar graphs where the width of each bar represents a person's age. However, I am having trouble applying the width attribute to the bars using the style method in d3 (I checked this by inspecting the elements in the browser). Other ...

Dynamically insert textboxes into a form by leveraging the power of the Jade template engine

Looking for a solution to a simple requirement that doesn't seem to have a clear answer online. I need a combobox on my jade page that accepts numbers as input. When the user selects a number, I want the page to refresh and display that many textboxes ...