Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Flex CSS and simulate a rowspan 2 and colspan 2

Tags:

html

css

flexbox

I want to build a responsive layout with 2 rows using divs .

I try this jsbin: http://jsbin.com/jiyanayayi/edit?html,css,output

The first row will have three cells, the last one (cell 3) must have a rowspan = 2

The second row (cell4) having a colspan = 2 must be limited by the cell 3.

I tried the CSS below, but the rowspan atribute did not work.

How can I create this responsive layout format?

.row{
  display: flex;
}
.cell{
  flex: 1;
  background:#eee;
  border: 1px solid #444;
  padding: 15px;
}

The HTML:

<div class="table">
    <div class="row">
        <div class="cell">Cell 1 </div>
        <div class="cell">Cell 2 </div>
        <div class="cell rowspan2">Cell 3 </div>
    </div>
    <div class="row">
        <div class="cell colspan2">Cell 4</div>
    </div>
</div>
like image 288
Ângelo Rigo Avatar asked May 04 '16 22:05

Ângelo Rigo


People also ask

Can we use Colspan and Rowspan together?

Of course, you can mix colspan and rowspan to get a range of various tables. Example 4-13 demonstrates a mix of column and row spanning.

How do you do Colspan and Rowspan?

The rowspan and colspan are <td> tag attributes. These are used to specify the number of rows or columns a cell should span. The rowspan attribute is for rows as well as the colspan attribute is for columns. These attributes have numeric values, for example, colspan=3 will span three columns.

What does flex 1 do in CSS?

If an element has flex: 1 , this means the size of all of the other elements will have the same width as their content, but the element with flex: 1 will have the remaining full space given to it.

What is Flex row in CSS?

Items display in a row (the flex-direction property's default is row ). The items start from the start edge of the main axis. The items do not stretch on the main dimension, but can shrink. The items will stretch to fill the size of the cross axis. The flex-basis property is set to auto .


1 Answers

Edit 2021 use the grid CSS part stand inside the @supports querie, unless you still need to supports some exotic browsers or very old ones. If you are not sure, you can use the jsbin sample (also linked at the end of that answer) within the browser you want to supports. - jsbin should work and be live edited with old browsers, at least it does with IE11.

grid placement options are grid-area or grid-column and grid-row ( both links are about Grid Layout Module Level 2 from W3C ). The code below uses grid-area and you will find the grid-column and grid-row syntax commented.

Also This is not a table-layout and Question/Answers here are not about HTML table.

end edit


you need to use also flex and flex-wrap:

.table {
  display: flex;
  border: solid;
}

.row {
  flex: 1;
  display: flex;
}

.row:first-of-type {
  flex-flow: wrap;
}

.row .rowspan2 {
  flex: 1 1 100%;
}

.row div {
  border: solid;
  padding: 1em;
  flex: 1;
}

/* ============================================================== */
/* if display grid and contents is supported then you may use it  */
/* ============================================================== */
/*
@supports (display:grid) and (display:contents) {
  .table {
    display: grid;
    grid-template-columns: 25% 25% 25% 25%;
    grid-template-areas: "cell1 cell2 cell3 cell3" "cell4 cell4 cell3 cell3";
    border: solid;
  }
  .row {
    display: contents/* hide them in the structure. .row respective children become sibblings *//*
  }
  .row:first-child> :first-child {
    grid-area: cell1;
  }
  .row:first-child div {
    grid-area: cell2;
  }
  .row .cell.rowspan2 {
    grid-area: cell3;
    /*grid-row:span 2; no need if grid-template-area is complete*//*
  }
  div div {
    border: solid;
    padding: 1em;
  }
  .colspan2 {
    grid-area: cell4;
    /*grid-column : span 2; no need if grid-template-area is complete*//*
  }
}
*/
<div class="table">
  <div class="row">
    <div class="cell">Cell 1</div>
    <div class="cell">Cell 2</div>
    <div class="cell rowspan2">Cell 3</div>
  </div>
  <div class="row">
    <div class="cell colspan2">Cell 4</div>
  </div>
</div>

jsbin updated with @supports uncommented : https://jsbin.com/wexisiyamo/1/edit?html,css,output

like image 131
G-Cyrillus Avatar answered Sep 18 '22 12:09

G-Cyrillus