What is the best method for resetting a flexbox item?

I'm trying to achieve a layout with flex box where the first item spans 100% width, the second item is centered at 50% on its own line, and the third and fourth items each take up 50% width on the same line.

Usually, clearing works fine on simple block items but I am facing challenges implementing it with flex layout.

Check out the code here

.container {
                          padding: 0;
                          margin: 0;
                          list-style: none;
                          display: -webkit-box;
                          display: -moz-box;
                          display: -ms-flexbox;
                          display: -webkit-flex;
                          display: flex;
                          -webkit-flex-flow: row wrap;
                          justify-content: space-around;
                        }

                        .item {
                          background: tomato;
                          padding: 5px;
                          width: 100%;
                          height: 150px;
                          margin-top: 10px;
                          margin-bottom: 10px;
                          line-height: 150px;
                          color: white;
                          font-weight: bold;
                          font-size: 3em;
                          text-align: center;
                        }

                        .container.block {
                          display: block;
                        }

                        .container.block .item {
                          float: left;
                          box-sizing: border-box;
                        }

                        .half {
                          width: 50%;
                        }

                        .container .item.centered {
                          float: none;
                          clear: both;
                          margin-left: auto;
                          margin-right: auto;
                        }
               
            

                     <ul class="container">
                      <li class="item">1</li>
                      <li class="item half centered">2 Clear after me</li>
                      <li class="item half">3</li>
                      <li class="item half">4</li>
                    </ul>
                    <br /><br /><br /><br /><br />
                    <ul class="container block">
                      <li class="item">1</li>
                      <li class="item half centered">2 Clear after me</li>
                      <li class="item half">3</li>
                      <li class="item half">4</li>
                    </ul>
                 
            

Answer №1

To handle floats differently with flex items, you can utilize the following method: set box-sizing: border-box on all items to include padding in the width calculation. Then, apply a small margin to the second item, forcing the remaining items onto a new line.

.container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
}

.item {
  background: tomato;
  padding: 5px;
  width: 100%;
  height: 150px;
  margin-top: 10px;
  margin-bottom: 10px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
  box-sizing: border-box;
}

.half {
  width: 50%;
}
.container .item.centered {
  margin: 0 10px;
}
<ul class="container">
  <li class="item">1</li>
  <li class="item half centered">2 Clear after me</li>
  <li class="item half">3</li>
  <li class="item half">4</li>
</ul>

Answer №2

I am hopeful that this solution will be effective for you. Personally, I have implemented this code and found success.

    .clear {
      width: 100%;
      border: none;
      margin: 0;
      padding: 0;
    }

<ul class="container">
  <li class="item">1</li>
  <li class="item half">2 Clear after me</li>
  <li class='clear'><li>
  <li class="item half">3</li>
  <li class="item half">4</li>
</ul>

This is a common practice in flow layout design.

.clear {
  border: 0;
  clear: both;
  height: 0;
}

Answer №3

To create a flexible layout, it is important to set proper widths and use the properties flex-wrap: wrap and box-sizing: border-box.

The first element will take up 100% of the width, pushing the following elements onto a new line.

The second, third, and fourth elements will each have a width of 50%.

Since each element also has horizontal padding, the second element will push the third and fourth elements onto a new line.

To ensure that all elements fit on one line, we can change the box model of the third and fourth elements from box-sizing: content-box to border-box, including padding in the width calculation.

.container {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-around;
  padding: 0;
  margin: 0;
  list-style: none;
}

li:nth-child(1) { flex: 0 0 100%;} /* flex-grow, flex-shrink, flex-basis */
li:nth-child(2) { flex: 0 0 50%; }
li:nth-child(3) { flex: 0 0 50%; box-sizing: border-box; }
li:nth-child(4) { flex: 0 0 50%; box-sizing: border-box; }

.item {
  background: tomato;
  padding: 5px;
  height: 150px;
  margin-top: 10px;
  margin-bottom: 10px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
}
<ul class="container">
  <li class="item">1</li>
  <li class="item half centered">2 Clear after me</li>
  <li class="item half">3</li>
  <li class="item half">4</li>
</ul>

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

Ways to conceal a division within the Repeater

Currently, I am developing a VB ASP.NET site using Visual Studio 2012 Express for Web. Within my project, there is a Repeater containing two div elements with the css classes .dnnFormLabel and .dnnFormItem. Here is a snippet of code from the Repeater: < ...

Seeking assistance in comprehending the method for styling table data using inline CSS in a JavaScript file

I am currently using a JavaScript file that was created for me to update form data based on user selections and display radio buttons along with the corresponding costs. Although the inline CSS in this script works perfectly fine in Chrome and Firefox, it ...

JavaScript strangeness

I am currently working on a dynamic page loaded with ajax. Here is the code that the ' $.get' jQuery function calls (located in an external HTML page): <script type="text/javascript"> $(function() { $('button').sb_animateBut ...

I would like to retrieve my data using my personal API and have them displayed as checkboxes

https://i.sstatic.net/qPSqe.jpgHere is an excerpt of the progress I have made main.html (it's actually a form) <div class="form-group form-check-inline"> <input class="form-check-input" type="radio" name=& ...

top margin is functioning properly in Internet Explorer, but not in Google Chrome

margin-top is behaving differently in IE compared to Google Chrome. Two menus are supposed to be displayed one above the other in my design. The issue lies in the line margin-top:30%; within .anothermenu ul. In Chrome, the second menu appears above the f ...

Is it possible to adjust the height of input fields in MUI Reactjs?

Having trouble adjusting the height of input textfields using in-line css. <FormControl sx={{ "& .MuiOutlinedInput-notchedOutline": { borderColor: "blue", }, "&.Mui-focused .MuiOutlinedInpu ...

changing html numbered list to xml format

Looking to convert HTML ordered lists with different types into XML markup? <ol type=a> <li>This is list item a</li> <li>this is list item b</li> </ol> <ol type=i> <li>This is list item 1</ ...

jQuery Form: Despite the Ajax response being visible on the page, it does not appear in the source code

Utilizing Jquery Form for an Ajax image upload. This is the relevant HTML code: <div class="input_con imageupload_con"> <form action="processupload.php" method="post" enctype="multipart/form-data" id="MyUploadForm"> < ...

What is the best way to incorporate both the left and right menus with the main content

My goal is to incorporate a left and right menu alongside a main feed in the center. However, I'm encountering an issue where the left and right menus are scrolling with the scroll bar. Ideally, I would like the main feed to scroll with the bar while ...

Tips for designing a table with a stationary first column in HTML/CSS

I am looking to design a table that can be horizontally scrolled with a dynamic number of columns. The goal is to keep the first column fixed/frozen while scrolling horizontally. I attempted to achieve this using the following CSS, which successfully keeps ...

The script remains static and does not alter the "href" or "div" content based on the current URL

I am having an issue with my NavMenu on my website. It is a table with images that link to product pages, and I want to change the links and names based on the language of the site. However, the script I wrote for this is not working properly. I have tried ...

Remain concealed once the lights dim

Looking for a way to fade out a logo in a preloader div after a call, but having issues with it becoming visible again once fully faded out. Ideally, I would like to keep it faded out or set it to display none using only CSS, although I can use jQuery if n ...

difficulties arise when adjusting font size in html and css

When I first created an all HTML/CSS website, I used clickable images as a menu that scaled perfectly on all monitors and browsers. Now, for my new project, I wanted to use a background image for the menu with hyperlinked text on top. I thought setting the ...

What is the technique for choosing the parent's ID with jQuery?

When a user clicks on any team, I need to retrieve the parent's parent ID. For example, if someone clicks on a Premier League Team, I want to store the ID 2 in a variable called league_id. Here are my attempts so far: Using Closest Method $('u ...

Balanced Height Panels - Utilizing Flexbox and flex-wrap: wrap

i'm having trouble with a layout design, specifically with getting the columns to be equal height, especially when the text wraps around. Can anyone provide some guidance? https://i.stack.imgur.com/bbDKj.png Within my HTML template, there are multipl ...

SCRIPT5007: The property 'href' cannot be assigned a value as the object is either null or undefined

This script is functioning properly in browsers like Chrome and Firefox, however it is encountering issues when used with Internet Explorer. An error message is displayed in the IE console: SCRIPT5007: Unable to set value of the property 'href': ...

Unable to retrieve table header information when clicking on a table cell

My code is working perfectly, except for the fact that when I click on a cell, I cannot retrieve the table header text. Retrieving the row text works fine based on the code. If I use Object.keys(data) inside the alert function to retrieve the data, it give ...

PHP code for uploading multiple images at once

I am currently facing an issue with uploading multiple files at once and not getting the desired result. Below is my code snippet: <?php if (isset($_FILES['uploadfiles'])) { print_r($_FILES); } ?> <form action="index.php" method="po ...

Set the height of the last child element to 100% in the CSS

Seeking assistance to adjust the height of the final list item to 100% in order to match the varying height of the right-floated div. ul.menu li:last-child {height:100%} Here is an example of my current situation: http://jsfiddle.net/kvtV8/1/ Any help ...

Consistent CSS alignment across all browsers

I have designed a four-digit counter that needs to be displayed in the bottom right corner of the page. Each digit is represented by a block image as a background. It is functioning properly in Chrome, but there are compatibility issues with IE7+ and Firef ...