javascript - 如何将一个div分成四部分?

标签 javascript jquery html css

这是我的代码:

.menu_box{
    background-color: whitesmoke;
    margin: auto;
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: 3px;
    width: 70vh;
    height: 60vh;
}
.menu_box_header{
    height: 45px;
    background-color: #f1f1f1;
    border-bottom: 1px solid #e1e1e1;
    position: relative;
    top: 0;
    margin-bottom: 10px;
}
.menu_options{
    border: 1px solid #e1e1e1;
    text-align: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="menu_box">
    <div class="menu_box_header">
    </div>
    <div class="menu_options col-xs-6">part1</div>
    <div class="menu_options col-xs-6">part2</div>
    <div class="menu_options col-xs-6">part3</div>
    <div class="menu_options col-xs-6">part4</div>
</div>

这是预期的结果:

enter image description here

我该怎么做?

最佳答案

您可以使用 flexbox 方法 - 我添加了一个内部容器,这样下方的盒子将动态填充剩余空间

.menu_box {
  background-color: whitesmoke;
  margin: auto;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 3px;
  width: 70vh;
  height: 60vh;
  display: flex;
  flex-direction: column; /* stack inner boxes vertically */
}

.menu_box_body {
  flex-grow: 1;    /* make this flex so 4 boxes will fill it equally */
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width:100%;
}

.menu_box_header {
  height: 45px;
  background-color: #f1f1f1;
  border-bottom: 1px solid #e1e1e1;
  position: relative;
  margin-bottom: 10px;
  width: 100%;
}

.menu_options {
  border: 1px solid #e1e1e1;
  display:flex;               /* use flex for centring */
  align-items:center;
  justify-content:center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="menu_box">
  <div class="menu_box_header">
  </div>
  <div class="menu_box_body">
    <div class="menu_options col-xs-6">part1</div>
    <div class="menu_options col-xs-6">part2</div>
    <div class="menu_options col-xs-6">part3</div>
    <div class="menu_options col-xs-6">part4</div>
  </div>
</div>

关于javascript - 如何将一个div分成四部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46951550/

相关文章:

javascript - onclick 不适用于按钮输入

javascript - TypeError : text. select 不是一个函数

php - 如何为搜索查询设置配额并避免分页占用配额

javascript - 一页上的多个 onchange ="this.form.submit()"不起作用

javascript - 使用 React-intl 的 Redux-form 字段级验证和错误翻译

javascript - 使用谷歌翻译 CSS 的 Bootstrap 菜单栏

javascript - 如何使用 jquery 为 slider 创建缩略图

javascript - Angular 6 - 将方法设置为元素的 `data-*` 属性

javascript - 基于日期比较使用 JQuery(或简单的 Javascript)动态更改 CSS

javascript - 如何根据按键点击过滤记录?