HTML元素在浏览器中显示为内联元素而不是块级元素。

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

HTML elements in the browser appearing as inline instead as block elements

问题

以下是您要翻译的内容:

我的以下代码用于在浏览器上创建一个简单的网页:

网页上的按钮显示为内联元素,而不是依次排列在顶部。使用 flexbox 也出现相同的问题。容器内的项目变成内联元素,而没有指定 display: flex-inline

我尝试添加了一个 <br> 标签。

英文:

My following code for a simple webpage on the browser:

The buttons on the webpage show up as inline elements instead of of one on top of the other consecutively. Happens as well with using flexbox. Items within the container become inline without stating display: flex-inline.

HTML元素在浏览器中显示为内联元素而不是块级元素。

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

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

&lt;h1 id=&quot;id1&quot;&gt;My Heading 1&lt;/h1&gt;

&lt;p id=&quot;demo&quot;&gt;&lt;/p&gt;

&lt;button type=&quot;button&quot; onclick=&quot;document.getElementById(&#39;id1&#39;).style.color = &#39;red&#39;&quot;&gt;
    Click Me!&lt;/button&gt;

&lt;button onclick=&quot;displayDate()&quot;&gt;Try it&lt;/button&gt;

<!-- end snippet -->

I tried adding a &lt;br&gt; tag

答案1

得分: 1

按钮默认是内联元素(实际上是内联块级元素)。

当你将一个元素设置为 display: flexdisplay: flex-inline 时,它的 子元素 会默认水平排列,或者垂直排列(如果你指定了 flex-direction: column)。

flexflex-inline 的区别在于 父元素 的渲染方式,而不是子元素。


如果你想要按钮显示在连续的行上,你可以:

  • 在它们之间使用 &lt;br&gt; 标签(不涉及到 flexbox),或者
  • 将每个按钮放在单独的块级元素内,比如 &lt;div&gt;(同样不涉及到 flexbox),或者
  • 还有其他很多选项。

我无法推断出你的数据的语义(主要因为它似乎是一堆演示元素的混合,而不是一段简单的文本),所以我不能评论哪种选项是合适的。

英文:

Buttons are inline elements (well, inline-block) by default.

When you make an element display: flex or display: flex-inline its children are arranged in a row (by default) or column (if you specify flex-direction: column).

The difference between flex and flex-inline is how the parent element is rendered, not the children.


If you want the buttons displayed on consecutive lines then you can:

  • use a &lt;br&gt; between them (without involving flexbox) or
  • put each on in a separate block element such as a &lt;div&gt; (also without involving flexbox) or
  • numerous other options

I can't infer the semantics of your data (largely because it seems to be a mishmash of demo elements and not a straight-forward text) so I can't comment on which option(s) would be appropriate.

答案2

得分: 0

&lt;button&gt; 不是一个块级元素。要将它们垂直堆叠,有几种方法:

1- 一种方法是将它们包裹在&lt;div&gt;中:

<div>
    <button type="button" onclick="document.getElementById('id1').style.color = 'red'">
        Click Me!
    </button>
</div>
<div>
    <button onclick="displayDate()">
        Try it
    </button>
</div>

2- 另一种方法是在它们上应用display: block样式:

<button style="display: block"></button>

3- 或者将它们都包裹在一个&lt;div&gt;中,将其显示设置为flex,并将flex-direction设置为column

<div style="display: flex; flex-direction: column">
    <button type="button" onclick="document.getElementById('id1').style.color = 'red'">
        Click Me!
    </button>
    <button onclick="displayDate()">
        Try it
    </button>
</div>
英文:

&lt;button&gt; is not a block element. To stack them on top of each other,

1- one way is to wrap them in &lt;div&gt;s

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

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

 &lt;h1 id=&quot;id1&quot;&gt;My Heading 1&lt;/h1&gt;

&lt;p id=&quot;demo&quot;&gt;&lt;/p&gt;
&lt;div&gt;
    &lt;button type=&quot;button&quot; 
    onclick=&quot;document.getElementById(&#39;id1&#39;).style.color = &#39;red&#39;&quot;&gt;
    Click Me!&lt;/button&gt;
&lt;/div&gt;
&lt;div&gt;
    &lt;button onclick=&quot;displayDate()&quot;&gt;Try it&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

2- Another is to use display: block style on them.

&lt;button style=&quot;display: block&quot;&gt;&lt;/button&gt;

3- Or to wrap both of them in a &lt;div&gt; with display set to flex and flex-direction to column

&lt;div style=&quot;display: flex; flex-direction: column&quot;&gt;
    &lt;button type=&quot;button&quot; 
    onclick=&quot;document.getElementById(&#39;id1&#39;).style.color = &#39;red&#39;&quot;&gt;
    Click Me!&lt;/button&gt;
    &lt;button onclick=&quot;displayDate()&quot;&gt;Try it&lt;/button&gt;
&lt;/div&gt;

答案3

得分: 0

默认情况下,按钮的显示为inline-block。如果您想更改此设置,请显式设置display属性。

button {
    display: block;
}
英文:

By default, buttons have a display of inline-block. If you want to change that, explicitly set the display.

button {
    display: block;
}

huangapple
  • 本文由 发表于 2023年6月5日 04:28:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402288.html
匿名

发表评论

匿名网友

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

确定