Align the content in the middle of an inline-block division

I have been struggling to center the content of my divs both horizontally and vertically. I have come across some threads discussing this issue, but I have not been able to successfully implement it in my code.

In my setup, there are three divs (.block) that are supposed to be centered inside another div (.container), which is also centered on the screen. However, the content of the blocks is not centered vertically. I attempted using line-height, but it caused text to overflow out of the block.

Below is a snippet of my code:

.container {
    width: 70%;
    margin: 10px auto;
    position: relative;
    text-align: center;
    border: 1px solid black;
}

.block {
  background-image: -webkit-linear-gradient(150deg, #D7D7D7, #979797);
  background-image: -o-linear-gradient(150deg, #D7D7D7, #979797);
  background-image: linear-gradient(240deg, #D7D7D7, #979797);
  border-radius: 10px;
  height: 100px;
  width: 100px;
  display:inline-block;
  margin: 10px;
  padding: 10px;
  vertical-align: middle;
  line-height: 1.5;
}

<div class="container">
    <div class="block">Hello</div>
    <div class="block">Everybody</div>
    <div class="block">Would like to center it please</div>
</div>

I tried enclosing the content in a p tag, which improved the alignment slightly, but it still remains off-center.

Any advice or pointers would be greatly appreciated. Thank you!

Answer №1

To achieve centering with flex, you can utilize both the container and the items within it by applying the following settings. In this scenario, each container's text content is automatically considered a flex item, enabling easy centering.

.container {
  width: 70%;
  margin: 10px auto;
  text-align: center;
  border: 1px solid black;
  display: flex;
  justify-content: center;
}

.block {
  background-image: -webkit-linear-gradient(150deg, #D7D7D7, #979797);
  background-image: -o-linear-gradient(150deg, #D7D7D7, #979797);
  background-image: linear-gradient(240deg, #D7D7D7, #979797);
  border-radius: 10px;
  height: 100px;
  width: 100px;
  margin: 10px;
  padding: 10px;
  vertical-align: middle;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="container">
  <div class="block">Hello</div>
  <div class="block">Everybody</div>
  <div class="block">Would like to center it please</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

How to center align an input vertically with Bootstrap 3

I am having trouble vertically aligning an input in my project. Interestingly, my code works for a span but not for the input! I'm puzzled - what could be the reason behind this? JSFiddle Here is the CSS code snippet: .float { display: table-cel ...

What is the most effective method to make a div span the entire width of the page?

My webpage has a header at the top and here is the HTML code: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <css link here> </head> <body onload="prettyPrin ...

The BorderWidth property is set to 2px, however, it is not visible on the

Struggling with adding a border to a div in my app built with react and material-ui. Here is the snippet of code I've been working with: <div className={classes.search}> ... </div> search: { ... borderWidth: '2px', borde ...

Picture featuring Bootstrap's column/div layout being broken

I'm encountering some challenges with integrating TinyMCE and Bootstrap into a CMS I built. On a particular page, there's a div outlined where users can select images from a modal by clicking on it. The selected image is then inserted into a Tin ...

"Create a function that allows for the dynamic addition of elements to a

I want to dynamically add more li elements, similar to the first one, by simply clicking a button. Unfortunately, the example provided on jsfiddle is not functioning as intended. document.onload = init; function init(){ document.getElementById('a ...

How can I automatically add and remove the active class to the navigation on a single page layout?

Is there a way to automatically add an active class to the navbar links when a specific navigation section appears as you scroll down a one-page layout? Similarly, can the active class continue to move to the corresponding section in the nav links as you s ...

Transmitting HTML5 application settings via URL

I am interested in creating an HTML5 app that utilizes the WebAudio API. This app will allow users to customize certain parameters. Users should be able to 'share' these settings by sending a share URL, which can be clicked by others to view the ...

Enhancing jQuery UI Tabs with HoverIntent

I'm currently working on incorporating HoverIntent into my jQuery tabs, but I seem to be facing some issues. Below is the code snippet that I've been using. Any suggestions or tips would be greatly appreciated. Thank you! The code was sourced di ...

Issue with CSS background images not resizing properly on iPad and iPhone devices

While my site's background image resizes nicely in Chrome and Safari using background-size: cover, I encountered an issue when testing it on an iPad or iPhone. The CSS background image appears to be extremely zoomed in, resulting in a horrible appeara ...

The value of Angular Input remains unchanged within a FormArray

I am experiencing an issue with the Sequence No in my PreprocessingForm's FormArray. When I add a new row, the Sequence No does not change as expected. <tr class="mat-row" *ngFor="let dynamic of PreprocessingForm.controls.arithmeticI ...

Make multiple images in one row resize proportionally instead of wrapping them to the next line

My phone's screen is small and I want the three images to remain in the same row. In case they don't fit, they should shrink proportionately to maintain alignment (refer to image at the bottom). Consider the code snippet below: <div id="exam ...

Can you place 4 table cells in the first row and 3 table cells in the second row?

Exploring the world of HTML and CSS designs for the first time. Here's a code snippet I've been working on: <html> <body> <table width="100%"> <tr> <td width="25%">&#160;</td> ...

Trouble with z-index | initial div out of four malfunctioning

Currently, I am attempting to practice by replicating the design shown in this image: https://i.sstatic.net/iIA2q.jpg However, I have encountered an issue that has been persisting for some time: https://i.sstatic.net/grABz.png I'm struggling to un ...

Using Python, Selenium, and Beautiful Soup to scrape LinkedIn data while being logged in and accessing user accounts

I have reached a point in my script where I am able to access a search page of profiles on LinkedIn. However, I am encountering difficulty in actually accessing these profiles. When attempting to view a profile, LinkedIn displays a message stating "You d ...

I'm puzzled as to why the color of my top border isn't displaying even though I've correctly implemented the border

Struggling to make the top border of my div change color. I've double-checked the syntax, and it all looks correct. border: 1px solid white; border-top-color: red; ...

Challenges with CSS floating and centering in responsive design

My footer consists of 3 elements structured in the following way: <div class="footer"> <div class ="jumperMenu"> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> ...

At the ready stance for a particular grade of students attending a class

I have created a page with a navigation menu that can be scrolled using the wheel mouse. My goal is to ensure that the li item with the 'selected' class is always positioned in the first position on the left. Is there a way to achieve this using ...

Tips for switching out images depending on the time of day

Currently, I have a script that dynamically changes the background color of my webpage based on the time of day. However, I am facing issues trying to implement a similar functionality for replacing an image source. The current code is also time zone-based ...

Implementing position sticky on a div depending on its content text - step by step guide

If the text inside the .newsDate matches the previous or next .newsDate, I want to make its position sticky when scrolling, until it reaches the next .newsMiddleCont div. What I'm trying to achieve: The same date on multiple news items should s ...

To view the element when the browser is not in full screen mode, the body overflow (both horizontally and vertically) can be used instead of resizing

I am trying to figure out how to create two vertical divs that stay the same size and height when the browser is not on full screen. I want users to be able to use the body's overflow scroll function to read both DIV elements along the x and y-axis. ...