元素不填满屏幕宽度固定的问题 tailwind

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

fixed element not filling the full width of the screen tailwind

问题

这是打开 Modal 的代码行。我想使宽度延伸至全屏宽度:

<div className="fixed flex inset-0 bg-black bg-opacity-80 blur2" />

我尝试使用 items-stretchw-full,但没有成功。我猜这是因为它是固定定位的。

英文:

I'm trying to fill the width of a fixed element in Tailwind to full width.

here's a screenshot of the modal:
Modal Screenshot

please ignore the text inside it as I'm still testing stuff out.

my code React:

import { Dialog, Transition } from &#39;@headlessui/react&#39;
import { Fragment, useState } from &#39;react&#39;
import { Link } from &#39;react-router-dom&#39;

export default function Modal2() {
  let [isOpen, setIsOpen] = useState(true)

  function closeModal() {
    setIsOpen(false)
  }

  function openModal() {
    setIsOpen(true)
  }

  return (
    &lt;&gt;
      &lt;div className=&quot; absolute right-1 top-5 p-2&quot;&gt;
        &lt;button
          type=&quot;button&quot;
          onClick={openModal}
          className=&quot;flex   rounded-3xl pl-6 pr-6  bb  p-2 &quot;
        &gt;
          Open dialog
        &lt;/button&gt;
      &lt;/div&gt;

      &lt;Transition appear show={isOpen} as={Fragment}&gt;
        &lt;Dialog as=&quot;div&quot; className=&quot;relative  z-10 &quot; onClose={closeModal}&gt;
          &lt;Transition.Child
            as={Fragment}
            enter=&quot;ease-out duration-300&quot;
            enterFrom=&quot;opacity-0&quot;
            enterTo=&quot;opacity-100&quot;
            leave=&quot;ease-in duration-200&quot;
            leaveFrom=&quot;opacity-100&quot;
            leaveTo=&quot;opacity-0&quot;
          &gt;
            &lt;div className=&quot;fixed  flex  inset-0 bg-black bg-opacity-80 blur2 &quot; /&gt;
          &lt;/Transition.Child&gt;

          &lt;div className=&quot;fixed top-20 right-2  overflow-y-auto &quot;&gt;
            &lt;div className=&quot;flex   justify-center p-4 text-center&quot;&gt;
              &lt;Transition.Child
                as={Fragment}
                enter=&quot;ease-out duration-300&quot;
                enterFrom=&quot;opacity-0 scale-95&quot;
                enterTo=&quot;opacity-100 scale-100&quot;
                leave=&quot;ease-in duration-200&quot;
                leaveFrom=&quot;opacity-100 scale-100&quot;
                leaveTo=&quot;opacity-0 scale-95&quot;
              &gt;
                &lt;Dialog.Panel className=&quot;mb-10  w-full max-w-md  transform overflow-hidden  rounded-3xl bg-white p-6 text-left align-top shadow-xl transition-all&quot;&gt;
                  &lt;Dialog.Title
                    as=&quot;h3&quot;
                    className=&quot;text-lg font-medium leading-6 text-gray-900&quot;
                  &gt;
                    Lorem, ipsum dolor.
                  &lt;/Dialog.Title&gt;
                  &lt;div className=&quot;mt-2&quot;&gt;
                    &lt;p className=&quot; flex flex-col text-md text-gray-500&quot;&gt;
                      &lt;Link to={&quot;/about&quot;} className=&quot;py-1 &quot;&gt;dada&lt;/Link&gt;
                      &lt;span className=&#39;h  bg-red-600&#39;&gt;&lt;/span&gt;
                      &lt;Link className=&quot;py-1 &quot; to={&quot;&quot;}&gt;dada&lt;/Link&gt;
                      &lt;span className=&#39;h  bg-red-600&#39;&gt;&lt;/span&gt;
                      &lt;Link className=&quot;py-1 &quot; to={&quot;&quot;}&gt;dada&lt;/Link&gt;
                      &lt;span className=&#39;h  bg-red-600&#39;&gt;&lt;/span&gt;
                      &lt;Link className=&quot;py-1 &quot; to={&quot;&quot;}&gt;dada&lt;/Link&gt;
                    &lt;/p&gt;
                  &lt;/div&gt;

                  &lt;div className=&quot;mt-4&quot;&gt;
                    &lt;button
                      type=&quot;button&quot;
                      className=&quot;inline-flex justify-center rounded-md border border-transparent bg-blue-100 px-4 py-2 text-sm font-medium text-blue-900 hover:bg-blue-200 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2&quot;
                      onClick={closeModal}
                    &gt;
                      Got it, thanks!
                    &lt;/button&gt;
                  &lt;/div&gt;
                &lt;/Dialog.Panel&gt;
              &lt;/Transition.Child&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/Dialog&gt;
      &lt;/Transition&gt;
    &lt;/&gt;
  )
}

this is the line that opens the Modal. I want to make the width stretch to full-screen width. :

  &lt;div className=&quot;fixed  flex  inset-0 bg-black bg-opacity-80 blur2 &quot; /&gt;

I would appreciate any help.

I tried to use items-stretch and w-full. didn't work. I guess its because it's fixed.

答案1

得分: 1

我无法精确重现你的问题,所以这可能不准确。

如果我正确理解你的目标,你可能正在定位错误的元素。Dialog 组件包含了你的模态框,因此应该在 Dialog 组件上添加 display: flexjustify-content: center 来水平居中模态框。

因此,你应该在 Dialog 组件上添加 flexjustify-center 类,还可以添加 items-center 类来垂直居中。

[...]
&lt;Transition appear show={isOpen} as={Fragment}&gt;
        &lt;Dialog as=&quot;div&quot; className=&quot;relative z-10 flex justify-center&quot; onClose={closeModal}&gt;
[...]
英文:

I couldn't reproduce your problem exactly, so this might be off.

If I understand your goal correctly, you're targeting the wrong element. The Dialog component contains your modal, so that's the component which should
have display: flex and justify-content: center to centralize the modal horizontally.

So you should add the classes flex and justify-center to the Dialog component. You can also add items-center to centralize it vertically.

[...]
&lt;Transition appear show={isOpen} as={Fragment}&gt;
        &lt;Dialog as=&quot;div&quot; className=&quot;relative z-10 flex justify-center&quot; onClose={closeModal}&gt;
[...]

huangapple
  • 本文由 发表于 2023年2月14日 07:22:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442087.html
匿名

发表评论

匿名网友

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

确定