Tips on Creating a Responsive Absolutely Positioned Div for Various Screen Sizes

I'm currently working on a website and I've come across an issue regarding the responsiveness of an absolutely positioned div across various screen sizes. Let me provide some context and share what I have attempted so far:

Issue: Within the header section of my HTML, I have placed a "call to action" (CTA) div with absolute positioning. This div includes multiple buttons. While the placement looks great on a specific screen size, it doesn't adapt properly when I resize the browser window or view it on different devices.

Code Sample: Below is a snippet of the relevant portion from my HTML and CSS:

<header class="min-h-[25vh] max-w-full relative">
    <div class="absolute top-[0px] md:top-[-155px] min-h-full">
        <video src="../../../videos/video-accueil.mp4" autoplay muted playsinline loop></video>
    </div>
    <div class="relative">
        <div class="flex flex-col md:flex-row md:space-x-7 items-center space-y-4 justify-around z-20 absolute bottom-[-300px] md:space-y-0 md:bottom-[-380px] left-0 right-0 mx-auto w-fit">
            <!-- Buttons here -->
        </div>
    </div>
</header>

Answer №1

Here is an example based on your code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive CTA Example</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <header class="header">
    <div class="background-video">
      <video src="https://file-examples.com/storage/fe4996602366316ffa06467/2017/04/file_example_MP4_1920_18MG.mp4" autoplay muted playsinline loop></video>
    </div>
    <div class="cta-container">
      <div class="cta">
        <button>Button 1</button>
        <button>Button 2</button>
      </div>
    </div>
  </header>

  </body>
</html>

Customize the styles according to your needs and screen size using the following CSS:

/* Style the header */
.header {
  min-height: 50vh; /* Adjust height as required */
  position: relative; /* Necessary for absolute positioning */
}

/* Style the video background */
.background-video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%; /* Ensure video fills the header */
  overflow: hidden; /* Prevent video overflow */
}

.background-video video {
  width: 100%;
  height: 100%; /* Stretch video to fit container */
  object-fit: cover; /* Maintain aspect ratio (if needed) */
}

/* Style the CTA container (optional for centering) */
.cta-container {
  display: flex; /* Turn parent container into a flexbox (optional) */
  justify-content: center; /* Horizontally center the CTA div (optional) */
  position: absolute;
  top: 67%; /* 80% from the top of the header (Adjust this as needed Important) */
  bottom: 10%; /* 10% from the bottom of the header  (Adjust this as per needed Important) * */
  left: 0;
  right: 0;
  z-index: 2; /* Ensure CTA div is above background video */
}

/* Style the CTA div */
.cta {
  width: fit-content; /** adjust if necessary */
  height: fit-content; /** adjust if necessary */
  background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent background */
  padding: 1rem; 
  border-radius: 5px; 
  color: #fff; 

}

/* Style CTA buttons (adjust as necessary) */
.cta button {
  padding: 0.5rem 1rem;
  border: none;
  color: inherit;
  background-color: #333; /* Adjust button color */
  cursor: pointer;
}

/* Media queries for further adjustments (optional) */
@media (max-width: 768px) {
  .cta {
    /* Adjust styles for smaller screens if needed */
  }
}

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

How does CSS affect the size and performance of a website?

Examining the content of this css file (focusing on the last 5 lines specifically): #page section .s_1 { overflow:hidden; width:435px; float:left;} #page section .s_1 h1 { font-size:36pt; color:#001744; line-height:1.1; margin-bottom:64px;height:107px;} # ...

Display all attribute connections for a product in Prestashop

I have updated the products in my shop to include different combinations. These combinations involve a 'packaging' attribute with two options: a 100 units box and a 200 units box, each with its own unique reference number. On the product page, t ...

Tips for updating the background color of a select option

I'm looking to customize the background color of select options in my Bootstrap 4 form. Can anyone provide guidance on how to achieve this? `` <div class="form-group "> <select class="form-control > <option va ...

Issues encountered when using Vue Transition in conjunction with v-autocomplete

Attempting to conditionally display or hide a selection text field using Vue's Transition feature, but experiencing an issue where the text field appears and disappears without any transition effect. <template> <Transition v-if="is ...

Using jQuery to toggle visibility on click within WordPress

Looking for a way to make three buttons change color on hover and display different content when clicked? You're not alone! Despite searching through tutorials and forums, the solution remains elusive. The buttons are structured like this: <div i ...

Add distinctive formatting for the final element that is not the last child

Presented with a fascinating challenge, I find myself with an ever-changing number of .child.red children. I am in need of referencing the last .child.red element dynamically using styles. Although I have attempted to achieve this on my own, I am curious a ...

Customizing the font style on a CSS changing table

Below is the CSS code snippet I am using: table.Table12 { border:1px solid blue; } table.Table12 td { border:1px solid blue; width:200px;} table.Table12 a:link { font-weight: bold;} Additionally, here is the HTML code: <table class="Table12" align="r ...

Can child elements' visibility be controlled by their parent's class using only CSS?

Is it possible to achieve this functionality using only CSS: When the parent element has a "hidden" class, its child elements with an "a" class should be hidden For example: <div class="parent"> <div class="a"></div> // Visible ...

Is it possible to display one division on top of another with a transparent opacity effect in HTML?

I'm struggling with designing web pages and currently working on a page on codepen.io using code from this link. <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1 ...

Ensure the placeholder remains front and center, growing in size as it moves vertically

.inpsearch{ border:2px solid #bbb; border-radius:14px; height:29px; padding:0 9px; } .inpsearch::-webkit-input-placeholder { color: #ccc; font-family:arial; font-size:20px; } <input type='search' class='inpsea ...

Show or hide the expand/collapse button based on the height of the container

Looking for a way to hide content in a Div if it's taller than 68px and display an expand option? The challenge lies in detecting the height of the responsive Div, especially since character count varies. I attempted using PHP to count characters bu ...

The timeline aesthetic can be achieved solely through the use of HTML, CSS, and potentially some Bootstrap functionalities

https://i.sstatic.net/EsDAE.png Up to this point, I've only been able to make it this far. I'm using a sample code from w3school https://i.sstatic.net/G1FcQ.png I've been attempting to get the year in the same position as in the image, bu ...

`CSS Content Placeholder Issue When Used Alongside JavaScript`

Let me explain a bit, I have a master page named UserProfile.master which has a content placeholder linked to UserProfileWall.aspx. Now, I am trying to incorporate some additional JavaScript and a second CSS file in the userprofilewall page. However, whene ...

Is it possible to customize the color of the placeholder and clear-icon in the ion-search bar without affecting

I am working with two ion-search bars and I need to customize the placeholder and clear icon color for just one of them. <ion-searchbar class="search-bar" placeholder="search"></ion-searchbar> My goal is to target a specific i ...

Looking to update the background color of the items in a sliding door menu using CSS

I'm having trouble changing the background color of my list items in a sliding door menu using CSS. I've successfully changed the left border to green and the text to white, but the background color remains unchanged. Below is the code I'm u ...

Sometimes the last column might not be visible in an HTML table within a collapsed Bootstrap element while using Chrome browser

I've encountered a strange issue when trying to wrap an HTML table inside a collapsing div element using Bootstrap. The last column is not consistently displayed in Chrome (44.0.2403.107 m) depending on the window width, but it works fine in Firefox. ...

Clone content upon pressing the Enter key

I am facing an issue with my editable container where I have a div inside it. Whenever I press enter, the div duplicates, resulting in two divs stacked on top of each other. The container has contenteditable set to true, which might be causing this problem ...

Tips on applying the "activate" class to a Bootstrap navbar in Angular 2 when implementing page anchor navigation

As I work on my single-page website using Angular 2 and Bootstrap 4, I have successfully implemented a fixed navbar component that remains at the top of the page. Utilizing anchor navigation (#id), the navbar allows smooth scrolling to different sections o ...

Issue with changing the opacity of a Javascript button style is not functioning correctly

The button's opacity used to change gradually in increments of 0.1 every 500 milliseconds, but now it instantly changes to 0.9 without the delay. Issue: Despite being placed within a window load handler and all elements loading properly, the loop is ...

Jquery plugin experiencing a malfunction

I am encountering an issue with my custom plugin as I am relatively new to this. My goal is to modify the properties of div elements on a webpage. Here is the JavaScript code I am using: (function($) { $.fn.changeDiv = function( options ) { var sett ...