What is the best way to center a div vertically inside another div that contains text?

I am attempting to vertically center a div within a parent div that contains text. Here is my current setup:

The alignment appears a bit off, as the text seems centered properly while the yellow boxes are not. This is how I am currently approaching it:

.btn {
      background-color: #ccc;
      color: #000;
      width: 200px;
      border: solid 1px black;
      text-align: center;
      vertical-align: middle;
      display: table-cell;
}
.square {
      background-color: #ff0;
      width: 20px;
      height: 20px;
      display: inline-block;
}
.inner {
      display: inline-block;
}

<div class="btn">
  <div class="square"></div>
  <div class="inner">Hello</div>
  <div class="square"></div>
</div>

Is "table-cell" + vertical-align the correct approach? I am specifically targeting modern versions of mobile Safari and only care about HTML5, so older browser compatibility is not a concern.

Here is a link to a JS Fiddle demonstrating my issue:

http://jsfiddle.net/TrJqF/

Thank you!

Answer №2

There is actually no discernible difference in height between the two squares. To visualize this, apply a yellow background color to the inner class and observe how explicit and no height display.

The square divs lack content while the inner div contains text. The alignment of the css box is determined by the content within it. To demonstrate this, add empty space to the square divs like so:

  <div class="btn">
    <div class="square">&nbsp;</div>
    <div class="inner">Hello</div>
    <div class="square">&nbsp;</div>
   </div>

To achieve your desired look, you may consider adding a top and bottom margin of 1 or 2 pixels.

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

Trouble with incorporating ellipses across multiple lines in Angular 7

I am attempting to display ellipses in a multi-line block using Angular, but I am encountering the following problem. component.html <div class="test" [innerHtml]="anchor1.length >= 200 ? anchor1.substring(0,242) + '...' : anchor">< ...

Unlock the Full Potential: Enabling Javascript's Superpowers through PHP Execution

I specialize in PHP and JavaScript. Currently, I am attempting to incorporate JavaScript functionalities into my PHP code. However, I am encountering an issue where the code is not functioning properly. The PHP code that executes the JavaScript code is as ...

Modify the color of the text input border upon interaction using CSS

Changing text input border color using CSS upon clicking Hey there! I have a text field in my HTML: <input type="text" value="test" id="ff"> Currently, I am using the following CSS to define its border: #ff { border: 1px solid #000; } I would ...

Issues with retrieving the output of a function nested within another function through an ajax request

I am attempting to use jQuery to add the results of an ajax call to a paragraph. My goal is to extract the "myResult" variable from the inner getResult function and transfer it to the outer buildParagraph function. Unfortunately, the returned value is und ...

Utilizing pop-up info boxes

After thoroughly reviewing the documentation on Imagemapster's website and browsing several posts, I am still struggling to get the tooltips working correctly. I attempted to share my HTML and JS code on jsfiddle.com without success, so I have provide ...

A two-column bootstrap design with zero padding or gaps

This is the layout I am aiming for: https://i.sstatic.net/4LgVB.png Below is the code I currently have: <div class="row "> <div class="col-lg-6 row-eq-height push-right"> <img src="1.jpg" class="img-responsive"> ...

Strange Behavior of an Image in the Center

I've utilized CSS to center my image. .center-block{ margin-left:auto; margin-right:auto; } When I apply this code, it centers the images in a strange way. Here is my HTML & CSS code . You can see the image below to understand what the issue is ...

Utilize CSS to align text to the right with floating properties

I have devised the following: Now, I want the 'independent' section at the top to be aligned to the right like the 'interactive' section. I attempted to use float: right; but it didn't work. Here is my code: .badgesblock{style: ...

Learn the steps to include an HTML checkbox in an AJAX HTML editor

I need help with adding an HTML checkbox to an Ajax HTML editor in ASP.NET. I have attempted to do this but am having trouble checking and unchecking it. Below is the code snippet that I have tried. Any suggestions would be greatly appreciated. webform.as ...

What is the correct way to load a column of images without affecting the readability of the text alongside them? (see image below)

https://i.stack.imgur.com/rBL50.png Whenever my page loads, there is a card with a vertical list of images. Due to the loading time of the images, the text appears first and gets squished as shown in the provided screenshot. Is there a way for the text to ...

Issues with JQuery onclick function on dropdown menu functionality not behaving as desired

Take a look at some example code here. Here's the HTML: <div> HTML<br> <li> <select id="Status" type="text"> <option value="New">New</option> <option value="Complete">Complete</option& ...

Customizing the Class of a jQuery UI ui-autocomplete Combobox Container

Is there a way to customize the ui-autocomplete div by adding a custom class? I have several autocomplete widgets on my webpage, and I need to style their drop-downs differently. Since editing the ui-autocomplete class directly is not an option, I am wor ...

Heatmap of the day's hours - Issue with displaying colors

I've been working on creating a d3.js heatmap chart, and I've encountered an issue with the colors. Initially, I used code from this link and then modified it to read data from a PHP file that outputs JSON format data. However, after making these ...

Ways to remove content that exceeds the boundaries of a div element

I am facing a challenge with a parent div that has a specified size. Inside this parent div, there are other child divs, and if any of those child divs start displaying their content outside of the parent div, I want to completely remove that div. I do not ...

Several components utilizing a popup module

I have created a modal popup example where scrolling is disabled in the background but enabled within the pop-up itself. Check out my demo here: View Demo My challenge lies in implementing a grid of images on the website: Take a look at the type of grid ...

Tips on positioning an image to align with a paragraph on the right side

[customized scrollspy example] After removing the padding in the 'about us' section, I am facing an issue where the scrollspy is not aligning properly with the 'about us' content. I need assistance in fixing the scrollspy while ensurin ...

What is the best way to adjust image size based on different screen sizes while ensuring the buttons at the top remain responsive

How can I make the image and image select and delete buttons responsive? I am looking to eliminate the gap after the delete button. Any suggestions are welcomed. <div class="container mt-0"> <div class="row"> ...

To create space between the input field and button, utilize the input-group class for

Is there a way to insert a space between the input field and the button while using input-group in Bootstrap 3? Check out this jsFiddle for more information. <div class="container"> <div class="row"> <div class="col-md-12"> ...

What is preventing my h1 styles from being applied to an h1 element within a <section> tag?

I have a headline in my header and in another part of the content. I've been told that this can impact SEO, but I'm just starting out by copying other people's pages and trying to replicate their styles without looking at their code. The st ...

challenges with positioning div elements using relative CSS styling

This is a section of my code: CSS : .body .left, .right { position:relative; z-index:6; display:inline-block; } .body .left { top:0; left:0; width:100px; height:300px; margin-right:10px; borde ...