Achieving a child div to occupy the entire available space when the parent is specified as `mx-auto` can be accomplished using Vue and Tailwind CSS

Sorry if this seems like a basic question, I'm still getting the hang of Vue and tailwind.

In my parent template, it looks like this:

<template>
  <div class="mx-auto mt-4 max-w-3xl">
    <slot></slot>
  </div>
</template>

Above and below this template, there are other elements. One way this template is used is by adding a child in the slot, like so:

<div class="inline-flex h-full w-full items-center justify-center bg-green-500 px-6 py-6">
    This is the text
</div>

The goal here is:

  • The child (green panel) should take up all available free space on the screen
  • It shouldn't cause a scroll to appear
  • Ideally, I don't want to modify the parent template since it's used in various places

However, using h-full creates a small frame instead:

https://i.sstatic.net/AWv2y.png

I've also tried using h-screen, but that didn't achieve the desired effect either. How can I set the height here to get the desired outcome, like this:

https://i.sstatic.net/GakBy.png

(taking up all available space without causing a scroll)

Answer №1

After reviewing the code you shared, it appears that setting the parent element to h-screen is causing overflow due to the margin created by the mt-4 class, which is not accommodated for by the 100vh of h-screen. Switching the margin to padding should resolve this issue and give you the desired outcome. I have provided a working example in this tailwind play link for reference.

I hope this explanation helps clarify things for you.

Answer №2

Your child is not receiving the h-full property because the parent element also lacks this property. Since you are unable to modify the styling of the parent template, one solution is to create a container outside the parent with full height and set a property on the outer container to ensure all its immediate children match the height of the parent.

Let me simplify this in basic HTML:

<!-- Container -->
<div class="h-full overflow-auto [&>*]:h-full bg-blue-600">
  <!-- Parent cannot be altered-->
  <div class="mx-auto mt-2 max-w-3xl">
  <!-- Child -->
    <div class="inline-flex h-full w-full items-center justify-center bg-green-500 px-6 py-6">
       This is the content
    </div>
  </div>
</div>

Live Example: https://play.tailwindcss.com/pOfeGcaeSG.

In the code, [&>*]:h-full instructs all immediate children to have the same full height.

Addressing the scrollbar issue: When setting the parent's height to 100% of the container, keep in mind that it does not account for the margin of the parent. One workaround is adjusting mt-2 to pt-2 within the parent (if possible).

Additionally, make sure the container div occupies the entire height by adding height: 100% to HTML and body:

html, body{
  height: 100%;
}

Note: While using h-screen may appear as an easy fix to fill an element to the whole viewport, it may not always be the best approach, especially in intricate nested layouts. h-screen limits the height to the viewport only.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method for calculating income and expenses within a JavaScript expense tracker?

I'm facing an issue where I can't add the value of a checked radio button to an object in a different function. Even though there are no errors, nothing gets added. My project involves creating an expense tracker, and adding either income or exp ...

Using Vuelidate with Vue 3, vue-class-component, and TypeScript combination

Is there anyone who has successfully implemented Vuelidate with Vue 3 using the Composition API? Although Vuelidate is still in alpha for Vue 3, I believe that if it works with the Composition API, there must be a way to make it work with classes as well. ...

Error - Invalid Link in strapi Image URL

I need some assistance with a question I've been struggling to solve. I'm currently developing a front-end using VueJs and making API calls to my Strapi-managed back-office. Everything is working well with string content, but I am facing issues w ...

Troubleshooting: Issues with Angular form validation functionality

I am completely new to Angular and attempting to create a signup form. Despite following tutorials, the form I've built doesn't seem to be validating properly. Below is the code that I have been using: <div class="signup-cont cont form-conta ...

Applying Tailwind styles to dynamically inserted child nodes within a parent div

Currently, I am in the process of transitioning my website stacktips.com from using Bootstrap to Tailwind CSS. While initially, it seemed like a simple task and I was able to replace all the static HTML with tailwind classes successfully. However, now I ha ...

Showing a solo item from an array in HTML using Angular

How can I display the account name instead of the accountId property value from my list of transaction items? I have a list of transactionitems with an accountId property, and all accounts are loaded in the accounts$ array. However, I am having trouble us ...

Is there a way to apply a class to the main div once the checkbox has been clicked?

Whenever the checkbox is checked, it should add a class to the main div. Additionally, if I try to check another checkbox, the previously checked one should have its class removed and the new one should have the class added. Here is the code snippet for r ...

Extract the HTML field value from an AJAX drop-down selection, rather than relying on user input

I have a field that allows users to select from a dropdown list using the following code: $(function() { $('.serial_number').live('focus.autocomplete', function() { $(this).autocomplete("/admi ...

Revamping HTML Label 'for' with JavaScript: Unveiling Effective Techniques

I'm facing an issue with changing the target of a label that is associated with a checkbox using JavaScript. Here's the code I have: <input type="checkbox" id="greatId" name="greatId"> <label id="checkLabel" for="greatId">Check Box&l ...

Why is the prefer-const rule in VueJS app not disabled?

While working on my @vue/cli app, I encountered an eslint error : 117:11 error 'resArray' is never reassigned. Use 'const' instead prefer-const pointing at line : let resArray = [] To disable this error, I added the ...

What is the best way to prevent vue cli3 build from adding vue.js.min to my chunk-vendors.js bundle?

Hey there! I'm currently starting a new project from the ground up and utilizing the default create project option in Vue using CLI 3. So far, everything is going smoothly but I've come across an issue with the chunk-vendors.js file that contains ...

Difficulty with navigation on Chrome for Android (also impacting Bootstrap's website and all other sites utilizing Bootstrap design)

While working on creating a responsive website using Bootstrap, I encountered an unusual issue with navigation specifically in Chrome on Android. After some investigation, I found that the same problem exists on Bootstrap's own website. To see the i ...

HTML drop-down menu malfunctioning/missing from view

I am encountering an issue where the drop-down menu is visible, but when I try to make it disappear and then hover over it again to bring it back, it does not reappear. Here is the CSS code I am using: * { margin: 0; padding: 0; } body { back ...

What is the best way to align a logo in a log-in screen?

I am having an issue with centering the logo on my login form. The "signin" text is centered perfectly, but the logo always stays at the top left corner. I don't understand why the logo is not centered. If anyone has a solution to propose, I would gre ...

When a fresh tile is loaded in Mapbox, an event is triggered

As I work with the Mapbox GL JS API to manipulate maps, I find myself wondering if there is an event that can inform me whenever a new tile HTTP request is made. My code snippet looks like this: map.on("dataloading", e => { if(e.dataType ...

Discover the position of a dynamically added element

Is there a way to identify the specific dynamically added checkbox that was clicked, whether by index or name? I came across a similar question with a solution in this JSFiddle: JSFiddle Instead of just displaying "clicked", I would like it to show someth ...

jquery has a strange behavior where the dialog window will cover up the scrollbar when the main

Currently, I am utilizing jQuery dialog to display a dialog window. I have managed to position it at the bottom left corner as desired. However, when the main window contains a scrollbar, the dialog ends up overlapping with the scrollbar instead of alignin ...

Unable to align span vertically using font-style "Luckiest Guy" in CSS

I have encountered an issue with vertically centering a span using the font-style "Luckiest Guy". https://i.sstatic.net/Lz8o3.png I attempted to use display: flex;align-items: center; on the span, but it did not work. App.vue <template> <div ...

Issue with importing styles within a media query

I am having trouble importing a style that is based on a media query. When I try to import the styles, it doesn't seem to work. However, if I directly include the styles within the media query itself, they show up on the page. For reference, you can ...

What are the methods for differentiating between a deliberate user click and a click triggered by JavaScript?

Social media platforms like Facebook and Twitter offer buttons such as Like and Follow to allow users to easily engage with content. For example, on Mashable.com, there is a Follow button that, when clicked, automatically makes the user follow Mashable&ap ...