Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

html - Flexbox width variation with content, should be fixed width

I'm making a mobile responsive page and I'm using flexbox for the first time.

The problem that I have is when there is a lot of information in the content div the div stretches very wide out of the page width.

I've tried using max-width styles and that takes away the responsiveness of the site.

JSFiddle

https://jsfiddle.net/z23rmo80/

CSS

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  text-align: center;
}
body .header {
  width: 100%;
  height: 182px;
  background: white;
}
body .holygrail-body {
  flex: 1 0 auto;
  display: flex;
  width:1000px;
  margin-right:auto;
  margin-left:auto;
}
body .holygrail-body .content {
  flex: 1 0 auto;
  background: lightgreen;
  overflow-y: auto;
}
body .holygrail-body .nav {
  width: 240px;
  list-style: none;
  text-align: left;
  order: -1;

  margin: 0;
}
body .holygrail-body .aside {
  width: 100px;
  background: orange;
}
body .footer {
  width: 100%;
  height: 60px;

}
.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #fff;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.sidenav a:hover {
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}
  .mobile-header{display: none;}
.hamburger{display: none;}
ul, ol {
    margin-top:10px;

}
.nav li {
    list-style: none outside none;
    line-height: 36px;
    margin-left:-15px;
}
.nav li a {
    color: #5f141f;
    text-decoration: none;
    text-shadow: 2px 1px #f6eaec;
    font-size: 18px;
    margin-left:-15px;
  }
  .sidenav li {
      list-style: none outside none;
      line-height: 36px;
      margin-left:-15px;
  }
  .sidenav li a {
      color: #5f141f;
      text-decoration: none;
      text-shadow: 2px 1px #f6eaec;
      font-size: 18px;
      margin-left:-15px;
    }
@media (max-width: 700px) {
  body .holygrail-body {
    flex-direction: column;
  }
  body .holygrail-body .nav, body .holygrail-body .aside {
    width: 100%;
  }
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
  .hamburger{display: inline;margin-left:-900px;}
  .nav{display: none;}
  .mobile-header{display: inline;}
    .header{display: none;}
}

HTML

  <header class="header">
    <img src="header.jpg" style="max-width:1848px; margin-right:auto;margin-left:auto;" alt="">
  </header>
  <header class="mobile-header">
    <img src="header.jpg" style="width:100%;max-width:1848px; margin-right:auto;margin-left:auto;" alt="">
  </header>
<div class="holygrail-body">
  <span class="hamburger" style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Menu</span>
  <div id="mySidenav" class="sidenav">
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">New Items</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
    </ul>
  </div>
  <div class="content">

  <h1>Test</h1>

  </div>

  <ul class="nav">
    <li><a href="#">Home</a></li>
    <li><a href="#">New Items</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>

  </ul>


</div>
<footer class="footer">?2017</footer>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

2 things you need to do:

  • Change the flex-shrink value in flex: 1 0 auto from 0 to 1 (flex: 1 1 auto) so the content element is allowed to shrink

  • As you have a very long word, you also need to add word-wrap: break-word to insert line breaks within words to prevent text from overflowing

Note, word-wrap has been renamed to overflow-wrap, though not all browsers support the new name, so if you to use it, I would keep the old as well for now.

Stack snippet

function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
}
      body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  text-align: center;
}
body .header {
  width: 100%;
  height: 182px;
  background: white;
}
body .holygrail-body {
  flex: 1 0 auto;
  display: flex;
  width:1000px;
  margin-right:auto;
  margin-left:auto;
}
body .holygrail-body .content {
  flex: 1 1 auto;                            /*  changed  */
  background: lightgreen;
  overflow-y: auto;
  word-wrap: break-word;                     /*  added  */
}
body .holygrail-body .nav {
  width: 240px;
  list-style: none;
  text-align: left;
  order: -1;

  margin: 0;
}
body .holygrail-body .aside {
  width: 100px;
  background: orange;
}
body .footer {
  width: 100%;
  height: 60px;

}
.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #fff;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.sidenav a:hover {
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}
  .mobile-header{display: none;}
.hamburger{display: none;}
ul, ol {
    margin-top:10px;

}
.nav li {
    list-style: none outside none;
    line-height: 36px;
    margin-left:-15px;
}
.nav li a {
    color: #5f141f;
    text-decoration: none;
    text-shadow: 2px 1px #f6eaec;
    font-size: 18px;
    margin-left:-15px;
  }
  .sidenav li {
      list-style: none outside none;
      line-height: 36px;
      margin-left:-15px;
  }
  .sidenav li a {
      color: #5f141f;
      text-decoration: none;
      text-shadow: 2px 1px #f6eaec;
      font-size: 18px;
      margin-left:-15px;
    }
@media (max-width: 700px) {
  body .holygrail-body {
    flex-direction: column;
  }
  body .holygrail-body .nav, body .holygrail-body .aside {
    width: 100%;
  }
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
  .hamburger{display: inline;margin-left:-900px;}
  .nav{display: none;}
  .mobile-header{display: inline;}
    .header{display: none;}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
  <header class="header">
    <img src="header.jpg" style="max-width:1848px; margin-right:auto;margin-left:auto;" alt="">
  </header>
  <header class="mobile-header">
    <img src="header.jpg" style="width:100%;max-width:1848px; margin-right:auto;margin-left:auto;" alt="">
  </header>
<div class="holygrail-body">
  <span class="hamburger" style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Menu</span>
  <div id="mySidenav" class="sidenav">
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">New Items</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
      <li><a href="#">Information Page</a></li>
    </ul>
  </div>
  <div class="content">

  <h1>Tessssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssst</h1>
        
  </div>

  <ul class="nav">
    <li><a href="#">Home</a></li>
    <li><a href="#">New Items</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>
    <li><a href="#">Information Page</a></li>

  </ul>


</div>
<footer class="footer">?2017</footer>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...