How to create a line break within a flex container

* {
  margin: 0;
  padding: 0;
}

.cnt {
  display: flex;
  margin: 50px;
  background: #ffff00;
  justify-content: space-between;
  flex-wrap: wrap;
}

.cnt div {
  width: 100px;
  height: 100px;
  background: #999;
  margin: 10px;
}

.cnt::after {
  content: '';
  width: 100%;
}

.flex-item:nth-child(6) {
  order: 1;
}
<div class="cnt">
  <div class="flex-item one"></div>
  <div class="flex-item two"></div>
  <div class="flex-item three"></div>
  <div class="flex-item four"></div>
  <div class="flex-item five"></div>
  <div class="flex-item six"></div>
</div>

The issue at hand is my inability to break lines whenever I want. Is there a way for me to dictate where the line should break? For instance, if I wish to create a line break after the .two class, how can this be achieved?

Amendment:

I've realized my mistake. This is indeed how you can set up line breaks. You just need to designate which items should appear on the next line by changing their order. Of course, this can only be done once.

To break the line after the second box, you must assign the .three, .four, .five, and .six classes to order: 1. Check out this example: https://jsfiddle.net/r33muub3/4/ I adjusted the justify-content to left for better visibility.

Answer №1

Through thorough investigation, I have come to realize that my initial response was not accurate. The method I suggested for adding a newline in the content: property is incorrect, and for that, I apologize.

The correct way to insert a newline (as discovered in this referenced answer to a similar query) is by using \A instead of <br> within the content: property. Additionally, the styling of the element generated by ::after (or ::before) should follow this format:

.two::after {
  content: "\A";
  white-space: pre;
}

While this code snippet successfully embeds a new line in the "anonymous element" produced by assigning a content: property to an ::after-selected element, it does not address the challenge of handling <div> elements within a flex layout.

I acknowledge my prior mistake and retain my original answer to explain the errors made and offer further context to this revised response.


Additional Resources:

  • Another solution to a similar issue that might shed light on this question: Line break in multi-line flexbox.
  • Explore another related inquiry regarding line breaks in flexboxes: Text not breaking inside of flexbox.

An Acknowledgment of My Previous Inaccuracy:

Reasoning Behind This Error: The

.class::after {content: "";}
declaration does not position items after the chosen element but adds its argument inside an unidentified element attached to the existing contents of the selected element. Visit this website for a detailed explanation of the ::after tag and refer to this page for insights into the actual functionality of the content: property.

To implement a style adjustment:

div.two::after {content: "<br />";}

This modification will introduce a line break following every <div> element with class="two". The ::after selector will directly apply the styling at the conclusion of any appropriate element's content. In this scenario, we include some text, a line break (<br />), in the space following a

<div class="two">
utilizing the content: CSS property. Note the inclusion of quotes around the line break "<br />"; these are essential for treating the argument as a string.

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

Mastering the art of selecting checkboxes and Font Awesome icons with JQuery

I find selecting elements using JQuery by their ID or classname quite challenging and it's a bit outside of my expertise. When a function is called, I would like to apply the following styles: .classname input[type="checkbox"] ~ i.fa.fa-circle-o { ...

What can be achieved by combining Text Wrangler and terminal?

I'm curious about how to open an index.html file in terminal. For instance, I know that using sublime works like this: sublime index.html But what about textWrangler? And in general, how do I locate similar commands like "sublime" for any program of ...

Looking to set the "min" attribute for an input type="date" in HTML5 with a specific date format preselected?

My upcoming date has a specific format as detailed below. Date_received = Tue Apr 05 2016 00:00:00 GMT+0530 (India Standard Time) To set the 'min' attribute for the HTML5 input type="date" datepicker, I need to initialize it using the following ...

Pressing enter on the pop-up will activate the submit buttons on the landing page

I am facing an issue with a page that contains two submit buttons and a popup (with the ID: addPopup) that also has a submit button. When the Enter key is pressed on the popup, the first submit button on the main page is triggered instead of the one on the ...

How can scriptlets be used to dynamically create a properly functioning model?

Having trouble populating data into the modal form. I've tried placing the modal code inside a for-each loop, and while it successfully displays the details in the modal, the functionality of the close button and clicking outside the modal to close it ...

Initiating the ngOnInit lifecycle hook in child components within Angular

I am facing an issue with controlling the behavior of child components in my Angular application. The problem arises when switching between different labels, causing the ngOnInit lifecycle hook of the children components not to trigger. The main component ...

Exploring Jinja: Navigating through nested JSON structures

I am currently delving into Jinja in order to construct a basic website. How can I retrieve values from nested JSON using Jinja syntax? For instance: How do I isolate the "Education" key from the JSON snippet below to fill in the HTML snippet? ...

Adding a tint to a transparent image

Is there a way to tint only the white elements in the navbar when they are clicked, rather than applying the color overlay to the entire image? I have tried different methods but have not been able to achieve this effect. Ideally, I would like the white pa ...

How can I position the navigation bar ul list on the opposite side of the logo?

I've been struggling to align the text within my id= "nav-bar" to the right of the header using float:right. Despite attempting options such as using float:left and absolute positioning for the logo on the left, I haven't been successful. Even af ...

Displaying the chosen plan value in a text field using vue js and html – a guide

Here is the json data I have: {"status": true, "plans": [{"planId": 1, "name": "Basic", "cost": 500.0, "validity": 365}, {"planId": 3, "name": "Free", "cost": 0.0, "validity": 999}, {"planId": 4, "name": "Premium", "cost": 500.0, "validity": 500}, {"planI ...

"Problem: Data entered into Internet Explorer forms disappears when the browser is refreshed

I am working on implementing sticky forms using AJAX-based JavaScript instead of PHP. While my setup functions smoothly on both Internet Explorer and Firefox for back/forward navigation, I am facing an issue with page refresh. Despite trying various cache ...

Issues with escaping HTML encoding

Currently working on an Android app that involves querying a webservice and receiving a JsonObject. Once I have the desired String, I encounter characters like: est&amp;aacute; I've experimented with two methods: StringEscapeUtils.escapeHTML4(te ...

The el-dialog is functioning properly, however, I am looking to add CSS that will animate it to open

I've set up a dialog to open on button click, and it's functioning correctly. My goal is to have the dialog open from the bottom with a height of 300px, show the contents inside it, and then hide when I click outside or inside any element. Here ...

Issues with modals appearing improperly after transitioning to NG-Bootstrap 15 and Bootstrap 5 upgrade

UPDATED following a thorough examination: In the process of upgrading our application from NG-Boostrap v11 with bootstrap 4 to NG-Boostrap v15 with Bootstrap 5 and Popper, we also made the switch from Angular 15 to Angular 16. Despite successfully resolvi ...

Display various sets of data from multiple models within a single view

My project is focused on Asp.net Mvc and I am encountering a challenge in displaying multiple model data in one view. However, I do not want to showcase it in a list or grid format, instead, I prefer a Form View style. The two modal classes involved are Pr ...

Can display transitions be achieved through CSS?

Something similar #apex1-1{ width: 100%; height: 40px; background: rgba(255, 255, 255, 0.8); } #apex1-1a{ display: none; transition: opacity 2s; } Instead of abruptly appearing when you change its display property, I want the element to gradual ...

Fetch real-time server information using the img src attribute

Currently, I am using ajax jQuery to dynamically populate an image src with a value that is retrieved from the server. However, I would like to streamline the code and eliminate the need for the jQuery portion. This is the current setup: Javascript (code ...

What is the best way to download a modified canvas with filters using the solutions from stackoverflow?

There have been numerous inquiries regarding how to apply CSS filters to an image, with some solutions that don't utilize CSS filters but still achieve the desired outcome. However, for someone new to JavaScript like myself, these answers can be confu ...

The sudden disappearance of the vertical scrollbar in an absolutely positioned element

UPDATE : Added link to the example: http://jsfiddle.net/bfNzH/1/ This is how my HTML structure looks: <body> <div class="wrapper"> <div class="left">left bar</div> <div class="right"> < ...

Could somebody please clarify the purpose of Bootstrap's `_root.scss` file?

After reading through the Bootstrap introduction [1], I added the following code snippet to my .html file: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384- ...