Perfectly Aligning Image at the Center of the Screen

I'm having trouble centering a loading image on my screen. It seems to be centered closer to the bottom rather than in the middle. Any suggestions on how I can adjust it to be more centered vertically?

Edit: I also want it to look good on mobile devices

https://i.sstatic.net/Ejeb4.png

Here is the CSS and HTML code:

<style>
    input[type=file]{
        float:left;
    }
    .loader {
        border: 16px solid #f3f3f3; /* Light grey */
        border-top: 16px solid #3498db; /* Blue */
        border-radius: 50%;
        width: 100px;
        height: 100px;
        animation: spin 2s linear infinite;
    }

    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }
    .bd-example-modal-lg .modal-dialog{
      position: absolute;
      left: 50%;
      top: 50%;
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
    }

    .bd-example-modal-lg .modal-dialog .modal-content{
        background-color: transparent;
        border: none;
    }
</style>

<div id="loading" class="modal fade bd-example-modal-lg" data-backdrop="static" data-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-sm">
        <div class="modal-content h-100 row align-items-center vertical-center-row" style="width: 48px">
            <span class="loader"></span>
         </div>
     </div>
</div>

Answer №1

To enhance the display of the image, I included a background which can be used to create custom dialogs in browsers. If you wish to remove this feature, simply delete the <div> with the id of dialog_bg. Alternatively, if you prefer nothing to be clickable behind the image, adjust the opacity of the background to rgba(44,44,44,.7).

#dialog_bg {
  position: fixed;
  top: 0%;
  left: 0%;
  width: 100%;
  height: 100%;
  margin-top: 0%;
  /* Set to a negative number 1/2 of your height*/
  margin-left: 0%;
  /* Set to a negative number 1/2 of your width*/
  background: rgba(44, 44, 44, .7);
  -webkit-border-bottom-left-radius: 10px;
  -webkit-border-bottom-right-radius: 10px;
  -moz-border-radius-bottomleft: 10px;
  -moz-border-radius-bottomright: 10px;
  border-bottom-left-radius: 10px;
  border-bottom-right-radius: 10px;
  -webkit-border-top-left-radius: 10px;
  -webkit-border-top-right-radius: 10px;
  -moz-border-radius-topleft: 10px;
  -moz-border-radius-topright: 10px;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}

#dialog_box {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 30em;
  height: 23em;
  margin-top: -11.5em;
  /* Set to a negative number 1/2 of your height*/
  margin-left: -15em;
  /* Set to a negative number 1/2 of your width*/
  background: rgba(44, 44, 44, .7);
  -webkit-border-bottom-left-radius: 10px;
  -webkit-border-bottom-right-radius: 10px;
  -moz-border-radius-bottomleft: 10px;
  -moz-border-radius-bottomright: 10px;
  border-bottom-left-radius: 10px;
  border-bottom-right-radius: 10px;
  -webkit-border-top-left-radius: 10px;
  -webkit-border-top-right-radius: 10px;
  -moz-border-radius-topleft: 10px;
  -moz-border-radius-topright: 10px;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}
<!DOCTYPE html>
<html>

<head>
  <title>
    Testing text
  </title>
  <link href="default.css" type="text/css" rel="Stylesheet" />
  <script language="JavaScript">
    function CloseErrorBox() {
        document.getElementById('dialog_bg').style.display = 'none';
    }
  </script>
</head>

<body style="height: 100%; width: 100%;">
  <div id="dialog_bg">
    <div id="dialog">
      <div style="height: 100%; min-height: 100px; width: auto; min-width: 10%; background-color: #454545;">
        <span id="error_title" style="width: 100%; height: auto; color: #cacaca; font-size: 25pt; text-align: center;">
            ALERT
          </span>
        <div style="height: 100%; width: auto; background-color: #cacaca; margin-top: 10px; padding: 10px;">
          <div id="error_text" style="color: #454545; overflow-y: auto; max-height: 125px; text-align: center;">
            You might need to edit this to make it work for your picture, but you can also use this as a error box
          </div>
          <br />
          <br />
          <button id="dialog_close_btn" onclick="CloseErrorBox()" style="height: 45px; width: 80%; border: 2.5px solid #CACACA; background-color: rgba(25, 25, 25, 0.5); color: #CACACA; font-size: 15pt; font-family: 'Ubuntu', sans-serif; border-radius: 5px;">
              OK
            </button>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

I trust this information is beneficial!

Answer №2

If you define the position: absolute; property on an element, it will be placed "absolutely" according to its nearest DECLARED parent with the position: relative; property.

My recommendation is to assign the position: relative; property to the enclosing element and experiment with adjusting its height using height: 100vh;, for example.

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 to create a custom button or menu design with ExtJS

My objective is to customize the appearance of an ExtJS button along with its menu. I am facing difficulty in applying styling to a button even with a CSS class. I have attempted the following code: CSS: .customStyle { font-size: 20px !important; ...

Style Vue slots one by one

I am a beginner delving into the world of vue (specifically, vue 2) and aiming to grasp the concept of utilizing slots in the most effective manner. Below is the code I'm working with: <template> <div> <button cla ...

What is the best way to obtain the current cursor location in draft.js?

As part of my draftjs project, I'm working on implementing a feature that allows users to easily insert links. One approach I've taken is creating a popup that appears when the shortcut cmk + k is pressed. To enhance the user experience, I am cu ...

What is the best way to save a JavaScript array in HTML5 local storage?

I am looking for a way to store an array of inputs using the local storage feature of HTML5. Currently, I am able to add weight inputs to the array, but they disappear once I refresh the page. Can anyone assist me with this? <!DOCTYPE html> <html ...

Creating a tooltip with a left arrow and a bordered design

I am looking to create a tooltip that displays its content after clicking on the tooltip tip icon. Currently, it only works on hover, but I want it to be clickable and mobile responsive. Desktop design should resemble: https://i.sstatic.net/FQPyt.png Mob ...

How to manipulate wrapping behavior in flexbox

My flex container holds three divs, arranged in two rows: First row: One div with a fixed width Another div that should stretch to fill the remaining space Second row: Just one item However, the first div stretches to 100% width and pushes the seco ...

What is the best way to access the front camera on both Android and iOS devices in order to capture a photo using Vue.J

I am currently developing a PWA Vue.Js application and I am trying to implement a feature that allows users to take a picture with the front camera on their mobile devices. Although I have managed to write code that works on my desktop browser, I have bee ...

The tooltip function is not functioning properly on Internet Explorer when the button is disabled

I have been utilizing a similar solution found at http://jsfiddle.net/cSSUA/209/ to add tooltips to disabled buttons. However, I am encountering an issue specifically in Internet Explorer (IE11). The workaround involves wrapping the button within a span: ...

What methods are available to test my website across different browsers such as outdated versions of Internet Explorer like IE8?

Currently, I am working on Chrome with Windows 7 OS installed. However, Windows 7 does not support Internet Explorer 8. This is causing issues when trying to view my web pages in the IE8 version and older. How can I resolve this problem? I have attempted ...

Label positioning for checkboxes

Is there a way to display the checkbox label before the actual checkbox without using the "for" attribute in the label tag? I need to maintain the specific combination of the checkbox and label elements and cannot use nested labels or place other HTML elem ...

Refreshing a page will disable dark mode

I'm in the process of implementing a dark mode for my website using the code below. However, I've encountered an issue where the dark mode resets when refreshing the page or navigating to a new page. I've heard about a feature called localst ...

When an image is loaded, use Jquery to automatically open a new tab, instead of

I need to implement a function where, on loading an image on a page, a new tab opens and starts downloading a file from a specific URL while keeping the original page open. This task is part of a WordPress site with a jQuery section in the backend. I' ...

Reinitializing AngularJS form controls

Check out this codepen example: http://codepen.io/anon/pen/EjKqbW I'm trying to reset the form elements when the user clicks on "Reset". The div elements f1,f2,f3 should be hidden and the searchText box cleared. However, the reset button is not trig ...

Clipping of the background image

http://jsfiddle.net/R82a4/ My attempt at achieving the desired result fell short: <div style="background-position: -50%; width: 100%; background-image: url('/assets/upload/stab/1/taccos.png'); background-repeat: no-repeat; background-size: 1 ...

Is there a way to partially conceal the H2 text using CSS?

Is there a way to partially hide H2 text using CSS? I have the following code: HTML: <div class="twocol-box1 superflex-content-6-12"> <h2 data-content="My - Text&amp;nbsp;<span style=&quot;float:right;&quot;>M ...

Unveiling hidden HTML elements with BeautifulSoup: A step-by-step guide

Currently, I am attempting to extract video content from a specific website. Although I can locate the video link using Chrome DevTools, it remains hidden when utilizing BeautifulSoup for extraction. I am hoping for assistance in modifying the code below t ...

clicking the anchor will toggle the div

Here is the HTML code that I am working with: <? while($arr as $data) { ?> <td> <div> <a href="javascript:void(0)" class="status <?= $data['SUGGESTION_CD'] ?>"> <img src="images/activate.gif" /> &l ...

Python code to generate a URL that opens in a new tab:

Currently, I am working on constructing a URL using Python 2.7 and have the following code snippet: link.append("<a href='" + tableLink + sysID[i] + " target='_blank'>" + ticketNumber[i] + "</a>") However, when the link is disp ...

Tips for concealing elements beyond div boundaries

First and foremost, I want to mention that my focus is on studying frontend development, so I could really use some assistance with a task. I am looking to create a tab slider where the content on the left and right will flank it, ensuring the tab slider ...

Setting up multiple base tags in AngularJS for different modules

When working with angularjs, there is a "#" that needs to be removed. This can be achieved by setting: $locationProvider.html5Mode(true); Additionally, adding the base tag ensures normal functionality when the page refreshes. If <base href="http://exa ...