Lädt...

🔧 How to Vertically Align Content with Tailwind CSS Across a Full-Screen Div


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Vertical alignment can often be a challenge in web design, but with Tailwind CSS, you can easily align elements in the center of the screen.

This quick guide will walk you through the steps to vertically align content within a full-screen div using Tailwind CSS, complete with nicely styled examples.

CSS meme

Step 1: Setting Up Tailwind CSS

First, make sure you have Tailwind CSS set up in your project. If you're starting from scratch, you can use the following CDN link in your HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vertical Alignment with Tailwind CSS</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
    <!-- Your content will go here -->
</body>
</html>

If you're using a build tool like Webpack or a framework like Next.js, refer to the Tailwind CSS installation guide for the appropriate setup.

Step 2: Creating the Full-Screen Div

To create a full-screen div, we'll use Tailwind's utility classes. We'll start by creating a div that spans the full viewport height and width. Here's a simple example:

<div class="min-h-screen flex items-center justify-center bg-gray-100">
    <!-- Content goes here -->
</div>
  • min-h-screen: This class sets the minimum height of the div to the full height of the viewport.
  • flex: This makes the div a flex container.
  • items-center: This vertically centers the content inside the flex container.
  • justify-center: This horizontally centers the content inside the flex container.
  • bg-gray-100: This adds a light gray background color to the div.

Step 3: Adding Content

Now, let's add some content inside our full-screen div. We'll use a simple card component as our example:

<div class="min-h-screen flex items-center justify-center bg-gray-100">
    <div class="bg-white p-8 rounded-lg shadow-lg">
        <h1 class="text-2xl font-bold mb-4">Vertically Aligned Content</h1>
        <p class="text-gray-700">This content is centered both vertically and horizontally using Tailwind CSS.</p>
    </div>
</div>
  • bg-white: This sets the background color of the card to white.
  • p-8: This adds padding to the card.
  • rounded-lg: This rounds the corners of the card.
  • shadow-lg: This adds a large shadow to the card.
  • text-2xl: This sets the font size of the heading to 2xl.
  • font-bold: This makes the heading bold.
  • mb-4: This adds a bottom margin to the heading.
  • text-gray-700: This sets the color of the paragraph text to a dark gray.

Step 4: Styling the Content

To make our example more visually appealing, we can add some additional styling. Let's enhance the card with a more polished look:

<div class="min-h-screen flex items-center justify-center bg-gradient-to-r from-blue-500 to-purple-600">
    <div class="bg-white p-8 rounded-lg shadow-2xl transform hover:scale-105 transition-transform duration-300">
        <h1 class="text-3xl font-extrabold mb-6 text-transparent bg-clip-text bg-gradient-to-r from-green-400 to-blue-500">
            Vertically Aligned Content
        </h1>
        <p class="text-gray-800 text-lg">
            This content is centered both vertically and horizontally using Tailwind CSS.
        </p>
    </div>
</div>
  • bg-gradient-to-r from-blue-500 to-purple-600: This creates a background gradient for the full-screen div.
  • shadow-2xl: This adds a larger shadow to the card.
  • transform hover:scale-105 transition-transform duration-300: This adds a scaling effect when the card is hovered over, with a smooth transition.
  • text-3xl: This sets the font size of the heading to 3xl.
  • font-extrabold: This makes the heading extra bold.
  • text-transparent bg-clip-text bg-gradient-to-r from-green-400 to-blue-500: This creates a gradient text effect for the heading.
  • text-lg: This sets the font size of the paragraph text to large.

Conclusion

By using Tailwind CSS's utility classes, you can easily vertically align content within a full-screen div. The flexbox utilities provided by Tailwind make it simple to center content both vertically and horizontally with just a few classes.

For even more styling options and to create beautiful designs effortlessly, check out the DevDojo Tails Tailwind CSS builder. It's a fantastic tool to help you with your workflow and create stunning designs with Tailwind CSS.

...

🔧 CSS Mask Div with Another Div- Complete Guide


📈 44.83 Punkte
🔧 Programmierung

🐧 How Can I Center Text (Horizontally and Vertically) Inside a div


📈 43.5 Punkte
🐧 Linux Tipps

🔧 How to Make Parent Div Activate Styling of Child Div for Hover and Active States


📈 37.72 Punkte
🔧 Programmierung

🐧 div in HTML: Alles Wichtige zur Verwendung des div-Containers


📈 37.72 Punkte
🐧 Server

🐧 How to Left and Right Align Text Within a Div in Bootstrap


📈 36.55 Punkte
🐧 Linux Tipps

🔧 How to Access Direct Children of a Div in Tailwind CSS v3


📈 36.28 Punkte
🔧 Programmierung

🎥 Jira Align Product Overview | Jira Align Product Demos | Atlassian


📈 35.38 Punkte
🎥 Video | Youtube

🔧 Tailwind CSS tutorial #29: Vertical Align


📈 35.11 Punkte
🔧 Programmierung

🔧 Tailwind CSS tutorial #25: Text Align


📈 35.11 Punkte
🔧 Programmierung

🔧 How to upgrade an Astro JS project from Tailwind CSS v3 to Tailwind CSS v4 ( Alpha )


📈 34.85 Punkte
🔧 Programmierung

🔧 Configurare Tailwind CSS: Guida all'Inizializzazione | Setting Up Tailwind CSS: Initialization Guide


📈 34.85 Punkte
🔧 Programmierung

🔧 How to Center Text Vertically with CSS


📈 31.75 Punkte
🔧 Programmierung

🐧 Flip/Mirror an Image Horizontally + Vertically With CSS


📈 31.75 Punkte
🐧 Linux Tipps

🔧 Choosing the Right CSS Approach: Tailwind CSS vs Bootstrap vs Vanilla CSS


📈 31.64 Punkte
🔧 Programmierung

🔧 Introduction of CSS, What is CSS, Why we use CSS and How CSS describe the HTML elements


📈 28.44 Punkte
🔧 Programmierung

🐧 How to Apply Hover and Other States with Align Self in Tailwind


📈 28 Punkte
🐧 Linux Tipps

🔧 Material TailWind - combining Material Design and Tailwind CSS


📈 27.73 Punkte
🔧 Programmierung

🔧 How to Customize Tailwind CSS in Next.js 15 Without tailwind.config.js?


📈 27.73 Punkte
🔧 Programmierung

🔧 Fix "Tailwind CSS IntelliSense Plugin Not Working in Tailwind v4"


📈 27.73 Punkte
🔧 Programmierung

🔧 How to Set Up Tailwind CSS v4 Using Tailwind CLI


📈 27.73 Punkte
🔧 Programmierung

🔧 Tailwind CSS: Tailwind Cost


📈 27.73 Punkte
🔧 Programmierung

🔧 Mastering Tailwind CSS: Overcome Styling Conflicts With Tailwind Merge and clsx


📈 27.73 Punkte
🔧 Programmierung

🔧 Mastering Tailwind CSS: Overcome Styling Conflicts with Tailwind Merge and clsx


📈 27.73 Punkte
🔧 Programmierung

📰 CISA Releases Plan to Align Cybersecurity Across Federal Agencies


📈 27.03 Punkte
📰 IT Security Nachrichten

📰 US Looks to Align Security Across Government


📈 27.03 Punkte
📰 IT Security Nachrichten

🎥 Pivot of Persevere? Manage work, time, and money across portfolios with Jira Align


📈 27.03 Punkte
🎥 Video | Youtube

🔧 How to center a Div in HTML and CSS?


📈 25.97 Punkte
🔧 Programmierung

🔧 TidyCoder day night loading only css/HTML, and single div


📈 25.97 Punkte
🔧 Programmierung

🔧 CSS: Position fixed div center container


📈 25.97 Punkte
🔧 Programmierung

🔧 3 Different Ways to Center A Div in CSS


📈 25.97 Punkte
🔧 Programmierung

🔧 DOOM...*rendered* using a single DIV and CSS! 🤯🔫💥


📈 25.97 Punkte
🔧 Programmierung

🔧 How to Center a Div : CSS Tips and Tricks


📈 25.97 Punkte
🔧 Programmierung