Align text in the center of multiple <td> elements in a table horizontally

I have a schedule table in the form of a timetable. My goal is to have the borders between two or more consecutive tasks removed. I have achieved this. Additionally, I want the text to be centered within each cell of the table.

For clarification, refer to the example below:

var tds = document.querySelectorAll("td")
for (var o = 0; o < tds.length; o++) {
  if ($(tds[o]).next().html() === $(tds[o]).html() && $(tds[o]).prev().html() === $(tds[o]).html()) {
    $(tds[o]).css("borderRight", "none");
    $(tds[o]).css("borderLeft", "none");
  } else if ($(tds[o]).next().html() === $(tds[o]).html()) {
    $(tds[o]).css("borderRight", "none");
  } else if ($(tds[o]).prev().html() === $(tds[o]).html()) {
    $(tds[o]).css("borderLeft", "none");
  }
}
table {
  border-collapse: collapse;
}
td {
  border: 2px solid grey;
  padding: 25px;
  text-align: center;
}
th {
  border: 2px solid grey;
  padding: 25px;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>1:00</th>
      <th>2:00</th>
      <th>3:00</th>
      <th>4:00</th>
      <th>5:00</th>
      <th>6:00</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Sleep</td>//There should only be one "Sleep" and it should be centered across all "Sleep td"
      <td>Sleep</td>
      <td>Sleep</td>
      <td>Sleep</td>
      <td>Sleep</td>
      <td>Wake Up</td>

    </tr>
  </tbody>
</table>

Answer №1

Utilize the colspan attribute.

var tableCells = document.querySelectorAll("td")
    for (var i = 0; i < tableCells.length; i++) {
            if ($(tableCells[i]).next().html() === $(tableCells[i]).html() && $(tableCells[i]).prev().html() === $(tableCells[i]).html()) {
                $(tableCells[i]).css("borderRight", "none");
                $(tableCells[i]).css("borderLeft", "none");
            } else if ($(tableCells[i]).next().html() === $(tableCells[i]).html()) {
                $(tableCells[i]).css("borderRight", "none");
            } else if ($(tableCells[i]).prev().html() === $(tableCells[i]).html()) {
                $(tableCells[i]).css("borderLeft", "none");
            }
        }
table {
        border-collapse: collapse;
    }

    td {
        border: 2px solid grey;
        padding: 25px;
        text-align: center;
    }

    th {
        border: 2px solid grey;
        padding: 25px;
        text-align: center;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <thead>
        <tr>
            <th>1:00</th>
            <th>2:00</th>
            <th>3:00</th>
            <th>4:00</th>
            <th>5:00</th>
            <th>6:00</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="5">Sleep</td> //There should only be one "Sleep" cell and it should be center-aligned across all "Sleep" cells        
            <td>Wake Up</td>            

        </tr>
    </tbody>

Answer №2

To enhance the layout, simply eliminate the

<td>Sleep</td>

Instead, insert:-

<td colspan="5">Sleep</td>

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

Can a linked checkbox be created?

Is it possible to create a switch type button that automatically redirects to a webpage when turned on by clicking a checkbox? I'm working on implementing this feature and would like to know if it's feasible. ...

utilizing a CSS3 animation for the seamless loading of a webpage

I stumbled upon a website recently and was fascinated by the page load effect it had. I'm curious to know if it was created using jQuery or CSS3 animations with sprites. Upon examining the site, I discovered that the loading logo is actually made up ...

How can I prevent Heroku from automatically running the script with 'npm start'?

I am currently in the process of developing a server-based application that utilizes automated scripts, also known as "bots," within a cloud environment. I have set up Heroku Scheduler to execute one of these scripts automatically, as illustrated in Figure ...

Encountered difficulties while attempting to install bourbon using ember framework

I am currently using ember-cli version 2.9.1, node version 9.6.1, bower version 1.8.2, and npm version 5.6.0. To create a sample ember app, I ran the following commands: ember new // to start a new ember project. ember g template application //To creat ...

There is a single div sandwiched between two others, with each of the surrounding divs reaching the edge of the browser

Require a small space between the middle div and the two on each side. The middle bar should have a fixed width. Here is my attempt so far, but it seems messy: <!DOCTYPE html> <html> <head> <style> #container { width: auto; ...

What causes the for loop to run indefinitely?

window.TicTacToet.compMove = function (row, col) { var player = window.TicTacToet.PlayerTurn; var board = window.TicTacToet.Board; for (i = 0; i < window.TicTacToet.Board.length; i++) { for (j = 0; j < window.TicTacToet.Board[ ...

What does {``} denote in react-native programming?

During my participation in a collaborative project, I noticed that there is a significant amount of {' '} being used. For instance: <Text> {' '} {constant.Messages.PointText.hey} {this._user.first_name || this._user ...

Assembly of Components

As someone new to angular, I am currently in the process of building an angular2 application. My goal is to dynamically create a series of DOM components using the data provided below: // Class construct with properties sorted alphabetically export class ...

Update the fixed reference to `localhost` with a variable that can

I'm looking to make the localhost in my code dynamic instead of hardcoded. Here's the original code: export default { data: function() { return { localhost: "http://10.10.10.132/", }; }, } Could ...

Add a transition or animation effect to the <details> element as it closes

I am experimenting with adding a transition effect to the details element when it opens and closes: details { height: 1em; transition: height 2s; border: 1px solid; overflow: hidden; white-space: pre; } details[open] { height: 6em } <det ...

Issue encountered during an asynchronous call to an ASP.NET web method using $Ajax

Currently, I am dealing with an async web method (asmx file) and I am trying to call this method within an ajax jquery method. However, I am encountering numerous issues because the method also utilizes Entity Framework for various functionalities. Below ...

Developing a quiz using jQuery to load and save quiz options

code: http://jsfiddle.net/HB8h9/7/ <div id="tab-2" class="tab-content"> <label for="tfq" title="Enter a true or false question"> Add a Multiple Choice Question </label> <br /> <textarea name ...

What is the reason for the attribute align="center" functioning in my HTML document?

Recently, I decided to delve into the world of HTML and created a simple basic file: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> My Webpage </title </head> ...

Is there a way to implement a bootstrap carousel where only one item moves with a single click?

Having trouble implementing a Bootstrap multi-item carousel with continuous movement after copying code from this source. The effect looks different in my localhost environment. Can anyone provide assistance? Links to CSS and JS Added: <link rel="st ...

Adjusting the decimal points of numbers within a table to create the illusion of a "centered" alignment within the cell

Looking for a table design featuring a column of decimal numbers, each with varying lengths before and after the decimal point, while keeping the decimal points aligned. The width of the column should be flexible, expanding to accommodate long numbers in ...

Pulling JavaScript - referencing an external script

I am currently facing a challenging dilemma that requires a simple yet intricate solution. My objective is to develop a script capable of playing chess on my behalf, utilizing an engine to determine the optimal moves for pieces such as rooks, bishops, king ...

I'm encountering issues with adding a div element using Jquery

I've been attempting to insert a div using jQuery, following the code example below. Unfortunately, it's not working as expected. jQuery: $('<div />', { $('<img />', { "src": "/Pages/Images/calendar.png").add ...

unable to assign value to `visibility` or `display` properties

I am having an issue with a dropdownlist box in the datagrid that I need to hide or show. I can retrieve the element, but when I try to hide it, I encounter an error 'Unable to set property 'display' of undefined or null reference.' I h ...

Trouble Strikes: Heroku Deployment Fails for Node.js/React App

Currently, I am in the process of working on a React/Node application using create-react-app and attempting to deploy it through Heroku. However, we are encountering an error message related to react-scripts when deploying on Heroku: Error: sh: 1: react-sc ...

"Utilize various sets of data (node.js, mongodb) for your project

I'm just starting out with node.js and mongoDB. I have multiple collections in my database - one for users, one for articles, and potentially more in the future. In my server.js file, I am trying to write to each of these collections. I've trie ...