What is the best way to use CSS to format a table where specific column rows are offset downwards by half of their height?

Perhaps the answer is elusive or quite challenging to find. Here's a table I've coded:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
    table {
        font-family: verdana,arial,sans-serif;
        font-size:11px;
        color:#333333;
        border-width: 1px;
        border-color: #3A3A3A;
        border-collapse: collapse;
    }
    table th {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #3A3A3A;
        background-color: #B3B3B3;
    }
    table td {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #3A3A3A;
        background-color: #ffffff;
    }
</style>

<title>Table</title>
</head>

<body>

<table>
<thead>
    <tr><th>boundary</th><th>slice</th><th>h_i</th><th>1/2 (h_{i+1}+h_i)</th></tr>
</thead>

<tbody>
    <tr><td>0</td><td></td><td>0.00</td><td></td></tr>
    <tr><td>1</td><td>1</td><td>2.26</td><td>1.13</td></tr>
    <tr><td>2</td><td>2</td><td>4.37</td><td>3.32</td></tr>
    <tr><td>3</td><td>3</td><td>6.32</td><td>5.35</td></tr>
    <tr><td>4</td><td>4</td><td>5.74</td><td>6.03</td></tr>
    <tr><td>5</td><td>5</td><td>4.90</td><td>5.32</td></tr>
    <tr><td>6</td><td>6</td><td>3.61</td><td>4.25</td></tr>
    <tr><td>7</td><td>7</td><td>0.00</td><td>1.80</td></tr>
</tbody>
</table>

</body>
</html>

I would like the table to be styled with CSS so that rows in odd columns are shifted halfway down their height. Here is an example image of how it should look: Formatting the table to shift rows downwards

Kindly refrain from using row spanning in the table structure. While it may be possible to achieve the desired outcome by spanning four rows for even columns and two rows for odd columns, this method lacks elegance and results in unnecessary empty td tags.

The objective is to find a pure formatting solution using CSS, if such a solution exists.

Answer №1

Utilize the transform property:

table {
  font-family: verdana, arial, sans-serif;
  font-size: 11px;
  color: #333333;
  border-width: 1px;
  border-color: #3A3A3A;
  border-collapse: collapse;
}
table th {
  border-width: 1px;
  padding: 8px;
  border-style: solid;
  border-color: #3A3A3A;
  background-color: #B3B3B3;
}
table td {
  border-width: 1px;
  padding: 8px;
  border-style: solid;
  border-color: #3A3A3A;
  background-color: #ffffff;
}

:empty {
  opacity:0;
}

tbody tr td:nth-child(even) {
  transform: translateY(-50%);
  box-shadow:0 0 0 1px #3a3a3a
}
<table>
  <thead>
    <tr>
      <th>boundary</th>
      <th>slice</th>
      <th>h_i</th>
      <th>1/2 (h_{i+1}+h_i)</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>0</td>
      <td></td>
      <td>0.00</td>
      <td></td>
    </tr>
    <... more table rows ...>
  </tbody>
</table>

Alternatively, without CSS, use the rowspan attribute and empty cells on the last row as well.

td:empty {
  opacity: 0;
}

table {
  font-family: verdana, arial, sans-serif;
  font-size: 11px;
  color: #333333;
  border-width: 1px;
  border-color: #3A3A3A;
  border-collapse: collapse;
}

table th {
  border-width: 1px;
  padding: 8px;
  border-style: solid;
  border-color: #3A3A3A;
  background-color: #B3B3B3;
}

table td {
  border-width: 1px;
  padding: 8px;
  border-style: solid;
  border-color: #3A3A3A;
  background-color: #ffffff;
}

:empty {
  opacity: 0;
}
<table>
  <thead>
    <tr>
      <th>boundary</th>
      <th>slice</th>
      <th>h_i</th>
      <th>1/2 (h_{i+1}+h_i)</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td rowspan=2>0</td>
      <td></td>
      <td rowspan=2>0.00</td>
      <td></td>
    </tr>
    <... more table rows ...>
  </tbody>
</table>

Visit this link for more information on using rowspan and colspan in tables

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

Retrieve the initial 100 links using XPath and PHP

I wrote the following code to extract href's (links) from a web resource that contains more than 1000 links: $dom = new DOMDocument(); @$dom->loadHTMLFile('https://www.domain.me/'); $xpath = new DOMXPath($dom); $entries = $xpath->quer ...

How come I am unable to connect my CSS to my EJS files?

Having difficulty linking my css files to my ejs files. Using a standard node.js/express setup, I'm attempting to connect my main.css file from the public/css directory to my index.ejs file in views. The html and routing are functioning correctly. Ple ...

Showing two divs on top of an image inside a container

Hey there, I apologize for the messy title but I've been searching for an answer for a couple of hours now. <div id="lightbox"> <img src="blabla.png"> <div href="#" id="next" onClick="next(1)"></div> <div href=" ...

Can you help me figure out how to configure my page so that pressing the Enter key does not trigger a postback action?

I'm encountering an issue on one of my asp web pages with a textbox. I am using jQuery to display content in the textbox above it when the user hits the Enter key. However, it always triggers a postback, causing the displayed content to disappear imme ...

The Flowbite CSS plugin seems to be malfunctioning when attempting to include it within the tailwind.config.js file

Incorporating Flowbite (both JS and CSS) into my application has been a bit of a challenge. I managed to successfully insert the JS using Webpack and import 'flowbite' in the entry point JS file. As for the CSS, I followed the documentation and ...

Determining the Width of a DIV Dynamically with CSS or LESS Depending on the Number of Siblings

One of my challenges involves a parent DIV with a width set to 100%. Dynamically, this parent DIV is filled with numerous children DIVs. I am trying to calculate and assign their widths using only the calc method in CSS or LESS. This is because the flex ...

Creating personalized images using HTML

Looking to create a website where you can purchase personalized shoes? One unique feature is the ability to customize your pair of shoes by selecting stickers and choosing their color. These options are conveniently displayed in a panel next to the shoe im ...

Is it possible to eliminate the default placeholder text from Safari browser?

My goal is to design a form that includes date and time input fields. The placeholder text should move up by X pixels when the user clicks on the field. While the form appears fine in Chrome, there seems to be an issue with overlapping form fields in Safa ...

File bootstrap.min.css is currently experiencing compatibility issues

I am currently working on a website where I have two images that are displaying vertically. However, I would like these images to be displayed horizontally with animation. I attempted to use Bootstrap to achieve this horizontal layout, but when I include ...

What is the best way to create a single title that is expressed in two distinct languages

My website features content in two local languages, but all the titles are currently only in one language. I would like to have titles in both languages alternating, with title1 displaying for 3 seconds before automatically switching to title2. This mean ...

Ways to ensure a div matches the size of the div above it within a Boostrap grid column

Hello there, this is my very first question on this platform. I'm still in the early stages of learning all these concepts, so please be patient with me ...

The border is not displaying for the Child Div element

I am struggling with a webpage that has two nested divs. Despite trying all the suggestions I have found, I still cannot get the inner div to display its border. On the other hand, I have noticed that the parent div's border shows up without any issu ...

Comparing React's Styled-components, JSS, and Emotion: Which is

As a CTO, I am faced with the decision of choosing between three popular CSS-in-JS libraries: jss emotion styled-component. I will keep this question focused and avoid straying into subjective territory. If necessary, I will answer it myself by asking sp ...

Is it possible to target all children with CSS3 if a certain number of children are present?

I've been racking my brain trying to select all children if there are x or more children present. I want to be able to select all child elements if there are at least x children. So far, I've managed to target all children when there are exactl ...

Decoding "multipart/form-data" in .NET using C#

Have a question regarding .NET/C#? I am looking for a way to parse input post data in "multipart/form-data" format to extract the username and password without having to write my own parsing code. Does anyone know how to achieve this? The input post data ...

Why use @media (max-width:-1) in your code?

While reviewing CSS code created by another department, I noticed an interesting media descriptor: @media (max-width:-1) { ...standard css stuff here } The value of max-width cannot be less than 0, so what could be the purpose of this? Perhaps it was ...

Creating a link with a POST method and without using a new line

I am attempting to generate a square matrix and send data using the post method. I have discovered a solution involving JavaScript and form submission, but each time a new form is created, it results in a new line being added. Alternatively, if I move th ...

Update Table Row Background Color in Separate Table by Clicking Button Using JQuery

Two tables are involved in this scenario - one that receives dynamically added rows, and another that stores the data to be included. The illustration above displays these tables. Upon clicking the Edit button, the information from the selected row in Tab ...

Tips for pressing the enter key to submit when faced with two buttons

I am developing a form with two input fields and their respective submit buttons. I want users to be able to enter text into either field, hit the Enter key, and have it trigger the same action as clicking the submit button. Currently, pressing Enter after ...

show small previews of pictures stored in a JavaScript array by using a for-loop

Currently stuck on a university project where I've set up an array of images like this: var testdata = [ { "imageID": 17, "uploadedDate": "2020-07-31 03:56:56.243139", "filepathOriginal": &qu ...