2

I want to add custom css to my gutenberg table block. I want to adjust the css width of the tds. Ive tried selected the "Fixed Width table cells" toggle but the table seems to have different widths. Is there a way to do that because all of the plugins I have used dont seem to work. I tried adjusting the css in the theme.css and style.css files in css/dist/block-library folder. Do I need to compile it and that is why I am not seeing the changes?

in the theme.css folder


.wp-block-table td,
.wp-block-table th {
  padding: 0.5em;
  border: 1px solid;
  word-break: normal;
  width:460px; //this is what i added
}

in the style.css folder

.wp-block-table .has-fixed-layout td,
.wp-block-table .has-fixed-layout th {
  word-break: break-word;
  width:460px; //this
}
.wp-block-table.alignleft td,
.wp-block-table.alignleft th, .wp-block-table.aligncenter td,
.wp-block-table.aligncenter th, .wp-block-table.alignright td,
.wp-block-table.alignright th {
  word-break: break-word;
  width:460px; //this
}
.wp-block-table.is-style-stripes th,
.wp-block-table.is-style-stripes td {
  border-color: transparent;
  width:460px; //this
}
1
  • 1
    Do not edit core files. Ever. If you’re any to style something add CSS to your theme or Additional CSS section of the customiser. Blocks a text just HTML so CSS works the same as anything else. May 17, 2022 at 4:34

1 Answer 1

0

One way to achieve this is to add a button to the Table Block in the editor. You create your Table, select the Table block, then click the button and the custom CSS is applied - then Publish.

Move the code into a file in a new folder and call it from the functions file. Try this example...

Edit: This has a custom colour - you may need to change that to get it working.

/assets/custom.php
    function custom_block_styles() {

        register_block_style( 
            'core/table', 
            array(
                'name' => 'blue-stripes',
                'label' => 'Blue Stripes',
                'inline_style' => '.wp-block-table.is-style-blue-stripes tbody tr:nth-child(2n+1) { background-color: var(--wp--preset--color--pale-blue);}.wp-block-table.is-style-blue-stripes td, .wp-block-table.is-style-blue-stripes th {border-color: transparent;padding: 0 2px;}.wp-block-table.is-style-blue-stripes td:nth-child(n+2) {border-left: 2px solid #eee;}',
            )
        );
    }
    add_action( 'init', 'custom_block_styles' );
    
functions.php
require get_template_directory() . '/assets/custom.php';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.