Place Div in the center of another Div

In my maze creation, I am trying to center an inner div, but no matter what I do, using margin: 0 auto; does not seem to work

The inner div displays a sad smiley face when the user enters a wall and loses

#highlight_lose {
    width: 550px;
    height:550px;
    position: absolute;
    display: none;
    margin: 0 auto;

}

For reference, here is the link to the fiddle:

http://jsfiddle.net/uqcLn/28/

Answer №1

If you plan on utilizing absolute positioning, ensure to follow this structure:

#highlight_lose {
    width: 550px;
    height:550px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -225px 0 0 -225px;
    display: none;

}

Update: remember to include position:relative; to the main div. Check out the updated fiddle here.

The alignment may seem off due to other elements not being perfectly centered.

UPDATE: As previously mentioned, the smiley appeared off-center due to issues in your code. Ideally, the maze should be contained within a separate division. Nevertheless, I managed to visually center it by adjusting the margins.

Check out the adjusted code here.

To achieve this, make sure your CSS follows this format:

#main {
    position: relative;
    width: 550px;
    height: 550px;
    float: left;
    margin-left: 220px;
    margin-top: 100px;
    background: grey;
    overflow: hidden;
}

#highlight_win {
    width: 550px;
    height: 550px;
    position: absolute;
    top: 50%;
    left: 50%;
    display: none;
    margin: -180px 0 0 -180px;
}

#highlight_lose {
    width: 550px;
    height:550px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -180px 0 0 -180px;
    display: none;
}

Answer №2

.outer {
            height: 600px;
            width: 500px;
            background-color: black;
        }

        .inner {
            height: 200px;
            width: 200px;
            margin: auto;
            position: relative;
            top: 200px;
            background-color: red;
        }

html code

<div class="outer">

        <div class="inner">

        </div>

    </div>

In this example, by using fixed size block elements and setting

margin:auto;

We can achieve horizontal centering.

For vertical alignment, set the child's top position to half the height of the parent - half the height of the child.

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

What is the best way to ensure the footer is always positioned at the bottom of each page

Hey there, I'm having an issue with printing a large table on my HTML page. I want the footer to appear at the very bottom of every printed page, but so far I haven't found a perfect solution. I came across using tfoot, which prints the footer at ...

I desire to share the JSON information and receive the corresponding response data

<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> ...

Troubleshooting a WordPress Blog Homepage CSS Dilemma

My goal is to keep the featured image size consistent on the main blog page. I made adjustments to this code: HTML: <div class="featured-image-excerpt"> <img class="attachment-post-thumbnail size-post-thumbnail wp-post-image" src="http://mrod ...

Creating a Form with Table-like Alignment Using Div Tags

Take a look at my code: <div> <label>First Name</label><input type="text" id='first_name'/><br /> <label>Last Name</label><input type="text" id='last_name'/><br /> <l ...

"Discover the magic of jQuery: Unveiling the hidden div with one simple CSS visibility change

I want to implement a functionality on my screen where the Previous button is hidden initially and only becomes visible when the user clicks the Next button. I have set the CSS property for the Previous button to hide it by default, but despite using an if ...

How can I apply styling to Angular 2 component selector tags?

As I explore various Angular 2 frameworks, particularly Angular Material 2 and Ionic 2, I've noticed a difference in their component stylings. Some components have CSS directly applied to the tags, while others use classes for styling. For instance, w ...

Ways to present a pop-up dialog box featuring word corrections

I have developed a word correction extension that encloses the incorrect word in a span element. Upon hovering over the word, a drop-down menu with possible corrections should be displayed. However, my current code is not functioning properly. How can I en ...

Choose a specific location on a vehicle illustration within a pop-up window. The image should be partitioned into 6 separate sections

I have a project for my client where they need to choose a car and then indicate where the damage is located on an image, which is divided into 6 sections. I'm struggling to figure out how to achieve this. I initially thought z-index would work, but ...

Tips for transforming a container div into a content slider

Working with Bootstrap 3, a custom div has been created as shown below: <div class="second-para"> <div class="container"> <div class="second-section"> <div class="c ...

Forms for uploading and submitting multiple files

On my single page, I have several file upload forms that are generated in a loop. The issue is that the first file upload control works fine, but the others do not. <div> <form action="/docs/1046/UploadDocument?Id=1046&amp;propertyTypeId ...

Tips for maintaining interactivity after a page refresh

$(document).ready(function () { $("#2").click(function () { $("#content").load("{% url 'about' %}"); }); $("#3").click(function () { $("#content").load("{% url ...

Using the combination of z-index and visibility:hidden makes the button go completely undetect

Can someone please review this code? .parent { visibility: hidden; position: relative; z-index: 1; } .child { visibility: visible; } <button class="parent"> <span class="child">content</span> </button> I' ...

Text is not visible when hovering over it

I am facing an issue with the hover effect on my menu element. When I hover over the element, the text does not appear as expected. Even after using z-index to position the text on top, it still doesn't work. Here is a snippet of my code: HTML: < ...

What are some methods for customizing the calendar icon displayed in an input field with type date?

Is there a way to customize the calendar logo without relying on jquery? The desired appearance should resemble the image shown below: https://i.sstatic.net/G06iZ.png input { width: 50%; padding: 8px; border-radius: 10px; ...

Positioning custom buttons using CSS and HTML

Need help with centering a button inside a div within a table cell. Is there a way to determine the current size of the hovered-over div and position the button in the middle both vertically and horizontally? https://i.sstatic.net/6NQ9J.jpg I hope you ...

Removing Inline Styles Using jQuery Scroll Event

Whenever I reach the scroll position of 1400 and then scroll back to the top, my chat elements (chat1, chat2, chat3, chat4) receive an "empty" inline style but only for a duration of 1 second. After that, the fadeIn effect occurs again every time I scroll ...

Why is there a CSS reference failure in Express and how can it be resolved?

Below, you'll find a simple express server I've put together: // check out my basic Express server var express = require("express"); var app = express(); var bodyparser = require("body-parser"); app.use("/public/", express.static("./public/")); ...

Can one utilize HTML's .querySelector() method to target elements by xlink attribute within an SVG document?

Here is the scenario: <body> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <a xlink:href="url"></a> </svg> </body> Now, can you utilize the HTML DOM's .querySe ...

Angular Script Linking

Hello there! I am currently attempting to add an HTML tag to my response message, but for some reason it isn't working as expected. Here is a snippet of my controller code (in case the API indicates that the account doesn't exist - see this scr ...

website with a horizontal scroll-bar

Looking to create a horizontal scrolling website where the divs float to the right without specifying the container's width, as it may vary on different pages. Any suggestions? ...