【问题标题】:Input element goes beyond the mobile screen width输入元素超出手机屏幕宽度
【发布时间】:2021-01-22 15:52:33
【问题描述】:

我有搜索输入元素,当我在移动设备上查看它时,它超出了屏幕的宽度。我不知道为什么:

<template>
 <div id="app" class="bg-gray-200 antialiased">
     <section class="flex justify-between bg-gray-800 px-4 py-3 ">
         <div class="relative">
             <input class= "bg-gray-900 text-white rounded-lg px-12 py-2
                           focus:outline-none focus:bg-white focus:text-gray-500"
                    placeholder="Search by keywords"/>
         </div>


         <button>Filters</button>
     </section>
</div>
</template>

感谢任何帮助。

编辑:

我试图删除设置填充的样式:

<template>
  <div id="app" class="bg-gray-200  ">
      <section class="flex justify-between bg-gray-800   ">
          <div class="  ">
              <input class= "bg-gray-900 "
                     placeholder="Search by keywords"/>
          </div>


          <button>Filters</button>
      </section>
</div>
</template>

但是没有用。

我希望通过 tailwind 类来解决它,而不是求助于原始 css 解决方案。

【问题讨论】:

  • 也许 id="app" 有一些 CSS 属性?
  • @aakash 不
  • 你能以jsfiddle之类的方式运行你的HTML吗?

标签: html css tailwind-css


【解决方案1】:

目前还不清楚是什么设置了输入的宽度(可能只是用户代理/浏览器的默认设置),但覆盖它会有所帮助:

.flex input { /* or put a custom class on your input, such as input-flex */
    min-width: 0;
}

body {
    width: 240px; /* demo only */
}
<link href="https://unpkg.com/tailwindcss@1.0.0/dist/tailwind.min.css" rel="stylesheet"/>

<div id="app" class="bg-gray-200 antialiased">
  <section class="flex justify-between bg-gray-800 px-4 py-3 ">
      <input class="bg-gray-900 text-white rounded-lg px-12 py-2
                           focus:outline-none focus:bg-white focus:text-gray-500" placeholder="Search by keywords" />

    <button>Filters</button>
  </section>
</div>

原来你可以使用w-full fluid width 类来达到类似的效果:

body {
    width: 240px; /* demo only */
}
<link href="https://unpkg.com/tailwindcss@1.0.0/dist/tailwind.min.css" rel="stylesheet"/>

<div id="app" class="bg-gray-200 antialiased">
  <section class="flex justify-between bg-gray-800 px-4 py-3 ">
      <input class="w-full bg-gray-900 text-white rounded-lg px-12 py-2
                           focus:outline-none focus:bg-white focus:text-gray-500" placeholder="Search by keywords" />

    <button>Filters</button>
  </section>
</div>

您可以使用sm 修饰符定位较小的屏幕,例如sm:w-full

【讨论】:

    【解决方案2】:

    输入中断是由于您添加的填充。 它有 3rem padding-rigth,当屏幕较小时,组件大于宽度。 您应该减小填充的大小。

    理想的做法是减少填充,并尝试在 css 中使用文本缩进。

    .flex input{
       text-indent: 10px;
     }
    <link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
    
    <div id="app" class="bg-gray-200 antialiased">
    
    <section class="grid grid-cols-2 bg-gray-800 px-3 py-3 ">
        <div class="relative">
            <input class="bg-gray-900 text-white w-full rounded-lg px-3 px-3 py-2
                       focus:outline-none focus:bg-white focus:text-gray-500" placeholder="Search by keywords" />
        </div>
    
        <div class="text-right">
            <button>Filters</button>
        </div>
    
    </section>
    </div>

    【讨论】:

    • 如果我删除填充 px py 类问题不会消失
    • 我尝试了你的代码,不知怎的,它显示的内容与我的第一个屏幕截图相同......
    • 您可以将“flex justify-between”替换为“grid-cols-2”,并在输入中添加一个“.w-full”类
    【解决方案3】:

    您需要处理响应式变体类,flex-1 将为此设置 元素的全宽:

        <div>
          <div id="app" class="bg-gray-200 antialiased">
            <section class="flex justify-between bg-gray-800 px-4 py-3  col-gap-2">
                <input class="block max-w-none sm:max-w-full overflow-auto flex-1 w-auto bg-gray-500 text-white rounded-lg py-2 px-4 focus:outline-none focus:bg-white focus:text-gray-500 placeholder-black " placeholder="Search by keywords"/>
                <button class="block text-gray-100">
                    <span class="hidden md:block">Filters</span>
                    <span class="md:hidden">
                        <svg class="w-10 h-10" fill="none" stroke="currentColor" viewBox="0 0 24 24"
                            xmlns="http://www.w3.org/2000/svg">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 4a1 1 0 011-1h16a1 1 0 011 1v2.586a1 1 0 01-.293.707l-6.414 6.414a1 1 0 00-.293.707V17l-4 4v-6.586a1 1 0 00-.293-.707L3.293 7.293A1 1 0 013 6.586V4z"></path>
                        </svg>
                    </span>
                </button>
            </section>
          </div>
      </div>
    

    在此处查看代码:Example Code and Output

    【讨论】:

    • 这在268px以下也有同样的问题。我不确定 OP 的规模目标是什么......
    • 是的,同样的事情:i.imgur.com/maA4a5R.png
    • 好的,知道了。我现在已经修好了。请查看最新编辑并查看游乐场链接。
    • 看来,如果我从您的代码中删除 overflow-auto,它就会停止工作。所以如果overflow-auto 是这里的关键,我尝试将它添加到我的原始代码中:play.tailwindcss.com/oeisp6ePUK。还必须将 input 元素从 div 移出 - 到 section 元素中。如果input 在另一个div 内,它为什么会中断。感谢overflow-auto 的想法和顺风游乐场网站。我很难找到一个。
    【解决方案4】:

    借助此处的一个答案,我找到了一个我认为比设置 div 宽度更好的解决方案: https://play.tailwindcss.com/R474hCHy19

    我所做的只是删除了&lt;div class="relative"&gt;,以便input 元素现在直接位于section 元素内。然后我添加了overflow-auto(强制所有元素保持在屏幕宽度内)和flex-1(强制input元素在屏幕宽度增加时填充所有可用空间)类到input元素。

    <section class="flex justify-between col-gap-4 bg-gray-800 px-2 py-3 ">
        <input class= "flex-1 overflow-auto bg-gray-900 text-white px-2 
                               focus:outline-none focus:bg-white focus:text-gray-500"
                        placeholder="Search by keywords"/>
     
        <button class="px-4  bg-gray-700 ">Filters</button>
    </section>
    

    【讨论】:

      【解决方案5】:

      TailwindCSS 搜索输入组示例:

      代码笔:Example

      <link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.1.2/tailwind.min.css" rel="stylesheet"/>
      <div class="flex grid grid-cols-6 place-content-center min-h-screen bg-gray-50">
        <div class="col-start-2 col-span-4">
          <span class="z-10 leading-snug font-normal absolute text-center text-blueGray-300 absolute bg-transparent rounded text-base items-center justify-center w-8 pl-2 py-2">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
            </svg>
          </span>
          <div class="flex justify-between">
            <input type="search" name="search" id="search" placeholder="Search Item..." class="px-5 text-md border-gray-400 rounded-l text-lg pl-11 py-1 shadow-sm float-left w-full border" />
            <button type="submit" class="w-24 flex items-center justify-center bg-gray-100 text-lg py-1 border-t border-r border-b border-gray-400 rounded-r text-gray-600">Search</button>
          </div>
        </div>
      </div>

      【讨论】:

        猜你喜欢
        • 2014-01-10
        • 2021-10-13
        • 2016-07-08
        • 1970-01-01
        • 2012-11-30
        • 1970-01-01
        • 1970-01-01
        • 2021-11-02
        • 1970-01-01
        相关资源
        最近更新 更多