Pranta
Pranta

Reputation: 3705

Html body is not getting full width in tailwindcss

I am new in tailwindcss. But I am having an issue here.

Look at the screenshots, the background color is not applying in the navbar. Also the whole html body is not getting full width in medium and small screens.

What I don't understand is that I still haven't used any of the responsive classes from tailwindcss like md, sm or lg. But there are serious problems with width and I tried using tailwind class of w-full and w-screen. None of them work. Here's the screenshot of the problem screenshot-2 screenshot-3 screenshot-4 screenshot-1

You can find the code here: https://codesandbox.io/s/focused-curran-jdyup

Thanks in Advance.

Edit:

ok, look at this GIF, guys.

for_github2

I am trying to recreate the problem in Tailwind Play. But I couldn't. Then I noticed that the same line of code works perfectly in Tailwind Play but not with nextjs. I don't know what the problem here is but I shared both the Tailwind Play and NextJS code below. Tailwind Play:

<div class="flex justify-between items-center p-5 bg-indigo-800 text-white">
      <div class="ml-16">
        <div class="flex items-center">
          <div class="">
            <h4 class="tracking-widest uppercase">GrayScale</h4>
          </div>
          <div class="lg:hidden">
            <button
              type="button"
              class="text-gray-400 mt-1 hover:text-white focus:text-white focus:outline-none"
            >
              <svg
                class="w-6 h-6"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth="2"
                  d="M4 6h16M4 12h16M4 18h16"
                ></path>
              </svg>
            </button>
          </div>
        </div>
      </div>
      <div class="mr-16">
          <a
            key={link.label}
            class="p-2 pr-2 uppercase tracking-widest font-semibold hover:bg-indigo-900 hover:text-gray-400 rounded-md"
          >
            Home
          </a>
          <a
            key={link.label}
            class="p-2 pr-2 uppercase tracking-widest font-semibold hover:bg-indigo-900 hover:text-gray-400 rounded-md"
          >
            Home
          </a>
          <a
            key={link.label}
            class="p-2 pr-2 uppercase tracking-widest font-semibold hover:bg-indigo-900 hover:text-gray-400 rounded-md"
          >
            Home
          </a>
          <a
            key={link.label}
            class="p-2 pr-2 uppercase tracking-widest font-semibold hover:bg-indigo-900 hover:text-gray-400 rounded-md"
          >
            Home
          </a>
          <a
            key={link.label}
            class="p-2 pr-2 uppercase tracking-widest font-semibold hover:bg-indigo-900 hover:text-gray-400 rounded-md"
          >
            Home
          </a>
      </div>
    </div>

NextJS Code:

export default function IndexPage() {
  return (
    <div className="flex justify-between items-center p-5 bg-indigo-800 text-white">
      <div className="ml-16">
        <div className="flex items-center">
          <div className="">
            <h4 className="tracking-widest uppercase">GrayScale</h4>
          </div>
          <div className="lg:hidden">
            <button
              type="button"
              className="text-gray-400 mt-1 hover:text-white focus:text-white focus:outline-none"
            >
              <svg
                className="w-6 h-6"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth="2"
                  d="M4 6h16M4 12h16M4 18h16"
                ></path>
              </svg>
            </button>
          </div>
        </div>
      </div>
      <div className="mr-16">
        <a className="p-2 pr-2 uppercase tracking-widest font-semibold hover:bg-indigo-900 hover:text-gray-400 rounded-md">
          Home
        </a>
        <a className="p-2 pr-2 uppercase tracking-widest font-semibold hover:bg-indigo-900 hover:text-gray-400 rounded-md">
          Home
        </a>
        <a className="p-2 pr-2 uppercase tracking-widest font-semibold hover:bg-indigo-900 hover:text-gray-400 rounded-md">
          Home
        </a>
        <a className="p-2 pr-2 uppercase tracking-widest font-semibold hover:bg-indigo-900 hover:text-gray-400 rounded-md">
          Home
        </a>
        <a className="p-2 pr-2 uppercase tracking-widest font-semibold hover:bg-indigo-900 hover:text-gray-400 rounded-md">
          Home
        </a>
      </div>
    </div>
  );
}

Upvotes: 17

Views: 30758

Answers (9)

Randeep Rana
Randeep Rana

Reputation: 11

If you are using typescript, make sure you are creating a component with the .tsx extension, for me this fixed the issue

Upvotes: -1

ujjwal limbu
ujjwal limbu

Reputation: 1

It seems that tailwind automatically apply the breakpoints at certain pixels as stated in the official docs of tailwind.

[And as you can see those breakpoints were automatically applied even though I hadn't specified those in my code at all. And you can monitor this behavior, yourself if you deselect all those media query, the intended effect will be reflected.

And I found that if you just use max-width-full, then you'll get the desired result as it'll over-ride the default behavior. ]1

Upvotes: 0

M Imam Pratama
M Imam Pratama

Reputation: 1325

I face exactly this same issue. Looks like it's not tailwindcss specific. This answer solves it for me:

Place min-w-fit to the top element or the direct child of the body.

<div class="min-w-fit lg:min-w-0 lg:max-w-5xl lg:mx-auto">
  <p>something</p>
</div>

Just note that min-width always override max-width so when you need max-width e.g. in larger screen, you might need to readjust the min-width.

Update

Turns out the root of the problem is overflowing flex items, which in my case, is caused by long words e.g. URL. Note: Normally the default min-width of an HTML element is 0 but for flex items it's auto. The thing is, when the min-width is reached (which in my case is decided by the longest word), as they overflow as the screen gets smaller, the min-width:auto keep the element from shrinking and instead shrinks everything else. It shrinks the fixed font size to keep the element width and because it also shrinks the parent element, it's true that the element is actually overflowing (check with inspect element).

Solution:

Add min-width: 0 to the outmost container that's overflowing its parent. If the overflow is caused by long words, don't forget to handle the text overflow with text-overflow or overflow-wrap.

Source:

Upvotes: 1

Shahin Akbari
Shahin Akbari

Reputation: 29

You can use "w-screen" class instead container and it solved :)

<div className='w-screen'>Header</div>

Upvotes: 2

n0thingLeft
n0thingLeft

Reputation: 61

Try adding max-w-full to the container that you want full width.

Upvotes: 6

Richard Dev
Richard Dev

Reputation: 1170

This is an issue with the chrome developer tool. At first, this worked.

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>  

However, the problem returned.

SOLUTION: It turned out the page had too much content, and chrome was not waiting to set the viewport size until all the content had loaded. Once I removed a large table of data. All started working as needed.

I will load the content in ajax and add pagination.

Another thing worked by class="hidden" on my long table of data and the after load. Remove the "hidden" class.

Upvotes: 6

Babis Patatoukos
Babis Patatoukos

Reputation: 9

I had the same problem. What did the trick for me was 'max-w-max' put that to your container (not the body tag), whick makes the max width equal to the max width of the content (so no more white on the right).

Upvotes: 0

Gerard Brull
Gerard Brull

Reputation: 1062

I faced the same problem and found the answer here.

Basically, a 'meta' viewport element gives the browser instructions on how to control the page's dimensions and scaling. So you need to add:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

Upvotes: 0

user12352274
user12352274

Reputation:

enter image description here

enter image description here

You should consider new responsive scenarios for these columns overflowing containers. Or you should give the container overflow: hidden. The problem is with them.

Upvotes: 1

Related Questions