What is the best way to create a border of white space around the background color of a table cell using CSS?

Is there a way to create a white space around the background color of a table cell using CSS? My current code has the background color extend all the way to the edge, and padding only affects the content, not the background color.

#main-monitor {
    width: 100%;
}

#main-monitor td, #main-monitor th {
    border: 3px solid #999;
    padding: 4px;
}

UPDATE: Below is the relevant CSS for these elements. I've made some adjustments but still face the same issue.

#main-monitor {
    width: 100%;
}

#main-monitor th {
    border: 3px solid #999;
    padding: 3px;
    text-align: center;
    padding-top: 12px;  
    padding-bottom: 12px;
}
#main-monitor td {
    background-color: white;
    border: 3px solid #999;
    margin: 2px;

}
#main-monitor td span {
    margin: 5px;
}

This code is applied within a partial view for a table:

<table id="main-monitor">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(m => m.Name)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.A)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.B)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.C)
            </th>
        </tr>
    </thead>
    <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>@item.Name</td>
                        <td style="background-color:@item.AColor"><span>@item.A</span></td>
                        <td style="background-color:@item.BColor"><span>@item.B</span></td>
                        <td style="background-color:@item.CColor">@item.C</td>
                   </tr>
                }
    </tbody>
</table>

Answer №1

Solution 1

In response to the suggestion made by "Advait S", you have the option of utilizing the border-spacing css property for table elements.

Here is an illustrative example:

td {
  background: red;
}

table {
  border-spacing: 10px;
}
<table>
  <tr>
    <td><span>xxx</span></td>
    <td><span>xxx</span></td>
  </tr>
  <tr>
    <td><span>xxx</span></td>
    <td><span>xxx</span></td>
  </tr>
</table>

Solution 2

Although Solution 1 provides a sophisticated approach, an alternative method involves changing the display attribute of your table cells to inline-block and then adding margin values to the cells accordingly.

Refer to the code snippet below for implementation:

td{ 
background:red;
margin:10px;
display:inline-block;
}
<table>
  <tr>
    <td><span>xxx</span></td>
    <td><span>xxx</span></td>
  </tr>
    <tr>
    <td><span>xxx</span></td>
    <td><span>xxx</span></td>
  </tr>
</table>

Answer №2

You might want to consider this alternative approach as well.

  1. When creating a table, enclose the text within a span element and style it accordingly.

HTML

<table>
  <tr>
    <td><span>Greetings</span></td>
  </tr>
</table>

CSS

td{
  border: 1px Solid Black;
  background-color: white;
}
td span{
  background-color: yellow;
  margin: 10px;
}

For a live demonstration, check out this link

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

Positioning of the navigation menu

After customizing my Bootstrap navbar, I noticed that when the responsive size changes to mobile, the burger menu opens on the left side. Is there a way to move it to the right? Any help would be appreciated. Here is the HTML code: <!DOCTYPE html> ...

What is the process for including echo HTML field in the WooCommerce order view?

I have successfully added HTML input fields within functions.php. However, these echoed fields are not displaying in WooCommerce orders after placing an order. I am looking for a way to include the values and labels of these fields in the orders view wit ...

An example of using quotes within quotes is an HTML tag embedded within JavaScript code

Currently, I'm working on a JavaScript code where clicking assigns the function getImage the source of an image to be displayed later on the page. The issue I'm facing revolves around dealing with quotation marks. <img src="bill.jpg" class=" ...

Swapping data between two HTML files and PHP

As I work on setting up a signup form for a MikroTik hotspot, I've encountered limitations with the device not supporting PHP. In order to pass the necessary variables from the device to an external PHP page where customer data will be saved and trial ...

Bootstrap 4 alert boxes extend the text to span the entire width of the alert

How can I make the paragraph text span across most of the box width in Bootstrap 4? <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJ ...

What are some effective methods for dynamically styling elements during iteration?

Need assistance with styling dynamic elements during iteration Located on the test.phtml page <?php foreach($tests => $test) { ?> <li class="<?php echo $test['class'] ?>"> <a href="#" title=""> Test In ...

Is there a way to eliminate the black seam that is visible on my floor mesh that has been separated? I am utilizing A

I recently imported a large .glb file into AFrame. The model has baked textures and the floor is divided into multiple mesh parts for improved resolution. However, I am facing an issue where black seams appear on the separated parts of the floor, only dis ...

Refine information by using checkboxes with the help of jQuery, MySQL,

How do I use PHP and jQuery to filter content with checkboxes? I'm getting a warning about an undefined index when I try to post the form via jQuery. Check out my JavaScript file: function filterContent(){ var all = $(".all").val(); $('input.al ...

Tips for saving the latest counter value in CSS and showcasing it as content in HTML

Here is an example of how my CSS code is structured: .page-number:after { counter-increment: page; } If we were to display 6 pages, it would look something like this: .page-number:after { content: 'Page ' counter(page) ' of'; } ...

Is it possible to add and delete DIVs simply by clicking on a link?

By utilizing a select dropdown list and appending the HTML code stored in the variable "code" (resembling <div>...</div>) right before the end of the div with ID mydiv using $(code).appendTo('#mydiv');. Within these dynamically added ...

The React Material UI table is having trouble stretching to fill the space

Currently, I am experimenting with developing my own virtualization technique using a different approach than react-virtualized. However, I am encountering difficulties when it comes to styling. I have come across an issue where the material table in the ...

Ensure that the content fills the entire height of the container and include a scrollbar

I'm currently utilizing CKEditor () and have developed my own customized file browser. The issue I'm encountering is that when the filebrowser is opened, it appears in a new popup window without scrollbars. I reached out for support through a ti ...

"Troubleshooting a Responsive Design Problem: Horizontal Scroll Bar Appears on Low Res

My website is experiencing an odd issue with responsiveness. When viewed on desktop resolution in Chrome, there is no horizontal scroll. However, when I resize the browser to lower resolutions (400px width and less), the horizontal scroll appears. It see ...

Having trouble resolving 'primeng/components/utils/ObjectUtils'?

I recently upgraded my project from Angular 4 to Angular 6 and everything was running smoothly on localhost. However, during the AOT-build process, I encountered the following error: ERROR in ./aot/app/home/accountant/customercost-form.component.ngfactory. ...

Angular - Uploading Files

Within my $scope.accept function, I am attempting to upload some files to my server. The process works fine when using the tag in my HTML. However, I wish to prevent the page from redirecting and instead handle this with AJAX in my controller. When tryin ...

What is the best way to target only the immediate children of a div when using tailwindcss?

Here is the HTML code snippet I am working with: <div class="section"> <div class="header">header</div> <div class="content"> <div>sub contents 1</div> <di ...

Sticky positioning with varying column widths

How can I create HTML and CSS columns that stick together without manually specifying the "left:" parameter every time? The current example in the fiddle achieves what I want, but requires manual setting of the "left:" value. Is there a way to make this m ...

CSS: choose all elements starting from the n-th element and apply styles to them

Is it possible to choose specific child elements beyond the first few in a parent element? To clarify, let's say there's a div containing 7 spans. How can I select only the spans starting from the 3rd element onwards - so specifically span 4, 5, ...

How can I turn off Angular Grid's virtualization feature, where Angular generates div elements for the grid based on height and width positions?

Currently, I am working with an Angular grid (ag-grid) that dynamically creates div elements in the DOM to display data as the user scrolls or views different sections. As part of my testing process using Selenium WebDriver, I need to retrieve this data fr ...

Tips on how to personalize the print layout of an Angular 4 page when pressing ctrl+p

I am working on an angular4 app and I encountered an issue when trying to customize the print page view by pressing ctrl+p in the browser. The page that needs to be printed contains only Angular components, and my attempts to modify the print view using ...