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

Position the background of the container in the center and keep it

I am trying to create a fixed background header using the following CSS properties: header { margin: 0; padding: 0; width: 100%; height: 300px; display: block; background-image: url('...'); background-repeat: no-repea ...

Issues regarding the CSS navigation bar

I'm currently working on creating a navigation bar using HTML and CSS. However, I'm facing an issue where the nav-bar doesn't extend to the full width of the page as intended. I've attempted adjusting the padding-left: 0px and padding-r ...

Struggling to find a solution for altering font color with jQuery while creating a straightforward menu

I'm having trouble changing the color of the active button pressed to "color:white;"... Here is the link to the jsfiddle: http://jsfiddle.net/wLXBR/ Apologies for the clutter in the file, my css is located at the bottom of the CSS box (Foundation cod ...

Unlocking the potential of resizable bootstrap table columns in React

Currently utilizing a bootstrap table <div class="table-responsive"> <table class="table table-bordered"> <thead> <tr> <th>#</th> <th>Table heading</th> </tr> < ...

Determine the placement of a <div> based on information stored in localStorage

I'm diving into the world of localStorage and trying to figure out how it works. To get a better understanding, I decided to create an HTML page that allows users to drag and drop tiles around the screen. Here's a snippet of the code I came up ...

Right-align SELECT-OPTIONS text

Here are the screenshots of the form I'm currently working on. I am aiming to create a select box in the form where the text within the options is aligned to the right. After an option is selected, the chosen text should be displayed as illustrated i ...

Animating a dropdown menu with jQuery

I have a typical submenu structure set up like this: <ul> <li> <a href="#">Parent link</a> <ul> <li><a href="#">Submenu link</a></li> </ul> </li> </ul> In my de ...

Make sure the navigation bar is fixed to the top of the page only

Looking for a solution to stick the navigation menu to the top of the screen only when the screen is wider than 782px. Progress so far: https://jsfiddle.net/ah4ta7xc/1/ Encountering an issue where there is an unintended gap at the top when applying the s ...

Discovering more about this topic

Looking for a way to create an expandable box that enlarges when "read more" is clicked, revealing text below it. And also looking to add a button that closes the expanded text back up. Experimented with the toggletext JavaScript command, as found on this ...

Tips for showing text on top of a responsive image using CSS

I am working on a layout with 4 columns, each containing images of different sizes followed by a hover effect with an absolutely positioned mask. The width of the mask is currently exceeding the width of the images. Is there a way to ensure that the mask ...

I'm having trouble getting my login page centered when using Semantic UI

Currently, I am working on creating a login page using Semantic UI. However, I am facing an issue where the column is not aligning to the center as intended; instead, it is occupying the entire page. I have attempted to link all the necessary files in the ...

Observing changes in the dimensions of flex children using MutationObserver

Considering the following structure of the Document Object Model... <div id="outer-container"> <div class="side-panel"><div width="200px"/></div> <div id="content"><!-- some content --></div> <div class=" ...

Leveraging CSS to automatically number nested sections

Assume we have an example HTML or XML document structured like this: <body> <section> <h1>This should be numbered 1</h1> <section> <h1>This should be numbered 1.1</h1> <p>blah& ...

Achieving left alignment for Material-UI Radio buttons: Float them left

Click here to view the demo https://i.stack.imgur.com/Yt4ya.png Check out the demo above to see the code in action. I'm currently struggling to align the radio buttons horizontally, wondering if there's an easier way to achieve this using Mater ...

Prevent issues with text overlap in HTML and CSS

My text is overlapping when I resize the window. How can I prevent this? I want the text to resize only if it collides with other elements, or stack under each other if resizing is not possible. problem I've already attempted using flex box setting ...

A guide to modifying the color of the Menu component in material-ui

Currently, I am utilizing the Menu component from material-ui and facing an issue while trying to modify the background color. The problem arises when I attempt to apply a color to the Menu, as it ends up altering the entire page's background once the ...

Resize a responsive grid of images

Within my slider, I have three slides with 3 images each. The layout consists of one large image on the left and two smaller ones on the right. To ensure responsiveness, I've used max-width: 100% on the images so they can scale down proportionally as ...

What is the best way to connect a CSS file with multiple pages located within a specific folder in HTML/CSS?

In my CS 205 class project, I am tasked with creating a website. To accomplish this, I used Notepad++ for the HTML files and Notepad for the CSS files. The site consists of an index.html page along with other content pages, each designed separately in Note ...

Adjusting the width of columns in a table

Previously in Bootstrap 3, I was able to use col-sm-xx on the th tags within the thead to resize table columns as needed. However, this functionality is no longer available in Bootstrap 4. How can I achieve a similar effect in Bootstrap 4? <thead> & ...

What is the process for aligning a Component to the right within the Navbar in an AppLayout

In my project, the MainView class is an extension of an AppLayout. I am attempting to populate the Navbar with a logo on the left and the user ID on the right. Despite my efforts, both components keep aligning to the left side. Desired Navbar layout: ...