css - 如何实现这种布局?表格标题未与内容对齐,每行的内容也未垂直对齐

标签 css twitter-bootstrap bootstrap-4

我正在尝试实现这种布局:

picture of desired output

  • 左侧的类型列比右侧的其他 2 项占用的宽度大得多,这是因为类型和描述可能很大。
  • 此外,每个元素的类型和描述应与数量选择菜单和小计内容垂直对齐。

我正在使用 Bootstrap ,但我是初学者,我正在尝试使用表格组件实现此布局。我不知道将表格组件用于此布局是否是更正确的方法或面板或其他一些东西。

我得到的布局是这样的:https://jsfiddle.net/bcvm3ap2/ , 但表格标题与内容不对齐,每行的内容也未垂直对齐。

你知道怎么修吗?还有如果表格是这种布局的正确方法?

html:

<div class="row no-gutters bg-white">
  <div class="col-12 col-md-8 px-4 pb-4">
    <table class="table table-responsive-sm border">
      <thead>
        <tr class="d-flex justify-content-between">
          <th scope="col" class="mr-auto">Type</th>
          <th scope="col">Quantity</th>
          <th scope="col">Subtotal</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="">
            <span class="text-heading-blue font-weight-semi-bold">Type1</span>
            <small class="font-weight-light d-block">Description</small>
          </td>
          <td><select class="form-control font-weight-normal text-gray" id="exampleFormControlSelect1">
            <option selected class="selected">0</option>
            <option>1</option>
            <option>2</option>
            </select>
          </td>
          <td>0</td>
        </tr>
        <tr>
          <td class="">
            <span class="text-heading-blue font-weight-semi-bold">Type2</span>
            <small class="font-weight-light d-block">Description</small>
          </td>
          <td><select class="form-control font-weight-normal text-gray" id="exampleFormControlSelect1">
            <option selected class="selected">2</option>
            <option>1</option>
            <option>2</option>
            </select>
          </td>
          <td>2</td>
        </tr>

      </tbody>
    </table>
  </div>
</div>

面板也不起作用:https://jsfiddle.net/jdpr21qw/1/标题与内容不符。

最佳答案

试试这个代码,它会给你你想要的布局。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="row no-gutters bg-white">
  <div class="col-12 col-md-8 px-4 pb-4">
    <table class="table table-responsive-sm table-bordered">
      <thead class='text-center'>
        <tr class="text-center ">
          <th scope="col" class="mr-auto">Type</th>
          <th scope="col">Quantity</th>
          <th scope="col">Subtotal</th>
        </tr>
      </thead>
      <tbody >
        <tr >
          <td class="">
            <span class="text-heading-blue font-weight-semi-bold">Type1</span>
            <br/><small class="font-weight-light d-block">Description</small>
          </td>
          <td><select class="form-control font-weight-normal text-gray" id="exampleFormControlSelect1">
            <option selected class="selected">0</option>
            <option>1</option>
            <option>2</option>
            </select>
          </td>
          <td>0</td>
        </tr>
        <tr>
          <td class="">
            <span class="text-heading-blue font-weight-semi-bold">Type2</span>
            <br/><small class="font-weight-light d-block">Description</small>
          </td>
          <td><select class="form-control font-weight-normal text-gray" id="exampleFormControlSelect1">
            <option selected class="selected">2</option>
            <option>1</option>
            <option>2</option>
            </select>
          </td>
          <td>2</td>
        </tr>

      </tbody>
    </table>
  </div>
</div>

</body>
</html>

关于css - 如何实现这种布局?表格标题未与内容对齐,每行的内容也未垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48192516/

相关文章:

HTML 在方形网格图片中指定分隔线间隙测量

修改a的style属性的CSS代码

html - 将div定位到css的右下角

html - 将鼠标悬停在图像上,显示文本和不透明度变化

css - 无法在 <DIV> 标签中居中

html - 左右浮动两个div : How can I keep them on the same level when a page resizes?

css - 在 Angular.js 中组织和加载 CSS 的模式

javascript - 我的网站在移动设备上显示为桌面

jquery - 如何为按钮添加悬停效果?

javascript - 删除JQuery中的单个单词属性