# How to target Dataview queries for CSS styling

**URL:** <https://forum.obsidian.md/t/how-to-target-dataview-queries-for-css-styling/76525>\
**Category:** Themes & CSS snippets\
**Tags:** dataview, custom-css\
**Created:** [February 7, 2024, 6:34pm UTC](https://forum.obsidian.md/t/how-to-target-dataview-queries-for-css-styling/76525 "2024-02-07T18:34:28Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [February 7, 2024, 6:34pm UTC](https://forum.obsidian.md/t/how-to-target-dataview-queries-for-css-styling/76525/1 "2024-02-07T18:34:28Z")

</div>

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()`: `table.dataview`
- `LIST` queries and `dv.list()`: `ul.list-view-ul`
- `TASK` queries and `dv.taskList()`: `ul.contains-task-list`

However these would target the result in the entire vault, so we would need some extra qualifiers to narrow the scope down. Using the `cssclasses` property we can limit the ordinary queries down to a single file, and for _dataviewjs_ queries we can use `dv.container.className += " clsname"` to limit to that specific query.

So one way to hide the table header using `cssclasses: hide_header` could then become:

```css
.hide_header table.dataview thead {
  display: none;
}

```

This should work in both _live preview_ and _reading mode_ as is.

## Targeting specific tables, rows or cells using tags

As already said, there are generally no unique ID’s given to a specific query result, with the exception of using `dv.container.className` in _dataviewjs_, but I’ve found one trick which is usable in the more recent version of Obsidian since we now have got access to the `:has()` CSS selector, and the fact that tags are rendered as `<a>` tags within tables in both _live preview_ and _reading mode_.

In the following text I’ll use nested tags starting with `#_/...`, but you’re free to use whatever tag names you’d like. I’ve chosen that prefix for my vault to denote that these tags are used for this purpose only, and this allows me to globally hide these tags using:

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

```

This particular rule matches all links having the `href` attribute _starting with_ the text `#_`. This now allow me to target a unique table, i.e. tagged with `#_/uniqueTableId`, using the following CSS:

```css
table:has([href="#_/uniqueTableId"] {
  /* Whatever styling I'd like applied to this table */
  & thead > tr {
    background-color: blue;
  }
}

```

Note that here I’ve skipped the more specific `table.dataview`, so this would also color any ordinary markdown table tagged with that tag in the same style. If that’s not wanted, change it into `table.dataview:has([href="#_/uniqueTableId"]) { ... } `. In addition note the nesting of CSS selectors where we due to the first line are within the context of tables tagged with our tag, and then we can use `& ...` to further specify a selector to target rows or cells or whatever we’d like to target _within that given table_. This is shown in the example to color the rows in the header blue.

This can also be combined using `cssclasses` or `dv.container.className` if one chooses to do so. And you can re-use the tags if you want to have a specific type of styling for a given type of tables various places in your vault.

### What about lists and task lists?

The above was specific to tables, and would apply to both _live preview_ and _reading mode_, but what about ordinary lists and task lists? Due to how _live preview_ renders stuff slightly different, it’s not as easy to target them using simple selectors as above.

In _reading mode_ you could use the variant below for a list, and the corresponding `ul.contains-task-list` alternative for tasks:

```css
ul.list-view-ul:has([href="#_/myListId"]) {
  /* Style the list */
}

```

However in _live preview_ each list item is its own `<div>` placed on the same level (and not within a given container). This means that you need to tag the specific item(s) you’d want to style, and it’s not as elegant as the solution above.

But then again, the tag can be hidden from view, so maybe you don’t care that much due to the possibilities it offers. So to target a task or list item tagged `#_` you could use:

```css
.is-live-preview .HyperMD-task-line:has(.cm-tag-_),
.is-live-preview .HyperMD-list-line:has(.cm-tag-_) {
    background-color: blue;
}

```

Note that if you remove the `.is-live-preview` it’ll also apply to _source mode_ for better or worse. And if you add `li:has([href="#_"])` it’ll also apply to the _reading mode_ (with a little caveat that the boxes are slightly different around the elements in _reading mode_).

These last examples are not specific to the _Dataview_ queries, but do apply to all task or list items tagged accordingly. But if you want you could precede either of these selectors with the basic selectors used to target _dataview_ queries as given in first section.

A last note on the tags in _live preview_ and that is to hide them you need to extend the first example of hiding tags using something like:

```css
.is-live-preview .cm-tag-_,
[href^="#_"] {
  display: none;
}

```

Or variations like `[class^="cm-tag-_"]` or similar.

* * *

In summary, this should give you a good start on how to target the various elements of both _dataview_ queries, and table, task and list items in general.

There are a few last minute references I would like to point your attention to:

- In [Style dataview table columns](https://s-blu.github.io/obsidian_dataview_example_vault/20%20Dataview%20Queries/Style%20dataview%20table%20columns/) @s-blu describes an alternate trick using `<span>` to style various parts of table cells (and columns)
- For more detailed variants of CSS Attribute selectors which are used in this post, see [MDN attribute selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors) or [W3 Schools CSS Attribute selectors](https://www.w3schools.com/css/css_attribute_selectors.asp)
- For a full example styling the header and highlighting cells, see [this thread](https://forum.obsidian.md/t/table-display-is-inconsistent-between-modes-and-tabs-html-table-with-css-snippet/74636/2)

---

<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:** [February 7, 2024, 8:33pm UTC](https://forum.obsidian.md/t/how-to-target-dataview-queries-for-css-styling/76525/2 "2024-02-07T20:33:39Z")

</div>

@CawlinTeffid , here is that share and showcase post you requested. It took me some time to write, so that [thread](https://forum.obsidian.md/t/can-you-apply-css-to-specific-sections-rather-than-the-entire-page/75582/4) got closed down. 😃

---

<div class="post-metadata">

**Author:** ![CawlinTeffid](https://forum.obsidian.md/user_avatar/forum.obsidian.md/cawlinteffid/32/82899_2.png) [@CawlinTeffid](https://forum.obsidian.md/u/CawlinTeffid)\
**Post date:** [February 8, 2024, 2:48am UTC](https://forum.obsidian.md/t/how-to-target-dataview-queries-for-css-styling/76525/3 "2024-02-08T02:48:00Z")

</div>

Nice! It hasn’t been so long, threads close quickly once they’re marked solved. I’ll share your findings with the CSS wizards on Discord.

---

<div class="post-metadata">

**Author:** ![garlicxd](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/g/e9bcb4/32.png) [@garlicxd](https://forum.obsidian.md/u/garlicxd)\
**Post date:** [February 11, 2024, 11:08am UTC](https://forum.obsidian.md/t/how-to-target-dataview-queries-for-css-styling/76525/4 "2024-02-11T11:08:46Z")

</div>

Might be a noob question but how do i add a tag to a section. Preferably to a callout.

````auto
> [!NOTE]+ Abilities 
> #_/uniqueTableId
> ```dataviewjs
> await forceLoadCustomJS();
> const {CreatureSheet} = customJS
> CreatureSheet.spell_table(app, dv)
> ```

````

I basically copied your example css.

```auto
table.dataview:has([href="#_/uniqueTableId"]) {
    display: none !important;
    visibility: hidden !important;
}

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

```

The tag disappears, but the table doesn’t.

 ![image](https://forum.obsidian.md/uploads/default/original/3X/3/6/361e52eb988e395f18278babbb521cfc2d895f9b.png)

---

<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:** [February 11, 2024, 7:18pm UTC](https://forum.obsidian.md/t/how-to-target-dataview-queries-for-css-styling/76525/5 "2024-02-11T19:18:38Z")

</div>

To make my examples work you need to have the tag _within_ the output from _dataview_, aka somewhere inside the _dataview_ codeblock, not on the outside. In addition, when you’re using _dataviewjs_ you can (and most likely is should) use the `dv.container.className += " clsname"`.

If you added that within either the outer codeblock, or within the `CreatureSheet.spell_table()`, you could use something like: `table.dataview:has( .clsname) ` to target your table.

If on the other hand you really want to target the callout div, you could try with something like:

```css

div.callout:has([href="#_/uniqueTableId"]) {
   & .dataview {
     display: none;
   }
}

```

---

<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:** [February 1, 2025, 10:30am UTC](https://forum.obsidian.md/t/how-to-target-dataview-queries-for-css-styling/76525/6 "2025-02-01T10:30:58Z")

</div>

As a general reminder, don’t forget to checkout this post if you’re unfamiliar with locating CSS information:

> [@Obsidian CSS Inspector Workflow](https://forum.obsidian.md/t/obsidian-css-inspector-workflow/58178):
>
> Obsidian CSS Inspector Workflow This was originally called “Obsidian CSS Quick Guide” but it ended up being more focused on the inspector. As a retrospective change to help people use this as a resource, it has been renamed. – FireIsGood Hi friends! CSS in Obsidian can be confusing to get into, so this is a guide to get you started with information on the tools, methods, and some background knowledge! Let’s get started by looking at how you can look behind the curtain of Obsidian’s visuals. …
