What is the best way to achieve flexible content width in a flexbox without compromising the layout? (expand to fit the content as needed while minimizing empty space)

On JSFiddle, I have provided an example of my issue with flexbox. My goal is to make the left column adjust its width based on the content and break a line if the text is too long. Currently, it is taking up minimal space, causing layout issues.

The fiddle below shows two blocks demonstrating how it currently looks and how I want it to look (I have set fixed widths for visualization purposes, but I intend for it to be dynamic with flexbox).

I believe solving this should be straightforward, but I seem to be overthinking it. Any assistance would be greatly appreciated :)

.flex {
  display: flex;
  background: #333;
  max-width: 380px;
}

.first {
  flex: 0;
  background: #666;
}

.second {
  flex: 1;
  background: #999;
}
<p>Current state using flexbox approach</p>

<div class="flex">
  <div class="first">
    Here is my Dynamic Text
  </div>
  <div class="second">
    Next to Text
  </div>
</div>

<br />

<div class="flex">
  <div class="first">
    Here is my Dynamic Text Here is my Dynamic Text 
  </div>
  <div class="second">
    Next to Text
  </div>
</div>

<hr />
<p>Desired outcome</p>
<!-- Ignore all code below, please - everything below is just here for visual reasons -->
<div>
  <div style="background: #666; width: 165px; float: left;">Here is my Dynamic Text</div>
  <div style="background: #999; float: left;">Next to text</div>
</div>
<div style="clear: both; height: 10px;">
</div>
<div>
  <div style="background: #666; width: 302px; float: left;">Here is my Dynamic Text Here is my Dynamic Text</div>
  <div style="background: #999;float: left; height: 36px;">Next to text</div>
</div>

Answer №1

Utilize the white-space:nowrap property on the second element to prevent it from collapsing.

  .flex {
  display: flex;
  border: 1px solid green;
}

.first {
  background: lightblue;
  border: 1px solid grey;
}

.second {
  white-space: nowrap;
  background: lightgreen
}

.narrow {
  width: 50%;
<div class="flex">
  <div class="first">
    Here is my Dynamic Text
  </div>
  <div class="second">
    Next to Text
  </div>
</div>
<hr/>


<div class="flex narrow">
  <div class="first">
    Here is my Dynamic Text Here is my Dynamic Text
  </div>
  <div class="second">
    Next to Text
  </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 process for recording a video?

After researching extensively, I am eager to use a plugin that combines jQuery and flash to record a video using the system's Webcam. I came across scriptcam, but unfortunately, I encountered an issue where the plugin hangs on document load and the li ...

CSS transforms the entire div, ensuring that all elements within it remain

When I try to rotate a set of divs using the following code: map-grid: -webkit-transform:skewX(-45deg) rotate(15deg) scaleX(1.785) scaleY(.8) translateX(7em) translateY(-4.5em); -moz-transform:skewX(-45deg) rotate(15deg) scaleX(1.785) scaleY(.8) trans ...

What is the best way to adjust the canvas size within a list item?

I have successfully created a 3D model with Zdog that dynamically resizes based on the screen size. The model includes a dome and brim, which rotate smoothly as intended. Here is the code snippet: const TAU = Zdog.TAU; let hat = new Zdog.Illustration({ ...

Creating a smooth transition for an element's width change as a neighboring element shows or hides using v-show

I'm working on a Vue.js application with tailwind styling. Within a flex parent container in my template, I have two child elements - one is always present, and the other is conditionally rendered using v-show. The issue arises when the conditional e ...

Transform information just like the capabilities of Google Drive

Is there a way to update my website content similar to Google Drive's functionality? For instance, when using Google Drive, the URL changes from drive/my-drive to drive/recent when navigating to the recent section. I attempted to achieve this by usin ...

Design portions of text that are not enclosed in a div element

Currently, I am retrieving data from an API through a SOAP call. After pulling in the data, the resulting HTML structure appears as follows: <div class="component_wrapper"> 2 man Teams<br> 20 Min AMRAP<br> 50 Double Unders<br> 50 ...

Wordpress Bubble Notification - Conceal at Zero / Reveal at One or More

I am a beginner in PHP and CSS. After extensive reading and learning little by little, I successfully implemented notifications in my project using PHP and CSS. It functions properly, displaying the counts (friends, messages, and notifications) and redire ...

Adjust the size of a Div/Element in real-time using a randomly calculated number

Currently, I am working on a script that is designed to change the dimensions of a div element when a button on the page is clicked. The JavaScript function connected to this button should generate a random number between 1 and 1000, setting it as both the ...

Reverse Text without Using CSS Transformation

I have a unique clock design where the hands are actually words (for example: < p >WORD< /p >), but when they pass 180deg, they naturally turn upside down. Normally, flipping them using transform properties like rotate would be simple, but sinc ...

When the screen width falls below 768px, the columns should expand to full

Currently, I am using Bootstrap 3.3.7 for my project. I'm facing an issue where my images are not extending to full width when the viewport size is between > 630px and < 768px. The images have a width of 400px, which works fine for sizes > 7 ...

Having trouble identifying the specific CSS property that is being used or inherited

I have incorporated components from Twitter Bootstrap CSS into a custom CSS file, focusing mainly on toolbar components. Specifically, I have set up a toolbar with various features. However, when applying additional CSS to the .signinbox_left:hover select ...

Customize dropdown item colors in React using a color picker

I am currently utilizing a react color picker to create a personalized 16 color gradient. The process involves selecting a color from a dropdown menu and then using the color picker to make adjustments. This action updates an array that dictates the stylin ...

Difficulty with Script Causing Margin Bottom to Fail

Check out my website at this link. Below is a div I have created: <div class="home" style="background-image: url(http://payload51.cargocollective.com/1/7/237315/3336908/HomeImage_o.jpg); background-attachment: fixed; height: 660px; width: 100%; opacit ...

GAE does not have access to background images

Having trouble loading background pictures in my GAE front-end app. "Failed to load resource: the server responded with a status of 404 (Not Found)". My app is a simple website, no GAE backend. Using Eclipse with Google AppEngine Plugin. Background pict ...

Retrieve the value of a dynamically added or removed input field in JQuery using Javascript

Check out this informative article here I'm looking for a way to gather the values from all the text boxes and store them in an array within my JavaScript form. I attempted to enclose it in a form, but I'm struggling to retrieve the HTML ID beca ...

aiming for divs that have the css property "left=0"

While working with masonry.js, I find it challenging to add a class to the elements positioned on the far left of the viewport as they are not placed in a specific order by the plugin. Using the nth selector has been difficult to target them effectively. ...

What modifications were implemented in Bootstrap 5 to cause controls to automatically assume their maximum size?

After upgrading NodeJs to version 16.13.0 from 14.x, one of the modules that was updated was bootstrap. This caused me to remove my Jumbotron control due to the upgrade, although I don't believe that is directly related to the issue I am experiencing. ...

Having trouble with Drag & Drop on Safari iOS? It seems that it won't drag or respond to drop on desktop or iPad

As I work on coding a webpage designed for viewing on an iPad, I have encountered an issue with Safari/Webkit's drag and drop functionality. Despite copying Safari's example code exactly, the drag and drop feature refuses to work as expected. Th ...

Django authentication ensures secure access for users on

What could be causing the issue with self.request.user.is_authenticated() not functioning correctly in this specific view? class ArticleDetailView(DetailView, CategoryListMixin): model = Article template_name = 'mainapp/article_detail.html&ap ...

Change the `display: none` of elements back to their original value

Explanation In my coding practice, I have implemented a script that hides certain elements if the user lacks the necessary permission to interact with them. To achieve this, I utilize the CSS property display: none. However, when it comes time to reveal ...