huangapple go评论55阅读模式
英文:

div with content align out of order with respect to div without content

问题

在下面的代码中,带有内容的<button>元素与其他没有内容的<button>元素的顺序不一致。当行中的所有元素都有内容时,不会发生这种情况。有任何想法为什么会发生这种情况吗?(提前感谢您的帮助)。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
    <style>
     button {
      background-color: #ffff;
      border: 1px solid #999;
      width: 120px;
      height: 120px;
      margin-top: -1px;
      margin-right: -1px;
      font-size: 50px;
      font-weight: bold;
    }
    </style>

    <div>
       <div>
    	<button> </button>
    	<button> </button>
    	<button> </button>
       </div>
       <div>
    	<button> </button>
    	<button> X </button>
    	<button> </button>
       </div>
       <div>
    	<button> </button>
    	<button> </button>
    	<button> </button>
       </div>
    </div>

<!-- end snippet -->
英文:

In the below code, the <button> element with the content is out of order with respect to the other <button> elements without content. This doesn't occur when all the elements in the row has some content in them. Any idea why this is happening? (Thanks in advance for the help).

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;style&gt;
 button {
  background-color: #ffff;
  border: 1px solid #999;
  width: 120px;
  height: 120px;
  margin-top: -1px;
  margin-right: -1px;
  font-size: 50px;
  font-weight: bold;
}

&lt;/style&gt;

&lt;div&gt;
   &lt;div&gt;
	&lt;button&gt; &lt;/button&gt;
	&lt;button&gt; &lt;/button&gt;
	&lt;button&gt; &lt;/button&gt;
   &lt;/div&gt;
   &lt;div&gt;
	&lt;button&gt; &lt;/button&gt;
	&lt;button&gt; X &lt;/button&gt;
	&lt;button&gt; &lt;/button&gt;
   &lt;/div&gt;
   &lt;div&gt;
	&lt;button&gt; &lt;/button&gt;
	&lt;button&gt; &lt;/button&gt;
	&lt;button&gt; &lt;/button&gt;
   &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

这是解决方案:

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-css -->
.button-container {
  display: flex;
  align-items: center;
}

button {
  background-color: #ffff;
  border: 1px solid #999;
  width: 120px;
  height: 120px;
  margin-top: -1px;
  margin-right: -1px;
  font-size: 50px;
  font-weight: bold;
}

<!-- 语言: lang-html -->
<div class="container">
  <div class="button-container">
    <button></button>
    <button></button>
    <button></button>
  </div>
  <div class="button-container">
    <button></button>
    <button>X</button>
    <button></button>
  </div>
  <div class="button-container">
    <button></button>
    <button></button>
    <button></button>
  </div>
</div>

<!-- 结束代码片段 -->
英文:

Here is the solution:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.button-container {
  display: flex;
  align-items: center;
}

button {
  background-color: #ffff;
  border: 1px solid #999;
  width: 120px;
  height: 120px;
  margin-top: -1px;
  margin-right: -1px;
  font-size: 50px;
  font-weight: bold;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;button-container&quot;&gt;
    &lt;button&gt;&lt;/button&gt;
    &lt;button&gt;&lt;/button&gt;
    &lt;button&gt;&lt;/button&gt;
  &lt;/div&gt;
  &lt;div class=&quot;button-container&quot;&gt;
    &lt;button&gt;&lt;/button&gt;
    &lt;button&gt;X&lt;/button&gt;
    &lt;button&gt;&lt;/button&gt;
  &lt;/div&gt;
  &lt;div class=&quot;button-container&quot;&gt;
    &lt;button&gt;&lt;/button&gt;
    &lt;button&gt;&lt;/button&gt;
    &lt;button&gt;&lt;/button&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

你应该将属性 vertical-align: top; 添加到按钮样式中:

button {
  background-color: #ffff;
  border: 1px solid #999;
  width: 120px;
  height: 120px;
  margin-top: -1px;
  margin-right: -1px;
  font-size: 50px;
  font-weight: bold;
  vertical-align: top;
}
英文:

you should add property vertical-align: top; to button styles:

button {
  background-color: #ffff;
  border: 1px solid #999;
  width: 120px;
  height: 120px;
  margin-top: -1px;
  margin-right: -1px;
  font-size: 50px;
  font-weight: bold;
  vertical-align: top;
}

huangapple
  • 本文由 发表于 2023年7月18日 00:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定