Juan Vargas
Juan Vargas

Reputation: 51

Three columns responsive

I can not use bootstrap for this task. I use reactjs with radium (Styles like react-native), I tried to do with flexbox but I'm able to use CSS and media queries.

I have a 3 column layout. When accessing it from the desktop, it shows like this:

--------------------------
|  column 1 |            |
------------|  column 2  |
|  column 3 |            |
--------------------------

The column 1 and 3 height will be the same as column 2.

I want it to be like this when view it from mobile/tablet / resize browser:

-------------
|  column 1 |
-------------
|  column 2 |
-------------
|  column 3 |
-------------

The column 2 needs to move between the column 1 and 3, the height for each column does not matter in this configuration.

My sample below, I use radium but I can use CSS and Media Queries.

columnsContainer: {
    display: "flex",
    flexDirection: "row",
    @media (min-width:"xxx"px): {
        flexDirection: "column"
    }
},
rightColumn: {
    width: "392px",
    @media (min-width:"xxx"px): {
        marginTop: "32px",
        width: "100%"
    }
},
leftColumn: {
    flex: "1",
    @media (min-width:"xxx"px): {
        marginRight: "0px",
        marginLeft: "0px"
    }
},
bottomColumn: {
    flex: "1",
    @media (min-width:"xxx"px): {
        marginRight: "0px",
        marginLeft: "0px"
    },
}

And below my html:

<div style={styles.columnsContainer}>
    <div style={styles.leftColumn}>
        Column 1
    </div>
    <div style={styles.rightColumn}>
        Column 2
    </div>
    <div style={styles.bottomColumn}>
        Column 3
    </div>
</div>

Upvotes: 3

Views: 408

Answers (2)

Juan Vargas
Juan Vargas

Reputation: 51

Well, I find the answer in CSS Grid:

columnsContainer {
    display: "grid",
    gridTemplateColumns: "1fr auto",
    @media (min-width:"xxx"px): {
        gridTemplateColumns: "auto"
    }
},
rightColumn: {
    gridColumn: "2",
    gridRow: "1 / 3",
    @media (min-width:"xxx"px): {
        gridColumn: "1 / 2",
        gridRow: "2",
        width: "auto"
    }
},
leftColumn: {
    @media (min-width:"xxx"px): {
        gridRow: "1",
    }
},
bottomColumn: {
    @media (min-width:"xxx"px):: {
        gridRow: "3",
    }
}

and the HTML :

<div style={styles.columnsContainer}>
    <div style={styles.leftColumn}>
        Column 1
    </div>
    <div style={styles.rightColumn}>
        Column 2
    </div>
    <div style={styles.bottomColumn}>
        Column 3
    </div>
</div>

Thank's a lot for the help!

Upvotes: 0

Dhaval Jardosh
Dhaval Jardosh

Reputation: 7299

As you are able to use Flexbox, here is an example you can try.

Used order to place the columns as per requirement.

.outer {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

.one {
  background: red;
  height: 200px;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
}

.two {
  background: blue;
  height: 200px;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
}

.three {
  background: green;
  height: 200px;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
}

@media only screen and (max-width: 600px) {
  .outer {
    max-height: 400px;
  }
  .one {
    width: 50%;
    order: 1;
  }
  .two {
    width: 50%;
    order: 3;
    height: 400px;
  }
  .three {
    width: 50%;
    order: 2;
  }
}
<div class="outer">
  <div class="one">1</div>
  <div class="two">2</div>
  <div class="three">3</div>
</div>

Upvotes: 4

Related Questions