【问题标题】:Setting a <button>'s display as table-cell将 <button> 的显示设置为表格单元格
【发布时间】:2013-10-23 12:25:46
【问题描述】:

我正在尝试将buttondisplay 属性设置为table-cell,但它的行为不像一个。

任何帮助将不胜感激。

jsFiddle Demo (演示包含一个固定的容器高度,但我需要它在没有它的情况下工作)

No fixed sizes Demo.

DOM

<div class="container">
    <div class="item"></div>
    <div class="item"></div>
    <button class="item"></button>
</div>

CSS

.container {
    border: 5px solid blue;
    display: table;
    table-layout: fixed;
}
.item {
    border: 3px solid red;
    display: table-cell;
}

结果:

编辑:我需要它完全像表格单元格一样工作,即使没有固定大小。

请注意,有些解决方案似乎在 Chrome 上运行良好,但在 FF 上却无法运行

【问题讨论】:

  • table-cell 不适用于按钮,您可以使用内联样式来完全适合。 stackoverflow.com/questions/12414573/…
  • 你真的用这个问题把我们搞砸了 :) 好一个
  • 虽然这里的大多数人都被 50rep 的赏金蒙蔽了双眼,但我想简单地问一下:你来这里的目的是什么?为什么&lt;button&gt; 元素如此重要以至于不能用div 替换它?
  • 这是个好问题。我需要这个元素来提交(button type='submit')一个表单,我不想使用 JS 来完成这个简单的任务。现在我正在使用 JS,因为我找不到任何其他解决方案。此外,这是一个有趣的问题:)
  • 刚刚测试,您的No fixed sizes Demo 在 IE9 中按预期工作。

标签: html css


【解决方案1】:

使用label 怎么样?这样你就可以获得按钮的功能,但标签的可见性。在 Firefox 和 Chrome 中测试。更新了表单提交示例。

  • 单元格区域的可点击性不涉及 JavaScript
  • 在容器上没有固定高度的情况下工作
  • 在不同单元格的高度大于带按钮的单元格时有效
  • 适用于多个纽扣电池

HTML:

<form onsubmit="alert(); return false;">
    <div class="container">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
    </div>
    <div class="container">
        <div class="item">4</div>
        <div class="item">5<br><br><br>Extended cell</div>
        <label class="item">Button 1<button type="submit"></button></label>
        <label class="item">Button 2<button type="submit"></button></label>
    </div>
</form>

CSS:

.container {
    margin: 10px;
    border: 5px solid blue;
    display: table;
    table-layout: fixed;
    width: 300px;
}
.item {
    border: 3px solid red;
    display: table-cell;
}
.item button {
    background-color: transparent;
    position: absolute;
    left: -1000px;
    height: 1px;
    width: 1px;
    overflow: hidden;
}

JSFiddle here.

【讨论】:

  • 哇!这是一个很好的解决方案。但是,我不能使用剪辑,因为我需要在 IE8 上支持它。您对这个重点问题有其他想法吗?
  • 我已更新代码以使用 IE8。 clip 没有必要。只需将其移出屏幕即可使其正常工作。作为旁注,clip 在 IE8 及以下版本中工作,你只需要使用它的非标准编写方式我相信:clip: rect(1px 1px 1px 1px);
  • 这样做的主要问题是标签无法使用选项卡聚焦,这里的真正问题是某些 html 元素在技术上不允许显示更改,尽管某些浏览器会接受它们。 stackoverflow.com/a/37445333/1213795
  • 好点!感谢您的洞察力和链接!我当时没有考虑到这一点。无法通过选项卡专注于按钮是我的一个可怕的疏忽。希望它可以被修改,以便隐藏的按钮仍然可以被关注。我已经有几年没有做过网络编程了。显然,最好的解决方案是寻求其他解决方案,而不是尝试将方形钉插入圆孔。
【解决方案2】:

http://jsfiddle.net/Rhhh7/7/

在此示例中,我将按钮包装在 div class="item" 中,就像其他 div 一样。但是这一次,我单独设置了按钮的样式,以拉伸到 div 的高度和宽度。

.item button{
background:transparent;
padding:0;
border:0;
width:100%;
height:100%;
}

编辑:

这里是修复http://jsfiddle.net/Rhhh7/10/

解决 Firefox 问题。

将此添加到“项目”类中:

.item {
    border: 3px solid red;
    display: table-cell;
    height:100%;
    vertical-align:top;
}

为了使 td 具有 100% 的高度,父级也必须具有 100% 的高度。 vertical-align:top 然后将按钮设置到 div 的顶部,而不是默认的中间。

【讨论】:

  • 看起来按钮的边框在搞砸我们。 +1,但我仍然想知道为什么边框会这样做。
  • 谢谢,但如果容器没有固定高度,它仍然无法工作
  • 即使使用“height:100%”,每个“单元格”的内容仍然是动态的
  • 我已经对此发表了两次评论,已经删除了之前的评论,但我已经在 Firefox 中再次对此进行了测试(因为我认为这是一个可行的解决方案),并且它不会拉伸可点击区域如果第二列更高(在 Firefox 中),则为按钮单元格。见这里:jsfiddle.net/Rhhh7/16
【解决方案3】:

button.item { width: 100%; height: 50px; }

【讨论】:

  • 这还不错,但如果我有多个按钮,它就不起作用了。 jsfiddle.net/itay1989/Rhhh7/2
  • +1 对于您的解决方案,但我找不到任何适合 FF 的东西:S
【解决方案4】:

您总是可以将按钮包装在一个 div 中。

<div class="container">
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
</div>
   <div class="container">
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"><button>Button</button></div>
</div>

CSS

button{
   width:100%;
   height:2.75rem;
}

所以我想在一天结束时,这里的最终解决方案是,如果没有固定的测量单位,可能无法跨浏览器:(

【讨论】:

  • 这不合适,因为在这种情况下,我需要按钮在整个 div 上拉伸。使用绝对定位在 FF 上不起作用。
  • 对于某些奇怪的原因,height:113% 更适合单元格?不太清楚为什么。
  • 拉我的头发,叫我雪莉:\
  • 其实 height:50px 更合适。
  • @Agony 在 FF 上检查它。height 100% 没有固定尺寸将无法工作
【解决方案5】:

这似乎有效: HTML

<div class="container">
    <div class="item">Some text to make the cell bigger</div>
    <div class="item"></div>
    <div class="item"><button class="button-item"></button></div>
</div>

CSS:

.container {
    margin: 10px;
    border: 5px solid blue;
    display: table;
    table-layout: fixed;
    width: 300px;
}
.item {
    border: 3px solid red;
    display: table-cell;
    background: transparent;
}
.button-item{
    border: 5px;
    -moz-border:5px;
    height:100%;
    width: 100%;
}

Fiddle Demo

它在 FF 上的外观:

【讨论】:

  • 你在FF上测试过吗?
【解决方案6】:

包装在 div 中是一种解决方案,但我不明白为什么您不能像更改所有其他元素一样更改按钮元素的显示属性。例如,您可以使链接标签的行为类似于 div 标签。

这可以防止做一些事情,比如改变按钮的显示顺序: http://codepen.io/bakers37/pen/emoKvK

在 Chrome/Firefox 中,这不能按预期工作。

HTML

<div class="wrap">
 <div class="btn bottom">Back</div>
 <div class="btn top">Continue</div>
</div>


<div class="wrap">
 <button class="btn bottom">Back</button>
 <button class="btn top">Continue</button>
</div>

CSS

.wrap {
 display: table;
 margin: 20px 0 30px 0;
 width: 100%;
}

.btn
{
 display: block;
 width: 100%;
 margin: 5px 20px;
 padding: 10px;
 position: relative;
 background: #ccc;
}

.top { 
  display: table-caption; 
}

【讨论】:

  • 嗯...这些是最流行的浏览器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 2014-08-11
相关资源
最近更新 更多