Place a pair of divs in the center next to one another

What is the best way to align two divs in the center beside each other with some padding in between that actually works? I attempted using display: inline, but it didn't have the desired effect.

.my-container {
          position: relative;
          text-align: center;
          color: red;
          width: 20%;
          margin: auto;
        }
        
        /* Centered text */
        
        .centered {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          -moz-border-radius: 50%;
          border-radius: 50%;
          background-color: blue;
        }
      
<div class="my-container">
        <img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
        <div class="centered">text</div>
      </div>
      <div class="my-container">
        <img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
        <div class="centered">text 2</div>
      </div>

(Note: I'm also interested in turning the blue text-elipse into a circle, but I'll save that for another question.)

Answer №1

To align the items side by side in your .my-container, use display:inline-block...

Additionally, wrap your .my-container divs into a wrapper div with text-align:center to center the inline items...

.wrapper {
  text-align: center;
}

.my-container {
  position: relative;
  text-align: center;
  color: red;
  width: 20%;
  margin: auto;
  display: inline-block;
}


/* Centered text */

.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -moz-border-radius: 50%;
  border-radius: 50%;
  background-color: blue;
}
<div class="wrapper">
  <div class="my-container">
    <img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
    <div class="centered">text</div>
  </div>
  <div class="my-container">
    <img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
    <div class="centered">text 2</div>
  </div>
</div>

Answer №2

To enhance the layout, remember to apply display: inline-block; to the my-container class.

  .my-container {
    position: relative;
    text-align: center;
    color: red;
    width: 20%;
    margin: auto;
    display: inline-block;
  }

  /* Centered text */

  .centered {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -moz-border-radius: 50%;
    border-radius: 50%;
    background-color: blue;
  }
<div class="my-container">
  <img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
  <div class="centered">text</div>
</div>
<div class="my-container">
  <img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
  <div class="centered">text 2</div>
</div>

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 table refuses to load

I've been troubleshooting this issue for the past two days. The array is visible in the console, but it refuses to show up on the table. I've tried multiple approaches, but none seem to work. I suspect that "tobodyHtml" is not defined properly, a ...

Ensure that the width of the inner table matches the width of its contents within an overflowing div

I'm facing an issue with a table inside a div styled as table-wrapper: .table-wrapper { display: inline-block; overflow-x: scroll; width: 800px; } Although I want the div to display a horizontal scrollbar so that the table inside it can ...

Expanding a list of object arrays through iteration: A step-by-step guide

// Initializing with one object let posi_val=[{top: '3px', left: '2px'}]; // Adding another object in the loop for (let i = 1; i < n; i++) { posi_val.push({ top: `${posi_val[i - 1].top.slice(0, -2) * 2}px`, l ...

Designing GWT FlexTable using pure CSS styling

Is there a way to style a GWT FlexTable using CSS alone? The API doesn't provide information on available CSS classes. Are there no predefined classes and do I need to define them myself? I am particularly interested in styling the "th" element. ...

Having difficulty integrating JSON data from a PHP page into jQuery

I'm having trouble parsing JSON data sent from a PHP page using jQuery. The result I'm getting is just an opening curly brace: { I'm not sure how to fix it. In my PHP code, I'm creating the JSON data like this: $load_op_cm = $DBM -> ...

Enlarge the DIV element along with its contents when those contents have absolute positioning

When attempting to overlay two divs like layers of content, I encountered a challenge. Because the size of the content is unknown, I used POSITION:ABSOLUTE for both divs to position them at the top left of their container. The issue: The container does no ...

Express.js route not being properly handled in post request

This is the structure of my folders: https://i.sstatic.net/9BvbH.png I am attempting to create a python script that receives input from an HTML file and displays its output in the HTML. Below is the jQuery script in my HTML: <script src="https:/ ...

Issues with updating the style in Internet Explorer and Firefox using JavaScript are currently unresolved

I am facing an issue with my program where a name from an auto-complete is sent to a JavaScript function that dynamically creates a label with a button inside. Surprisingly, the DOM methods used to set style properties do not work in Firefox and IE 7, but ...

Glitch in jQuery causing multiple instances of text being added to a textarea consecut

I created a response system where users can reply to comments by clicking on the reply button. Once clicked, it triggers a function that adds @user at the beginning of their comment: $('#inputField').focus(); $('#inputField').text(&apo ...

Working with jQuery: Retrieving the ID of an element that is generated dynamically

Looking for some guidance: I'm working on dynamically creating a table based on the response from an AJAX call. The table includes headers and rows, with new rows added using the "after" function. However, I'm facing an issue with accessing the ...

Is there a way for me to manipulate the RGB values of the canvas in such a manner that the Red and Green components of the gradient are determined by dividing the position of my mouse cursor

My homework assignment requires using RGB colors where the red value is set to 0, green is the x-coordinate divided by 2, and blue is the y-coordinate divided by 2. I've been trying to control the colors on a canvas using addColorStop functions, but I ...

Tips for eliminating the bottom border on a nav-tab in Bootstrap 4

When a vertical navigation menu item is selected, a border appears below the item. How can I remove this border? .nav-tabs li, .nav-tabs li a { border-bottom: none; } Here is a picture of the border: ...

Finding a specific cell in a Django HTML table: tips and tricks

Recently delving into django and its intricacies. Struggling to grasp the concept of accurately pinpointing a specific cell within an html table. Yearning to modify the background color of select cells based on certain conditions derived from the generate ...

Vue application experiencing issues with applying Sweet Alert CSS styles

I successfully integrated vue-sweetalert2 into my Vue app (version 2.6). The setup in my main.js file looks like this: import VueSweetalert2 from 'vue-sweetalert2'; Vue.use(VueSweetalert2); In one of my components, I have a basic button with a ...

The issue with using HTML code inside a variable that is passed into a partial view is that the code is not being

I have created a partial view to show the page header, which takes in two variables - an Icon and a title. The code for pageHeader.blade.php is as follows: <div class="page-header"> <div class="row"> <!-- Page header, center on small sc ...

How can you achieve the effect of "hovering over an image to automatically start playing a muted video within the image itself"?

[![enter image description here][1]][1]I am working with WordPress and Elementor, and I want to create a hover effect where an image triggers a muted video to play within the image area on mouseover. The video should stop playing when the mouse is not hove ...

Show advancement bar during the process of creating an Excel spreadsheet in php

I'm currently working on an HTML form that generates an Excel file using PHPExcel when submitted. Everything is functioning correctly, but I've noticed that the process takes a long time for large excel files. I would like to implement a progress ...

The minmax() function is causing grid items to not wrap correctly

I have been working on creating a responsive web page where the "boxes" automatically adjust to a new row when the browser window is resized. To achieve this, I utilized the following CSS code: grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); ...

The NVDA screen reader is unable to detect visually hidden text

In the code snippet below, part of a table is shown where some text is meant to be displayed visibly and some should be visually hidden. However, it seems that the screen reader does not detect the visually hidden text when hovering over it with a mouse. T ...

What could be causing the text-end to fail in Bootstrap 5?

Why can't I align the text to the right? Feeling frustrated as a beginner. <div class="top-bar"> <div class="container"> <div class="col-12 text-end"> <p><a href="tel:06 ...