Aligning two separate divs within a parent container div

I'm seeking a solution to the following issue: I have a container div that adjusts its height and width based on a div/img inside it with dynamic height. Inside that container, I want to add another square div measuring 72x72px that will be centered both horizontally and vertically.

A CSS-based solution is preferred.

Thank you for any assistance provided.

Current HTML:

<div class="post">
    <div class="post-like"></div>
    <img src="dsada"/>
</div>

Current LESS code:

.post:before{
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em; /* Adjusts for spacing */
  background: #808080; width: 5px;
}

.post{
  background: @white;
  direction: rtl;
  overflow: hidden;
  width: 42%;
  margin-left: 2%;
  margin-right: 2%;
  margin-top: 4%;
  position: relative;
  text-align: center;
  white-space: nowrap;
}
img{
  display: inline-block;
  vertical-align: middle;
  width: 100%;
}
.post-like {
  display: inline-block;
  vertical-align: middle;
  opacity: 1;
  cursor: pointer;
  height: 72px;
  width: 72px;
  border-radius: 50%;
  background: rgba(0,0,0,0.7);
}

Answer №1

One clever and effective method to solve this issue is an old trick

http://jsfiddle.net/9x1zLhz8/

The HTML Code

<div class="container">
    <img src="http://placehold.it/200x500" alt="" />
    <div class="inner"></div>
</div>

The CSS Styling

.container {
    float: left;
    margin: 20px;
    position: relative;
}
.inner {
    background: #fff;
    border: 2px solid #333;
    width: 72px;
    height: 72px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin: -36px 0 0 -36px;
}

Answer №2

If you want to shrink a div based on content such as an image, you can utilize the display:table property and then use traditional relative/absolute positioning to center your box over the image.

.ib {
  display:table;
  margin:auto;
  position:relative;
}
.top {
  z-index:1;
  position:absolute;
  top:50%;
  left:50%;
  background:rgba(255,255,255,0.75);

}
.h72.w72.center {
  height:72px;
  width:72px;  
  margin:-36px;
}
picture,legend {
  box-shadow:0 0 5px, inset 0 0 2px;
  text-align:center;
}
picture img {
  display:block;
}
/* extra if understood by browser*/
.flex {
  display:flex;
  flex-direction:column;
  justify-content:center;
}
<picture class="ib">
  <img src="http://lorempixel.com/200/400"/>
  <legend class="h72 w72 center top flex">
    center top layer
  </legend>
</picture>

Check out this Codepen link to see it in action!

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

The exact location of the mouse is currently unknown

When I try to double click on a td, it should change the value of an input field to display the coordinates of the mouse cursor. However, instead of showing the coordinates, it just displays unidentified. How can I modify this script to fix this issue? Her ...

The line directly following the first line within the <li> element is located in the same position

I need help with customizing the bullet background-image for an <li> element. I want the text in the bullet to be displayed outside the marker using the CSS property list-item-position: outside. However, my current implementation doesn't seem to ...

It seems like Django static files are failing to load and the runserver is not displaying any 404 errors

My Django admin panel is up and running on my website, but I'm encountering 404 errors for the static CSS files. While inspecting the browser's network tab, I noticed this issue, but strangely, when I run python manage.py runserver, there are no ...

I am attempting to increase the date retrieved from my database by three years

I need assistance with updating dates in my PHP script. I want to add 3 years to the date stored in the database for each record. Specifically, I need to retrieve the date when a certain class was first taken (date_aerial) and then return a new date by add ...

Can a form be successfully submitted by a spam bot even if there is no visible submit button present on the webpage?

Curious if a spam bot can still submit a form without a visible submit button on the page. Exploring simple ways to prevent spam without relying on CAPTCHA by using jQuery to display the submit button when the user engages with the form. Open to any thou ...

Guide on retrieving JSON data and inserting it into an array with the fetch function in JavaScript

Being new to javascript, I am uncertain about how to properly add JSON data to an array. I am using the fetch command to retrieve the JSON file. Below is the code snippet for fetching the JSON file: fetch("event.json") .then(response => response.json ...

The contents of the div do not display when the button is pressed except in jsfiddle

I have written a script that triggers the appearance of a div where users can adjust the time settings for a timer. The functionality works perfectly on JSFiddle, with the div displaying as intended. However, when tested on other online IDEs such as Coding ...

Tips for concealing components with the assistance of Bootstrap's classes?

Currently, I am experimenting with Bootstrap to display h1 only on desktop/large screens and h2 only on mobile devices. However, despite my efforts, both h1 and h2 appear on all screen sizes. The h1 and h2 elements are input fields and the condition for t ...

The problem arises when using `$state.go` as it does not properly transfer the value to `$stateParams`

Within componentA, I am using an angular code: $state.go('home', {selectedFilter: "no_filter"}); In componentB, I have the following code: if ($stateParams.selectedFilter === 'no_filter') However, I am encountering an issue where $s ...

How can you ensure that the right data types are sent through ajax requests?

My goal is to ensure the server receives data in the correct data type when using ajax. For example, I want boolean values to be received as actual booleans, integers as integers (not strings), and so on. I attempted a solution where I sent the data as JS ...

"Receiving incorrect results when trying to identify the class of the clicked image

I am currently exploring how to identify the specific image that I have clicked on using jQuery. Although my current code is returning IMG as the output when I click on an image, this is not the desired result. My intention is to get the output of the clas ...

Steps for Downloading an Image from Google

I'm brand new to programming and I'm trying to figure out how to save images from Google Images. Specifically, I want to filter for only the free ones. I've been using a Google API to get the image URLs, but I'm having trouble passing t ...

Effective Ways to Redirect During or After Executing the onClick Function of Button

I am currently working on implementing a feature for my Next.js website. The functionality involves allowing users to create a new group by clicking a button, and then being redirected to an "Invite members" page with the auto-generated group_id included i ...

instructions on invoking a function within the knockout $.getJSON() operation

I am currently experimenting with a basic knockout script (still in the learning process). $.getJSON(clientUrl + "/list/" + 1, function (data) { var viewModel = { clients: ko.observableArray(data) }; ko.applyBindings(viewModel); }); The initial arg ...

React data retrieval and processing

import React,{useState, useEffect} from 'react' import { useParams } from 'react-router-dom' import Home from './Home' import './detailpage.css' function DetailPage({name, info, genre, _id, episodeNumber, ...

Can I install more than one instance of Framework7 on the same device?

Currently, I am working on a project using cordova 6.2.0 and framework7 1.6.5. However, now I need to initiate a new project that will be based on cordova 7.1.0 and framework7 2.0.7. I am aware that there is version-manager-cordova-software [1] available ...

Initiate timer when mouse leaves; pause timer upon mouse hovering using JavaScript

I am currently working on creating a volume bar for a video. The idea is that when you click the volume button, a div will appear allowing you to control the volume level. As soon as you hover out of the div, a timer will start counting down from 7 and the ...

JavaScript backbone's setTimeOut feature

I am encountering an issue with the setTimeOut method that calls a function and sets a delay. The function should be called repeatedly after each request is completed, but it only runs once. It works fine without using backbone.js, but I am not sure why it ...

Develop a regular expression tailored for jQuery validation purposes

I'm working with jQuery validation in my web form and I need to validate a text field with specific criteria: 1. Must contain a number. 2. The letters 'a' or 'A', 'b' or 'B' are allowed after the number. 3. Th ...

Issue with Bootstrap Scrollspy: Scrollspy function not functioning as expected

I need help with creating a one-page website where the navbar links change based on the section of the page you are on. I tried implementing it using HTML, but it didn't work out as expected. The code I used was within the container holding different ...