Creating flex items that mimic the fluidity of text

Is there a way to achieve fluid text behavior for flex items like in the following example:

.container{
        display: flex;
        flex-wrap: wrap;
        max-width: 200px;
        background: red;
      }

      div:not(.container){
        word-break: break-word;
        background:blue;
        color: white;
        font-weight: 600;
      }
    
<div class="container">
        <div>Looooooooooooooooooong text looooooooong</div>
        <div>Smooll</div>
        <div>Smoll</div>
      </div>
    

Is it possible for the items to behave like fluid text? For example, have "Smooll" appear on the same row as the end of "looooong text," as if they were just normal text rather than being in a flex container. The number of items is dynamic. Thank you for taking the time to read this!

Edit:

I need to use flex to remove an element (dynamically chosen) based on an additional class on the specific item. The desired end result is illustrated here: https://i.sstatic.net/c6Y1z.png

Answer №1

One technique I’ve observed in handling cases similar to this is to enclose every character within <span> tags and apply white-space: break-spaces to achieve a layout resembling regular text flow using flexbox.

You can then group multiple characters that belong together under the same “unit” by assigning them a specific class. For instance:

<style>
div {
  display: flex;
  flex-wrap: wrap;
}

span {
  display: block;
  white-space: break-spaces;
}
</style>

<div>
  <span class="part1">L</span>
  <span class="part1">o</span>
  <span class="part1">o</span>
  ...
  <span class="part1">n</span>
  <span class="part1">g</span>
  <span> </span>
  <span class="part2">S</span>
  <span class="part2">m</span>
  <span class="part2">o</span>
  <span class="part2">l</span>
</div>

Given the current details available, perhaps a strategy like this could be effective?

Another option to explore is wrapping words in <span> tags instead of individual characters and using the column-gap CSS property for spacing.

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

Executing a post request asynchronously and storing the retrieved data into a variable

Currently, I am in the process of learning AngularJS and attempting to upgrade my current method of fetching data from a web service and dynamically assigning it to a variable that is binded using ng-repeat. My main objective is to implement an asynchronou ...

Javascript code requires server to have default text field values

Based on the choice made by a user, a text field box will either remain concealed or revealed: $("#aForm").on("change", function() { if ($(this).val() == "a") $("#textField").hide(); else $("#textField").show(); }); The issue arises when the ...

When Google Chrome encounters two variables or functions with the same name, how does it respond?

I am curious what happens when Google Chrome encounters two variables with the same name. In my particular case, this issue arises in the code snippet available at this link, which is a small portion of the entire code. I am facing an issue where placing C ...

Place a div at the bottom of a flexbox while adding some padding around it

Here's what I'm dealing with: .flex-container { display: flex; justify-content: center; padding: 5%; position: relative; } .box1 { position: absolute; bottom: 0; width: 100%; height: 100%; } <div class="flex-container"> ...

What is the reason for the lack of letter-spacing between a nested inline element and the next character that follows?

Upon reviewing the specifications for letter-spacing, it is evident that runs of atomic inlines (e.g. inline-block) are considered as a single character (http://www.w3.org/TR/css3-text/#letter-spacing): For letter-spacing purposes, each consecutive run ...

Tips for adjusting animations using @media queries

My animation, , appears correctly on mobile but not desktop. It is too wide on desktop and I have tried adjusting the CSS without success. @media (max-width: 5000px) and (min-width: 768px) {.contact::before.contact::after {content: attr(data-text); posit ...

Implementing a jQuery function to trigger window resize on window load

I'm facing an issue with my resize function where the contents within a window change when it reaches a certain width. I'm trying to achieve this same effect on window load but haven't been successful so far. Here's what I've attem ...

Unable to retrieve the sum total of all product items and display it in the designated text element

My product items are dynamically generated in a list. I have used the calculateItemTotal() method to determine the total for each item. Now, I need to sum up all these item totals and display the result in the total text field. However, instead of showing ...

deployJava.js injects a new <embed> element into the header section of the webpage

I've ran into an issue with the Java applets on my website. I included the deployJava.js load tag in the head section of the page, but when I look at the resulting HTML in Chrome debugger, this script seems to be breaking my head content and starting ...

Adjust the background color of alternate elements

I am looking to customize the background colors of every other element within the structure provided below: <div class="dets"> <div>Simple Layout</div> <div>Few Pictures/Links</div> <div>Element Uniformity</div> ...

Tips on designing checkboxes with CSS

Can someone help me figure out how to style a checkbox using the code below? <input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" /> I've tried applying this style but it doesn't seem to work. The chec ...

javascript passing a window object as an argument to a function

In Slider function, I am passing a window object that does not contain the body element. Additionally, my code only functions correctly on mobile screens. While debugging the code below: console.log(windows.document); If (!mySlider) {console.log(windows. ...

Animating Divs with jQuery to Expand their Size

I am currently designing a services page for my portfolio website. The layout consists of three columns, with the central column containing a large box and the left and right columns each containing three smaller boxes. These smaller boxes function as clic ...

Connecting components in React using their IDs on the same page

In my project to create a one-page website in React, I have divided it into different components such as a navbar component and an education component. My goal now is to link the navbar to the education section. In my App.js file, I am including the educat ...

Leveraging AJAX for implementing PHP scripts

While I may not be an MVC model expert, I'm trying to keep my page design separate from my logic in order to simplify things. I have already created a basic template and now I want hyperlinks to open PHP files within the same page. For example: Next ...

Divide the webpage into four equal sections, each showcasing distinct information sourced from the database

Is there a way to divide my page into 4 sections with data from a database source? Currently, I am displaying the data from the database in a single table, but I would like to split it into four parts, each showing different results from the database. Ad ...

Transfer information from the client to the server using AJAX and PHP by

When attempting to post a JavaScript variable called posY to a PHP file, an error occurred with the message: Notice: Undefined index: data in C:\xampp\htdocs\Heads_in_the_clouds\submitposY.php The posY variable is defined in the JavaSc ...

How do I input text into a Google Doc using an XPATH or element that is difficult to manipulate?

I'm currently working on a Python code that is designed to automatically open a Google Doc and input a specific set of combinations for a predetermined length. While I've successfully created the code to navigate to my Google Docs and open a new ...

What is the method for inserting form control values into a QueryString within HTML code?

Struggling with passing HTML form control values into a QueryString for page redirection. While I can easily input static values into a QueryString and retrieve them using PHP's GET method, I am encountering difficulties when it comes to dynamic valu ...

Updating the color of tick marks on checkboxes

I have successfully updated the background color of my checkboxes using a custom function. However, the tick mark on the checkbox now turns black instead of remaining white. How can I keep the tick mark white while changing the background color? Here is t ...