输入框不是全宽。

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

Input box is not full width

问题

我无法理解为什么我的输入框不能占满整个宽度。

我使用Tailwind CSS。

<div class="flex flex-col md:flex-row md:items-center md:justify-between">
  <div class="flex flex-row items-center">
    <div class="icon">ICON</div>
    <div class="brand">BRAND</div>
  </div>
  <div class="flex grow">
    <span class="relative flex w-full items-center bg-green-200 p-4">
      <label for="search"></label>
      <div class="icon absolute">ICON</div>
      <input placeholder="WHY NOT FULL WIDTH FOR THIS INPUT BOX?????" class="">
    </span>
  </div>
  <div class="flex flex-row">SECTION-2</div>
</div>
英文:

I cannot understand why my input box does not take full width.

I use tailwind css.

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

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

&lt;div class=&quot;flex flex-col md:flex-row md:items-center md:justify-between&quot;&gt;
  &lt;div class=&quot;flex flex-row items-center&quot;&gt;
    &lt;div class=&quot;icon&quot;&gt;ICON&lt;/div&gt;
    &lt;div class=&quot;brand&quot;&gt;BRAND&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;flex grow&quot;&gt;
    &lt;span class=&quot;relative flex w-full items-center bg-green-200 p-4&quot;&gt;
      &lt;label for=&quot;search&quot;&gt;&lt;/label&gt;
      &lt;div class=&quot;icon absolute&quot;&gt;ICON&lt;/div&gt;
      &lt;input placeholder=&quot;WHY NOT FULL WIDTH FOR THIS INPUT BOX?????&quot; class=&quot;&quot;
    /&gt;&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&quot;flex flex-row&quot;&gt;SECTION-2&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

If I replace the input element with a div for example then it is all correct, that div takes full width. But for some reason the input element does not. Why?

答案1

得分: 2

翻译结果:

&lt;!-- Flex 容器 --&gt;
&lt;div class=&quot;flex flex-col md:flex-row md:items-center md:justify-between&quot;&gt;
    &lt;!-- Flex 项列表 --&gt;
  
    &lt;!-- 1. --&gt;
    &lt;div class=&quot;icon&quot;&gt;图标&lt;/div&gt;
  
    &lt;!-- 2. --&gt;
    &lt;div class=&quot;brand&quot;&gt;品牌&lt;/div&gt;
  
    &lt;!-- 3. - 使用 &quot;grow&quot; 填充空位 --&gt;
    &lt;!-- 需要单行,因此我们添加了 flex 类 -&gt; (子) Flex 容器 --&gt;
    &lt;span class=&quot;grow bg-green-200 p-4 flex&quot;&gt;
        &lt;!-- (子) Flex 项列表 --&gt;

        &lt;!-- 1. --&gt;
        &lt;label for=&quot;search&quot;&gt;&lt;/label&gt;

        &lt;!-- 2. --&gt;
        &lt;div class=&quot;icon&quot;&gt;图标&lt;/div&gt;

        &lt;!-- 3. --&gt;
        &lt;!-- 使用 &quot;grow&quot; 填充空位 --&gt;
        &lt;input placeholder=&quot;为什么不是输入框的完整宽度?????&quot; class=&quot;grow&quot; /&gt;
    &lt;/span&gt;

    &lt;!-- 4. --&gt;
    &lt;div&gt;
      第二部分 --&gt; 可能是正确的部分
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
英文:

Photo from Solution (original --> successfully)

输入框不是全宽。
Link to Example

More information: How to use "grow" in item of "flex"

Code

&lt;!-- Flex Container --&gt;
&lt;div class=&quot;flex flex-col md:flex-row md:items-center md:justify-between&quot;&gt;
    &lt;!-- Flex Item List --&gt;
  
    &lt;!-- 1. --&gt;
    &lt;div class=&quot;icon&quot;&gt;ICON&lt;/div&gt;
  
    &lt;!-- 2. --&gt;
    &lt;div class=&quot;brand&quot;&gt;BRAND&lt;/div&gt;
  
    &lt;!-- 3. - fill empty places by &quot;grow&quot; --&gt;
    &lt;!-- Need one-line so we added flex class -&gt; (Sub) Flex Container --&gt;
    &lt;span class=&quot;grow bg-green-200 p-4 flex&quot;&gt;
        &lt;!-- (Sub) Flex Item List --&gt;

        &lt;!-- 1. --&gt;
        &lt;label for=&quot;search&quot;&gt;&lt;/label&gt;

        &lt;!-- 2. --&gt;
        &lt;div class=&quot;icon&quot;&gt;ICON&lt;/div&gt;

        &lt;!-- 3. --&gt;
        &lt;!-- fill empty places by &quot;grow&quot; --&gt;
        &lt;input placeholder=&quot;WHY NOT FULL WIDTH FOR THIS INPUT BOX?????&quot; class=&quot;grow&quot; /&gt;
    &lt;/span&gt;

    &lt;!-- 4. --&gt;
    &lt;div&gt;
      SECTION-2 --&gt; Maybe right Section
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

答案2

得分: 1

为使输入框宽度全屏,请添加class="w-full",同时检查父级元素是否具有明确指定的宽度,或者定义为块级或内联块级,并且具有w-full类。 元素没有默认宽度,也不是块级元素或内联块级元素。

英文:

For making that input full width add class=&quot;w-full" also check if parent span has explicit width specified or defined as block or inline-block in additon to having w-full class. Spans have no default width also are not block nor inline-block elements.

答案3

得分: 1

根据您提供的代码,似乎输入框包含在一个具有p-4类设置的固定宽度的span元素中,该类代表padding: 1rem。这导致输入框不占据父容器的整个宽度。

要使输入框占满整个宽度,您可以从span元素中移除p-4类,并在输入元素中添加w-full类。这将使输入框拉伸以填充其父容器内的可用空间,其父容器具有flex类并带有“grow”效果,表示它应该扩展以填满任何剩余空间。

以下是应用了这些更改的您的代码的更新版本:

<div class="flex flex-col md:flex-row md:items-center md:justify-between">
  <div class="flex flex-row items-center">
    <div class="icon">ICON</div>
    <div class="brand">BRAND</div>
  </div>
  <div class="flex grow">
    <span class="relative flex w-full items-center bg-green-200">
      <label for="search"></label>
      <div class="icon absolute">ICON</div>
      <input placeholder="为什么这个输入框不是全宽????" class="w-full" />
    </span>
  </div>
  <div class="flex flex-row">SECTION-2</div>
</div>
英文:

Based on the code you provided, it seems that the input box is contained within a span element with a fixed width set by the p-4 class, which stands for padding: 1rem. This is causing the input box to not take the full width of the parent container.

To make the input box take the full width, you can remove the p-4 class from the span element and add the w-full class to the input element. This will make the input box stretch to fill the available space within its parent container, which has a flex class with "grow" utility, indicating that it should grow to fill any remaining space.

Here's an updated version of your code with these changes applied:

&lt;div class=&quot;flex flex-col md:flex-row md:items-center md:justify-between&quot;&gt;
  &lt;div class=&quot;flex flex-row items-center&quot;&gt;
    &lt;div class=&quot;icon&quot;&gt;ICON&lt;/div&gt;
    &lt;div class=&quot;brand&quot;&gt;BRAND&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;flex grow&quot;&gt;
    &lt;span class=&quot;relative flex w-full items-center bg-green-200&quot;&gt;
      &lt;label for=&quot;search&quot;&gt;&lt;/label&gt;
      &lt;div class=&quot;icon absolute&quot;&gt;ICON&lt;/div&gt;
      &lt;input placeholder=&quot;WHY NOT FULL WIDTH FOR THIS INPUT BOX?????&quot; class=&quot;w-full&quot; /&gt;
    &lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&quot;flex flex-row&quot;&gt;SECTION-2&lt;/div&gt;
&lt;/div&gt;

答案4

得分: -1

所有 div 默认为 display: block。将 display: block 添加到输入框,它也将变为全宽。

input {
    display: block;
}

你也可以使用 tailwind 提供的 block 类。

英文:

All divs have display: block by default. Add display: block to the input and it will also become full width.

input {
    display: block;
}

You can also use the block class provided by tailwind

huangapple
  • 本文由 发表于 2023年4月17日 04:24:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030147.html
匿名

发表评论

匿名网友

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

确定