【问题标题】:Vertical positioning inside anchor and button锚点和按钮内的垂直定位
【发布时间】:2023-03-25 07:45:01
【问题描述】:

button, a {
  height: 30px;
  display: inline-block;
  border: 1px solid black;
  vertical-align: middle;
}

button > div, a > div {
  width: 30px;
  height: 10px;
  background-color: red;
}
<button>
  <div class="buttonDiv"></div>
</button>

<a href="#">
  <div class="anchorDiv"></div>
</a>

我们的项目中有带有图标的按钮,这些按钮根据道具呈现为按钮或锚点。我们偶然发现了按钮内容未对齐的问题。在代码 sn-p 中有示例。

为什么内部div 在按钮和锚点中的对齐方式不同?如何正确垂直对齐按钮的内容?我本可以添加padding-top,但它似乎不对。

【问题讨论】:

  • 添加显示:表格单元格;而不是 display:inline-block;在按钮中,一个用于垂直居中

标签: javascript html css button alignment


【解决方案1】:

恐怕您没有自然预期行为的原因是在button 中包含div 不是有效的HTML(根据HTML5)。这意味着每个浏览器可能有不同的呈现方式。

正如您所说,您可以轻松地使用填充或定位来解决问题,以使其按您的意愿工作,但它仍然不是有效的 HTML。一种可能的解决方法是在锚标记上重置填充(以覆盖默认样式)和垂直居中内容(不取决于元素本身的高度)。 https://jsfiddle.net/ne037nL7/

按钮只能包含短语内容,其中不仅包括纯文本,还包括span、i 等(此处为https://www.w3.org/TR/2012/WD-html5-20120329/content-models.html#phrasing-content 的完整列表)。

来源:W3https://www.w3.org/TR/2012/WD-html5-20120329/the-button-element.html#the-button-element

【讨论】:

  • 迄今为止的最佳答案,如果它包含完整正确的解决方案,我会接受。
  • 我刚刚添加了一个变通解决方案,让我知道它是否可以按需要工作。
  • 是的,解决方法有效,但我想要 html-valid 解决方案:3
  • 我判断错了,anchor是w3c-valid,只有button里面的div是w3c-invalid,我想我会用span来代替。
【解决方案2】:

试试这个代码。

  <style>
            .main{
        display:table;
        }
            button, a {
              height: 30px;
              display: table-cell;
              border: 1px solid black;
              vertical-align: middle;
              padding:0 10px;
            }
            button {
              background: transparent;
              height: 32px;
              margin-right: 10px;
            }

            button > div, a > div {
              width: 30px;
              height: 10px;
              background-color: red;
            }
            </style>
          <div class="main">
            <button>
              <div class="buttonDiv"></div>
            </button>

            <a href="#">
              <div class="anchorDiv"></div>
            </a>
        </div>

【讨论】:

    【解决方案3】:

    试试这个

    a{
    display:table-cell ;
    }
    

    button, a {
      height: 30px;
      display: inline-block;
      border: 1px solid black;
      vertical-align: middle;
    }
    a{
    display:table-cell ;
    padding:0.5px 6px;
    }
    button > div, a > div {
      width: 30px;
      height: 10px;
      background-color: red;
    }
    <button>
      <div class="buttonDiv"></div>
    </button>
    
    <a href="#">
      <div class="anchorDiv"></div>
    </a>

    【讨论】:

      【解决方案4】:

      button {
        height: 30px;
        display: inline-block;
        border: 1px solid black;
        vertical-align: middle;
        float:left;
      }
      
      a {
        height: 30px;
        border: 1px solid black;
        vertical-align: middle;
        display: table-cell;
      }
      
      button > div {
        width: 30px;
        height: 10px;
        background-color: red;
      }
      
      
      a > div {
        width: 30px;
        height: 10px;
        background-color: red;
        vertical-align:middle;
      }
      <button>
        <div class="buttonDiv"></div>
      </button>
      
      <a href="#">
        <div class="anchorDiv"></div>
      </a>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-08
        相关资源
        最近更新 更多