Zinebs Zinebs - 2 months ago 12
CSS Question

Table structure without table tags, only divs

I have a table structure with only divs and float left. I want it to be responsive (I know I have to add media queries). There is something wrong in my code. For now I want to reduce the window it changes to horizontal scroll which I tried and it doesn't work.
image shows the structure i want

Here is my code:



<div style="">
<div style="border: 2px solid green;height:100px;width:100%;">
<div style="width:6rem;background:aqua;top: 4.2rem;position:absolute;">FM</div>
<div style="width:6rem;float:left;background:aqua;position: relative;top:2.4rem;">TNT</div>
<div style="text-align:center;float:left;border:1px solid;">
<div style="background:red;">Alarmes</div>
<div style="width:100%;">
<div style="float: left;width:6rem;background:pink;">Criticité</div>
<div style="float: left;width:6rem;background:pink;">Sans Sup</div>
<div style="float: left;width:6rem;background:pink;">Non nominale</div>
</div>
<div>

<div style="float: left;width:6rem;background:gold;">(chart)</div>
<div style="float: left;width:6rem;background:gold;">2</div>
<div style="float: left;width:6rem;background:gold;">5</div>
</div>
<div>

<div style="float: left;width:6rem;background:gold;">(chart)</div>
<div style="float: left;width:6rem;background:gold;">10</div>
<div style="float: left;width:6rem;background:gold;">1</div>
</div>
</div>

<div style="text-align:center;float:left;border:1px solid;">
<div style="background:red;">TICKETS</div>
<div>
<div style="float: left;width:6rem;background:pink;">Non nominale</div>
<div style="float: left;width:6rem;background:pink;">A suivre</div>
<div style="float: left;width:6rem;background:pink;">Clôs</div>
</div>
<div>
<div style="float: left;width:6rem;background:gold;">5</div>
<div style="float: left;width:6rem;background:gold;">-</div>
<div style="float: left;width:6rem;background:gold;">2</div>
</div>
<div>
<div style="float: left;width:6rem;background:gold;">9</div>
<div style="float: left;width:6rem;background:gold;">-</div>
<div style="float: left;width:6rem;background:gold;">0</div>
</div>
</div>

<div style="text-align:center;float:left;border:1px solid;">
<div style="background:red;">SITES</div>
<div>
<div style="float: left;width:6rem;background:pink;">Perte HF</div>
<div style="float: left;width:6rem;background:pink;">-3DB</div>
<div style="float: left;width:6rem;background:pink;">Décro HS</div>
<div style="float: left;width:6rem;background:pink;">Alarme RX</div>
<div style="float: left;width:6rem;background:pink;">Alarme RX</div>
<div style="float: left;width:6rem;background:pink;">GE</div>
</div>
<div>
<div style="float: left;width:6rem;background:gold;">-</div>
<div style="float: left;width:6rem;background:gold;">8</div>
<div style="float: left;width:6rem;background:gold;">3</div>
<div style="float: left;width:6rem;background:gold;">7</div>
<div style="float: left;width:6rem;background:gold;">1</div>
<div style="float: left;width:6rem;background:gold;">6</div>
</div>
<div>
<div style="float: left;width:6rem;background:gold;">-</div>
<div style="float: left;width:6rem;background:gold;">5</div>
<div style="float: left;width:6rem;background:gold;">4</div>
<div style="float: left;width:6rem;background:gold;">8</div>
<div style="float: left;width:6rem;background:gold;">0</div>
<div style="float: left;width:6rem;background:gold;">2</div>
</div>
</div>
</div>
</div>




Answer Source

Step 1. Remove all the styles from the divs

Step 2 use inline-block instead of float.

Step 3 make each table-row enclosed in its own div with display: block except the ones you want alongside each other, which should be inline-block

Step 4 add a width to the parent div and overflow-x: auto;

Step 5 add a non-wrapping parent div around the two sections you want side-by-side, and put each of those in its own div

(if you want to only have the scrolling on the sites section, put that inside the div with class container)

HTML

    <div class="container">
  <div class="no-wrap">
  <div class="side-by-side">
    <div class="row">
      <div>&nbsp;</div>
    </div>
    <div class="row">
      <div>&nbsp;</div>
    </div>
    <div class="row">
        <div class="aqua">FM</div>
    </div>
    <div class="row">
        <div class="aqua">TNT</div>
    </div>
  </div><!--/side-by-side-->
  <div class="side-by-side">
  <div class="row">
        <div class="red">Alarmes</div>
  </div>
  <div class="row">
          <div class="pink">Criticité</div>
          <div class="pink">Sans Sup</div>
          <div class="pink">Non nominale</div>
        </div>
        <div class="row">
          <div class="gold">(chart)</div>
          <div class="gold">2</div>
          <div class="gold">5</div>
        </div>
        <div class="row">
          <div class="gold">(chart)</div>
          <div class="gold">10</div>
          <div class="gold">1</div>
        </div>

 </div>

      <div class="side-by-side">
      <div class="row">
        <div class="red">TICKETS</div>
      </div>
        <div class="row">
          <div class="pink">Non nominale</div>
          <div class="pink">A suivre</div>
          <div class="pink">Clôs</div>
        </div>
        <div class="row">
          <div class="gold">5</div>
          <div class="gold">-</div>
          <div class="gold">2</div>
        </div>
        <div class="row">
          <div class="gold">9</div>
          <div class="gold">-</div>
          <div class="gold">0</div>
        </div>
</div>

      <div class="side-by-side">
      <div class="row">
        <div class="double-red">SITES</div>
      </div>

      <div class="no-wrap">
        <div class="side-by-side">
        <div class="row">
          <div class="pink">Perte HF</div>
          <div class="pink">-3DB</div>
          <div class="pink">Décro HS</div>
        </div>
        <div class="row">
          <div class="gold">-</div>
          <div class="gold">8</div>
          <div class="gold">3</div>
        </div>
         <div class="row">
          <div class="gold">-</div>
          <div class="gold">5</div>
          <div class="gold">4</div>
        </div>
  </div>
        <div class="side-by-side">
          <div class="row">
            <div class="pink">Alarme RX</div>
            <div class="pink">Alarme RX</div>
            <div class="pink">GE</div>
          </div>
          <div class="row">
            <div class="gold">7</div>
            <div class="gold">1</div>
            <div class="gold">6</div>
          </div>
          <div class="row">
            <div class="gold">8</div>
            <div class="gold">0</div>
            <div class="gold">2</div>
        </div>
      </div>
  </div>
    </div>
</div>
</div>
 </div><!--/container-->

CSS

.pink {
   display: inline-block;
   width:6rem;
   background:pink;
}

.gold {
   display: inline-block;
   width:6rem;
   background:gold;
}
.aqua {
    width:6rem;
    display: inline-block;
    background:aqua;
}

.red {
  background-color: red;
  color: #fff;
  width: 18.5rem;
}

.double-red {
  background-color: red;
  color: #fff;
  width: 37.25rem;  
}

.container {
  width: 18.5rem;
  overflow: auto;
}

.row {
  display: block;
}

.side-by-side {
  display: inline-block;
}

.no-wrap {
  width: 37.25rem;
  white-space: nowrap;
}

CodePen here: https://codepen.io/vogelbeere/pen/awxQMW