如何使旁边的列随着并行列中内容的大小而增长

时间:2019-03-18 13:05:40

标签: html css css-float css-tables

嗨,我如何使侧面菜单随并行容器的内容一起增长,该内容也应随其内容一起扩展。

我试图确保aside标记中包含的左列与div.right_panel中包含的右侧内容的大小相同。通过对stackoverflow进行类似的查询,我可以通过将htmlbody标记以及.wrapper类的大小设置为height: 100%来实现这一点。

但是,当我向main添加更多内容时,.wrapper并没有扩展。我试图将height更改为min-height,但将其缩小到其内容大小。

我知道我可以使用flex布局来完成此操作,但是因为flex不在我的课程范围内,并且因为我想学习替代方法,以防将来将来需要使用它们以与较旧的浏览器兼容,寻找不使用弹性版式的解决方案。

下面的代码段和JSFiddle link

html,
body {
    height: 100%;
}

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    background-color: blue;
}

h1,
h2,
h3,
h4,
h5,
h6 {
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}

.wrapper {
    margin: 10px auto 30px;
    padding: 0;
    width: 80%;
    background-color: white;
    height: calc(100% - 40px);
    max-height: 100%;
}

aside.left_panel {
    float: left;
    min-height: 100%;
    width: 130px;
    background-color: #9eb9f1;
    padding-left: 30px;
    overflow: auto;
}

.right_panel {
    margin-left: 160px;
}

nav ul {
    list-style-type: none;
    padding: 0;
}

header {
    background-color: #6f90d1;
    padding: 20px;
}

header h1 {

    font-size: 60px;
    color: darkblue;
}

main {
    padding: 20px 20px 0;
}

main h2 {
    color: #6f90d1;
}

main #lantern {
    height: 400px;
    width: 200px;
}

main img {
    margin: 10px;
}

main h2{
    margin-bottom: 30px;
}
main p {
    margin-bottom: 30px;
}

footer {
    text-align: center;
    margin: 10px 0;
}

.f-right {
    float: right;
    overflow: auto;
}

.f-left {
    float: left;
    overflow: auto;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="styles/styles.css">
</head>

<body>
    <div class="wrapper">
        <aside class="left_panel">
            <nav>
                <ul>
                    <li>Home</li>
                    <li>Manu item 1</li>
                    <li>Manu item 2</li>
                    <li>Manu item 3</li>
                </ul>
            </nav>
        </aside>
        <div class="right_panel">
            <header>
                <h1>Name of the website</h1>
            </header>
            <main>
                <img id="lantern" class="f-right" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" alt="">
                <h2>Subheading 1</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
                <h2>Subheading 2</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. </p>
                <h2>Subheading 3</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
                <h2>Subheading 4</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius.</p>
                
            </main>
            <footer>
                <p>Copyright &copy; 2019</p>
            </footer>
        </div>
    </div>
</body>

</html>

2 个答案:

答案 0 :(得分:2)

使用表格布局

一种解决方案是在display: table上使用wrapper使用表布局,并将left-sectionright-section变成{{1 }}-参见下面的演示:

table-cell
html,body {
  height: 100%;
}
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  background-color: blue;
}
h1,h2,h3,h4,h5,h6 {
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
.wrapper {
  margin: 10px auto 30px;
  padding: 0;
  width: 80%;
  background-color: white;
  height: calc(100% - 40px);
  max-height: 100%;
  display: table; /* added */
}
aside.left_panel {
  /* float: left; */
  min-height: 100%;
  width: 130px;
  background-color: #9eb9f1;
  padding-left: 30px;
  overflow: auto;
  display: table-cell; /* added */
  vertical-align: top; /* added */
}
.right_panel {
  /*margin-left: 160px;*/
  display: table-cell; /* added */
}
nav ul {
  list-style-type: none;
  padding: 0;
}
header {
  background-color: #6f90d1;
  padding: 20px;
}
header h1 {
  font-size: 60px;
  color: darkblue;
}
main {
  padding: 20px 20px 0;
}
main h2 {
  color: #6f90d1;
}
main #lantern {
  height: 400px;
  width: 200px;
}
main img {
  margin: 10px;
}
main h2 {
  margin-bottom: 30px;
}
main p {
  margin-bottom: 30px;
}
footer {
  text-align: center;
  margin: 10px 0;
}
.f-right {
  float: right;
  overflow: auto;
}
.f-left {
  float: left;
  overflow: auto;
}

使用浮动

您可以尝试执行此操作-但您没有正确设置溢出,或者无法使列的高度相同-请参见示例以解决问题:

<div class="wrapper">
  <aside class="left_panel">
    <nav>
      <ul>
        <li>Home</li>
        <li>Manu item 1</li>
        <li>Manu item 2</li>
        <li>Manu item 3</li>
      </ul>
    </nav>
  </aside>
  <div class="right_panel">
    <header>
      <h1>Name of the website</h1>
    </header>
    <main>
      <img id="lantern" class="f-right" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" alt="">
      <h2>Subheading 1</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
      <h2>Subheading 2</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. </p>
      <h2>Subheading 3</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
      <h2>Subheading 4</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius.</p>

    </main>
    <footer>
      <p>Copyright &copy; 2019</p>
    </footer>
  </div>
</div>
html,body {
  height: 100%;
}
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  background-color: blue;
}
h1,h2,h3,h4,h5,h6 {
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
.wrapper {
  margin: 10px auto 30px;
  padding: 0;
  width: 80%;
  background-color: white;
  height: calc(100% - 40px);
  max-height: 100%;
  overflow: hidden; /* to clear the float */
}
aside.left_panel {
  float: left;
  min-height: 100%;
  width: 130px;
  background-color: #9eb9f1;
  padding-left: 30px;
  overflow-y: auto; /*changed to overflow-y */
}
.right_panel {
  float: left; /* added */
  width: calc(100% - 160px); /* added */
  /*margin-left: 160px;*/
}
nav ul {
  list-style-type: none;
  padding: 0;
}
header {
  background-color: #6f90d1;
  padding: 20px;
}
header h1 {
  font-size: 60px;
  color: darkblue;
}
main {
  padding: 20px 20px 0;
}
main h2 {
  color: #6f90d1;
}
main #lantern {
  height: 400px;
  width: 200px;
}
main img {
  margin: 10px;
}
main h2 {
  margin-bottom: 30px;
}
main p {
  margin-bottom: 30px;
}
footer {
  text-align: center;
  margin: 10px 0;
}
.f-right {
  float: right;
  overflow: auto;
}
.f-left {
  float: left;
  overflow: auto;
}

但是有一个 hack -使用 large <div class="wrapper"> <aside class="left_panel"> <nav> <ul> <li>Home</li> <li>Manu item 1</li> <li>Manu item 2</li> <li>Manu item 3</li> </ul> </nav> </aside> <div class="right_panel"> <header> <h1>Name of the website</h1> </header> <main> <img id="lantern" class="f-right" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" alt=""> <h2>Subheading 1</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p> <h2>Subheading 2</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. </p> <h2>Subheading 3</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p> <h2>Subheading 4</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius.</p> </main> <footer> <p>Copyright &copy; 2019</p> </footer> </div> </div>margin。请注意,您需要删除在padding上设置的heightmax-height-请参见下面的演示:

wrapper
html,body {
  height: 100%;
}
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  background-color: blue;
}
h1,h2,h3,h4,h5,h6 {
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
.wrapper {
  margin: 10px auto 30px;
  padding: 0;
  width: 80%;
  background-color: white;
  /*height: calc(100% - 40px);
  max-height: 100%;*/
  overflow: hidden; /* to clear the float */
}
aside.left_panel {
  float: left;
  min-height: 100%;
  width: 130px;
  background-color: #9eb9f1;
  padding-left: 30px;
  overflow-y: auto; /*changed to overflow-y */
  margin-bottom: -100000px; /* a large value */
  padding-bottom: 100000px; /* a large value */
}
.right_panel {
  float: left; /* added */
  width: calc(100% - 160px); /* added */
  /*margin-left: 160px;*/
  margin-bottom: -100000px; /* a large value */
  padding-bottom: 100000px; /* a large value */
}
nav ul {
  list-style-type: none;
  padding: 0;
}
header {
  background-color: #6f90d1;
  padding: 20px;
}
header h1 {
  font-size: 60px;
  color: darkblue;
}
main {
  padding: 20px 20px 0;
}
main h2 {
  color: #6f90d1;
}
main #lantern {
  height: 400px;
  width: 200px;
}
main img {
  margin: 10px;
}
main h2 {
  margin-bottom: 30px;
}
main p {
  margin-bottom: 30px;
}
footer {
  text-align: center;
  margin: 10px 0;
}
.f-right {
  float: right;
  overflow: auto;
}
.f-left {
  float: left;
  overflow: auto;
}


使用弹性框

最后但并非最不重要;而且由于flexbox具有IE11 +并在所有现代浏览器中均受支持,所以我更喜欢它-感谢您也让我重新掌握了我的知识。 :)

<div class="wrapper">
  <aside class="left_panel">
    <nav>
      <ul>
        <li>Home</li>
        <li>Manu item 1</li>
        <li>Manu item 2</li>
        <li>Manu item 3</li>
      </ul>
    </nav>
  </aside>
  <div class="right_panel">
    <header>
      <h1>Name of the website</h1>
    </header>
    <main>
      <img id="lantern" class="f-right" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" alt="">
      <h2>Subheading 1</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
      <h2>Subheading 2</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. </p>
      <h2>Subheading 3</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
      <h2>Subheading 4</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius.</p>

    </main>
    <footer>
      <p>Copyright &copy; 2019</p>
    </footer>
  </div>
</div>
html,body {
  height: 100%;
}
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  background-color: blue;
}
h1,h2,h3,h4,h5,h6 {
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
.wrapper {
  margin: 10px auto 30px;
  padding: 0;
  width: 80%;
  background-color: white;
  /*height: calc(100% - 40px);*/
  /*max-height: 100%;*/
  display: flex; /* added */
}
aside.left_panel {
  /* float: left; */
  min-height: 100%;
  width: 130px;
  background-color: #9eb9f1;
  padding-left: 30px;
  overflow-y: auto; /* changed to overflow-y*/
}
.right_panel {
  /*margin-left: 160px;*/
}
nav ul {
  list-style-type: none;
  padding: 0;
}
header {
  background-color: #6f90d1;
  padding: 20px;
}
header h1 {
  font-size: 60px;
  color: darkblue;
}
main {
  padding: 20px 20px 0;
}
main h2 {
  color: #6f90d1;
}
main #lantern {
  height: 400px;
  width: 200px;
}
main img {
  margin: 10px;
}
main h2 {
  margin-bottom: 30px;
}
main p {
  margin-bottom: 30px;
}
footer {
  text-align: center;
  margin: 10px 0;
}
.f-right {
  float: right;
  overflow: auto;
}
.f-left {
  float: left;
  overflow: auto;
}

答案 1 :(得分:1)

您可以除去浮子并将包装器设置为display: flex。还要删除包装器上的height属性和右列上的左边距。