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

The 2-way data binding feature in AngularJS is failing to function when used with a factory in a MEAN stack application

I am currently in the process of creating a single-page application that displays files from my local MongoDB database. However, I have encountered an issue where AngularJS is not displaying the data properly. When I retrieve the files using Postman to acc ...

“How can controllers from multiple modules be integrated into the markup using angular syntax?”

Here's this particular question on SO that suggests the possibility of utilizing controllers from different modules by defining specific tag attributes like: <div ng-controller="submodule1.controller1">{{ whatever1 }}</div> <div ng-con ...

Discovering duplication in multiple rows within Angular ui-grid

As a novice in AngularJS, I have a straightforward query. Are there any built-in methods or example code in ui-grid that can be used to detect duplicate values across multiple rows? I am looking to highlight the cell where a duplicate value is found. ...

Show just a single error message if there are two validation errors present

In my AngularJS timepicker, users can choose multiple time segments for each day. The code has validation to detect duplicates and overlapping time segments. For example, entering 11:00am - 12:00am twice will trigger two error messages: 'Overlapping t ...

Exploring elements in Javascript

I am attempting to retrieve values from various elements when a 'a' element is clicked. Consider the following code: <tr data-id="20"> <div class="row"> <td> <div class="btn-group"> <a data-checked= ...

The element.find() function is experiencing issues when utilizing a templateUrl within a directive

My aim is to apply focus on an input field using a custom directive within a form. Initially, everything was functioning correctly when utilizing the template property in the directive. However, upon transferring the template into a separate HTML file usin ...

ImageMapster for perfect alignment

I'm struggling with centering a div that contains an image using imagemapster. When I remove the JS code, the div centers perfectly fine, indicating that the issue lies with the image mapster implementation. It's a simple setup: <div class=" ...

What could be causing my directive to not display my scope?

I am currently developing a directive that includes a custom controller, and I am testing the scope functionality. However, I am facing an issue where the ng-show directive is not functioning as expected when trying to test if the directive has a scope fro ...

transforming an angularJS directive into an angular6 component/directive

I have inherited an angularJS directive from an old application that I need to convert to angular6. Since I am not familiar with angularJS and only work with angular6, I am seeking guidance on how to proceed. Below is the existing angularJS code: define ...

Does the use of CSS positioning impact the performance of browser rendering?

Given two DIVs, A and B, where A contains B, and the following CSS styles: A { margin-left: -2000px; } B { margin-left: 2000px; } With these CSS styles applied, the position of B remains unchanged compared to its original position without any CSS. I am c ...

The .keypress() function isn't behaving as expected

I've encountered a coding dilemma. Below is the code in question: $(document).ready(function () { var selectedDay = '#selected_day'; $(function () { $("#date").datepicker({ dateFormat: "DD, d M yy", a ...

What causes the Footer to appear as a floating element in Tailwind CSS?

I've implemented the tailwind CSS footer into my NextJS project to display fetched data. However, I encountered an issue where the footer only floats when there is less data present. To address this problem, I created a separate footer file within my ...

The datepicker feature has been programmed to only allow past or present dates

I've integrated a date picker on a website like so: <input type="text" id="popupDatepicker" placeholder="Select Date" name="from_date" class="input" size="50" /> Here's the script being used: $(function() { $('#popupDatepicker&apos ...

Is it beneficial to vary the time between function calls when utilizing setInterval in JavaScript?

My website is displaying two words one letter at a time, with a 0.1s delay between letters and a 3s pause after each full word. I attempted using setTimeout, but it's not functioning as expected. What could be the issue in my code? var app = angular. ...

Despite the fact that `ng-show="true"`, it is still hiding due to having the class `ng-hide`

I'm a beginner when it comes to AngularJS, so I may be missing a straightforward solution to my issue. The challenge I'm facing involves working on a form with two inputs - one for the number of doors and the other for the number of windows. I ha ...

Is it possible to position my label above my text box input?

Having trouble aligning the checkbox label with the edge of the text area in my login form code. It always ends up on the right side, outside of the div container when inspected. I've attempted setting a left value for the label, but it causes issues ...

Access scope information when clicking with ng-click

I am currently using a php api to update my database, but I want the ability to choose which item gets updated with ng-click. app.controller('ReviewProductsController', function ($scope, $http) { $scope.hide_product = function () { ...

"Discover the latest feature in the new Angular router: automatic scrolling functionality

The old ui-router and 1.4 angular router used to support autoscroll="true" to automatically scroll the page to the top when navigating to another route. Is there a way to achieve this with the latest new angular router? It seems like ng-outlet does not ha ...

What could be causing the target to malfunction in this situation?

Initially, I create an index page with frames named after popular websites such as NASA, Google, YouTube, etc. Then, on the search page, <input id="main_category_lan1" value="test" /> <a href="javascript:void(0)" onmouseover=" window.open ...

Positioning Multi-level Drop Down Div in Javascript - How to do it efficiently?

I'm currently working on a horizontal menu using CSS and JavaScript with multiple levels. I've implemented a toggle function to show the submenu container, but it's causing the links below it to be pushed down. Is there a way to make the dis ...