Tips for designing a bordered HTML table that spans the full width of the page

Current Markup:

    <section class="Product-Info">
      <table>
        <tr>
          <th colspan="2">Product Infromation</th>
        </tr>
        <tr>
          <th>Product Name:</th>
          <td>Name</td>
        </tr>
        <tr>
          <th>Product Description:</th>
          <td>Description</td>
        </tr>
      </table>
    </section>

Desired Outcome:

Question:

How can I use CSS to add borders and adjust the width of my current HTML in order to achieve the desired look?

What I Have Tried:

I have attempted utilizing the following CSS:

  table {
    border: 1px solid black;
  }

However, this only adds a border around the table. How can I further customize it to match the desired outcome?

Answer №1

table {
  border-collapse: collapse;
  /* add this line to prevent "double" borders */
  border: 1px solid black;
  width: 100vw;
}

th{
color: white;
background-color: blue;
}

td{
background-color: white;
width: 70%;
}

td, th {
  border: 1px solid black;
}
<section class="Product-Info">
  <table>
    <tr>
      <th colspan="2">Product Information</th>
    </tr>
    <tr>
      <th>Product Name:</th>
      <td>Name</td>
    </tr>
    <tr>
      <th>Product Description:</th>
      <td>Description</td>
    </tr>
  </table>
</section>

Here is the snippet you requested!

Answer №2

I hope this information proves useful to you

section {
width:100wh;
}
table{
width:100%
}
<section class="Product-Info">
      <table border="1>
        <tr>
          <th colspan="2">Product Information</th>
        </tr>
        <tr>
          <td>Product Name:</td>
          <td >Name</td>
        </tr>
        <tr>
          <td  > Product Description:</td>
          <td  >Description</td>
        </tr>
      </table>
    </section>

Answer №3

To make it easier, in the example provided, you just need to add the background color to the table header cells (th). Here's how:

    th {
    background: darkblue;
    color: white; /* If you want white text on a dark blue background */
    }

To remove the standard border around the table cells, simply collapse the border on the main table element like this:

    table {
    border-collapse: collapse;
    }

Once you have done that, you can customize the borders of your table however you want - thickness, color, and style can all be adjusted to your preference.

Answer №4

My recommendation would be the following:

table, th, td {
  border: 1px solid black;
}

.Product-Info > table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  text-align: center;
}

.Product-Info tr > *:first-child {
  background: blue;
  color: white;
  text-align: left;
}

.w-25 {
  width: 25% !important;
  max-width: 25% !important;
}

.text-center {
  text-align: center !important;
}
<section class="Product-Info">
  <table>
    <colgroup>
      <col class="w-25 blue">
      <col class="">
    </colgroup>
    <tr>
      <th colspan="2" class="text-center">Product Information</th>
    </tr>
    <tr>
      <th class="text-left">Product Name:</th>
      <td class="text-center">Name</td>
    </tr>
    <tr>
      <th class="text-left">Product Description:</th>
      <td class="text-center">Description</td>
    </tr>
  </table>
</section>

Answer №5

Additional details provided below @Random-COSMOS' answer.

The table is considered a block-level element.

"A block-level element always starts on a new line and takes up the full width available. https://www.w3schools.com/html/html_blocks.asp"

You can set any desired width for the table (e.g., 400px or 30%) ==> 100% in your case (occupying 100% of its parent).

<table style="width: 100%;">

To define table borders in CSS, use the border property.

table, th, td {
  border: 1px solid black;
}

Off-topic - Creating Accessible Tables

For Web Accessibility => Establish relationships between header and data cells (scope="row" / scope="col"). Full article: https://www.w3.org/WAI/tutorials/tables/two-headers/

<table>
  <tr>
    <th scope="col" colspan="2">Product Information</th>
  </tr>
  <tr>
    <th scope="row">Product Name:</th>
    <td>Some Name</td>
  </tr>
  <tr>
    <th scope="row">Product Description:</th>
    <td>Some Description</td>
  </tr>
</table>

Answer №6

put simply:


table {
border-collapse: collapse; /* Adding this line prevents "double" borders */
border: 1px solid black;
}

table th,
table td {
text-align: left;
border: 1px solid black;
}

see the demonstration here

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 tip display script is unable to revert back to its original content

I managed to show a block of text in a td after a mouseover event, but I want to revert back to the original content on mouseout/mouseleave. The code I used is below. Please help. I am getting an undefined error when running the code. I suspect the issue l ...

The search bar on the screen does not span the entire width as expected using Bootstrap CSS styling

I have a search form with an input field and a button. When I place the form in a standalone HTML file, the input and button span across the width of the div. However, when I embed it into the specific div I am working on, it shrinks to the left. Here is t ...

Use JavaScript to Generate a hash Table

Take a look at my code below, it's supposed to extract data results and display them in an HTML Table. However, I can't seem to get it working: <html> <head> <script type="text/javascript"> data = { d : { resu ...

Unable to assign a value to a variable in JavaScript

Today, I delved into the world of JavaScript and decided to test my skills by creating a page that changes images when clicking on a div. Everything worked perfectly until I wanted to add an input element to specify how many steps to jump each time the but ...

Samsung Galaxy S7 can interpret alphabetical parameters as numbers in a link sent via SMS

When trying to open a text message with a new message on my website using a link, I encountered an issue specifically with the Galaxy S7. The following code works on most Android phones: sms:5555555555?body=JOIN However, on the Galaxy S7, the "?body=JOIN ...

Discovering the best method for accessing CSS classes locally within an Angular component

In order to style a 3rd-party component with custom styles, I need to access the dynamically generated css classname from within an angular component. Angular applies transformations to local css classnames for scoping purposes. However, when trying to st ...

What is the most effective way to showcase the value of a field nested within an object?

I am struggling to frame my question correctly and finding the right information on Google. Therefore, I will provide a simple example instead. If you can assist me by answering my question or guiding me in the right direction, I would greatly appreciate i ...

Dynamically switch between showing more and showing less content on each div

Whenever the specific show more button is clicked, the content should be displayed without causing the entire component to re-render. I have tried using a useState, but each time the button is clicked, the whole component re-renders, resulting in long load ...

The issue of Markdown not being recognized inside nested divs

As I develop a Bootstrap-themed website with Jekyll, I've encountered an issue when formatting content in markdown. My goal is to organize my posts using Bootstrap grids like this: <div class="row"> <div class="col col-md-6" markdown="1"&g ...

Making a request to access another HTML page using the GET method

Currently, I am utilizing HTML, JQuery, and NodeJS for my project. The main issue I am facing involves posting a form from index.html to Node, processing the data (database operations, etc), responding with JSON back to JQuery, and then redirecting to anot ...

JQuery is throwing an unhandled reference error

I'm currently working on a jQuery script that looks like this: <script type="text/javascript"> $(document).ready(function () { var $containerHeight = $('.container').height(); if ($containerHeight < 760) { ...

Crafting an iframe that dynamically adjusts with Tailwind CSS

I'm having trouble ensuring that an iframe fits properly within a div while using tailwind CSS. I'm struggling to make the iframe responsive, no matter what class I try. Can anyone suggest a class that would help me achieve responsiveness for an ...

the toggle feature is not functioning properly on smartphones and tablets

I have embarked on the journey of creating a responsive website. The @media tag is new territory for me, and I'm unsure if I've implemented it correctly. My goal is to have a slidetoggle menu for the nav when the window size is less than 550px. ...

I'm struggling to concentrate and unable to type in the email field

Today while working on a website, I encountered something strange. In the footer section of the site, there is a quick contact form. However, I noticed that in Firefox, I am unable to focus on the email field. Surprisingly, this issue does not occur in Chr ...

Unable to modify jwplayer css styles to customize the chromecast icon

Is there a way to customize the chromecast icon within the JWPlayer skin to have a specific color? I've identified that the styles I need to change are --connected-color and disconnected-color when inspecting the element. Despite my attempts through ...

Is it possible to position two fixed divs side by side?

I am currently in the process of building my online portfolio. I am looking to create two fixed divs that will not scroll, each occupying the full height of the page. The content between these two divs should be scrollable while the bars on the left and ri ...

Executing a JavaScript function within the HTML body and passing a variable as an argument to the function

I recently created the following HTML code: <html> <title> Upload Infected File </title> <body> <header id = "header"> <h1 align="center">Upload Malware File</h1> <p align="center"> Pleas ...

table with stationary header

I have been attempting to keep the header fixed so that, when scrolling down to view other data, the <th> elements always remain visible. I tried following the instructions in this post, but when adding position: block to the styles for <th> an ...

Tips for passing variables through onclick to JavaScript file

My task involves querying a database and implementing a filter based on country selection from a map. After writing the JavaScript and PHP code, everything seems to work fine except for one issue - passing a name with an onclick function to initiate the qu ...

Should the username be specified but the password left blank, then an error message will be shown

<div class="form"> <h1>Sign In</h1> <form action="" method="post" name="login"> <input type="text" name="username" placeholder="Enter username" required /> <input type="password" name="password" placeholder="Enter password" ...