6

I can style a table in react via:

  var tableStyle = {
       "border": "1px solid black"
    };
    return (
      <div>
        <h1>My Awesome Table</h1>
        <table style={tableStyle}>
          <th>Awesome Header</th>

Coupling my style and html into a reusable component is the react way of doing things. How can I effectively style my whole table? I could style each header via:

<th style={headerStyle}>
<th style={headerStyle}>
<th style={headerStyle}>

and

<tr style={rowStyle}>
<tr style={rowStyle}>

That's not very efficient. In plain old CSS I can just do

 table {
       //boom style all the things
    }
    th {

    }
    tr {
    }

Using CSS, particularly in a SPA application can become a maintenance headache. So I like the idea of sticking my style into this component where nobody else will inherit it. How can I do it without writing a bunch of repetitive code?

8
  • I take it using regular css classes from a separate css file is not acceptable in your application? Commented Feb 5, 2016 at 22:34
  • Polymer scope polymer-project.org/1.0/docs/devguide/styling.html Commented Feb 5, 2016 at 22:34
  • @chris right. For reasons I mentioned I dont see this as a possibility Commented Feb 5, 2016 at 22:40
  • You can't create class names that are guaranteed to be unique? Commented Feb 5, 2016 at 22:46
  • I suggest also looking up the information about styling tables on w3schools. They have some very simple to follow instructions on there and great information! w3schools.com/css/css_table.asp Commented Feb 5, 2016 at 23:03

3 Answers 3

8

Not entirely sure I understand what you're looking for, but you want a better way of having css and markup in one file with no external dependencies?

If so, this might work:

return (
  <style>{`
    table{
     border:1px solid black;
    }
  `}</style>
  <div>
    <h1>My Awesome Table</h1>
    <table>
      <th>Awesome Header</th>
      ...
)

Using template literal string formatting seems necessary here to support the <style> contents to span across multiple lines.

Alternatively:

<style>{"table{border:1px solid black;}"}</style>
Sign up to request clarification or add additional context in comments.

4 Comments

I think something like return ( <div> <style scoped> styles </style> <table> ... would be better.
@MrLister, sure. Valid option, but it depends on the component I suppose and we don't know much about it. I think OPs' main issue was how to declare the css rules inside react, rather than how to apply them to each element. I would personally prefer to give the div a class and then style each html element that's a child of that div class. -- but that's just preference.
I want to thank you for writing that <style> example above, I have spent weeks on trying to find a way how to apply a style to a specific component at runtime only as I'm dynamically changing styles/etc. This method seems to work for me!!! thank you!! No need for class names in css etc etc, I love it!!!
You're welcome @Dariusz! I use a library called emotion (styled-components lib is similar) which gives tons of options to add styles. You could take a look there too.
0

You should have all of your styles in a separate folder/ and file and maintain separation of concerns. No CSS declarations should be in your JavaScript. If must style your components, you should be using CSS classes instead of inline-styles.

Then you could style all of your tables from one CSS file.

/*CSS file*/

table.myAwesomeTable {
  ...code
}

/*Markup */
<table className="myAwesomeTable">

</table>

2 Comments

As long as you are using React I see no issue with inline styles.
He asked explicitly not to use a separate CSS file
0

define style in your global theme.js

 "table, th, td" :{
      border: "1px solid white" },

      "th, td" : {
        textAlign: "center"
    },

All tables in App will now display white border

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.