Position an absolutely centered element with responsive design and fixed margins

I am facing an issue with a CSS banner that is positioned absolutely and has responsive width and height based on viewport size. The challenge is that the element is centered on desktop and tablet devices, but not on mobile when the banner's width starts to shrink.

Unfortunately, I cannot rely on the traditional margin: 0 auto; method because fixed margins are required for the banner to function properly.

The code in question is as follows:

HTML:

<h1 class="banner">A Simple CSS Banner</h1>

CSS:

@import url(https://fonts.googleapis.com/css?family=Rye);

body 
{ background: #eee; }

.banner {
  position: absolute;
  left: 50%;
  display: block;
  margin: 100px -200px;
  width: 400px;
  max-width: calc(100% - 150px);
  height: 60px;
  border: 1px solid #8a1;
  font: normal 30px/60px 'Rye';
  text-align: center;
  color: #451;
  background: #9b2;
  border-radius: 4px;
  box-shadow: 0 0 30px rgba(0,0,0,.15) inset,
    0 6px 10px rgba(0,0,0,.15);
}

.banner::before,
.banner::after {
  content: '';
  position: absolute;
  z-index: -1;
  left: -70px;
  top: 24px;
  display: block;
  width: 40px;
  height: 0px;
  border: 30px solid #9b2;
  border-right: 20px solid #791;
  border-bottom-color: #94b81e;
  border-left-color: transparent;
  transform: rotate(-5deg);
}

.banner::after {
  left: auto;
  right: -70px;
  border-left: 20px solid #791;
  border-right: 30px solid transparent;
  transform: rotate(5deg);
}

@media (max-width: 475px) {

  .banner {
    height: 90px;
    line-height: 45px;
  }

  .banner::before,
  .banner::after {
    border-width: 45px;
    border-right-width: 30px;
  }

  .banner::after {
    border-left-width: 30px;
    border-right-width: 45px;
  }

}

Check out the working codepen here: https://codepen.io/Adam0410/pen/QZOKdV

Your assistance is greatly appreciated!

Answer №1

Feel free to try this solution.

@import url(https://fonts.googleapis.com/css?family=Rye);

body {
  background: #eee;
}

.banner {
  position: absolute;
  left: 50%;
  transform: translate(-50%);
  display: block;
  margin: 100px 0;
  width: 400px;
  max-width: calc(100% - 150px);
  min-height: 60px;
}
.banner span {
  display: block;
  position: relative;
  z-index: 1;
  border: 1px solid #8a1;
  font: normal 30px/60px "Rye";
  text-align: center;
  color: #451;
  background: #9b2;
  border-radius: 4px;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.15) inset, 0 6px 10px rgba(0, 0, 0, 0.15);
}
.banner::before,
.banner::after {
  content: "";
  position: absolute;
  z-index: -1;
  left: -70px;
  bottom: -24px;
  display: block;
  width: 40px;
  height: 0px;
  border: 30px solid #9b2;
  border-right: 20px solid #791;
  border-bottom-color: #94b81e;
  border-left-color: transparent;
  transform: rotate(-5deg);
}

.banner::after {
  left: auto;
  right: -70px;
  border-left: 20px solid #791;
  border-right: 30px solid transparent;
  transform: rotate(5deg);
}

@media (max-width: 475px) {
  .banner {
    min-height: 90px;
    line-height: 45px;
  }

  .banner::before,
  .banner::after {
    border-width: 45px;
    border-right-width: 30px;
  }

  .banner::after {
    border-left-width: 30px;
    border-right-width: 45px;
  }
}
<h1 class="banner"><span>Try out this Awesome CSS Banner</span></h1>

Answer №2

To achieve a responsive design, Media queries can be utilized to adjust the width and margin accordingly.

For example:

@media (max-width: 475px) {
  .header{
    width: 250px; 
    margin: 50px -50px;
  }
}

Answer №3

To maintain both the top and bottom margins, use this method:

left: 0; right: 0;
margin: 100px auto;

Answer №4

To perfectly align an absolute or fixed element in the center, follow these steps:

  position: absolute;
  left: 0%;
  right: 0%;
  margin: 0 auto;

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

Utilize Regex to isolate domain names from HTML content

Is there a way to use Regex to extract domain names from this list? <option value=68385>domain1.com</option><option value=72737>domain2.com</option><option value=72739>domain3.com</option> ...

The boostrap grid system is malfunctioning and not behaving as anticipated

After reviewing the documentation, I came to understand the following: xs = small devices like phones sm = iPads and other tablets md = smaller laptops lg = big laptops and desktops <div class="col-xs-12 col-md-4 col-lg-3"> <div class=" ...

Center aligning text within an accordion button using Bootstrap 5 framework

I'm struggling to center align the text inside the accordion button while keeping the arrow on the right side Here is an example of what I'm trying to achieve: https://i.sstatic.net/2brrY.png In the Bootstrap 5 documentation, there's an e ...

I kindly request your assistance in resolving the issues with the append() and empty

Here is some code I've been working on: <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> $(document).ready(function(){ ...

The functionality of updating input values via jQuery AJAX in HTML response is currently not functioning as expected

Hello, I need some assistance. I'm encountering difficulty with jQuery in modifying HTML values that are dynamically inserted through an AJAX response. Here is the link to the JSFiddle. Below is the HTML on the page: <button id="test">test&l ...

Styling a CSS property using React

I’m having trouble figuring out how to properly format the marginLeft value in my code. I’ve tried various methods, but haven’t been successful yet. Can someone please provide assistance on escaping the dash when defining properties this way? If it ...

Exploring Videogular Integration with Angular 2 through the VgAPI

My experience with Videogular2 has been interesting as I am attempting to play and control multiple videos simultaneously. The documentation suggests using a master and slave video setup to control both videos with the same controls, but this approach lim ...

Construct a table featuring nested rows for every parent entry

Table 1 orderid customerName totalCost ---------------------------------- 1 Jonh £200.00 2 Ringo £50 Table 2 orderlineid orderid productName productPrice Quantity ----------------------------------------------- ...

What is the best method to exclude the <a> tag when displaying content in HTML?

Is there a way to hide the link within an a tag when printing? .p { display: none; } @media print { .p { display: initial; } .np { display: none; } } <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css ...

Is there a way to perform unit testing on JavaScript code that runs following the completion of a CSS transitionEnd event

I am working with a bootstrap modal window and using qunit+sinonjs (fake timers). However, I have noticed that one element (div class='modal-backdor') remains on the page despite my efforts. You can view the issue here: http://jsfiddle.net/valu ...

Creating a collapsible sidebar feature in Angular 2

Currently in the process of converting an HTML jQuery AdminLTE dashboard to Angular 2 In the past, I would hide and show the sidebar using toggle() in jQuery. Now I'm wondering how to achieve the same functionality in Angular 2. I am utilizing the ...

Troubleshooting alignment of divs and bullet styling across different web browsers

Here is some HTML code: <div class="wrapper"> <div class="LItms"> <div class="clr"> </div> <span>text con</span> <ul> <li>first</li> <li&g ...

Can a CSS rule be prevented from inheriting?

Currently, I have implemented this CSS rule in the body selector: body { text-align:center } However, it seems like this rule is inheriting down to all other text elements on the page. Is there a way to prevent CSS from applying this rule to other elem ...

Issues encountered with MP4 HEVC video playback on Chrome browser

My Wordpress website hosts videos uploaded by users in mp4 format. While it's not ideal for performance, I am doing this for a beta test period of a few months. Some of the mp4 files aren't functioning correctly, showing only sound with a black ...

Click on the canvas to fill a specific area with JavaScript

My goal is to implement a JavaScript function that will shade specific areas of canvas drawings when clicked. Below is the code I have that draws a square inside a circle. <!DOCTYPE HTML> <html> <head> </head> <body&g ...

Issue with fetching API data and sending it to the Node server

My node backend is all set up and running smoothly for GET requests, but I'm facing an issue with POST requests as the data doesn't seem to be getting sent to the backend. Here's the code snippet: fetch("http://localhost:3000/", ...

Unable to decrease the height of the textbox

Need help adjusting the height of a bootstrap textbox. <div class="form-group row"> <label class="col-xl-3">Posted Speed Limit (mph)</label><input type="text" class="col-xl-1 ps" autocomplete="off" id="sl" onkeydown="return key ...

Rails pagination will only show the link to the current page

Is there a way to show only the current page link with the << Previous link hidden when on the first page and next >> link hidden when on the last page? It should look like this: On the first page: 1 | next >> On the last page (with 4 ...

Issue with custom checkbox not changing when bootstrap collapse occurs

I've been searching for a solution and experimenting with different methods, but I just can't seem to make it work. Below is a CodePen example that showcases a custom checkbox with a toggle function to show/hide a div when checked, along with a ...

Use a fixed height to set a background image on your website

My landing page design is currently boxed, but I want to set a background image that spans the entire width of the page. I chose to set the background image to the body element to achieve this effect. body { background-image: url("bg-image.png") !import ...