Enable a single flex item to wrap across multiple lines

I have an HTML code snippet that needs to be styled using Flexbox. Specifically, I want to re-order the elements and create a 'line break' before the last item. How can I utilize Flexbox so that most flex items are treated as atomic units, but one particular item—#c in this case—starts on the same line as the previous item and then wraps like a standard inline element?

section {
   display: flex;
   flex-wrap: wrap;
   width: 450px;
   background:orange;
   padding:0.2em;
}
span {
   background:yellow;
   padding:2px 4px;
   border-radius:3px;
   border:1px solid rgba(0,0,0,50%);
   margin:0 3px;
}
#a { order:1 }
#b { order:2 }
#c {
   order:3;
   background:none;
   border:none;
}
section::before {
   content:'';
   order:4;
   width:100%;
}
#d { order:5 }
<section>
<span id="c">with an earthquake, birds and snakes and aeroplanes, and Lenny Bruce is not afraid</span>
<span id="a">That's great</span>
<span id="b">it starts</span>
<span id="d">— R.E.M.</span>
</section>

To clarify, in the above scenario, I aim to have "That's great it starts with an earthquake" all on one line with text wrapping, followed by "— R.E.M." on a separate line.

Answer №1

I believe that finding a perfect solution may require using some clever workarounds, as there might not be a one-size-fits-all answer

section {
   display: flex;
   flex-wrap: wrap;
   width: 450px;
   background:orange;
   padding:0.2em;
}
span {
   background:yellow;
   padding:2px 4px;
   border-radius:3px;
   border:1px solid rgba(0,0,0,50%);
   margin:0 3px;
}
#a { order:1 }
#b { order:2 }
#c {
   order:3;
   background:none;
   border:none;
/* added this*/
    margin-top: -1.2em;
    text-indent: 148px;
/**/
}

section::before {
   content:'';
   order:4;
   width:100%;
}
#d { order:5 }
<section>
<span id="c">with an earthquake, birds and snakes and aeroplanes, and Lenny Bruce is not afraid</span>
<span id="a">That's great</span>
<span id="b">it starts</span>
<span id="d">— R.E.M.</span>
</section>

Answer №2

Consider updating your CSS (section) as follows:

section {
   display: grid;
   grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
   gap: 10px;
   background: purple;
   padding: 0.5em;
}

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

Hiding a div using the Bootstrap `.hidden-xs` class may not work as

I have been working on a website and I want to hide a specific section for users viewing it on small screens. I tried using Bootstrap's .hidden-xs class, but when I tested it using Chrome's mobile emulator set to "iPhone 5", the div did not hide ...

What are the buttons featured in the header of this mobile-friendly bootstrap panel

Can anyone offer advice on how to make a bootstrap panel header responsive? The panel has a title with several buttons on the right side. While it looks good on a large screen, it becomes messy on smaller browser sizes. You can find my complete code here ...

Creating a mandatory 'Select' component in Material UI with React JS

Is there a method to show an error message in red until a choice is selected? ...

The form yields no response and fails to send any data

Ensuring that the form on this site successfully sends the name and phone number is crucial. However, upon clicking the send button, I encounter an empty modal window instead of a response indicating whether the data was sent or not. The contents of the fi ...

Is it feasible to automate the cropping process with selenium?

Exploring a new challenge: simulating a cropping feature. I understand that replicating human cropping is impossible, but the image I intend to crop remains a fixed size each time I run the test. My goal is to establish the cropping WebElement at a constan ...

CSS media queries with precise inequality restrictions

Imagine the following scenario: @media only screen and (max-width: 600px) { nav { display: none; } } @media only screen and (min-width: 601px) { nav { display: block; } } This setup can lead to undefined behavior for a wid ...

Add a decorative frame around the heading and text block

I'm looking to encase both the header and paragraph in a single border. Right now, they each have their own separate borders. How can I combine them into one? <style type="text/css> h1, p { font-family: consolas; border: 2px solid #73AD21; bor ...

JavaScript and jQuery experiencing difficulty rendering proper style and image in output display

I am currently working on code that extracts information from a JSON variable and displays it on a map. The code looks like this: marker.info_window_content = place.image + '<br/>' +"<h4>" + place.name + "</h4> ...

Ways to update row background color based on specific column values

I need to customize the background color of my table rows based on the value in the "Category" column. For example: Name Category Subcategory A Paid B C Received D If the Category value is 'Paid', I want the ro ...

Creating a scrolling background effect using HTML and CSS: ``Background Parallax

I'm trying to keep the background of my HTML document fixed in the upper left corner, so that when a user scrolls through the page, they only see parts of it. Here's the code I'm using: <body style="background-image: url(background.png) ...

The issue of jumpy CSS background on full screen mode for mobile devices is causing

While developing a web app, I encountered an issue with the responsive (parallax) full screen background. On mobile devices, the background does not extend below the toolbar, causing a jumpy effect when scrolling up and down in Firefox. I have tried differ ...

How to Align Video Alongside Image in HTML without Using CSS

Is there a way to place a video on the left and an image on the right of the same line in HTML without relying on CSS? ...

Modifying the color of a placeholder in an HTML input using CSS

Version 4 of Chrome now supports the placeholder attribute on input[type=text] elements (and likely other browsers do too). Despite this, the following CSS code does not affect the color of the placeholder text: input[placeholder], [placeholder], *[pla ...

Incorporate a platform-specific button in a user interface with React Native

I need to display a button that is only shown on iOS, not on android. I have tried using the following style, but it doesn't seem to be working. ...Platform.select({ android: { display:'none', }, The code for my button is: <Bu ...

Tips for directing attention to a specific row with an input field in JavaScript

I duplicated a table and added an input field for users to search or focus on a specific row. However, there are two issues: When a user enters a row number, the table displays the wrong row - for example, if the user enters 15, the table shows row number ...

Troubleshooting problem with CSS borders in Microsoft Edge Browser

I am currently working on implementing the concept found at the following link: https://www.w3schools.com/howto/howto_js_tabs.asp, but with the additional feature of borders around the elements. However, I have encountered an issue specifically with Micro ...

Issue with the top margin of the body

Recently, I've encountered a peculiar problem with a standard website layout that I use. The issue seems to be centered around the space between the top and the first div: navbar. No matter what I try, I just can't seem to remove it, as the reas ...

Center the text within the div and have it expand outwards from the middle if there is an abundance of text

I have a div that is in the shape of a square box and I want it to be centered. No matter how much text is inside, I would like it to remain in the middle. I am using JQuery to create my square box and would like to center it using CSS. Here is my code: &l ...

Add at the beginning of each row in the text area

I have a small text area on my webpage where I want to add ">>" to the beginning of each line. My initial attempt was to use this code: $("#mytextarea").prepend("EvilHacker001>>"); Unfortunately, this did not produce the desired result. I searched ...

Tips on how to navigate to the end of a div that has been created through ng-repeat in Angular JS with the

Here is the template code I am working with: <div class="chatbox" id="mailBody" > <div class="panel-body" ng-repeat="mail in mails"> <div class="m-b-none" ng-if="mail.inboxMessageType == 1"> <a href class="pull-left ...