Position the DIV in the center within another DIV that contains an image

Having trouble centering a DIV horizontally inside another DIV that contains an image? It can be tricky, especially when the image is added into the mix.

I've attempted various methods, but my goal is to have box2 centered at the bottom of the parent DIV, on top of the image, regardless of its size.

You can take a look at an example image here:

Any suggestions or advice on how to achieve this?

For reference, here is the CSS and HTML code I've been working with:

#box1{
    position:relative;
    display:inline-block;
    width: 200px;
    height: 200px;
}

#box2{
   width:50px;
   margin: 0 auto;
   background-color: yellow;  
}
<div id="box1">
<img src="http://i44.tinypic.com/2j4akqb.jpg"/> 
    <div id="box2">box</div>
</div>

Answer №1

Give this a try: I removed the width from the outer div (setting both can cause the image size to vary). Instead, set the image height to 100%. Choose only one - height or width - and the other will automatically adjust to maintain the image ratio. Lastly, I positioned the bottom div absolutely with bottom set to 0, and left and right set to 0, then used margin:auto to center it.

https://jsfiddle.net/carinlynchin/uz0L5oow/10/

<div id="box1">
    <img src="http://i44.tinypic.com/2j4akqb.jpg"/>
    <div id="box2">box</div>
</div>

CSS:

#box1{
    position:relative;
    display:inline-block;
    height: 200px;
}

img {
    height: 100%;
}

#box2{
   width:50px;
   background-color: yellow;  
   position: absolute;
    bottom:0;left:0; right:0;
   margin:auto;
}

Answer №2

Since box1 served as the outer box, you have the option to include the text-align property so that #box2 can inherit from it. Additionally, if your images were larger than 200px, you could adjust the width to 239px; Check out the demo

#box1{
    position:relative;
    display:inline-block;
    width: 239px;
    height: 200px;
    text-align:center;

}

#box2{
   background-color: yellow;  
}

Alternatively, you could also set the width to 200px for the image

View the updated demo here

#box1{
    position:relative;
    display:inline-block;
    height: 200px;
    text-align:center;

}

#box1 img{
 width:200px   
}


#box2{
   background-color: yellow;  
}

Answer №3

Check out the Live Demo

Give this a try

Here's the HTML code

<div id="box1">
    <img src="http://i44.tinypic.com/2j4akqb.jpg"/>
    <div id="box2">box</div>

</div>

And here's the CSS code

#box1{
    position:relative;
    display:inline-block;
    width: auto;
    height: 200px;
    background-color:#fff;

}

#box2{
   width:50px;
   text-align:center;
   position:relative;
   bottom:0%;
   float:bottom;
   margin: 0 auto;
   background-color: yellow;  
}

Answer №4

Do you find this solution helpful?

#box1 {
    position:relative;
    display:table;
    width: 200px;
    height: 200px;
}

img {
    position: absolute;
    z-index: -2;
    width: 100%;
}

#box2 {
    width:50px;
    margin: 0 auto;
    left: 0%;
    right: 0%;
    bottom: 0%;
    background-color: yellow; 
    z-index: 100;
    position:absolute;
}

See it in action: https://jsfiddle.net/ba5z1zm3/2/

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

Material-UI: Eliminate the missingOppositeContent:before element from TimelineItem

I'm currently using Material-UI to construct a timeline. Here is the code snippet I am working with: <Timeline align="right" className={classes.monthlyContainer}> <TimelineItem > <TimelineSeparator className={class ...

Conceal image and substitute with a different image using JavaScript

My query pertains to JavaScript: how can I create an effect where clicking the jump-down button opens a text box and causes the button to change direction and rotate? I am considering using two photos - one hides when the jump-down button is clicked, and ...

Generating an interactive Datepicker using Jquery

How can I design a dynamic date picker similar to the image provided below? https://i.sstatic.net/euoNI.png I have attempted to create one, but I am looking for a more interactive date picker. Is there a way to achieve the design shown in the image? The ...

Making adjustments to a Jquery data attribute can have a direct impact on CSS

My CSS is using a data attribute, but when I try to change it with jQuery, the change is not reflected on the screen or in the DOM. $('#new-1').data('count', 5); .fa-stack[data-count]:after { position: absolute; right: -20%; to ...

Position a form in the center of a container and showcase an additional div on the right with an indentation

My goal is to center a form within a div and have another div on the right that is slightly indented, but I'm struggling to align the elements properly. How can I achieve this layout using CSS? Additionally, is there a way to center the right-hand d ...

Customizing Sass Bootstrap Variables for Tailored Responsive Designs (Specifically for Mobile or Desktop)

Currently, I am in the process of working on a website that has opted for the adaptive approach, using separate mobile and desktop templates instead of responsive design. Despite this choice, Bootstrap is still being utilized on these templates, loading al ...

Tips for Organizing a Vast Array of Repetitive "Nth-Of-Type" CSS Selectors

Imagine an array of vibrant highlights, all controlled through CSS. Is there a way to condense this extensive block of CSS code while achieving the same result? In simpler terms, is it possible to reduce the repetition in the code by using only CSS when ...

The CSS is getting overridden by the a:-webkit-any-link user agent stylesheet

I've found myself faced with a page full of lists and links that I need to style individually. No matter what I try, the fonts and colors keep getting overridden by an unknown user agent stylesheet. I've experimented with styling the divs using c ...

Curious about the distinction between express-ejs-layouts and DHTML?

According to Google search results, DHTML involves generating HTML content dynamically using Javascript. Meanwhile, EJS also claims to do the same thing with its abbreviation of Embedded JavaScript. It seems like both DHTML and EJS have similar functional ...

Send the user to the login page

Hello everyone, I have developed a function that allows me to redirect users to a specific URL. The function is called 'redirect' and uses the header function: public function redirect($url, $code = null) { if($code == 301){ header( ...

pattern to eliminate particular anchor elements

I am facing a challenge with a large HTML file that contains approximately 300 similar anchor tags that need to be removed: <a href="javascript:void(0);" onclick="javascript:thumbs(198, 0, 'architecture')" class="icon"></a> The prob ...

tips for creating a mobile-friendly responsive button

I have been scouring the internet for a solution to this issue. Essentially, I am trying to position the button on the right side in desktop view. Here is an example: chrome view However, when the site is resized or viewed on mobile devices, the button sh ...

The font-weight property appears to be ineffective across all browsers

I'm struggling with the font-weight property in my CSS. Despite trying different values like lighter, bold, and even numerical values like 300, it doesn't seem to have any effect on the font weight in any browser. I am using the following code to ...

Steps to display text in a div upon clicking on an image

I am trying to create an image with two DIVs separated by a black line. The left DIV will contain 4 images, and I want the following functionality: When a user clicks on any of the buttons in the left DIV, a corresponding text should be revealed in the ri ...

Automatically trigger a PHP reload/refresh based on user input

My website has a form that populates a table with data. Currently, after the user submits the data, they have to manually refresh the page in order to see the new data displayed. Is there a way to automatically refresh the table every time the user hits ...

I am currently working on a website that offers different themes, and I am attempting to update the iframe to reflect the selected theme on the site

I'm feeling lost on how to accomplish this task. Despite my efforts, I have been unable to find a solution so far. Perhaps utilizing javascript might be the key here, yet I am uncertain about integrating it into the existing script that I use for modi ...

Is there a notable distinction when utilizing http for referencing assets?

As I work on creating a new website, the question of displaying images has come up. I plan on using an img tag, but I'm unsure if it matters how I structure the source link: <img src="img/mypic.png"> or like this: <img src="http://www.mysi ...

How can I style the inner div by adding a class to the first div?

In my project, I have a list of elements that are generated dynamically, all styled the same way but with different content. The first element has a specific styling, and if it doesn't render, I want the second element to inherit that styling. < ...

Display user input within a modal dialogue box

I have a subscription form that requires users to enter their name and email address. After clicking on "Compete Now," a pop-up appears asking for workshop information and postal code. The form is functioning correctly as intended. However, I want the em ...

Is there a method to ensure equal alignment of the <div class="card"> elements in Bootstrap?

I need help figuring out how to align this card uniformly with the others: https://i.sstatic.net/x2wZ2.png In the image below, you can see that the card for the top reason for downtime doesn't match the other cards. I want them all to be the same he ...