React Tailwind横向滚动包含卡片的行为异常

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

react tailwind horizontal scroll containing cards behaving weird

问题

因此,由于某种原因,当组件溢出时,无法在组件内水平滚动,卡片会缩小并适应组件。我不明白为什么,非常感谢任何解释。

React Tailwind横向滚动包含卡片的行为异常

React Tailwind横向滚动包含卡片的行为异常

<div className='w-5/6 h-64 mx-auto mt-24 mb-20'>
    <p className='h-12 text-xl font-medium ml-5 flex items-center drop-shadow-lg'>Animations</p>
    <div className='bg- w-full h-48 flex flex-row overflow-x-scroll scrollbar-hide'>
        <div className='bg-blue-300 h-full w-72 ml-4 rounded-md '></div>
        <div className='bg-blue-300 h-full w-72 ml-4 rounded-md '></div>
        <div className='bg-blue-300 h-full w-72 ml-4 rounded-md '></div>
        <div className='bg-blue-300 h-full w-72 ml-4 rounded-md '></div>
    </div>
</div>
英文:

So, for some reason instead of being able to scroll horizontally inside the component when it is overflown, the cards shrink and fit the component. I don't understand why, any explanation is greatly appreciated. Code and images are here bellow, a great thanks in advance.

React Tailwind横向滚动包含卡片的行为异常

React Tailwind横向滚动包含卡片的行为异常

&lt;div className=&#39;  w-5/6 h-64 mx-auto mt-24 mb-20 &#39;&gt;
    &lt;p className=&#39;h-12 text-xl font-medium ml-5 flex items-center drop-shadow-lg&#39;&gt;Animations&lt;/p&gt;
    &lt;div className=&#39;bg- w-full h-48 flex flex-row overflow-x-scroll scrollbar-hide&#39;&gt;
        &lt;div className=&#39;bg-blue-300 h-full w-72 ml-4 rounded-md &#39;&gt;&lt;/div&gt;
        &lt;div className=&#39;bg-blue-300 h-full w-72 ml-4 rounded-md &#39;&gt;&lt;/div&gt;
        &lt;div className=&#39;bg-blue-300 h-full w-72 ml-4 rounded-md &#39;&gt;&lt;/div&gt;
        &lt;div className=&#39;bg-blue-300 h-full w-72 ml-4 rounded-md &#39;&gt;&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

答案1

得分: 1

弹性布局的子元素默认会隐式地具有 flex-shrink: 1,这将尝试缩小子元素的主轴尺寸以适应容器。如果您希望不发生这种缩小,可以通过应用 flex-shrink: 0 类(shrink-0)来覆盖默认行为:

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

<!-- language: lang-html -->
    <script src="https://cdn.tailwindcss.com"></script>
    <div class='w-5/6 h-64 mx-auto mt-24 mb-20'>
        <p class='h-12 text-xl font-medium ml-5 flex items-center drop-shadow-lg'>Animations</p>
        <div class='bg- w-full h-48 flex flex-row overflow-x-scroll scrollbar-hide'>
            <div class='bg-blue-300 h-full w-72 ml-4 rounded-md shrink-0'></div>
            <div class='bg-blue-300 h-full w-72 ml-4 rounded-md shrink-0'></div>
            <div class='bg-blue-300 h-full w-72 ml-4 rounded-md shrink-0'></div>
            <div class='bg-blue-300 h-full w-72 ml-4 rounded-md shrink-0'></div>
        </div>
    </div>

<!-- end snippet -->

注意:只有 shrink-0 类被应用到子元素上,才能覆盖默认的 flex-shrink: 1 行为。

英文:

Children of a flex layout implicitly have flex-shrink: 1 by default, which will attempt to shrink the main axis dimension of the children to fit the container. If you would rather this shrinking not happen, override flex-shrink: 1 by applying flex-shrink: 0 via the shrink-0 class:

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

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

&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;

&lt;div class=&#39;w-5/6 h-64 mx-auto mt-24 mb-20&#39;&gt;
    &lt;p class=&#39;h-12 text-xl font-medium ml-5 flex items-center drop-shadow-lg&#39;&gt;Animations&lt;/p&gt;
    &lt;div class=&#39;bg- w-full h-48 flex flex-row overflow-x-scroll scrollbar-hide&#39;&gt;
        &lt;div class=&#39;bg-blue-300 h-full w-72 ml-4 rounded-md shrink-0&#39;&gt;&lt;/div&gt;
        &lt;div class=&#39;bg-blue-300 h-full w-72 ml-4 rounded-md shrink-0&#39;&gt;&lt;/div&gt;
        &lt;div class=&#39;bg-blue-300 h-full w-72 ml-4 rounded-md shrink-0&#39;&gt;&lt;/div&gt;
        &lt;div class=&#39;bg-blue-300 h-full w-72 ml-4 rounded-md shrink-0&#39;&gt;&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定