Width of custom table in html R Notebook

Is there a way that a custom table such as

| fruit  | price  |
|--------|--------|
| apple  | 2.05   |
| pear   | 1.37   |
| orange | 3.09   |

be set to do not expand the whole width of the page when rendered in HTML from a R Notebook file?

One approach is adding <style> tags at the beginning of your document, specifying the width of the table.

---
title: ""
output: html_document
---

<style type="text/css">
  .table {width: 25%;}
</style>


| fruit  | price  |
|--------|--------|
| apple  |  2.05  |
| pear   |  1.37  |
| orange |  3.09  |

I understand this solution sets the table width for the entire document (please correct me if I am wrong). Any alternative to set the width of one given table?

You can wrap the table in a <div> and give it it's own class. Then, update the CSS accordingly.

---
title: ""
output: html_document
---

<style type="text/css">
  .mytable {width: 25%;}
</style>


<div class="mytable">
| fruit  | price  |
|--------|--------|
| apple  |  2.05  |
| pear   |  1.37  |
| orange |  3.09  |
</div>

| fruit  | price  |
|--------|--------|
| apple  |  2.05  |
| pear   |  1.37  |
| orange |  3.09  |

Using CSS would be the way to tweak the styling. You can add an id to the table to target this one more easily in CSS.

Otherwise, you can use a table package like https://gt.rstudio.com/ which will have some styling option offered

With markdown you can also control some column size using pipe table syntax
https://pandoc.org/MANUAL.html#extension-pipe_tables

Hope it helps