Set auto height and width in CSS/HTML for different screen sizes

后端 未结 3 1784
抹茶落季
抹茶落季 2021-02-04 21:18

I have 2 issues with this layout :

  1. .feature_content (grey background) adapt it\'s height and width to different screen sizes
相关标签:
3条回答
  • 2021-02-04 21:40

    This is what do you want? DEMO. Try to shrink the browser's window and you'll see that the elements will be ordered.

    What I used? Flexible Box Model or Flexbox.

    Just add the follow CSS classes to your container element (in this case div#container):

    flex-init-setup and flex-ppal-setup.

    Where:

    1. flex-init-setup means flexbox init setup; and
    2. flex-ppal-setup means flexbox principal setup

    Here are the CSS rules:

     .flex-init-setup {
         display: -webkit-box;
         display: -moz-box;
         display: -webkit-flex;
         display: -ms-flexbox;
         display: flex;
     }
     .flex-ppal-setup {
         -webkit-flex-flow: column wrap;
         -moz-flex-flow: column wrap;
         flex-flow: column wrap;
         -webkit-justify-content: center;
         -moz-justify-content: center;
         justify-content: center;
     }
    

    Be good, Leonardo

    0 讨论(0)
  • 2021-02-04 21:40

    ///UPDATED DEMO 2 WATCH SOLUTION////

    I hope that is the solution you're looking for! DEMO1 DEMO2

    With that solution the only scrollbar in the page is on your contents section in the middle! In that section build your structure with a sidebar or whatever you want!

    You can do that with that code here:

    <div class="navTop">
    <h1>Title</h1>
        <nav>Dynamic menu</nav>
    </div>
    <div class="container">
        <section>THE CONTENTS GOES HERE</section>
    </div>
    <footer class="bottomFooter">
        Footer
    </footer>
    

    With that css:

    .navTop{
    width:100%;
    border:1px solid black;
    float:left;
    }
    .container{
    width:100%;
    float:left;
    overflow:scroll;
    }
    .bottomFooter{
    float:left;
    border:1px solid black;
    width:100%;
    }
    

    And a bit of jquery:

    $(document).ready(function() {
      function setHeight() {
        var top = $('.navTop').outerHeight();
        var bottom = $('footer').outerHeight();
        var totHeight = $(window).height();
        $('section').css({ 
          'height': totHeight - top - bottom + 'px'
        });
      }
    
      $(window).on('resize', function() { setHeight(); });
      setHeight();
    });
    

    DEMO 1

    If you don't want jquery

    <div class="row">
        <h1>Title</h1>
        <nav>NAV</nav>
    </div>
    
    <div class="row container">
        <div class="content">
            <div class="sidebar">
                SIDEBAR
            </div>
            <div class="contents">
                CONTENTS
            </div>
        </div>
        <footer>Footer</footer>
    </div>
    

    CSS

    *{
    margin:0;padding:0;    
    }
    html,body{
    height:100%;
    width:100%;
    }
    body{
    display:table;
    }
    .row{
    width: 100%;
    background: yellow;
    display:table-row;
    }
    .container{
    background: pink;
    height:100%; 
    }
    .content {
    display: block;
    overflow:auto;
    height:100%;
    padding-bottom: 40px;
    box-sizing: border-box;
    }
    footer{ 
    position: fixed; 
    bottom: 0; 
    left: 0; 
    background: yellow;
    height: 40px;
    line-height: 40px;
    width: 100%;
    text-align: center;
    }
    .sidebar{
    float:left;
    background:green;
    height:100%;
    width:10%;
    }
    .contents{
    float:left;
    background:red;
    height:100%;
    width:90%;
    overflow:auto;
    }
    

    DEMO 2

    0 讨论(0)
  • 2021-02-04 21:43

    Using bootstrap with a little bit of customization, the following seems to work for me:

    I need 3 partitions in my container and I tried this:

    CSS:

    .row.content {height: 100%; width:100%; position: fixed; }
    .sidenav {
      padding-top: 20px;
      border: 1px solid #cecece;
      height: 100%;
    }
    .midnav {
      padding: 0px;
    }
    

    HTML:

      <div class="container-fluid text-center"> 
        <div class="row content">
        <div class="col-md-2 sidenav text-left">Some content 1</div>
        <div class="col-md-9 midnav text-left">Some content 2</div>
        <div class="col-md-1 sidenav text-center">Some content 3</div>
        </div>
      </div>
    
    0 讨论(0)
提交回复
热议问题