如何使旁边的列随着并行列中内容的大小而增长 [英] How to make aside column grow with the size of the content in parallel column

查看:71
本文介绍了如何使旁边的列随着并行列中内容的大小而增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi how can I make the side menu to grow with the content of the parallel container, that should expand with its content as well.

我正在尝试确保 aside 标记中包含的左列与内容的大小相同 div 中包含的权利。我能够通过设置 html body 标签以及的大小来实现此目的。 .wrapper 类到 height:100%,类似于对stackoverflow的查询。

I am trying to ensure that the left column enclosed in aside tag, is the same size as the content on the right enclosed in div column .right_panel. I was able to accomplish this by setting the size of html and body tag as well as .wrapper class to height: 100% as per similar queries on stackoverflow.

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

However now the .wrapper is not expanding when I add more content to main. I tried to change height to min-height but it shrink the aside to its content size.

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

I know I can accomplish this with flex layout but because flex is outside the scope of my course and also because I want to learn alternative approaches in case I would need to use them in the future for compatibility with the older browsers I am looking for solution that would not use flex layout.

下面的代码段和 JSFiddle链接

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>

推荐答案

使用表格布局

一种解决方案是使用表格布局使用 wrapper 上的 display:table 并使您的左部分右侧部分放入表单元格-请参见下面的演示:

One solution is to use a table layout using display: table on the wrapper and making your left-section and right-section into a table-cell - see demo below:

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>

使用Float

您可以尝试执行此操作-但是您无法正确获得溢出或无法执行列的高度相同-请参见一个示例来进行修饰:

You can try this - but you don't get the overflow right or won't be able to make the height of the columns the same - see an example to fiddle with that:

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;
}

<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>

但是一个 hack 为此-使用 large margin & 填充。请注意,您需要删除包装器上设置的高度最大高度 -参见下面的演示:

But there is a hack for that - using large margin & padding. Note that you need to remove the height and max-height set on the wrapper - see demo below:

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;
}

<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>

使用弹性框

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

Last, but not the least; and since flexboxes have IE11+ and support in all modern browsers, I'd prefer it - thanks to you for letting me brush up on my knowledge too. :)

>
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;
}

<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>

这篇关于如何使旁边的列随着并行列中内容的大小而增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆