Is there a way to center align this div element?

I found this website page that I need help with:

On the page, there is a map and a blue div with content that I want to center display.

Here is the HTML code snippet:

<div style="float:left;width:100%;padding:0 10%;text-align:center;margin:10px auto;display:block;">
<div class="date-contact">proba</div>
<?php echo do_shortcode( '[huge_it_maps id="1"]' ); ?>
</div>

This is the corresponding CSS code snippet:

@media (min-width: 800px) {
.date-contact
{
width:300px;
height:150px;
background:blue;
position:absolute;
z-index:10;
}
}

I have tried using margin: 0 auto, but it's not working as expected. Can someone help me solve this issue?

Thanks in advance!

Answer №1

To maintain the absolute position of your element, you can utilize the calc function for determining the top and left properties. However, it is crucial to have knowledge of the height and width of your div.

Additionally, the immediate parent of this blue box must have a positioning value of relative, absolute, or fixed:

Check out a demonstration here

<div></div>

div {
    background: blue;
    position: absolute;
    width: 100px;
    height: 100px;
    top: calc(50% - 50px);
    left: calc(50% - 50px);
}

Answer №2

Here is my recommendation: Enclose the two divs, div#huge_it_google_map1_container and div.date-contact, within a parent div. The width of the parent div will match that of div#huge_it_google_map1_container. Therefore, the revised html code will be:

<div class="map_parent_wrapper">
<div id="huge_it_google_map1_container"></div>
<div class="date-contact"></div>
</div>

The corresponding css will be:

.map_parent_wrapper {
position:relative
}
.date-contact {
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
margin:auto;
}

Answer №3

.contact-info {
  background: none repeat scroll 0 0 blue;
  clear: both;
  height: 150px;
  margin: 0 auto -208px;
  position: relative;
  width: 300px;
  z-index: 10;
}

The div styled in blue is currently absolutely positioned, making it difficult to center align.

Change its positioning to relative and align it at the center using margin 0px auto;

Add a negative bottom margin of -208px so that it overlaps with the map.

Adjust the z-index as needed to ensure the blue box appears above the map.

Cheers! 🎉

Answer №4

UPDATE: Upon reviewing your site after your edit, I noticed the need for a position: absolute in your .date-contact style. Therefore, the code I originally recommended may not be applicable here. Nevertheless, you can still benefit from the explanations provided.

To start, it's important to note that you cannot use margin: 0 auto with position: absolute. It is also advisable to use classes in a separate CSS file instead of inline styles as this ensures better code organization and adheres to the Don't Repeat Yourself (DRY) principle.

I've revised your code to achieve the desired effect. Take a look and feel free to ask any questions if needed; I'll do my best to assist you.

HTML

<div class="outer-div">
    <div class="date-contact">proba</div>
</div>

CSS

.date-contact {
    width:300px;
    height:150px;
    background:blue;
    margin: 0 auto;
}

.outer-div {
    text-align:center;
    margin:100px auto;
    padding:0 10%;
}

REVISED ANSWER FOR EDITED QUESTION

While other responses suggest using absolute positioning for the blue div, I believe this approach will not center it as desired. Instead, consider placing the blue div within another div set to position absolute. This way, the blue div will be centered with margin: 0 auto;. Additionally, I have placed the blue div inside div#huge_it_google_map1 as it seems more fitting there.

HTML

<div class="yourMapDiv">
    <div class="outer-div" id="huge_it_google_map1">
    <div class="date-contact">proba</div>
    <!-- Additional divs and map content inside div#huge_it_google_map1 -->
    </div>
</div>

CSS

.yourMapDiv {
    position: relative;
    background-color: yellow;
    padding: 10px;
}

.outer-div {
    position: absolute;
    top:0;
    left:0;
    width:100%;
    text-align:center;
    background-color:red; /* Remove to display your map div (yellow) */
}

.date-contact {
    background-color:blue;
    width:300px;
    /*height:150px;*/
    margin: 0 auto; 
    /* or "margin: 50px auto 0" for a 50px margin-top */
}}

For easy reference, here is a working example. I hope this helps you achieve your desired outcome.

Answer №5

A div styled in blue is set to have an position : absolute. To achieve centered display, you must specify the values for left and top like so:

left: 50% - width of the block (for instance, 40%)
top: 50% - height of the block (for example, 40%)

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 implement conditional CSS?

My current project involves a component called item, which represents a card displaying information about the item. All cards follow the same basic structure, showing categories like price, color, size, and photo. However, I am interested in applying condi ...

Conceal the div by clicking outside of it

Is there a way to conceal the hidden div with the "hidden" class? I'd like for it to slide out when the user clicks outside of the hidden div. HTML <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.c ...

Python-JavaScript Integration Problem

I'm a beginner when it comes to Javascript, Python, and PHP. In my Javascript program, I have a button that triggers a Python script and displays the returned value on the screen. My goal is to steer clear of using Jquery and Json. Here are the snipp ...

What is the best way to deactivate the second selection option?

<select id="title0"> <option value="0">--- disable</option> <option value="1"> books</option> </select> <button id="save" type="submit">Save</button> <select id="title1"> <option value="0"& ...

Unusual spacing issue observed between <div> elements on Internet Explorer, but not on Firefox or Opera

I am facing a common question that many others may have asked before, but I haven't been able to find a solution to my specific issue. When viewing my website on FF, Opera, and IE on Windows 7, everything displays correctly. However, when using IE7 o ...

How to create a gelatin-like effect on a rectangle using HTML5 canvas

Currently, I'm working on a platformer game project in JavaScript. The concept involves players as rectangles jumping on and off platforms. While the basic functionality is set up, I'm now aiming to incorporate a unique 'gelatine effect&apos ...

What is preventing the usage of $(this) in the jQuery .animate() function?

My progress bar is built with Bootstrap and here is the HTML code: $(".progress-bar").animate({ width: $(this).data('width') + '%' }, 2500); <!-- Latest compiled and minified CSS --> <link rel="styles ...

Struggling to implement CSS for second div onto selected item in the first div

As a beginner in Angular, I am seeking some guidance and examples. In my code, there are two divs. One is created using ng-repeat with an ng-click function to detect the clicked item. The other div below has CSS properties like style="width:100px; float ...

disabling tabs that are next to each other using react hooks

When the user clicks on item one button in the first tab, it should disable the next two tabs. If the user clicks it back, it should enable the other two tabs. The same functionality should apply to the other tabs as well. Currently, I have disabled the ...

Firefox is not allowing the form to be submitted at this time

My form is functioning correctly on Chrome and Safari, but not on Firefox. Solution Attempt #1 <form id="subscribe-form" class="footer-sign-up" action="/subscribe" method="POST"> <input type="text&q ...

Creating a vertical navigation panel with md-menu and material icons

Struggling to implement the md-menu to create a vertical menu using material icons. Unfortunately, I am only able to create a horizontal menu and the icons are misaligned, failing to work when placed within <md-icon md-svg-icon="hotel" md-menu-align-ta ...

Struggling to change h2 style on WordPress?

In a WordPress page, I create three heading texts and adjust the CSS when the screen width is less than 820px. Here is the code snippet: <h1 class="block-h1">Heading 1</h1> <h2 class="block-h2">Heading 2</h2> <h3 class="block-h3 ...

What is the process for registering a click using a class in jQuery and retrieving the ID of the clicked element?

Currently, I am working on developing a webpage where I need to use jQuery to register a click using the items class and then extract the ID of that particular object. For example: HTML: <div class="exampleclass" id="exampleid1"></div> <d ...

Function for jQuery Slide Toggle is not being invoked

I'm struggling with jQuery Slide Toggle. $(document).ready(function() { $("a").click(function() { $(this).parent().parent().children("p").Toggle(400); return false; }); }); I have multiple posts displayed on the page fetched from ...

Loop through a vector or data frame in RCurl's postForm() function to input values into a text field form

My objective is to transmit each individual "finput" observation into an HTML form under the name "text". The process works smoothly when dealing with a single observation. However, I am encountering difficulties while attempting to iterate through the ve ...

Using Jquery to iterate through and find input elements in a collection

Here is the code I am currently working with: <div class="phoneNumbers"> <input /> </div> <div class="phoneNumbers"> <input /> </div> <div class="phoneNumbers"> <input /> </div> My goal is ...

What is the best way to expand a div in a downward direction exclusively?

My task involves a parent div containing a centered child div. My goal is to have the child div grow downwards only upon clicking it, not in both directions. Initial state of the two divs: https://i.sstatic.net/cWYyV.png Outcome after clicking on div 2: ...

What is the best way to align text next to an image when the image size is larger?

I have developed a code for a used vehicle platform using HTML with flex, and while it functions properly, the aesthetic appeal is lacking: body { font-family: Verdana, sans-serif; font-size: 16px; line-height: 18px; } header.infog { background ...

Middle align an absolutely positioned block within a table cell vertically

Here is the code I am working with: td { border: 1px solid #cecece; position: relative; padding: 80px 20px; } .hint { background: yellow; position: absolute; top: 0; bottom: 0; left: 100px; } <table style="width:800px"> <tr> ...

Unusual occurrence with the nth-child selector

For the fiddle, click here - http://jsfiddle.net/ashwyn/a45ha/ To see the HTML code, take a look below: <div class="parent"> <div class="a">Class A</div> <div class="b">Class B1</div> <div class="b">Class B ...