【问题标题】:Set Button height to 100% of parent element without setting parent height将按钮高度设置为父元素的 100%,而不设置父高度
【发布时间】:2020-08-17 17:46:49
【问题描述】:

我有一个 div,其中包含一个按钮、一个表格和另一个按钮,我希望这些按钮位于表格的左侧和右侧,并且与表格具有相同的高度。按钮应该是用于更改表中数据的后退和前进按钮。

我已经接近了我想要在 div 上使用 float: none 和在所有项目上使用 float: left 的内容,这可以正确对齐它们但是当我在按钮上尝试 height: 100% 时,它们会在整个窗口上展开,这不是什么我要。

在怪癖模式下,我可以实现类似的效果

div {
    height: 50%;
}
    
button {
    height: 100%;
}
    
table {
    display: inline-block;
}

由于我的屏幕尺寸匹配,所以它在意外测试中起作用,因此它显示为我想要的,但是一旦我调整窗口大小或添加<!DOCTYPE html>,这种情况就发生了变化。

有什么方法可以在不使用display: flex 的情况下完成此操作?

HTML:

<html>
<head>
    <meta charset="utf-8">
    <title>Under construction</title>
    <link rel="stylesheet" href="../css/stylesheet.css" />
</head>

<body>

<div>
    <button>&larr;</button>

    <table style="border: none">
    <thead><th>a</th><th>b</th><th>c</th></thead>
         <tbody>
             <tr><td>A</td><td>B</td><td>C</td></tr>
             <tr><td>D</td><td>E</td><td>F</td></tr>
             <tr><td>G</td><td>H</td><td>I</td></tr>
         </tbody>
    </table>

    <button>&rarr;</button>
</div>

</body>

</html>

CSS:

td {
    background: green;
    border-radius: 7px;
}

【问题讨论】:

  • 分享您的 HTML 代码
  • @TemaniAfif 它只是
    没有别的
  • 它不仅是那个代码,你的表里肯定有数据..我们需要一个真实代码来提供准确的解决方案,我可以为你提供一个解决方案代码,但可能不适用于您的情况
  • 为什么不能使用 flexbox?这是这里的简单解决方案
  • @TemaniAfif 目前还没有内容,只是占位符,稍后将由 php-sql 后端动态填充,这只是纯文本,我已将当前代码添加到问题中。

标签: html css html-table htmlbutton


【解决方案1】:

position:absolute 似乎是这里的唯一解决方案:

td {
  background: green;
  border-radius: 7px;
}
.box  {
  border:1px solid red;
  position:relative;
  display:inline-block;
  padding:0 50px; /* 50px is considered to be the width of button so adjust it based on your case*/ 
}
.box button {
  position:absolute;
  top:0;
  height:100%;
  left:0;
}
.box button:last-of-type {
  left:auto;
  right:0;
}
<div class="box">
  <button>&larr;</button>

  <table style="border: none">
    <thead>
      <th>a</th>
      <th>b</th>
      <th>c</th>
    </thead>
    <tbody>
      <tr>
        <td>A</td>
        <td>B</td>
        <td>C</td>
      </tr>
      <tr>
        <td>D</td>
        <td>E</td>
        <td>F</td>
      </tr>
      <tr>
        <td>G</td>
        <td>H</td>
        <td>I</td>
      </tr>
    </tbody>
  </table>

  <button>&rarr;</button>
</div>

或者您可以考虑另一种表格结构,但您需要调整 HTML 代码:

.box table td {
  background: green;
  border-radius: 7px;
}

.box {
  border: 1px solid red;
  border-spacing: 0;
}
.box,
.box > tbody,
.box > tbody > tr > td{
  height:100%;
}

.box button {
  height: 100%;
}
<table class="box">
  <tr>
    <td><button>&larr;</button></td>

    <td>
      <table style="border: none">
        <thead>
          <th>a</th>
          <th>b</th>
          <th>c</th>
        </thead>
        <tbody>
          <tr>
            <td>A</td>
            <td>B</td>
            <td>C</td>
          </tr>
          <tr>
            <td>D</td>
            <td>E</td>
            <td>F</td>
          </tr>
          <tr>
            <td>G</td>
            <td>H</td>
            <td>I</td>
          </tr>
        </tbody>
      </table>
    </td>
    <td><button>&rarr;</button></td>
  </tr>
</table>

【讨论】:

  • 虽然这看起来像是一个简单任务的很多代码,但我找不到任何其他解决方案。我现在会使用它,但内心深处我拒绝相信这是唯一的方法。
  • @TheSupremeGod 还有其他方法,但会涉及 Flexbox 或 CSS 网格或其他表格,但您需要调整 html(检查更新)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多