How to make a div fill a remaining horizontal space?

前端 未结 25 1440
野的像风
野的像风 2020-11-22 00:45

I have 2 divs: one in the left side and one in the right side of my page. The one in the left side has fixed width and I want the one of the right side to f

相关标签:
25条回答
  • 2020-11-22 01:38

    The problem that I found with Boushley's answer is that if the right column is longer than the left it will just wrap around the left and resume filling the whole space. This is not the behavior I was looking for. After searching through lots of 'solutions' I found a tutorial (now link is dead) on creating three column pages.

    The author offer's three different ways, one fixed width, one with three variable columns and one with fixed outer columns and a variable width middle. Much more elegant and effective than other examples I found. Significantly improved my understanding of CSS layout.

    Basically, in the simple case above, float the first column left and give it a fixed width. Then give the column on the right a left-margin that is a little wider than the first column. That's it. Done. Ala Boushley's code:

    Here's a demo in Stack Snippets & jsFiddle

    #left {
      float: left;
      width: 180px;
    }
    
    #right {
      margin-left: 180px;
    }
    
    /* just to highlight divs for example*/
    #left { background-color: pink; }
    #right { background-color: lightgreen;}
    <div id="left">  left  </div>
    <div id="right"> right </div>

    With Boushley's example the left column holds the other column to the right. As soon as the left column ends the right begins filling the whole space again. Here the right column simply aligns further into the page and the left column occupies it's big fat margin. No flow interactions needed.

    0 讨论(0)
  • 2020-11-22 01:39

    Here is a little fix for accepted solution, which prevents right column from falling under the left column. Replaced width: 100%; with overflow: hidden; a tricky solution, if somebody didn't know it.

    <html>
    
    <head>
        <title>This is My Page's Title</title>
        <style type="text/css">
            #left {
                float: left;
                width: 180px;
                background-color: #ff0000;
            }
            #right {
                overflow: hidden;
                background-color: #00FF00;
            }
        </style>
    </head>
    
    <body>
        <div>
            <div id="left">
                left
            </div>
            <div id="right">
    
    
    right
        </div>
    </div>
    

    http://jsfiddle.net/MHeqG/2600/

    [edit] Also check an example for three column layout: http://jsfiddle.net/MHeqG/3148/

    0 讨论(0)
  • 2020-11-22 01:39

    I wonder that no one used position: absolute with position: relative

    So, another solution would be:

    HTML

    <header>
      <div id="left"><input type="text"></div>
      <div id="right">Menu1 Menu2 Menu3</div>
    </header>
    

    CSS

    header { position: relative;  }
    #left {
        width: 160px;
        height: 25px;
    }
    #right {
        position: absolute;
        top: 0px;
        left: 160px;
        right: 0px;
        height: 25px;
    }
    

    Jsfiddle example

    0 讨论(0)
  • 2020-11-22 01:40

    I have been working on this problem for two days and have a solution that may work for you and anyone else trying to make a responsive Fixed width left and have the right side fill in the remainder of the screen without wrapping around the left side. The intention I assume is to make the page responsive in browsers as well as mobile devices.

    Here is the Code

    // Fix the width of the right side to cover the screen when resized
    $thePageRefreshed = true;
    // The delay time below is needed to insure that the resize happens after the window resize event fires
    // In addition the show() helps.  Without this delay the right div may go off screen when browser is refreshed 
    setTimeout(function(){
        fixRightSideWidth();
        $('.right_content_container').show(600);
    }, 50);
    
    // Capture the window resize event (only fires when you resize the browser).
    $( window ).resize(function() {
        fixRightSideWidth();
    });
    
    function fixRightSideWidth(){
        $blockWrap = 300; // Point at which you allow the right div to drop below the top div
        $normalRightResize = $( window ).width() - $('.left_navigator_items').width() - 20; // The -20 forces the right block to fall below the left
        if( ($normalRightResize >= $blockWrap) || $thePageRefreshed == true ){
            $('.right_content_container').width( $normalRightResize );
            $('.right_content_container').css("padding-left","0px");
            
            /* Begin test lines these can be deleted */
            $rightrightPosition = $('.right_content_container').css("right");
            $rightleftPosition = $('.right_content_container').css("left");
            $rightwidthPosition = $('.right_content_container').css("width");
            $(".top_title").html('window width: '+$( window ).width()+"&nbsp;"+'width: '+$rightwidthPosition+"&nbsp;"+'right: '+$rightrightPosition);
            /* End test lines these can be deleted */
        
        
        }
        else{
            if( $('.right_content_container').width() > 300 ){
                $('.right_content_container').width(300);
            }
            
            /* Begin test lines these can be deleted */
            $rightrightPosition = $('.right_content_container').css("right");
            $rightleftPosition = $('.right_content_container').css("left");
            $rightwidthPosition = $('.right_content_container').css("width");
            $(".top_title").html('window width: '+$( window ).width()+"&nbsp;"+'width: '+$rightwidthPosition+"&nbsp;"+'right: '+$rightrightPosition);
            /* End test lines these can be deleted */
        
        }
        if( $thePageRefreshed == true ){
            $thePageRefreshed = false;
        }
    }
    /* NOTE: The html and body settings are needed for full functionality
    and they are ignored by jsfiddle so create this exapmle on your web site
    */
    html {
        min-width: 310px;
        background: #333;
        min-height:100vh;
    }
    
    body{
    	background: #333;
    	background-color: #333;
    	color: white;
        min-height:100vh;
    }
    
    .top_title{
      background-color: blue;
      text-align: center;
    }
    
    .bottom_content{
    	border: 0px;
    	height: 100%;
    }
    
    .left_right_container * {
        position: relative;
        margin: 0px;
        padding: 0px;
        background: #333 !important;
        background-color: #333 !important;
        display:inline-block;
        text-shadow: none;
        text-transform: none;
        letter-spacing: normal;
        font-size: 14px;
        font-weight: 400;
        font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
        border-radius: 0;
        box-sizing: content-box;
        transition: none;
    }
    
    .left_navigator_item{
    	display:inline-block;
    	margin-right: 5px;
    	margin-bottom: 0px !important;
    	width: 100%;
    	min-height: 20px !important;
    	text-align:center !important;
    	margin: 0px;
    	padding-top: 3px;
    	padding-bottom: 3px;
    	vertical-align: top;
    }
    
    .left_navigator_items {
        float: left;
        width: 150px;
    }
    
    .right_content_container{
        float: right;
        overflow: visible!important;
        width:95%; /* width don't matter jqoery overwrites on refresh */
        display:none;
        right:0px;
    }
    
    .span_text{
    	background: #eee !important;
    	background-color: #eee !important;
    	color: black !important;
    	padding: 5px;
    	margin: 0px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <div class="top_title">Test Title</div>
    <div class="bottom_content">
        <div class="left_right_container">
            <div class="left_navigator_items">
                <div class="left_navigator_item">Dashboard</div>
                <div class="left_navigator_item">Calendar</div>
                <div class="left_navigator_item">Calendar Validator</div>
                <div class="left_navigator_item">Bulletin Board Slide Editor</div>
                <div class="left_navigator_item">Bulletin Board Slide Show (Live)</div>
                <div class="left_navigator_item">TV Guide</div>
            </div>
            <div class="right_content_container">
                <div class="span_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ullamcorper maximus tellus a commodo. Fusce posuere at nisi in venenatis. Sed posuere dui sapien, sit amet facilisis purus maximus sit amet. Proin luctus lectus nec rutrum accumsan. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut fermentum lectus consectetur sapien tempus molestie. Donec bibendum pulvinar purus, ac aliquet est commodo sit amet. Duis vel euismod mauris, eu congue ex. In vel arcu vel sem lobortis posuere. Cras in nisi nec urna blandit porta at et nunc. Morbi laoreet consectetur odio ultricies ullamcorper. Suspendisse potenti. Nulla facilisi.
    
    Quisque cursus lobortis molestie. Aliquam ut scelerisque leo. Integer sed sodales lectus, eget varius odio. Nullam nec dapibus lorem. Aenean a mattis velit, ut porta nunc. Phasellus aliquam volutpat molestie. Aliquam tristique purus neque, vitae interdum ante aliquam ut.
    
    Pellentesque quis finibus velit. Fusce ac pulvinar est, in placerat sem. Suspendisse nec nunc id nunc vestibulum hendrerit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Mauris id lectus dapibus, tempor nunc non, bibendum nisl. Proin euismod, erat nec aliquet mollis, erat metus convallis nulla, eu tincidunt eros erat a lectus. Vivamus sed mattis neque. In vitae pellentesque mauris. Ut aliquet auctor vulputate. Duis eleifend tincidunt gravida. Sed tincidunt blandit tempor.
    
    Duis pharetra, elit id aliquam placerat, nunc arcu interdum neque, ac luctus odio felis vitae magna. Curabitur commodo finibus suscipit. Maecenas ut risus eget nisl vehicula feugiat. Sed sed bibendum justo. Curabitur in laoreet dolor. Suspendisse eget ligula ac neque ullamcorper blandit. Phasellus sit amet ultricies tellus.
    
    In fringilla, augue sed fringilla accumsan, orci eros laoreet urna, vel aliquam ex nulla in eros. Quisque aliquet nisl et scelerisque vehicula. Curabitur facilisis, nisi non maximus facilisis, augue erat gravida nunc, in tempus massa diam id dolor. Suspendisse dapibus leo vel pretium ultrices. Sed finibus dolor est, sit amet pharetra quam dapibus fermentum. Ut nec risus pharetra, convallis nisl nec, tempor nisl. Vivamus sit amet quam quis dolor dapibus maximus. Suspendisse accumsan sagittis ligula, ut ultricies nisi feugiat pretium. Cras aliquam velit eu venenatis accumsan. Integer imperdiet, eros sit amet dignissim volutpat, tortor enim varius turpis, vel viverra ante mauris at felis. Mauris sed accumsan sapien. Interdum et malesuada fames ac ante ipsum primis in faucibus. Ut vel magna commodo, facilisis turpis eu, semper mi. Nulla massa risus, bibendum a magna molestie, gravida maximus nunc.</div>
            </div>
        </div>
    </div>

    Here is my fiddle that may just work for you as it did for me. https://jsfiddle.net/Larry_Robertson/62LLjapm/

    0 讨论(0)
  • 2020-11-22 01:41

    These days, you should use the flexbox method (may be adapted to all browsers with a browser prefix).

    .container {
        display: flex;
    }
    
    .left {
        width: 180px;
    }
    
    .right {
        flex-grow: 1;
    }
    

    More info: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

    0 讨论(0)
  • 2020-11-22 01:43

    Simplest solution is to just make the left div width equal 100% - the width of the right div plus any margin between them.

    <div class="cont">
      <div class="search">
        Big Logo Text
      </div>
      <nav>
        <ul class="navbar">
          <li><a href="#1">NavLink1</a></li>
          <li><a href="#2">NavLink2</a></li>
          <li><a href="#3">NavLink3</a></li>
          <li><a href="#4">NavLink4</a></li>
          <li><a href="#5">NavLink5</a></li>
        </ul>
      </nav>
    </div>
    
    .cont{
      display: inline-grid;
      grid-template-columns: 160px 10px calc(100% - 170px);
      grid-template-rows: auto;
      grid-template-areas: "search .  navigation";
      width: 100%;
      height: auto;
      text-align: center;
    }
    
    .search {
      grid-area: search;
      height: 90px;
      background-color: #00FF00;
      line-height: 80px;
      font-size: 1.4rem;
      font-weight: 600;
    }
    nav {
      grid-area: navigation;
      height: 90px;
      background-color: #A53030;
    }
    
    .navbar{
      display: flex;
      height: 30px;
      width: 100%;
      padding: 0%;
      list-style-type: none;
      flex-flow: row wrap;
      flex: 0 1 auto;
      justify-content: space-between;
      align-content: flex-start;
      align-items: flex-start;
    }
    
    .navbar a{
        outline: 0;
        text-decoration: none;
    }
    

    https://codepen.io/tamjk/pen/dybqKBN

    0 讨论(0)
提交回复
热议问题