当前位置: 动力学知识库 > 问答 > 编程问答 >

html - Two left-floating cols plus right-floating sidebar Bootstrap

问题描述:

My goal is to achieve the following image on my page:

I managed to achieve this with the HTML and CSS you can find below, but it doesn't seem very viable, because the sidebar is losing it's physical height because of the position: absolute.

I'm wondering if it's possible to make one row with two columns on the left and a sidebar on the right, without having to use positioning.

I tried position: relative with a negative top, but since the top col-md-9 has a changing height (depending on what is entered), I can't give it a negative top. It'll simply be too static and impossible to maintain.

Changing the order in the HTML doesn't change anything, since the physical height of the sidebar will move the 2nd content down.

.sidebar {

position: absolute;

width: 100%;

}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="row">

<div class="col-md-9">

Changing content

</div>

<div class="col-md-3 sidebar">

Sidebar

</div>

<div class="col-md-9">

More content

</div>

</div>

网友答案:

I use xs columns for this example, but you can change to md in your page.

Firstly create a 9-column and a 3-column div. Then put two divs inside the 9-column one.

.content, .sidebar {
  margin: 10px;
  padding: 20px;
}

.content {
  background-color: navy;
  color: white;
}

.sidebar {
  background-color: yellow;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="row wrapper">
  <div class="col-xs-9">
    <div class="row">
      <div class="col-xs-12">
        <div class="content">Content</div>
      </div>
      <div class="col-xs-12">
        <div class="content">Content</div>
      </div>
    </div>
  </div>
  <div class="col-xs-3">
    <div class="sidebar">Sidebar</div>
  </div>
</div>
网友答案:

You can nest col-x-x inside other col-x-x You just have to create 2 parents: content and sidebar, then add multiple contents into the content parent :

<div class="row">
    <div class="col-md-9">
        <div class="col-md-12">
          Content
        </div>
        <div class="col-md-12">
          More content
        </div>

    </div>
    <div class="col-md-3 sidebar">
        Sidebar
    </div>

</div>
网友答案:

As a solution, you can make sidebar to stay at the right side of screen if you'll make left section overflow: auto.

.sidebar {
  height: 100vh;
  background: lightgreen;
}
.left-section {
  height: 100vh;
  background: lightblue;
  overflow: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
    <div class="col-sm-9 left-section">
      <div class="row">
        <div class="col-sm-12">
          Changing content
        </div>
        <div class="col-sm-12">
          More content
        </div>
      </div>
    </div>
    <div class="col-sm-3 sidebar">
        Sidebar
    </div>
</div>
</div>
网友答案:

You cannot have more that 12 columns in a row unless it is not defined in your custom grid.

What you can try is this:

<div class="row">
    <div class="col-md-9">
        <div class="row">
                <div class="col-sm-12">
                     Changing content
                </div>
                <div class="col-sm-12">
                    More content
                </div>
            </div>
    </div>
    <div class="col-md-3" style="position: relative;">
        <div class="row">
            <div class="col-sm-12 sidebar">
                 Sidebar
            </div>
        </div>
    </div>
</div
分享给朋友:
您可能感兴趣的文章:
随机阅读: