# Dataview and CSS

**URL:** <https://forum.obsidian.md/t/dataview-and-css/75888>\
**Category:** Help\
**Tags:** dataview\
**Created:** [January 28, 2024, 1:42pm UTC](https://forum.obsidian.md/t/dataview-and-css/75888 "2024-01-28T13:42:33Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [January 28, 2024, 4:51pm UTC](https://forum.obsidian.md/t/dataview-and-css/75888/3 "2024-01-28T16:51:57Z")

</div>

So, if one wants to style the table using a CSS snippet, for both _live preview_ and _reading mode_, one could adapt the query to look like this:

````auto
```dataview
TABLE WITHOUT ID col1 as "Deadline", col2 as "Project #_"
WHERE projecttitle
FLATTEN projectdeadline as projcol1
FLATTEN "_<span class='title'>" + projecttitle + "</span>_<br>" + projectsub as projcol2
FLATTEN list(true, false) as alternateLine
FLATTEN choice(alternateLine, projcol1, "") as col1
FLATTEN choice(alternateLine, projcol2, "") as col2
```

````

And add a CSS file like the following:

```css
a[href^="#_"] {
    display: none;
}

body.theme-dark table:has([href^="#_"]) {
    /* Define the colors I've used in this example */
    --my-bg-header: var(--color-base-40);
    --my-bg-rows: var(--color-base-20); 
    --my-border-color: #a0a0a0;
    --my-title-color: #b070b0;
    
    /* Some styling to be applied to the header row */
    & thead > tr {
        background-color: var(--color-base-40); 
        border: 1px solid var(--my-border-color);

        & th {
            color: var(--my-title-color);
            border: 1px solid var(--my-border-color);
        }
    }

    /* Hide the result counter, since we doubled the lines... */ 
    & span.dataview.small-text {
        display: none;
    }

    /* Styles the project title class */
    & span.title {
        color: var(--my-title-color);
    }

    /* resets the background-color to outside color */ 
    & tr:nth-child(even) {
        background-color: inherit;
    }
        
    /* Sets a background-color and border for the cells with text */ 
    & tbody tr:nth-child(odd) td {
        background-color: var(--color-base-20);
        border: 1px solid var(--my-border-color); 
    } 
}

```

To get an output like this:

 ![image](https://forum.obsidian.md/uploads/default/original/3X/1/0/10ff0f6578627ff7f88e332bde3cba40548cc85d.png)

Note that since we now use a CSS snippet to style, we can leave out some of the styling from within the query, and make that a little cleaner. I use one tag in the header, `#_`, to mark the entire table, and in addition since I was lazy and didn’t want to merge cells I used a `<span class="title">` around the project title to target just that text.

> **Bonus tip: How to add a custom CSS snippet**
>
> - Goto _Settings \> Appearance_ and scroll down to “CSS snippets” section, and hit the folder icon on the far right. This opens up a file explorer window, in the folder `vault/.obsidian/snippets`, which is were you want to save your css snippet
> - In this new window create a file, like `myCss.css`, where you copy the CSS into. Make sure this actually is a text file, and that the name ends in `.css`
> - Back in Obsidian, you should now see your `myCss` in the list of CSS snippets. If not, hit the refresh button
> - Final step is to click the _enable_ button to the right of your file, and now your new CSS should be in effect
> - If you later on make changes in the CSS snippet file, the effect should be immediate. If not, try disabling and re-enabling the snippet, or in some strange cases, you would need to reload Obsidian. In 99% of the cases, the changes are immediate, though.

See also the post below:

> [@How to target Dataview queries for CSS styling](https://forum.obsidian.md/t/how-to-target-dataview-queries-for-css-styling/76525):
>
> This thread will show some of the options available for styling the result of your Dataview queries by providing you with CSS selectors which targets the outer elements of the query results. The specific styling within each is left up to the reader. After first covering the basics, I’ll show a nifty hack using #tags to single out specific queries. The basic options for styling The three most common options to target every query of the various query types are: TABLE queries and dv.table(): tab…

---

_[View the full topic](https://forum.obsidian.md/t/dataview-and-css/75888)._
