Ways to center text within a nested div in a parent div with table-cell styling

The issue I am facing involves a parent div with the CSS property display: table;. Nested inside is a child div with display: table-cell;.

Despite applying text-align: center;, the text within the second div does not align to the center as expected.

Here's the HTML snippet:

<div class="hometab" id="groupList">
    <div ng-repeat="group in groupsData">
      <div class="groupButton"
            ng-style="{'background-color':'#'+group.color}">
            {{group.name}}
      </div>
    </div>
  </div>

And here is the corresponding CSS code:

.hometab{
  z-index:10;
  position:fixed;
  width: 100%;
  top:100%;
  transform: translateY(-100%);
  height:10vh;
  overflow: auto;
  white-space: nowrap;
  display: table;
  border-collapse: collapse;
  table-layout: fixed;
  border-spacing: 10px;
  border-collapse: separate;

  background-color: #ebebeb;
  -webkit-animation: fadein 1s; /* Safari, Chrome and Opera > 12.1 */
  -moz-animation: fadein 1s; /* Firefox < 16 */
  -ms-animation: fadein 1s; /* Internet Explorer */
  -o-animation: fadein 1s; /* Opera < 12.1 */
  animation: fadein 1s;
}

.hometab > div {
  display: table-cell;
}

.groupButton{
    text-align: center;
  vertical-align: middle;
    color: white;
  font-family: "Roboto";
    font-size: 20px;
  height: 100%;
  font-style: regular;
  border: 3px ;
  border-radius: 15px;
  -o-border-radius: 15px;
  -moz-border-radius: 15px;
  -icab-border-radius: 25px;
  -khtml-border-radius: 25px;
  -webkit-border-radius: 15px;
}

My challenge lies in getting the vertical-align: middle; property to function properly. Strangely enough, removing the height attribute seems to resolve the issue.

Answer №1

It seems like there may be some interference with your styles. I recommend checking the inspector tool in your browser to troubleshoot. Your code appears to be functioning correctly.

* {
  padding: 0;
  margin: 0;
}
.hometab {
  z-index: 10;
  position: fixed;
  width: 100%;
  top: 100%;
  transform: translateY(-100%);
  height: 10vh;
  overflow: auto;
  white-space: nowrap;
  display: table;
  border-collapse: collapse;
  table-layout: fixed;
  border-spacing: 10px;
  border-collapse: separate;
  background-color: rgba(0, 0, 0, .1);
  -webkit-animation: fadein 1s;
  /* Safari, Chrome and Opera > 12.1 */
  -moz-animation: fadein 1s;
  /* Firefox < 16 */
  -ms-animation: fadein 1s;
  /* Internet Explorer */
  -o-animation: fadein 1s;
  /* Opera < 12.1 */
  animation: fadein 1s;
}
.hometab > div {
  display: table-cell;
  background-color: rgba(0, 0, 0, .1);
  text-align: center;
}
.groupButton {
  text-align: center;
  vertical-align: middle;
  color: black;
  font-family: "Roboto";
  font-size: 20px;
  height: 100%;
  font-style: regular;
  border: 3px;
  border-radius: 15px;
  -o-border-radius: 15px;
  -moz-border-radius: 15px;
  -icab-border-radius: 25px;
  -khtml-border-radius: 25px;
  -webkit-border-radius: 15px;
  background-color: rgba(0, 0, 0, .1);
  display: inline-block;
  padding: 0 1em;
}
<div class="hometab" id="groupList">
  <div>
    <div class="groupButton">
      Lorem ipsum
    </div>
    <div class="groupButton">
      Lorem ipsum
    </div>
    <div class="groupButton">
      Lorem ipsum
    </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

Taking steps when a number is enclosed within a span

Testing a simple code with similar action to what I want. Apologies for any language errors, hoping to be understood :-) The HTML code snippet: <div class="pagination"> <a href="#" class=""><span>1</span></a> <a href=" ...

AngularJS's support for html5mode on GitHub pages is now available

Is it feasible for GitHub pages to accommodate AngularJS in html5mode? I came across a source online suggesting that it can be done with a fallback page for 404 errors. However, this solution seems flawed as it may result in multiple 404 errors, which wou ...

Tips for redirecting when the push notification is clicked in an Angular app that has been compiled using Cordova

I found this awesome plugin over at https://github.com/phonegap-build/PushPlugin Can anyone guide me on how to set up a redirect option to a specific view in my Angular app using this plugin? For instance, on an iOS device, the configuration looks like t ...

Creating distance between two text boxes in bootstrap is easy with the use of margins. By

I am looking to create a small white space between two text boxes. <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" /> <div class="row col-lg-12 col-sm-12"> <div class ...

Tips for simulating the $timeout service with sinon?

I am looking to write a unit test for a method within an Angular controller that uses the $timeout service. However, I have been advised not to use inject in this scenario. Therefore, I need to mock $timeout on my own. Can someone guide me on how I can a ...

Tips for pinpointing specific components inside a bespoke directive using protractor

Below is the unique custom directive I am currently utilizing: <custom-select custom-model="newStuff"> <div ng-if="itemsLoaded"> <select ng-disabled="model.disabled" data-placeholder="test" class="select-search select2-hid ...

The $http.get request triggers the success callback immediately before beginning the processing of the second call

I'm currently working on developing a basic hybrid app using the Ionic Framework. My goal is to implement an infinite scrolling feature, and here's my code snippet: .controller('DiscoverCtrl', function (uris, $scope, $cordovaToast, $h ...

Extract content within Python pre tags

A Python code snippet is being used to extract content between PRE tags: s = br.open(base_url+str(string)) u = br.geturl() seq = br.open(u) blat = BeautifulSoup(seq) for res in blat.find('pre').findChildren(): seq = res.string ...

Social media platform-inspired grid layout for displaying photos in a stylish and orderly manner

Looking to create a photos grid layout similar to Facebook using angularjs and bootstrap. Came across the angular-masonry plugin which seems like it could work. Here are some sample layouts I'm aiming for: Any tips on achieving this? Open to suggesti ...

When utilizing dynamic binding within *ngfor in Angular 4, the image fails to display properly

I'm encountering an issue with an <img> tag that isn't behaving as expected in my code snippet: <div *ngFor="let familyPerson of userDataModel.family" class="col-md-6 col-lg-4 family-member"> <div class="fm-wrapper"> ...

Utilizing unique background images tailored to different screen resolutions

In order to enhance the user experience on my website, I am looking to implement a feature that dynamically changes the background images based on the user's screen resolution. My plan is to use a small snippet of JavaScript within the <head> s ...

The functionality of images and links is compromised when they are assigned as values to properties within a JSON object

My images and links are not working, even after declaring them globally. When I write the src directly into src, everything seems fine but the alert pops up with the same URL and img src. Can anyone help? var combo0; var combo1; var combo2; var combo3; ...

Tips for aligning website content (including the body and navigation bar) at the center in rtd sphinx

Trying to create documentation with the Read the Docs theme for Sphinx documentation. Want to center the entire webpage, including both the body and left navigation bar so that as the window width increases, the margins on both sides increase equally. Righ ...

Steps to configure JavaScript to initially conceal a div within a continuous loop

I have implemented a JavaScript function to toggle the show/hide functionality of a div class. The content of the div and the button that triggers the toggle action are both inside a while loop. Initially, I used "div id" in my JavaScript code, but it was ...

Are you attempting to retrieve the most up-to-date trading price of a stock with Python?

After attempting to extract the LTP of a stock with the code below, I am not getting any value in return. Can you identify the error in the following code: from selenium import webdriver from bs4 import BeautifulSoup driver=webdriver.Chrome("C:&bsol ...

Leveraging multiple HTML files in Onsen UI

Recently, I came across onsen ui and fell in love with its design and overall feel. However, I encountered a problem: In the above link, it states: "Instead of creating menu.html and content.html in separate files, you can also define the page content in ...

Issues with AngularJS edit functionality for records not functioning as expected

I have implemented a feature on my page where users can add objects to an array. These objects are then displayed on the page along with links for editing each item in the array. Each added item is assigned a primary key, allowing users to edit it later e ...

Resizing Bootstrap Modal using React with a Personalized Dimension

Is there a way to manually set the width of Bootstrap's Modal React component on my website? I want to be able to define the width in pixels, and if possible, use a const object in the .jsx file for CSS. If not, I can resort to using a .css file. Be ...

Difficulty adjusting table heights in Dreamweaver, Struggling to set table height with <p style declarations

I'm currently designing an email signature in Dreamweaver using a table. However, I encountered an issue when I added the <p=style tag to customize my font size and color - it caused my table heights to change unexpectedly. Despite trying various s ...

Is it possible to simultaneously center and display an iframe inline?

I have a YouTube video that I'm trying to center within a div. After adding display: block; to the CSS following advice from How to center an iframe horizontally?, it centers correctly, but now I want to display two images inline next to the iframe ...