Guidelines for creating a uniform table border with varying border thicknesses

I have created a crossword puzzle generator that arranges words by rows and fills each cell with one letter. The keywords are distinguished by a wider border and a gray background, as shown in this image.

Here is the general structure of my code:

#crossword {
   border-spacing: 0px;
   border-collapse:collapse;
}

.word {
  vertical-align: middle;
  align-items: center;
  text-align: center;
  justify-content: center;
  margin: 0;
  padding: 0;
}

.cell {
  width: 30px;
  height: 30px;
  margin: 0;
  padding: 0;
}

.letter {
  border: 1px solid black;
}

.keyword {
  border-width: 2px;
  background-color: gray;
}
<table id="crossword">
    <tr class="word">
        <td class="cell letter">W</td>
        <td class="cell letter keyword">C</td>
        <td class="cell letter">A</td>
    </tr>
</table>

In my current implementation, the issue I am facing (as highlighted by red ellipses in the image) is that the top and bottom lines within the keyword cells extend beyond the borders of other cells. Removing the border-collapse property caused double borders at row connections.

Although I currently use table, trs, and tds for generation, I am open to switching to divs if it offers a solution.

So, here is my question: How can I ensure that the top and bottom borders remain inside the keyword cells instead of extending beyond them while uniquely addressing each cell?

I did find a workaround involving box-shadow, but it's not universally supported across all browsers and involves complex styling, so I would prefer an alternative approach if possible.

Answer №1

Ensure the outer border of the table matches the default border using the following CSS:

table#crossword tr:first-child td {
  border-top: 1px solid black;
}
table#crossword tr:last-child td {
  border-bottom: 1px solid black;
}
table#crossword tr td:first-child {
  border-left: 1px solid black;
}
table#crossword tr td:last-child {
  border-right: 1px solid black;
}

Although it may appear slightly off, this method is necessary to maintain an evenly aligned outermost border.

Demo

#crossword {
   border-spacing: 0px;
   border-collapse:collapse;
}

.word {
  vertical-align: middle;
  align-items: center;
  text-align: center;
  justify-content: center;
  margin: 0;
  padding: 0;
}

.cell {
  width: 30px;
  height: 30px;
  margin: 0;
  padding: 0;
}

.letter {
  border: 1px solid black;
}

.keyword {
  border-width: 2px;
  background-color: gray;
}

table#crossword tr:first-child td {
  border-top: 1px solid black;
}
table#crossword tr:last-child td {
  border-bottom: 1px solid black;
}
table#crossword tr td:first-child {
  border-left: 1px solid black;
}
<table id="crossword">
    <tr class="word">
        <td class="cell letter">W</td>
        <td class="cell letter keyword">C</td>
        <td class="cell letter">A</td>
    </tr>
    <tr class="word">
        <td class="cell letter">W</td>
        <td class="cell letter keyword">C</td>
        <td class="cell letter">A</td>
    </tr>
    <tr class="word">
        <td class="cell letter">W</td>
        <td class="cell letter keyword">C</td>
        <td class="cell letter">A</td>
    </tr>
</table>

Answer №2

Applying the 'box-sizing: border-box;' property specifically to the .keyword class resolves the issue of elements overflowing outside the table's outer border:

#crossword {
  border-spacing: 0px;
  border-collapse: collapse;
}
#crossword .keyword {
  box-sizing: border-box;
}

.word {
  vertical-align: middle;
  align-items: center;
  text-align: center;
  justify-content: center;
  margin: 0;
  padding: 0;
}

.cell {
  width: 30px;
  height: 30px;
  margin: 0;
  padding: 0;
}

.letter {
  border: 1px solid black;
}

.keyword {
  border-width: 2px;
  background-color: gray;
}
<table id="crossword">
    <tr class="word">
        <td class="cell letter">W</td>
        <td class="cell letter keyword">C</td>
        <td class="cell letter">A</td>
    </tr>
<tr class="word">
        <td class="cell letter">B</td>
        <td class="cell letter keyword">K</td>
        <td class="cell letter">L</td>
    </tr>
<tr class="word">
        <td class="cell letter">F</td>
        <td class="cell letter keyword">R</td>
        <td class="cell letter">V</td>
    </tr>  
  <tr class="word">
        <td class="cell letter">T</td>
        <td class="cell letter">E</td>
        <td class="cell letter">Q</td>
    </tr>  
</table>

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

Utilizing float positioning in Responsive Web Design

Attempting to implement a two-column float CSS layout with specific width percentages. My CSS styles for the two columns are as follows: .div1 { width: 25%; float: left; } .div2 { width: 75%; float: right; } .container { width: 960px; } @media scr ...

The Chrome browser allows interaction between two separate divs, where a button located in one div can impact

One issue I encountered involves a button located within a div that has unintended consequences for another div. Here is the basic structure of the elements: <div> <div> <div> <div> <butto ...

Error message: The import from './components/headerComponent/header' failed because it does not have a default export. Make sure to export it as default to be able to import it

I've been trying to bring a header from one file into another, but it's not cooperating. import React from 'react'; import { Typography, Card, CardContent } from '@material-ui/core'; import Header from './components/head ...

Having trouble extracting data from Moz Bar through selenium using Python?

Having trouble retrieving the spam score from Moz bar using Selenium WebDriver? I've attempted various methods such as XPath, class, and tag name without success. Any assistance would be greatly appreciated: from selenium.webdriver.common.by import By ...

Tips for saving the visibility status of a <div> in your browser bookmarks?

Currently, I am in the process of developing a single webpage (index.html). The navigation bar at the top includes links to hash references (DIV IDs). If JavaScript is enabled, clicking on these links triggers a JavaScript function with the onclick attribu ...

The issue at hand is that one of the JavaScript buttons is functioning properly, while the other is not playing the video as intended. What steps can

I've been working on a script for my school project website that should make buttons play videos, but I'm running into an issue where it only works for the first button (btn). Programming isn't my strong suit, and I put this together based o ...

How can I force a variable width font to behave like a fixed width font in HTML?

Is there a method to emulate the appearance of a fixed-width font using a variable width font in HTML? For instance, if I were to display the phrase "The quick grey fox jumped over the lazy dog" in "Courier New," a fixed-width font, it would appear wider ...

What is the best way to showcase my products in a horizontal line? I am seeking a solution using PHP and Bootstrap to exhibit all the items retrieved

I am experiencing an issue with displaying the most recent events from my database using PHP. The Bootstrap framework and CSS are causing them to appear as a single column on the page, but I would like them to be displayed in rows of three. I have attempt ...

Customizing Bootstrap SCSS variables by substituting them with different Bootstrap variables

What is the most effective way to customize variables? I have found that following this specific order yields the best results: @import "bootstrap/_functions.scss"; $primary: red; @import "bootstrap/_variables.scss"; @import "bo ...

Retrieving CSS properties of an element using JavaScript

How can I efficiently retrieve all CSS rules associated with a specific element using JavaScript? I am not seeking a particular solution, just looking to capture all CSS rules for the given element. For example, consider the following HTML code: <div ...

Can you help me with compiling Twitter Bootstrap 3.0 on my Windows 8?

Can anyone guide me on compiling Twitter Bootstrap using Less on a Windows machine? I tried following a tutorial but ran into issues with installing lessc (it was not in the npm registry). Also, the tutorial was for BS2 and not 3.0 which made me skeptical ...

Is it possible for CSS to prevent the insertion of spaces?

When filling out a form, I am able to insert spaces in inputs but not in the textarea (which is necessary). Interestingly, inserting spaces in the textarea works flawlessly. <form action="/#wpcf7-f519-o1" method="post" class="wpcf7-form" enctype="mu ...

The Function(JS) is specifically designed to only function within the topmost div or quote

Currently, I am facing an issue with a function I have implemented. This function adds a blur effect to words in a Blockquote when the page is loaded. However, I need this function to work for all Blockquotes and different divs on the page, not just the to ...

Utilizing a background image in the slick slider

<div id="largeCarousel" style="display:inline-block; float:right;"> <div class="homepage-item" style="padding-bottom:52%; position:relative; box-sizing:border-box;"> <div id="largeCarouselContent" style="position:absolute ...

Achieving equal height for the englobing/parent div and the inner divs

To achieve a standard left, middle, and right column layout, it is necessary for me to enclose various inner divs of different heights within a surrounding/parent div. It is crucial that the parent div adjusts its height to match the tallest inner div, whi ...

Reorganizing content under an image as the browser is resized by implementing a media query

In the footer section of my document, I have text positioned next to an image (floated to the right). However, when I resize my browser to a minimum width of 768px, both the text and image lose alignment. My goal is to centralize both elements with the ima ...

Choose different components that possess the X designation

Can X number of elements (h1, p, span...) be modified only if they have a specific class? I'm searching for a solution like this: (elem1, elem2, elem3, elem4).class { /* add customization here */ } I previously attempted using parentheses, curly b ...

Combining the Power of Bootstrap with Yii Framework

As a newcomer to the Yii framework, I am looking to incorporate my Bootstrap design into this platform. One issue I have encountered is related to my custom CSS. In Bootstrap, we use navbar-default for styling the navbar. I customized my navbar color by ...

Using SVG background images in Internet Explorer versions 7 and 8: a guide to HTML, CSS,

I have created a unique SVG image to use as a background, but it's not displaying in Internet Explorer 8 and earlier versions. I tried using tools like or http://code.google.com/p/svgweb/, but they only support SVG for IMG and Object elements, not ba ...

When scrolling the page, the Circle Mouse Follow feature will dynamically move with your cursor

Hey everyone! I'm currently working on implementing a mouse follow effect, but I'm facing an issue where the circle I've created moves along with the page when scrolling, instead of staying fixed to the mouse. Any suggestions or tips would b ...