Centering an image and text both vertically and horizontally within a container div

I am currently working on creating a series of thumbnail-style divs with fixed dimensions. These divs will contain an image and a title text, both of which need to be centered vertically and horizontally within the container div. While I have successfully centered the image, I am struggling to position the text correctly. My goal is to achieve this design using only CSS.

.thumb {
  height: 100px;
  width: 96px;
  padding: 3px;
  cursor: pointer;
}

.thumb img,
.thumb svg {
  height: 100%;
  width: 100%;
  max-height: 100%;
  max-width: 100%;
}

.thumb div {
  position: absolute;
}
<div id="Thumb" class="thumb ml-10 mt-10 float-left ui-sortable-handle">
  <img src="../something.svg">
  <div>Name of something</div>
</div>

If you have any suggestions for adjustments to the CSS or HTML code, please let me know.

Answer №1

Can you clarify what you mean by 'Both the image and the title text need to be in the vertical and horizontal center of the container div'?

Do you want them to be centered both horizontally and vertically at the same time? If so, you can achieve this using flexbox like the example below:

.thumb {
 display : flex;
 flex-direction: row; // change this to 'column' if you want vertical
 // for center
 align-items: center;

}

I hope this solution helps you with your layout.

Answer №2

/*To solve this issue, simply utilize a wrapper*/

.thumb {
      height: 100px;
      width: 96px;
      padding: 3px;
      cursor: pointer;
    }

    .thumb img,
    .thumb svg {
      height: 100%;
      width: 100%;
      max-height: 100%;
      max-width: 100%;
    }

    .thumb div {
      position: absolute;
    }
    
    
    /*The solution is to use a wrapper and set it as flex*/
    .wrapper{ 
    display: flex;
    align-items: center;
    justify-content: center;
    }
<div class="wrapper">
<div id="Thumb" class="thumb ml-10 mt-10 float-left ui-sortable-handle">
  <img src="https://www.f-cdn.com/assets/img/fl-logo-c555380d.svg">
  <div>Name of something</div>
</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

Prevent content overlap in Tumblr's sidebars by using CSS positioning techniques

I'm facing an issue with my blog on Tumblr where the sidebar keeps overlapping the main content. I have modified a premade template and am trying to position the sidebar in the top right corner using absolute or fixed positioning: #sidebar{ position: ...

Is there a way to delete a stylesheet if I only have limited information about the url?

I am attempting to dynamically remove a CSS stylesheet using JavaScript or jQuery. I am aware that the target stylesheet is located within the 'head' element and includes the text 'civicrm.css', however, I do not possess the full URL o ...

By simply clicking the link, the content comes to life with animation

Is there a way to replicate the scrolling effect seen on this website ? The content shifts to the left and reveals new site content. Please excuse my English. ...

Customize your webpage's style to reflect your unique identity with user

Is there a way to allow users to customize the style of a webpage? I know I need multiple CSS files, but how can I implement the code that enables this customization based on user preferences? ...

What could be causing the vertical alignment to be disrupted by the img tag in relation to line-height?

Trying to vertically align text within a div by adjusting line height to match div height has been successful with just text or small images. However, when larger images are added to the div, it causes the text to be pushed downward for some reason. Take a ...

Dynamic Column Image and Text Display

I am working on a website where I am trying to have an image on the left and a paragraph on the right, but when the screen size is reduced, I need the layout to switch so that the image is at the top and the paragraph is at the bottom. Even though I have ...

Difficulty executing for loop due to multiple buttons, resulting in malfunctioning buttons in JavaScript code

Currently encountering an issue while trying to implement modal windows on my first website. Everything works fine without the for loop, but I wanted to have multiple buttons and windows, so I decided to use a for loop to handle each button. Strangely, the ...

Converting data from Random.org into an integer using JavaScript

I have been working on a small web application to experiment with CSS animations. Although it's functioning, I'm seeking more genuine randomness. To achieve this, I am exploring the use of Random.org. How can I import the output from Random.org i ...

Angular Igx-calendar User Interface Component

I need assistance with implementing a form that includes a calendar for users to select specific dates. Below is the code snippet: Here is the HTML component file (about.component.html): <form [formGroup]="angForm" class="form-element"> <d ...

Finding all anchor tags in raw HTML using htmlagilitypack

My HTML document contains the following code snippet: string htmlData = "<!DOCTYPE html><html><Head></Head><body>&lt;div&gt;&lt;a target=\"_blank\" href=\"http://blender.palmbeachschools.org/GetFile ...

Implementing a pre-defined value for ajax in ajax programming for beginners

I'm looking for guidance on setting up a hardcoded value for the OnSuccess parameter. The code I'm referencing is not my own, but I believe it may be related to Ajax. I'm a bit confused about it. Specifically, I need to focus on the OnSucce ...

Omitting items from the duplicated text on Safari

I am looking for a way to allow users to copy and paste a block of text without including inline controls, such as buttons. In Chrome, adding user-select: none; to the controls achieves this. When the user selects the entire paragraph, the buttons are not ...

Extracting data from the web using PHP

I'm facing an issue with my code that scrapes data from a website. The current output looks like this: Agriculture Food Apparel I want to only display the first/nth category, for example just "Agriculture". I attempted: echo $sub_title[1].&ap ...

How can I set a video as the background of a div?

Can someone help me with setting a server-side video as the background of a div and ensuring that the text (h1) appears on top of it? I want the video to completely fill the div, hide the overflow, and be responsive. The div has a width of 100% and a fixed ...

The sub-menu options are constantly shifting... How can I keep them in place?

Here we go again... I'm having trouble with my sub-menu elements moving when I hover over them. I've searched for a solution but haven't found anything helpful. The previous advice I received about matching padding and adding transparent bo ...

The appropriate use of spacing after text in the context of HTML and

Why is it that when I add padding or margin, no extra space is created after the text? Here is the output: https://i.sstatic.net/1MwjE.png I have tried adding padding and margin after the Stay Tuned text, but it seems to be not working. Any insights on ...

Simple ways to conceal the style of the underline for disabled input fields

When customizing the appearance of an HTML element such as paper-input and disabled, a dotted underline style is automatically applied. https://i.sstatic.net/utwHg.png To modify the disabled styles, you can do the following: paper-input { --paper-input ...

What are the steps to remove text within a list item using jQuery or regular JavaScript in a WordPress theme?

I need to remove the word "previous" from an li element in a wordpress theme. the text to delete <ul class="flex-direction-nav"> <li class="flex-nav-prev"> <a class="flex-prev" href="#">Previous ...

Is there any practical reason to choose tables over CSS for controlling layouts?

As I dive into updating a large amount of code for a web application, I've noticed that tables are heavily used throughout to manage layout. Being relatively new to HTML programming, I find myself questioning the necessity of using tables when CSS co ...

TypeError: 'array' type does not contain the method 'fetch_attribute'

I've encountered an issue with a piece of code that I'm working on and I'm trying to retrieve a link from an element identified as "statistic_info_link". However, when I attempt to do so, I receive the following error message: AttributeErr ...