The table border is not being displayed when using the display:table CSS property

<html>
    <style type="text/css">
        .table   { display: table;}
        .tablerow  { display: table-row; border:1px solid black;}
        .tablecell { display: table-cell; }
    </style>
    <div class="table">
        <div class="tablerow">
            <div class="tablecell">Greetings</div>
            <div class="tablecell">universe</div>
        </div>
        <div class="tablerow">
            <div class="tablecell">apples</div>
            <div class="tablecell">bananas</div>
        </div>
    </div>
</html>

My intention was to have a black border around each row specified with the tablerow class. However, I am encountering an issue where the border is not appearing.

I also wanted to adjust the height of a row. When specifying the height in pixels it works fine, but using a percentage value does not produce the desired result. Here is what I tried:

.tablerow  { 
    display: table-row;
    border:1px solid black;
    position: relative; // Not having any effect and causing the border to disappear!!
    //position: absolute; // If set, rows overlap but the border displays
    height: 40%; // Works only when set in pixels, doesn't work when set in percentage
}

There seems to be something going wrong somewhere, but I can't pinpoint the exact issue. Any assistance would be greatly appreciated!

Answer №1

To achieve the desired outcome, make sure to include border-collapse: collapse; in your .table class like so:

<html>
<style type="text/css">
    .table   { display: table; border-collapse: collapse;}
    .tablerow  { display: table-row; border: 1px solid #000;}
    .tablecell { display: table-cell; }
</style>
<div class="table">
    <div class="tablerow">
        <div class="tablecell">Hello</div>
        <div class="tablecell">world</div>
    </div>
    <div class="tablerow">
        <div class="tablecell">foo</div>
        <div class="tablecell">bar</div>
    </div>
</div>
</html>

Answer №2

To create a border around table rows, avoid using the border attribute. Instead, give all cells a top and bottom border and add classes to the left-most and right-most cells for a left and right border respectively.

Check out this JS fiddle link

Additionally, don't forget about the border-collapse:collapse property which can also achieve the desired effect.

Answer №3

To enhance the appearance, it is necessary to include a border in the styling of the tablecell class.

In order to achieve a visually appealing layout, make sure to add border-collapse:collapse; to the table class as well.

For a demonstration, refer to this example: http://jsfiddle.net/jasongennaro/4bvc2/

UPDATE

In response to a query from a user:

If I apply a border to a div, shouldn't it appear on screen?

Indeed, when you assign the display property of the element to table, it will function as a table and adhere to the corresponding CSS rules for tables.

If your intention is to apply borders solely to the rows, utilize border-top and border-bottom, along with specific classes for the leftmost and rightmost cells as needed.

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

Struggling to make `align-items="baseline"` function properly

I'm encountering an issue with alignment in my sandbox project. Specifically, I want the bottom of texts with different font sizes to be on the same y axis but I can't seem to figure out how to make it happen. Check out this picture to see exact ...

The minimum height property in CSS seems to be ineffective on Firefox, but it functions properly on Internet Explorer

Within my facelets template, there is a div with a specified minimum height. However, this does not seem to be functioning correctly in the Firefox browser. <div class="body"> <table> <tr> <td v ...

Is it possible to adjust the size of a carousel image without compromising its quality?

In the carousel currently displayed on my website, there are three images that are enclosed within the following HTML tag: Despite exploring various online solutions, I have not been able to resolve the issue. Adjusting the height and width settings has o ...

What is preventing me from using JavaScript to remove this class?

Struggling to implement a skeleton loading screen with CSS classes and JavaScript. The idea is to apply the 'skeleton' class to elements, style them accordingly, then remove the class using a timeout set in JavaScript. However, I'm encounter ...

Is the div height set to 100% until the content surpasses the height of the browser window

Is there a method to make a div fill 100% of the available page height, until it contains enough content to require a scrollbar? // For example, if the browser height is 600px: <div> // Initially empty, so it will be 600px tall. </div> . ...

Increase the padding on the left side of a background image

UPDATE: Solution discovered at this link thanks to Mr. Alien I am attempting to create a heading that resembles the following Title ------------------------------------------------- I have an image that I intend to use as a backdrop for it. However, I& ...

Missing Bootstrap 3 Options Menu trip away

I created a table with 4 columns using the Bootstrap 3 grid system. Instead of utilizing the col-xs-* classes, which work well on mobile devices, I opted for the col-md-* classes. This decision was based on my preference to have larger and more visible ico ...

Incorporating dynamic HTML content into a Bootstrap 2.3.2 popover

I'm trying to create a dynamic Bootstrap popover with HTML content when hovering over a button. Here's what I have so far: $(".btn.btn-navbar").hover(function() { html = '<ul class="nav"><li><a href="#">hello</li>& ...

Modify the CSS of CKEditor using JavaScript after the instance has been initialized

When creating a CKEditor instance, the following steps are typically taken: require_once 'ckeditor/ckeditor.php'; $CK = new CKeditor(); $CK->basePath = './ckeditor/'; $config = array(); echo '<textarea name="content" id="con ...

Position a button and input element in alignment

I recently decided to challenge myself by recreating the Netflix registration page as a practice project. Using the flexbox model on the webpage, I encountered an issue with aligning the email input and the "Get Started" button. Despite trying different me ...

The CSS3 Border-Radius feature is showing some color bleeding on a border that is colored differently

I designed a dynamic-width container with rounded edges using border-radius, along with a bold border on one side of the container in a different color. Everything looks fine when the browser window is small, but as I expand the window size, I notice the ...

Steps for altering the primary color in PrimeNG__Changing the primary color

What is the most effective way to modify the default primary color for primeNG (saga-blue theme)? Altering --primary-color does not have the desired effect, as elements in node_modules/..../theme.css are styled using the main color hex rather than '-- ...

Displaying a div on click is simple with CSS and JQuery, but what if you want it to stay visible even after a second click? By utilizing classes within <li> tags instead of buttons

My current code involves clicking on a menu, which should reveal a list where each item has a corresponding section that slides down and slides back up when clicked. However, only the sliding down functionality is working at the moment. I'm new to jQu ...

Employing IF conditions within a form to refine options

I am looking to streamline the options in my form based on the car and transmission selected. I have set up the form, but I am struggling with the JavaScript code. I need help figuring out how to only display certain color options when Car 1 is chosen alon ...

Center text vertically within an HTML Bootstrap row

I am creating a Bootstrap row with three columns - col-sm-2, col-sm-8, and col-sm-2. In the first column, I plan to insert a card, in the second column some text, and in the third column a tooltip. This row will be repeated multiple times. As a beginner ...

"Unlock the secrets to commanding respect in the web form layout by mastering the art

I have a fieldset that includes a legend with potentially long text content. I need the legend to only take up 50% of the width of the fieldset. Currently, when the legend is too lengthy, it still occupies 50% of the space but causes the entire fieldset ...

CSS: Achieving Layered Effect with Child Image Over Parent Background Image

I have set a background image on the .section class in my CSS stylesheet. The background also has an opacity effect applied to it. However, I am facing an issue where the child img (which is a transparent png) is displaying behind the background image of t ...

Applicable exclusively to the parent list item

I am creating a menu with a submenu that has a varying line-height. Unfortunately, I am struggling to prevent this line-height from impacting the list items in my child (submenu). I attempted using ul.nav > li but it continues to affect the lower menu ...

Extensive titles disrupt the layout of a grid on smaller screens that adapt to different devices

My approach to creating a responsive grid involves using clean CSS. You can check out the example here: https://tympanus.net/codrops/2013/04/17/responsive-full-width-grid/ While this method works well for displaying gallery images with fixed-width titles, ...

browsing through a timeline created using HTML and CSS

I am currently working on a web project that involves creating a timeline with rows of information, similar to a Gantt chart. Here are the key features I need: The ability for users to scroll horizontally through time. Vertical scrolling capability to na ...