To ensure the text appears over the image while still avoiding the use of absolute positioning, you can modify the layout slightly while still maintaining the z-index functionality. We can achieve this by applying a flex layout on the parent and using z-index on the text div without using absolute or relative positioning.
Here is the revised code:
<!DOCTYPE html>
<html lang="en" data-theme="night">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.6.0/dist/full.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.tailwindcss.com"></script>
<link rel="shortcut icon" href="Images/UpdatedFavIcon.png" type="image/x-icon">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300..800;1,300..800&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Sour+Gummy:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
<title>Hotel Management System</title>
<link rel="stylesheet" href="CSS/style.css">
<style>
.sour-gummy {
font-family: "Sour Gummy", sans-serif;
font-weight: 400;
}
.text-gradient {
background: linear-gradient(to right, #5ee0b5, #ead588, #df81b0);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
font-family: Mulish;
font-size: 64px;
font-weight: 900;
line-height: 86px;
}
/* Ensures that the image is responsive */
.carousel-img {
width: 100%;
height: auto;
object-fit: cover;
}
/* Flex container to stack image and text */
.image-wrapper {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 410px; /* Adjust to the desired height */
overflow: hidden; /* Ensures no overflow */
position: relative; /* Allows z-index to work correctly */
}
.text-overlay {
color: transparent; /* Text color is transparent until the gradient is applied */
background: linear-gradient(to right, #5ee0b5, #ead588, #df81b0);
-webkit-background-clip: text;
background-clip: text;
font-family: Mulish;
font-size: 64px;
font-weight: 900;
text-align: center;
line-height: 1.2;
z-index: 1; /* Put above the image */
display: flex;
justify-content: center; /* Center text horizontally */
align-items: center; /* Center text vertically */
width: 100%; /* Occupy full width */
height: 100%; /* Occupy full height */
}
</style>
</head>
<body class="flex flex-col items-center text-center">
<div class="image-wrapper">
<img class="carousel-img rounded-3xl" src="Image/Hotel.jpg" alt="Hotel">
<div class="text-overlay">
<h1 class="text-4xl sm:text-5xl md:text-6xl lg:text-7xl">Welcome to Our Hotel</h1>
</div>
</div>
<div class="my-6">
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4 gap-4 px-10">
<button class="btn btn-primary text-xl mx-2 text-white">
<a href="customer.html">Customer</a>
</button>
<button class="btn btn-primary text-xl mx-2 text-white">
<a href="reservation.html">Reservation</a>
</button>
<button class="btn btn-primary text-xl mx-2 text-white">
<a href="payment.html">Payment</a>
</button>
<button class="btn btn-primary text-xl mx-2 text-white">
<a href="invoice.html">Invoice</a>
</button>
</div>
</div>
</body>
</html>
Key Adjustments:
-
Positioning for Z-Index:
- The
.image-wrapper retains position: relative; to set a stacking context for the .text-overlay.
- The
.text-overlay gains a z-index of 1, ensuring it overlays the image properly.
-
Flexbox for Centering:
- The
.text-overlay class is styled as a flex container with justify-content: center; and align-items: center; to center the text both vertically and horizontally.
-
Removal of Absolute Positioning:
- No absolute positioning is used, keeping the design clean and maintaining responsiveness across various devices.
This setup will ensure that the text remains centered over the image and is responsive to different screen sizes.