Tailwind css fixed width not working with other element in flex - tailwind-css

I have to place two column side by side. One of these columns has a fixed width. I am really trying hard to understand why the fixed width in one column is not working on Tailwind CSS flexbox.
I have the following code:
<div class="flex flex-row justify-between items-start shrink-0">
<p class="mt-4 h-14 overflow-hidden leading-7">This title pushed back the next element</p>
<div class="p-1 w-16 h-16 text-white text-center">
<p class="text-sm">05</p>
<p class="text-3xl leading-none font-bold">JUNE</p>
</div>
</div>
Tailwind Version: 3.0.23
Output:
Inspect Element:
Expected output:

Actually when using flex , it shrinks itself if not explicitly specified for responsive design
Refer the tailwind flex-shrink documentation https://tailwindcss.com/docs/flex-shrink
It says to use the flex-shrink-0 css property i.e shrink-0 class to avoid the width from being shrinked due to variable width size.
So code should look like:
<div className="flex flex-row justify-between items-start">
<p className="mt-4 h-14 overflow-hidden leading-7">
This title pushed back the next element and this is the long text to
check the overflow of the text and lets see if this actually works
</p>
<div className="p-1 w-16 h-16 text-white text-center shrink-0 bg-purple-500">
<p class="text-sm">JUN</p>
<p class="text-3xl leading-none font-bold">05</p>
</div>
And the output looks like :

<div class="flex flex-row justify-between items-start shrink-0">
<p class="mt-4 h-14 w-[70%] overflow-hidden leading-7">This title pushed back the next element</p>
<div class="p-1 w-[15%] h-16 text-white text-center">
<p class="text-sm">05</p>
<p class="text-3xl leading-none font-bold">JUNE</p>
</div>
</div>
Maybe like this work

You can simply use flex properties here while keeping the width same.
<script src="https://cdn.tailwindcss.com"></script>
<div class="flex flex-row justify-between items-start ">
<div>
<p class="py-2 px-2 h-14 overflow-hidden leading-6">This title pushed back the next element
his title pushed back the next element.</p>
</div>
<div class="px-2 w-16 h-16 mr-2 text-white bg-purple-500 text-center">
<p class="text-sm">June</p>
<p class="text-3xl leading-none font-bold">05</p>
</div>
</div>

Related

Tailwind CSS technique for a horizontal line with words in the middle [duplicate]

I want to create a <hr> divider using Tailwind CSS, but instead of the horizontal rule spanning the entire width of the page unbroken, I want to add some text in the middle.
For example:
----------------------------------- Continue -----------------------------
I can't find anything like this in the documentation. How can I achieve this effect?
If necessary, I can change the HTML to something other than an <hr> element. That was just the only way I knew how to create a horizontal rule.
You can use this HTML syntax to create what you want :
<div class="relative flex py-5 items-center">
<div class="flex-grow border-t border-gray-400"></div>
<span class="flex-shrink mx-4 text-gray-400">Content</span>
<div class="flex-grow border-t border-gray-400"></div>
</div>
See here the result: https://play.tailwindcss.com/65JULZ5XES
Try this instead...
<script src="https://cdn.tailwindcss.com"></script>
<div class="relative py-4">
<div class="absolute inset-0 flex items-center">
<div class="w-full border-b border-gray-300"></div>
</div>
<div class="relative flex justify-center">
<span class="bg-white px-4 text-sm text-gray-500">Continue</span>
</div>
</div>
Example
https://play.tailwindcss.com/Yx4OmAlBsv
Yes, you can do this:
<script src="https://cdn.tailwindcss.com"></script>
<h1 class="text-center overflow-hidden before:h-[1px] after:h-[1px] after:bg-black
after:inline-block after:relative after:align-middle after:w-1/4
before:bg-black before:inline-block before:relative before:align-middle
before:w-1/4 before:right-2 after:left-2 text-xl p-4">Heading
</h1>
Try this.
Need to adjust the height, margin, bg-color, etc. to fit your website.
https://play.tailwindcss.com/FzGZ1fMOEL
<div class="h-5 border-b-4 border-black text-2xl text-center">
<span class="bg-white px-5">your text</span>
</div>

TailwindCSS fixed width property not working on resolution change

I have this div that's supposed to represent a button next to an input. Ideally on resolution change, the input element's width shrinks but the button stays the same height and width.
<div className="flex flex-row rounded-lg border border-white border-solid hover:cursor-pointer py-2 px-2 w-28">
<AddIcon></AddIcon>
<div className="flex font-sans text-base font-medium">Add site</div>
</div>
I set the w-28 property thinking it could achieve this. However, the button changes width if I shrink the window or screen it appears on, and I'm not sure why.
This is the full component:
<div id="settings" className="w-3/4 h-5/6 bg-blue-300 self-center">
<div id="settings-content" className="flex flex-col self-center mx-5">
<div id="header" className="flex flex-col mt-5">
<div id="title" className="font-sans font-bold text-3xl">Block Sites</div>
</div>
<div id="subtitle" className="font-sans text-base font-medium text-neutral-600">Visiting these sites will stop point acquisition until you reset the timer</div>
<div id="block-site-add-list" className="flex flex-row mt-16 justify-between bg-amber-400 shrink-0">
<div className="flex flex-row rounded-lg border border-white border-solid hover:cursor-pointer py-2 px-2 w-28">
<AddIcon></AddIcon>
<div className="flex font-sans text-base font-medium">Add site</div>
</div>
<input className="ml-5 flex-grow bg-red-500"></input>
</div>
</div>
</div>
This is what the button looks like with a smaller screen, when both words should be on the same line:
You could try adding whitespace-nowrap to your label div to ensure that the text doesn't wrap to two lines. Since you have your elements (button and input) wrapped in another flex, you can add shrink-0 to your wrapper div to keep its size.
For example:
<div id="block-site-add-list" className="flex flex-row ...">
<div className="shrink-0 flex flex-row ...">
<AddIcon></AddIcon>
<div className="whitespace-nowrap font-sans text-base font-medium">Add site</div>
</div>
<input className="ml-5 grow bg-red-500"></input>
</div>

Using flexbox (tailwindcss: flex-1) how do I add background colour to WHOLE element not just the text?

Looking for some help with my tabs design. I use a flexbox which has two parts. The top part for the main content, and the bottom for 'tab' buttons that would change the main content data based on the tab selected.
I want to be able to hover over the button and I want the WHOLE div housing this element to be highlighted (Different background colour) and when selected to be permanently highlighted with a different colour.
Currently when I hover ONLY the text inside the div gets highlighted + margin specified. How do I make the whole div change the background colour?
CodeSandbox
<template>
<!-- CONTAINER -->
<div
class="flex-col bg-gray-200 w-11/12 rounded-xl border-2 m-auto pt-2 mt-2"
>
<!-- MAIN CONTENT -->
<div class="flex pb-2 bg-green-500 py-10">MAIN CONTENT</div>
<!-- Tabs -->
<div class="flex w-full text-sm text-center items-center bg-blue-500">
<div class="flex-1 px-1">
<div class="flex justify-center hover:bg-red-500 cursor-pointer">
<span class="my-4">Tab 1</span>
</div>
</div>
<div class="flex-1 px-1">
<div class="flex justify-center hover:bg-red-500 cursor-pointer">
<span class="my-4">short tab 2</span>
</div>
</div>
<div class="flex-1 px-1">
<div class="flex justify-center hover:bg-red-500 cursor-pointer">
<span class="my-4">Long Tab name 3</span>
</div>
</div>
<div class="flex-1 px-1">
<div class="flex justify-center hover:bg-red-500 cursor-pointer">
<span class="my-4">This is super Long Tab name 4</span>
</div>
</div>
<div class="flex-1 px-1">
<div class="flex justify-center hover:bg-red-500 cursor-pointer">
<span class="my-4">This is SUPER extra long tab name 5</span>
</div>
</div>
</div>
</div>
</template>
You need to make some minor adjustments to get this working since you are using a flexbox, where each div (Let's call it div-1) holds a sub-div which further contains the span element, we can convert div-1 to a flexbox with the direction set to be a column and further apply the flex-grow so that it occupies the full height of the parent div, along with flex-1 which already exists, this is because this div resides under the div whose flex-direction is specified to be row .flex-grow grows on y-axis when the flex-direction is column. The sub-div again needs to have the properties, align-items:center, justify-content:center, and flex-grow:1, so that the text within is centered and again, this div to occupy the full height of its parent div(div1 as per our example).
<template>
<!-- CONTAINER -->
<div
class="flex-col bg-gray-200 w-11/12 rounded-xl border-2 m-auto pt-2 mt-2"
>
<!-- MAIN CONTENT -->
<div class="flex pb-2 bg-green-500 py-10">MAIN CONTENT</div>
<!-- Tabs -->
<div class="flex text-sm text-center bg-blue-500">
<div class="flex -1 flex flex-col flex-grow hover:bg-red-500">
<div class="flex justify-center items-center flex-grow">
<span>Tab 1</span>
</div>
</div>
<div class="flex-1 flex flex-col flex-grow hover:bg-red-500">
<div class="flex justify-center items-center flex-grow">
<span class="my-4">short tab 2</span>
</div>
</div>
<div class="flex-1 flex flex-col flex-grow hover:bg-red-500">
<div class="flex justify-center items-center flex-grow">
<span class="my-4">Long Tab name 3</span>
</div>
</div>
<div class="flex-1 flex flex-col flex-grow hover:bg-red-500">
<div class="flex justify-center items-center flex-grow">
<span class="my-4">This is super Long Tab name 4</span>
</div>
</div>
<div class="flex-1 px-1 flex flex-col flex-grow hover:bg-red-500">
<div class="flex justify-center items-center flex-grow">
<span class="my-4">This is SUPER extra long tab name 5</span>
</div>
</div>
</div>
</div>
</template>

How to align a div within a div using flex and flex-col Tailwind CSS

I'm trying to vertically center the div with a white border onto the div with a green background. Colors just for show. I cannot figure out why justify-center and items-center do not work. I know I can use margin to achieve what I want but I know there's a better way. I have tried referencing this SO post but have had no luck:Reference Post
Also, if there is a better solution to implement what I want I'm all ears.
My code:
<div class="flex flex-col min-h-screen bg-coolGray-900">
<div class="w-screen justify-center items-center h-screen bg-green-500 ">
<div class="text-center text-white border-2 ">
<h1 class="mb-10 text-6xl font-bold font-lato">Message Here</h1>
<h2 class="mb-8 text-2xl font-semibold font-lato">More Detailed Message Here</h2>
<button class="mx-auto btn btn-learn">Learn More</button>
</div>
</div>
</div>
You haven't displayed your container class as flex, this is why your justifies aren't working.
Add the class flex to your bg-green-500 class.
<link href="https://unpkg.com/tailwindcss#^2/dist/tailwind.min.css" rel="stylesheet"/>
<section>
<div class="flex flex-col min-h-screen bg-coolGray-900">
<div class="flex w-screen justify-center items-center h-screen bg-green-500 ">
<div class="text-center text-white border-2 w-screen">
<h1 class="mb-10 text-6xl font-bold font-lato">Message Here</h1>
<h2 class="mb-8 text-2xl font-semibold font-lato">More Detailed Message Here</h2>
<button class="mx-auto btn btn-learn">Learn More</button>
</div>
</div>
</div>
</section>

Set Fixed Gap on Flex Tailwind

This is my design with flex. You can see both the div's are having different gap in between. How Can I make it equal?
I just want to set a fixed width for the second column however I am not able to achieve it in Tailwind.
Code:
<div className='bg-gray-1000 rounded shadow border border-opacity-50 text-white flex flex-col lg:flex-row w-7/12 mx-auto justify-between p-2 mb-2 items-center'>
<div className='bg-primary rounded w-14 h-14 flex items-center'>
<img src={props.logo}/>
</div>
<div className='flex flex-col'>
<div>{props.title}</div>
<div className='flex'>
<div><img className='w-6 mt-1' src={props.flag}/></div>
<div><FaInfoCircle className='inline ml-2 text-xl'/></div>
<div><button className={`ml-2 rounded px-4 py-0 text-sm uppercase bg-${props.color}-600`} disabled>{props.mode}</button></div>
</div>
</div>
<div className='flex flex-col'>
<div><FaCalendar className='inline mr-2 text-xl'/> {props.date}</div>
<div className='mt-1'><FaClock className='inline mr-2 text-xl'/> {props.time}</div>
</div>
<div className='flex flex-col'>
<div> <FaUsers className='inline mr-2 text-xl'/> {props.vs}</div>
<div className='mt-1'><FaUsers className='inline mr-2 text-xl'/> {props.slots} Slots</div>
</div>
<div className='flex flex-col'>
<div><AiFillDollarCircle color='green' className='inline mr-2 text-xl'/></div>
<div className='mt-1'><AiFillTrophy className='inline mr-2 text-xl md:text-2xl'/> {props.prize}</div>
</div>
</div>
I would love to get some help on this, cheers.
Add flex-grow class to block with title and set space-x-{value} class for parent so it will add gap between child elements. Demo
P.S. You have bg-${props.color}-600 which will be purged in a production mode

Resources