Two columns with variable width and a fixed gap between them

前端 未结 3 735
北海茫月
北海茫月 2021-01-22 06:42

I need to style two columns dynamically. They should have a width of 50% each, but a fixed gap of 10px between them.

When I collapse my menu the columns should widen to

3条回答
  •  生来不讨喜
    2021-01-22 07:03

    I know this is a few months old, but I have another potential solution, which I will post here for posterity, in case it helps someone else:

    What I did was snug two columns right in next to each other, and then place the real columns inside of those columns, using a 5px margin on each, on the shared side, to create the illusion of a 10px gap.

    I have set up a demo similar to the original one Chris put up, only using this trick to create the gap between the columns.

    Here is a fiddle of the snippet below:

    $(document).ready(function() {
      $('#left').click(function() {
        $('#left').text('').animate({
          width: '0%'
        }, 'slow');
        $('#right').animate({
          width: '98%'
        }, 'slow');
      });
    });
    #container {
      height: 500px;
      width: 90%
    }
    #left,
    #right {
      border: 1px Red solid;
      height: 100%;
      width: 48%;
      float: left;
    }
    .halfWidthColumn {
      float: left;
      height: 100%;
      width: 50%;
      margin: 0px;
      padding: 0px;
    }
    .interiorColumn {
      border: 1px Black solid;
      background-color: White;
      height: 100%;
    }
    .leftSide {
      margin: 0 5px 0 0;
      background-color: Blue;
    }
    .rightSide {
      margin: 0 0 0 5px;
      background-color: Green;
    }
    
    
    Click to collapse

    Notice that I am no longer using JavaScript to resize the two interior columns. I believe this will do what was originally requested, if I am not mistaken.

提交回复
热议问题