twitter-bootstrap 如何将 bootstrap col-md div 垂直划分为一半?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40012682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 00:19:56  来源:igfitidea点击:

How to divide bootstrap col-md div to half vertically?

csstwitter-bootstrap

提问by Juntae

I am trying to make below layout using bootstrap grid. There is a container <div class="row">and half width two divs in that (<div1> and <div2>). The <div1>'s height would be changeable as content size. And again, the <div2>should have two child divs(<div2-1>, <div2-2>) that half height of <div1>each. How can I make this layout with Bootstrap grid?

我正在尝试使用引导网格进行以下布局。里面有一个容器<div class="row">和两个半宽的 div ( <div1> and <div2>)。该<div1>的高度将是多变的内容大小。同样,<div2>应该有两个子 divs( <div2-1>, <div2-2>),<div1>每个子高度的一半。如何使用 Bootstrap 网格制作此布局?

enter image description here

在此处输入图片说明

I tried like that, but not working. :

我试过这样,但没有工作。:

<div class="row">
    <div class="col-md-6" id="div1">
        Some content...
    </div>
    <div class="col-md-6" id="div2">

        <div id="div2-1" style="height:50%;">

        </div>
        <div id="div2-2" style="height:50%;">

        </div>
    </div>
</div>

回答by Sankar

Try this...

试试这个...

div {
  border: 1px solid black;
}
#div2-1,
#div2-2 {
  height: 50%;
  border: 1px solid red !important;
}
#div1,#div2{
  height:50px;
  
  }
#div2.col-xs-6
{
  padding:0px !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col-xs-6" id="div1">
      Some content...
    </div>
    <div class="col-xs-6" id="div2">

      <div id="div2-1">

      </div>
      <div id="div2-2">

      </div>
    </div>
  </div>
</div>

回答by Jones Joseph

you can use jQuery if you want height of colunmn to be auto and also the inside divs to be half of it.

如果您希望 colunmn 的高度为 auto 并且内部 divs 为它的一半,则可以使用 jQuery。

But you might want to keep the divs overflow-yto autoif one div has content more than it can hold.

但是overflow-yauto如果一个 div 的内容超出其容纳范围,您可能希望保留 div 。

$(document).ready(function() {

  var totHeight = $("#col").height();

  $("#div-1").css("height", totHeight / 2);
  $("#div-2").css("height", totHeight / 2);

})
body * {
  color: #ccc;
}
#div-1,
#div-2 {
  overflow-y: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <div class="row">
    <div class="col-sm-6" id="col">
      <div id="div-1" style="background-color:red;">
        Content
        <br>Content
      </div>
      <div id="div-2" style="background-color:blue;">

        Content
        <br>Content
        <br>Content
        <br>Content
        <br>Content
        <br>Content
        <br>Content
        <br>Content

      </div>
    </div>
    <div class="col-sm-6"></div>

  </div>

</body>

回答by TauFeeQ

//Try this one...

//试试这个...

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<section>
<div class="col-md-12 bg-warning" border="1">
 <div class="row" border="1">
     <div class="col-md-6" border="1">
        <h3>First Column</h3>
     </div>
        <div class="col-md-6" border="1">
         <div class="row bg-danger">
             <h3>Second Column First Row<h3>
            </div>
            <div class="row bg-success">
             <h3>Second Column Second Row</h3>
            </div>
        </div>
    </div>
</div>
</section>
</body>
</html>

回答by Amit

Using Bootstrap 4

使用引导程序 4

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

<div class="row">
  <div class="col-md-6 bg-primary" style="min-height: 500px;"></div>
  <div class="col-md-6">
    <div class="row h-100 flex-column">
      <div class="col-md-6 mw-100 bg-danger"></div>
      <div class="col-md-6 mw-100 bg-success"></div>
    </div>
  </div>
</div>

<style>
  /* just for displaying correctly without content */
  
  [class*='col'] {
    min-height: 200px;
  }
</style>

回答by Asifuzzaman Redoy

hello try the below fiddle hope it helps also add the custom style if you want to

你好,试试下面的小提琴,如果你想的话,它也有助于添加自定义样式

<div class="container">
<div class="row">
<div class="col-xs-6 col-sm-6">
        <div id="navigation">
            <label>side Column</label>
        </div>           
    </div>
    <div class="col-xs-6 col-sm-6">
        <div id="text1">
            <label>First Divider</label>
        </div>
        <div id="text2">
            <label>second Divider</label>
        </div>
    </div>      
</div>
</div>

fiddle demo

小提琴演示