How to keep width of the main container to maximum width in a 3 column fluid layout?

StackOverflow https://stackoverflow.com/questions/20952255

  •  24-09-2022
  •  | 
  •  

Question

I have a 3 column fluid (100% width) layout. I need to hide columns on click, while the main container remains to maximum width.

Imagine that bellow is my layout:

-------------------------------------------------------
|           |                             |           |
| Col 1     |    Main container (Col 2)   |  Col 3    |
| (20%)     |        (60%)                |  (20%)    |
|(or 100px) | This is a paragraph in which|(or 100px) |
|           | some text is written.       |           |
-------------------------------------------------------

If I click on Col 1, I need to have bellow layout:

------------------------------------------------------
|                                         |           |
|      Main container  (Col 2)            |  Col 2    |
|         (80%)                           |  (20%)    |
| This is a paragraph in which some text  |(or 100px) |
| is written.                             |           |
------------------------------------------------------

Then if I click on Col 3, I need to have bellow layout:

------------------------------------------------------
|                                                    |
|                 Main container (Col 2)             |
|                    (100%)                          |
| This is a paragraph in which some text is written. |
|                                                    |
------------------------------------------------------

How can I write css styles in a way that the mainContainer expands to the maximum possible width?

Here is the code I have curenlty written (which is not what I exactly want) enter link description here

Was it helpful?

Solution 2

to display div inline....display:table would be helpful instead of float and also expands when there is no div around

working demo

html, body {
    width: 100%;
}
#columnContainer {
    display:table;  /* remove float and added this */
    width: 100%;
    border: 1px dotted black;
}
#left {
    display:table-cell; /* remove float and added this */
    min-width: 20%;
    max-width: 100%;/* helps you expanding the divs after others collapse*/
    display:table-cell;
    border: 1px solid blue;
}
#main {
    display:table-cell;  /* remove float and added this */
    min-width: 58%;
    max-width: 100%; /* helps you expanding the divs after others collapse*/
    border: 1px solid green;
}
#right {
    display:table-cell;   /* remove float and added this */
    min-width: 20%;
    max-width: 100%;/* helps you expanding the divs after others collapse*/
    border: 1px solid blue;
}

Make sure to give max-width...this will decide, how much a div should expand......

OTHER TIPS

You should really look at CSS3 Flexbox module.

Your HTML structure :

<div class="container">
   <div class="col1"></div>
   <div class="col2"></div>
   <div class="col3"></div>
</div>

Your CSS :

.col1, .col2, .col3, .container {
    display: flex;
    flex-wrap: nowrap;
}
.col1 {
    flex: 1 20%;
}
.col2 {
    flex: 3 60%;
}
.col3 {
    flex: 1 20%;
}

Full example running on JSFiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top