Ensure that the flex item is displayed on a single line with maximum force

I have this example below and I am looking to always display the blue item text on a single line, regardless of how much content is inside. (dynamic content) Essentially, I want the red item to shrink in width when the blue item has more text.

Is there a way to achieve this?

Thank you

PS. Please do not question my use of flex here. It is necessary for a larger example where it is needed, this example is just simplified.

.flexbox {
  display: flex;
}
.red {
  background: red;
}   
.blue {
  background: blue;
}
<div class="flexbox">
  <div class="red">red red red red red red red red red red red red red red red red  red red red red red red </div>
  <div class="blue">blue blue blue blue blue</div>
</div>

Answer №1

If you want to prevent the blue section from shrinking, one option is to use flex-shrink:0source:

.container {
  display: flex;
  margin:10px;
}
.red {
  background: red;
}   
.blue {
  background: blue;
  flex-shrink:0;
}
<div class="container">
  <div class="red">red red red red red red red red red red red red red red red red  red red red red red red </div>
  <div class="blue">blue blue blue blue blue blue blue blue blue blue blue</div>
</div>

Alternatively, you can also apply white-space:nowrap to the blue part:

.container {
  display: flex;
  margin:10px;
}
.red {
  background: red;
}   
.blue {
  background: blue;
  white-space:nowrap;
}
<div class="container">
  <div class="red">red red red red red red red red red red red red red red red red  red red red red red red </div>
  <div class="blue">blue blue blue blue blue blue blue blue blue blue blue blue</div>
</div>

Another approach would be to set flex:1 on the red element to allow the content of the blue part to wrap into multiple lines:

.container {
  display: flex;
  margin:10px;
}
.red {
  background: red;
  flex:1;
}   
.blue {
  background: blue;
}
<div class="container">
  <div class="red">red red red red red red red red red red red red red red red red  red red red red red red </div>
  <div class="blue">blue blue blue blue blue blue blue blue blue blue blue</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

What is the best way to execute a function based on the width of the screen?

Two functions need to be called based on the screen width. Here is one attempt: var winWidth = $(window).width(); var sections = function() { $('.image').each(function(){ var $this = $(this), x = $this.find('img'). ...

Assigning a height of 100% to a div element results in the appearance of

In a div within the body, there are only 3 lines of text. The background color was only filling up to those 3 lines of text. To make the color fill up the entire vertical space of the browser, I adjusted the CSS height properties of html & body to be ...

What could be causing the incorrect display of updates when my Grails checkbox onclick remote function Ajax call is triggered

Hi, I have a page named taskReminder that renders two templates: one for tasks and another for reminders. The task template includes a checkbox that, when checked, should update the task list. Everything is functioning correctly, but there seems to be an i ...

Instructions on how to navigate back one page to determine which page initiated the action

I am looking for a solution to implement a back button that takes me back one page while caching it. Currently, I am using the following code: <a id="go-back" href="javascript:history.go(-1)" type="button" class="btn btn-danger">Back</a> Howe ...

What are some effective strategies for keeping content from shrinking in a drawer menu?

I am currently using a tutorial from https://www.w3schools.com/howto/howto_js_sidenav.asp to create a drawer sidebar menu. However, in my scenario, I need to include a wall of text instead of <a> links. The issue I am facing is that the text expands ...

Design inspired by dot matrix patterns

Is it possible to create a background HTML page filled with a dot matrix designing tool? Can anyone provide guidance on how to achieve this? If I need to use a background-image for this, where can I find the appropriate picture? Thank you to everyone in ...

A guide on adding up all the values in a table's columns with JavaScript

I have implemented a web application feature where table rows are dynamically added using JavaScript. Each row contains a Qty Col, Unit Amount Col, and Line Total Col. Users can also remove a line after adding it to the table. In this scenario, I need t ...

Printing the footer on a page in ASP.NET is essential for providing additional information

I need to print an HTML page with specific content. The content includes a div with some text to be printed. Additionally, I have a footer with a class of divFooter that contains more content. <div>content to print</div> The CSS for styling t ...

ISP Blocks Flash Access

My innovative set of flash games has been adopted by numerous schools throughout the UK. However, I recently encountered an issue where one school was unable to load these Flash games... After reaching out to the school's Internet Service Provider (S ...

Guide to customizing WordPress header menu buttons with CSS, excluding a specific button

I need help with custom CSS for my WordPress site. I want to apply a hover effect to all menu buttons except for the "Contact" button, which already has its own styling. The CSS code adds a growing line beneath the hovered menu item and a static line unde ...

Retrieve the checkbox value and assign it to an object in the $scope of an Angular

As a newcomer to angularjs, I've been struggling with this code snippet for the past two days. My goal is to retrieve values from selected checkboxes within my angular js controller. I have experimented with both ng-true-value and ng-false-value, bu ...

When utilizing Angular, the mat-datepicker is displayed underneath the modal, even after attempting to modify the z-index

I am encountering a problem with a mat-datepicker displaying below a modal in my Angular application. Here are the key details: Html: <div class="col-12"> <mat-form-field appearance="fill"> <mat-label>Start Date ...

Troubleshooting email form validation issues in Vue.js

Even after entering the email id, I am still receiving the error message stating that the email is required. Here is a screenshot for reference: . I want the error message to be hidden when the email id is entered. HTML: <form class="footer-f ...

"Delve into the art of utilizing negative space between elements with

Looking to implement hover detection between rows in a grid container, rather than on individual items. The number of items in the container and per row may vary. https://i.sstatic.net/yBgKI.png It's important to note that the item count in the cont ...

How can I remove the gaps between Bootstrap panels on a website?

The Issue https://i.stack.imgur.com/I5EFR.png My problem is with the spacing between the panels, highlighted in red. I've attempted to remove them by using the following CSS: .panel { margin-top: 0; margin-bottom: 0; padding-top: 0; ...

Uncertainties surrounding the complexity of this multi-stage form

Exploring this multi-step form I have a couple of questions: 1) Is there a way to transfer the value from the first step to the second step of the form? 2) How can I ensure that the datepicker value is not empty (to prevent user progress in the firs ...

Navigating the battleground between native and HTML5 mobile applications

As an experienced iOS developer with knowledge in Windows Phone and Android, I have observed the increasing popularity of HTML5 web-apps over native apps. This shift towards HTML5 development frustrates me, as a strong argument can be made for native app d ...

Toggle the visibility of content with jQuery or Ajax

As I work on designing a webpage at this link, I am facing an issue with the projects tab due to my lack of experience in proper protocol. The plan is to have a side menu where users can select different content options (CAD, FEA, MATLAB, etc.), which wi ...

Show fixed div at specific height

Is there a way to have a fixed title in a DIV that only displays once the user scrolls to the relevant section on the website? I'm looking for a solution where the DIV is hidden until the section is reached during scrolling. ...

Managing the height of a TextArea within a Flexbox environment

After exploring various methods for achieving auto-height in a textarea, I decided to go with the contenteditable approach as it seemed cleaner to me. However, while it worked perfectly on its own, it failed when placed within flexbox containers. I experi ...