输入元素超出移动屏幕宽度
Input element goes beyond the mobile screen width
我有搜索输入元素,当我在移动设备上查看它时,它超出了屏幕的宽度。我不确定为什么:
<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 解决方案。
仍然不清楚是什么设置了输入的宽度(可能只是 user-agent/browser 默认值),但覆盖它有帮助:
.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 class 来达到类似的效果:
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
。
输入中断是由于您添加了填充。
它有3rempadding-rigth,当屏幕较小时,组件大于宽度。
您应该减小填充的大小。
理想的做法是减少填充,并尝试使用 css 中的 text-indent。
.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>
您需要处理响应变体 类 并且 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
借助这里的一个答案,我找到了一个答案,我认为这是比设置 div 宽度更好的解决方案:
https://play.tailwindcss.com/R474hCHy19
我所做的只是删除了 <div class="relative">
,这样 input
元素现在直接位于 section
元素内。然后我添加了 overflow-auto
(强制所有元素保持在屏幕宽度内)和 flex-1
(当屏幕宽度增加时强制 input
元素填充所有可用的 space ) 类 到 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>
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>
我有搜索输入元素,当我在移动设备上查看它时,它超出了屏幕的宽度。我不确定为什么:
<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 解决方案。
仍然不清楚是什么设置了输入的宽度(可能只是 user-agent/browser 默认值),但覆盖它有帮助:
.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 class 来达到类似的效果:
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
。
输入中断是由于您添加了填充。 它有3rempadding-rigth,当屏幕较小时,组件大于宽度。 您应该减小填充的大小。
理想的做法是减少填充,并尝试使用 css 中的 text-indent。
.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>
您需要处理响应变体 类 并且 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
借助这里的一个答案,我找到了一个答案,我认为这是比设置 div 宽度更好的解决方案: https://play.tailwindcss.com/R474hCHy19
我所做的只是删除了 <div class="relative">
,这样 input
元素现在直接位于 section
元素内。然后我添加了 overflow-auto
(强制所有元素保持在屏幕宽度内)和 flex-1
(当屏幕宽度增加时强制 input
元素填充所有可用的 space ) 类 到 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>
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>