如果父元素的宽度太小,如何让<h1>个元素占据下一行?

EXAMPLE:

Each word here is an <h1>element. They are spaced by their parent <div> using display: flex; and gap: 1rem;. enter image description here

Now, the width of the parent <div> is reduced, so how can I make the other <h1> elements occupy the next line like the image below? enter image description here

CODE:

<div className="flex gap-4">
    {
        text.split(" ").map((e, i) => (
            <h1 key={i} className="text-color-1 text-3xl font-bold text-center lg:text-left md:text-5xl lg:text-7xl whitespace-pre-line inline">
                {e}
            </h1>
        ))
    }
</div>

推荐答案

考虑通过flex-wrap Tailwind实用程序类将flex-wrap: wrap应用于<div>元素:

const text = 'This is an example';

ReactDOM.createRoot(document.getElementById('app')).render(
  <div className="flex gap-4 flex-wrap">
    {text.split(' ').map((e, i) => (
      <h1
        key={i}
        className="text-color-1 text-3xl font-bold text-center lg:text-left md:text-5xl lg:text-7xl whitespace-pre-line inline"
      >
        {e}
      </h1>
    ))}
  </div>
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.3.0/umd/react.production.min.js" integrity="sha512-ZA7si8ER+VYYp/DB0qHGoBUUHww+JcrRLyNhlLHpZOnvWZppaUp7TgKT3VRb7TUhOwE9I4RTdGesYYYM0U73CQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.3.0/umd/react-dom.production.min.js" integrity="sha512-ZezdfPGeFUpe5Och082E7dtOcO51kZtQmF7skp34UfOkROcAWXhQZ4pHODBBXDF01jSjskWy5B9z8z2GGTqsPw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.tailwindcss.com/3.4.3"></script>

<div id="app"></div>

Css相关问答推荐

Tailwind CSS:如何使图像从迪夫中弹出

Chrome/MacOS和Firefox/MacOS能够使用两种字体的字形渲染字符串,但Chrome/iOS和Safari/Any不能

如何将bslib::card()中的扩展按钮移到右上方,其中full_screen = TRUE?

如何改变图标的 colored颜色 时悬停在

可折叠/可展开的数据表行:如何使Shadcn数据表中的可折叠内容全宽?

防止表格水平拉伸整个Blazor页面

CSS Grid我不想要的东西

将CSS栅格行高设置为其余视口高度

如何通过重复的网格区域来简化网格布局?

如何向 Highcharts 迷你图添加具有淡入淡出效果的线性渐变?

如何增加 PrimeFlex 网格的间隙?

:required 或 [required] CSS Select 器

Tailwind:如何设置网格的自动填充?

使用 Flex Box Tailwind CSS 的元素不相等

使用之前和之后为锚标签添加画笔描边效果

bootstrap 模式对话框中的谷歌 map 自动完成结果

如何计算所需的色调旋转以生成特定 colored颜色 ?

如何停止 setTimeout 循环?

为什么我的 div 边距重叠,我该如何解决?

HTML5 和边框