为什么我的内部 div 无法滚动?(TailwindCSS)

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

Why is my inner div not scrollable? (TailwindCSS)

问题

Sure, here is the translated part of your code:

const Chat = () => {
  return (
    <div className="flex flex-col items-center h-screen justify-between">
      <div className="flex justify-center w-full">
        <h1 className="text-3xl font-bold my-5">Golfdommer</h1>
      </div>
      <div id="messages" className="flex flex-col w-full h-full justify-end overflow-y-auto"> // <-- this div
          {messages.map((message, index) => {
            if (message.role === "user") {
              return <UserMessage key={index} text={message.content} />;
            }
            return <BotMessage key={index} text={message.content} />;
          })}
      </div>
      <form
          onSubmit={handleInputSubmit}
          className="flex flex-row w-full mb-5 mt-2"
        >
          <textarea
            className="border-2 rounded-md p-3 w-full mr-1"
            type="text"
            value={inputValue}
            placeholder="Ask me something..."
            onChange={(e) => setInputValue(e.target.value)}
          />
          <button type="submit" className="ml-1 py-2 px-4 rounded-md bg-green-500">
            <PaperAirplaneIcon className="w-6 h-6 text-white" />
          </button>
        </form>
    </div>
  );
};

I hope this helps!

英文:

I have the following layout made with React and TailwindCSS. It is supposed to be a chatbot, which I am making while learning React. I need the inner div with id=&quot;messages&quot; to be scrollable, so the user can scroll through the message history. Though, at the moment the div is not scrollable, and older messages disappear behind the h1 and out of view of the screen. How can I make it scrollable?

const Chat = () =&gt; {
  return (
    &lt;div className=&quot;flex flex-col items-center h-screen justify-between&quot;&gt;
      &lt;div className=&quot;flex justify-center w-full&quot;&gt;
        &lt;h1 className=&quot;text-3xl font-bold my-5&quot;&gt;Golfdommer&lt;/h1&gt;
      &lt;/div&gt;
      &lt;div id=&quot;messages&quot; className=&quot;flex flex-col w-full h-full justify-end overflow-y-auto&quot;&gt; // &lt;-- this div
          {messages.map((message, index) =&gt; {
            if (message.role === &quot;user&quot;) {
              return &lt;UserMessage key={index} text={message.content} /&gt;;
            }
            return &lt;BotMessage key={index} text={message.content} /&gt;;
          })}
      &lt;/div&gt;
      &lt;form
          onSubmit={handleInputSubmit}
          className=&quot;flex flex-row w-full mb-5 mt-2&quot;
        &gt;
          &lt;textarea
            className=&quot;border-2 rounded-md p-3 w-full mr-1&quot;
            type=&quot;text&quot;
            value={inputValue}
            placeholder=&quot;Ask me something...&quot;
            onChange={(e) =&gt; setInputValue(e.target.value)}
          /&gt;
          &lt;button type=&quot;submit&quot; className=&quot;ml-1 py-2 px-4 rounded-md bg-green-500&quot;&gt;
            &lt;PaperAirplaneIcon className=&quot;w-6 h-6 text-white&quot; /&gt;
          &lt;/button&gt;
        &lt;/form&gt;
    &lt;/div&gt;
  );
};

答案1

得分: 1

The justify-content: flex-end; being applied from the justify-end class interferes with the scrollable calculation. Removing the class makes the content scrollable.

英文:

The justify-content: flex-end; being applied from the justify-end class interferes with the scrollable calculation. Removing the class makes the content scrollable.

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

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

&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;flex flex-col items-center h-screen justify-between&quot;&gt;
  &lt;div class=&quot;flex justify-center w-full&quot;&gt;
    &lt;h1 class=&quot;text-3xl font-bold my-5&quot;&gt;Golfdommer&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div id=&quot;messages&quot; class=&quot;flex flex-col w-full h-full overflow-y-auto&quot;&gt;
    &lt;UserMessage&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla commodo vitae magna sit amet molestie. Phasellus dictum nec nulla nec dictum. Phasellus rhoncus libero id arcu tempus dapibus. Aenean ac facilisis lorem. Phasellus efficitur justo enim, a dapibus libero semper eget. Quisque id turpis vel risus aliquet condimentum et pellentesque elit. Morbi commodo orci augue. Aenean elit turpis, rhoncus ut neque non, pulvinar sagittis libero. Sed congue odio vitae orci condimentum, at cursus lacus feugiat. Donec accumsan enim vel leo cursus aliquam. Fusce eu tellus quam. Aliquam ut est nibh. Cras congue sit amet dui vel imperdiet.&lt;/UserMessage&gt;
    &lt;BotMessage&gt;Quisque et magna pulvinar, eleifend justo in, luctus nunc. Suspendisse iaculis imperdiet velit non posuere. Sed interdum, turpis vitae ultrices aliquet, nibh nibh ultrices nulla, non sollicitudin augue risus ac leo. Aliquam at ante ipsum. Mauris porttitor eu urna eget rutrum. Proin efficitur suscipit lorem, eu volutpat ipsum rutrum convallis. Duis ultricies mi eros, vel mattis enim tempor placerat. Vivamus nec magna sed tellus tincidunt placerat quis id tellus. Mauris sed turpis in sapien gravida faucibus non non orci. Maecenas ac ante porta, auctor diam vehicula, tincidunt sem. Suspendisse potenti. Fusce finibus, augue at mollis sagittis, nulla elit cursus dui, eu mollis tortor est eget augue.&lt;/BotMessage&gt;
    &lt;UserMessage&gt;Morbi venenatis nulla sit amet leo mattis, ut posuere est egestas. In suscipit, tortor nec fermentum molestie, odio turpis condimentum odio, tempor ultricies neque nibh lacinia urna. Nam tempus fermentum vehicula. Donec vulputate sem vel lorem molestie, hendrerit dapibus urna ornare. Interdum et malesuada fames ac ante ipsum primis in faucibus. Duis aliquam quis urna posuere accumsan. Suspendisse scelerisque enim enim, id tempus mi tempor sit amet. Suspendisse suscipit mauris odio, vitae tincidunt magna pretium vitae.&lt;/UserMessage&gt;
    &lt;BotMessage&gt;Ut aliquam elit ac lacus finibus varius. Proin bibendum nisi id pretium condimentum. Integer blandit, nibh in congue porta, dolor purus condimentum augue, pellentesque mattis nunc mauris vitae leo. Sed lorem est, porta vel nulla a, viverra dapibus ante. Phasellus consequat commodo dui, non varius felis. Nulla viverra dictum pharetra. Aenean dapibus congue sem non facilisis. Ut eget bibendum massa. Donec porttitor sit amet odio non condimentum. Maecenas libero purus, auctor ut ullamcorper et, condimentum eu orci. In malesuada tempor quam sed porttitor. Cras convallis nisi at purus luctus, ornare tincidunt eros volutpat. Proin tempus magna molestie purus condimentum varius. Maecenas a massa quis ante bibendum accumsan et non elit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent dictum cursus suscipit.&lt;/BotMessage&gt;
  &lt;/div&gt;
  &lt;form class=&quot;flex flex-row w-full mb-5 mt-2&quot;&gt;
    &lt;textarea class=&quot;border-2 rounded-md p-3 w-full mr-1&quot; type=&quot;text&quot; placeholder=&quot;Ask me something...&quot;&gt;&lt;/textarea&gt;
    &lt;button type=&quot;submit&quot; class=&quot;ml-1 py-2 px-4 rounded-md bg-green-500&quot;&gt;
            &lt;PaperAirplaneIcon class=&quot;w-6 h-6 text-white&quot;&gt;PaperAirplaneIcon&lt;/PaperAirplaneIcon&gt;
          &lt;/button&gt;
  &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月19日 22:42:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055833.html
匿名

发表评论

匿名网友

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

确定