Position the div block in the center of the screen

Can someone help me with centering a div block on the screen?

.pop-up{
    width: 850px;
    height: 640px;
    z-index: 10;
    left: 20%;
    position: fixed;
    top:1%;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 0 3px rgba(0,0,0,0.5);
}

I just realized, it should actually be position:absolute;.

Answer №1

To center the div, set its position to absolute and adjust the top, bottom, right, and left values to 0. Provide a fixed width and use margin auto. Using fixed width and height is important for this method to work effectively. Check out the demo here to see it in action. If this solution doesn't work for you, feel free to let me know.

For more information on positioning, you can refer to the following link: Detailed Positioning

Answer №2

Insert the following code snippet into the header section of your HTML document:

        <script>
        $(document).ready(function(){
            adjustPosition();
            $(window).on('resize', adjustPosition);
        });

        function adjustPosition() {
            $('.center').css({
                position:'absolute',
                left: ($(window).width() - $('.center').outerWidth())/2,
                top: ($(window).height() - $('.center').outerHeight())/2
            });
        }
    </script>

Also, make sure to assign the following class to the element you want to center:

class="center"

By doing this, the desired effect will be achieved.

Answer №3

To center the <div> on the screen, you can implement the following CSS:

.pop-up{
    z-index: 10;
    height: 640px;
    width: 850px;
    position: fixed;
    top: 50%;
    margin-top: -320px; /* half of the height */
    left: 50%;
    margin-left: -425px; /* half of the width */
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 0 3px rgba(0,0,0,0.5);
}

You can see an example 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

Express Node + Styling with CSS

I am in need of a single EJS file (configuration includes Express and Request). app.get('/space', function(req, res){ res.render('space.ejs'); }); The file successfully renders, but only two out of three CSS stylesheet links work ...

What is stopping me from utilizing ES6 template literals with the .css() method in my code?

I am currently working on a project where I need to dynamically create grid blocks and change the color of each block. I have been using jQuery and ES6 for this task, but I am facing an issue with dynamically changing the colors. Below is the snippet of m ...

What is the best way to add margin-bottom to every Bootstrap card on a webpage?

I added a margin-bottom to my Bootstrap card, but it only seems to affect the second row and not the first row. How can I make it work for both rows? https://i.sstatic.net/fBfdK.png This is the code I'm using: .card{ position: absolute; margin-top: ...

Is it true that JqueryUI resize and draggable functions are incompatible?

I am experiencing issues with using both jqueryui resize and draggable in the same element. How can I make them work together seamlessly? Currently, I am able to resize the image but unable to drag it as intended. The goal is to allow dragging of the image ...

Avoid the issue of float right sections overlapping the wrapper

My issue is that the sections floated to the right are overlapping with the wrapper. How can I prevent this from happening? I also attempted to use the float property within the nth-child(odd/even) of the CSS, but it didn't work as expected. I would ...

What is the method for obtaining distinct hover-over values for individual items within a dropdown menu?

If utilizing angular2-multiselect-drop down, what is the correct method to obtain distinct hover over values for individual items in the drop down? When dealing with standard HTML, you can directly access each element of the drop down list if it's ha ...

Unable to insert flag image into the <option> tag

Struggling to add a flag image to the options using any method (html,css)! <select> <option value="rus" selected>Rus</option> <option value="Uzb" >Uzb</option> <option value="eng">Eng</option> </s ...

Is there a way to modify the background color of the basic HTML title tooltip without the need for an additional span

Is there a way to easily change the background color of tooltips without adding extra elements like spans or divs? I have many tooltips in different projects and it would be tedious to go through each one individually. <!DOCTYPE html> <html&g ...

Troubleshooting Google Custom Search Engine (CSE) glitches on mobile browsers like Chrome and Safari. Resolve issues with the Search Icon and Menu Icon. Steps to set Google CSE as

Update: I have discovered that Safari, in addition to Chrome, is also experiencing the same issue. Here's an update on some information I've uncovered. But before delving into that, let me outline the problem we're encountering with the sea ...

Accessing the text content of the clicked element using vanilla JavaScript

Retrieve an item from the dropdown list and insert it into a button's value. function updateButton() { document.getElementById("ul").classList.toggle("show"); } var dropdown = document.getElementById('ul'); var items = ["HTML", "CSS", "Ja ...

Hiding the div element with 'display: none' causes the disappearance

Whenever I execute this piece of code; document.getElementById("loading").style.display = "none" It unexpectedly hides the div named "myDiv" as well. By setting MyDiv to block, the text appears correctly. However, when loading is set to none, it strange ...

Adjust the space between spans by utilizing the margin

There are two spans (although there could be many more) on this web page that I need to adjust the distance between. I tried using the margin-bottom attribute, but it doesn't seem to have any effect. The spans remain in their original positions, which ...

Empty space found at the bottom of a webpage while viewing on smaller browser resolutions due to the CSS-Grid layout

Recently, I've been working on designing a website layout using CSS-Grid. Everything seems to be functioning properly and adapting well to different screen sizes until I encountered an issue with scroll bars appearing at very small resolutions. This c ...

Isotope grid shatters when browser window is resized

My goal is to design a 3-column grid using Twitter Bootstrap and Isotope. However, when I resize the browser to force the layout into a single column mode, Isotope fails and leaves large spaces both vertically and horizontally. Regular Layout Issues in ...

Show a popover within a parent div that has limited visible space due to its hidden overflow

Struggling with AngularJS, I can't seem to find a simple solution for this problem. In my code, there's a div element with the overflow: hidden property set due to an internal scrollbar. Inside this div, there's a dropdown menu that is trigg ...

What is the method to replace the default font family in Bootstrap?

Adding my own unique style with CSS @font-face { font-family: 'CustomFont'; src: url(./fonts/custom/CustomFont.otf); } h4.CustomFont-style { font-family: 'CustomFont', sans-serif !important ; } Using the custom fo ...

Is it possible for CSS to bypass an HTML element?

Summary: Can CSS be instructed to ignore an HTML element without affecting its children? The ignored element should not impact the styling of its children, treating them as if they were direct descendants of the parent. In Depth Analysis: Imagine a caref ...

Select element's width decreases by 2 pixels following the application of width adjustment via jQuery

After browsing through numerous pages, I finally managed to adjust the width of my textbox and select element using jQuery 2.0. Snippet of Code from my Project: $(document).ready(function(){ $("#Text2").css('width','20 ...

Optimal method for storing text descriptions across two platforms (react native and react)

Hello, I hope you are doing well. I have a question regarding two platforms that I am using - React Native and React (NextJS). Both platforms have forms to save data, one of which is a "text description." Here's the issue: When I input a long passag ...

Tips for showcasing the dynamic legend in a line graph

Within my project, the legend is being displayed dynamically. However, I am looking to showcase names such as "student-marks" and "studentannualscore" instead of numerical values like 0, 1, 2, etc. Currently, the values are appearing as numbers (e.g., 0 ...