Achieving the perfect alignment for an element while adjusting its z-index

Page Structure:

div#help
-- div
  -- ul
    -- li
    -- li
-- div
  -- ul
    -- li
    -- li
-- div.message_shower
  -- p  /*tiny `x` for close button*/
  -- div.content

When a user clicks on any list item <li>, a div.message_shower will open displaying some text.


FIDDLE (Feel free to disregard the JS and HTML tab)


Issue: I am trying to vertically and horizontally center the div.message_shower on the screen. Despite using percentage units, it is not working as expected. Setting margin: 0 auto; did not produce the desired result either. It is important to note that the .message_shower has a z-index of 2.

In a normal screen - centered properly:

Slightly smaller screen - not centered properly:

This is how it should appear properly on a small screen:


Assistance needed specifically for Google Chrome.

Note: The .message_shower should stay within the visible screen area even when scrolling down, without getting hidden.

UPDATE: I will wait for a week, and if no answer successfully centers the div (@mutil's answer only widens the div), I will accept @mutil's answer.

Answer №1

Why not try adding this CSS:

width: calc(100% - 200px);
margin: 0 70px;

and removing the line left: 30%; from the .message_shower class?

This adjustment will make the element more flexible in size while still keeping it centered.

Check out the updated jsFiddle link


If you prefer a fixed width for the div, simply set both left and right to zero and margin to auto.

Take a look at the demo here

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

I am experiencing an issue on my webpage where clicking on the login button does not seem

My website code includes: <form method="POST" action="{% url 'lawyerdash' %}"> {% csrf_token %} Username<input type="text" name="username"><br> Password<input type="password" name="password" ><br> & ...

What is the best way to align a div using position:absolute in a body that has a width of 100%, while maintaining equal spacing on the left and

How can I position a <div> with position:absolute so that it is spaced 20px from the left and right sides while maintaining a width: of 100%? Currently, I have applied margin: 0 20px 0 20px, but I am wondering if there is a way to achieve this withou ...

The spacing in a nested flexbox form gets distorted due to CSS3 styling

Working on creating a flexbox form where labels and inputs are on the same line. Have successfully achieved the desired layout using flex properties like flex-basis. However, encountered an issue when trying to nest the flexbox within an element with a fi ...

The transparent DIV/waiting message may not be displayed in IE10

Here is a DIV container that serves as a transparent background with a loading message. This is the HTML code for the DIV container: <div id="generatingexcel" style="display:none; position: fixed; width: 100%;height: 100%; z-index: 999; top: 0; left: ...

Creating a stylish curved right border within a CSS3 box

Is there a way to create a border-right effect within a box using CSS3, similar to the one shown in this image? ...

Is the modal popup displayed in the center of the screen when the submit button is clicked?

My bootstrap modal popup is not working when I click the submit button. I can only use Bootstrap4, JS, and jQuery3.0. JS Fiddle function check() { var inputs = document.getElementsByTagName("input"); var inputs = document.getElementsByTagName("sele ...

React Native: The behavior of the 'top' property is consistent, while the 'bottom' property is not performing as anticipated

Within my React Native application, I have implemented the following code: <View style={{ width: 50, height: 50, borderWidth: 1, }} > <View style={{ width: 5, height: 5, backgroundColor: 'red', top: 10, ...

Implementing the 'active' class on a hyperlink in CodeIgniter: A step-by-step guide

My template is divided into three sections: header, footer, and content. The main menu links are located in the header section. I am trying to apply a CSS class 'active' to the hyperlink that is selected. I have written jQuery code to achieve thi ...

Transferring data-id to a Bootstrap modal without the use of jQuery

I've come across numerous questions similar to this, all suggesting the same solution of using JQuery. However, incorporating JQuery into my Reactjs project just to pass an id to a modal is something I wish to avoid. Below is my code snippet : <! ...

Leveraging an HTML form for searching and fetching data from MySQL Workbench using PHP

I'm currently working on fetching data from my database by utilizing an HTML form and establishing a PHP connection to my SQL workbench. The data retrieval is successful, as it appears at the top of my webpage. However, I am facing issues with executi ...

Unable to display PHP response in a div using AJAX - issue persisting

Hey there! I'm currently working on a project where I want the form submission to load a response into a div without refreshing the page. I've looked at various examples of ajax forms with PHP online, but haven't been able to solve my issue ...

The footer div appears distinct on various pages despite being the same

After fixing one issue with the footer div, I seem to have created another problem :) The footer is currently broken on this page: You can see that the footer extends across the entire page. Now, compare it to this page: On this page, the footer spans ...

Retrieve the border color using Selenium WebDriver

Hey everyone, I'm currently trying to retrieve the border color of an extjs 4.2 form control text field using the getCssValue method. However, I'm having trouble getting the value as it's coming back blank. Below is a sample of my code that ...

Animate the transition of the previous element moving downward while simultaneously introducing a new element at the top

I currently have a hidden element called "new element" that is controlled by v-if. My goal is to create a button labeled "display" that, upon clicking, will reveal the new element on top after sliding down an old element. How can I achieve this using CSS ...

How can I modify the class attribute in JavaScript when the onclick event is triggered

Hello, I am looking to achieve this functionality using JavaScript only (not jQuery). How can you accomplish something similar like the code below but using pure JavaScript instead of jQuery? <body> <div class="wysiwyg">text......text...< ...

Develop a JavaScript function to declare variables

I am currently attempting to develop a small memory game where the time is multiplied by the number of moves made by the player. Upon completion of all pairs, a JavaScript function is executed: function finish() { stopCount(); var cnt1 = $("#cou ...

The validation functionality fails to handle dynamically generated inputs

I am facing an issue with validating dynamically created form fields (name, age) using JavaScript. Currently, the validation script only works for the first record of the age field, while the rest are not being validated. Here is the code snippet: <sc ...

Encountering blank space in the first table row after a page break when creating a PDF using dompdf

Encountered a strange bug while working with dompdf for PDF generation. The issue arises when a page break occurs within a table. Subsequent to the break, the first tr on the next page displays an odd empty space on the left side (as depicted in the image ...

Image tag src displaying a React icon causes a malfunction

I am currently following a guide on ReactJS and encountered an error when trying to use an Img tag as directed in the tutorial. <Img src={keepLogo} alt="Google keep logo" /> This resulted in the following error: Warning: Invalid value for ...

Swap out a picture with one that has been uploaded

Can anyone help me with an issue I'm having regarding image uploading? I am currently using an HTML file upload form along with a jQuery function to check the file type of any image selected by the user. My goal is to replace the existing image on a w ...