# Plugin \> Wrap element with another element

**URL:** <https://talk.jekyllrb.com/t/plugin-wrap-element-with-another-element/8737>\
**Category:** Help\
**Created:** [December 2, 2023, 7:07pm UTC](https://talk.jekyllrb.com/t/plugin-wrap-element-with-another-element/8737 "2023-12-02T19:07:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsiege](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vsiege](https://talk.jekyllrb.com/u/vsiege)\
**Post date:** [December 2, 2023, 7:07pm UTC](https://talk.jekyllrb.com/t/plugin-wrap-element-with-another-element/8737/1 "2023-12-02T19:07:40Z")

</div>

I’d like help with writing a plugin to wrap all first level `<table>` elements generated from markdown with a `<div class="customClass">`

**Below is my markdown**

```auto
|---
| Default aligned | Left aligned | Center aligned | Right aligned
|-|:-|:-:|-:
| 1st body: row 1, column 1 | row 1, column 2 | row 1, column 3 | row 1, column 4
| row 2, column 1 | row 2, column 2 | **row 2, column 3** | row 2, column 4
| row 3, column 1 | row 3, column 2 | row 3, column 3 | row 3, column 4
|---
| 2nd body: row 1, column 1
| row 2, column 1
|===
| Footer row

```

**In my template/layout (article.liquid), I’d like to wrap the table generated from the markdown with a div that has a class**  
`{{ page.content | wrap_first_level_table: '<div class="customClass">' }}`

**The final HTML should look exactly like this**

```auto
<div class="customClass">
<table>
  <thead>
    <tr>
      <th>Default aligned</th>
      <th style="text-align: left">Left aligned</th>
      <th style="text-align: center">Center aligned</th>
      <th style="text-align: right">Right aligned</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1st body: row 1, column 1</td>
      <td style="text-align: left">row 1, column 2</td>
      <td style="text-align: center">row 1, column 3</td>
      <td style="text-align: right">row 1, column 4</td>
    </tr>
    <tr>
      <td>row 2, column 1</td>
      <td style="text-align: left">row 2, column 2</td>
      <td style="text-align: center"><strong>row 2, column 3</strong></td>
      <td style="text-align: right">row 2, column 4</td>
    </tr>
    <tr>
      <td>row 3, column 1</td>
      <td style="text-align: left">row 3, column 2</td>
      <td style="text-align: center">row 3, column 3</td>
      <td style="text-align: right">row 3, column 4</td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <td>2nd body: row 1, column 1</td>
      <td style="text-align: left">&nbsp;</td>
      <td style="text-align: center">&nbsp;</td>
      <td style="text-align: right">&nbsp;</td>
    </tr>
    <tr>
      <td>row 2, column 1</td>
      <td style="text-align: left">&nbsp;</td>
      <td style="text-align: center">&nbsp;</td>
      <td style="text-align: right">&nbsp;</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Footer row</td>
      <td style="text-align: left">&nbsp;</td>
      <td style="text-align: center">&nbsp;</td>
      <td style="text-align: right">&nbsp;</td>
    </tr>
  </tfoot>
</table>
</div>

```

Does anyone know a plugin that can do something like this? I don’t have a need for more robust plugin and would prefer a very simple; focused execution. Thanks for your help in advance.

_Note: When I say first level `<table>`, I look to avoid wrapping `<table>`'s embedded in other tables as I **don’t** need a `<div>` around those._

---

<div class="post-metadata">

**Author:** ![chuckhoupt](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/chuckhoupt/32/523_2.png) [@chuckhoupt](https://talk.jekyllrb.com/u/chuckhoupt)\
**Post date:** [December 2, 2023, 10:46pm UTC](https://talk.jekyllrb.com/t/plugin-wrap-element-with-another-element/8737/2 "2023-12-02T22:46:46Z")

</div>

Although I don’t know of a plugin, it might be useful to mention other options in case they are useful (both for you and others in the future or past). I realize you must have a specific goal that requires the DIVs, and so none of these may apply.

If your goal is just to mark the tables with a class, then you could add classes via [Kramdown’s Block Attribute extension](https://kramdown.gettalong.org/quickref.html#block-attributes). This assumes you’re using the default Jekyll Markdown processor, based on Kramdown. For example (tested code):

```auto
|a|b
|-|-
|1|2
{: .class1 .class2}

```

Producing:

```auto
<table class="class1 class2">
...
</table>

```

Similarly, you can use [Kramdown’s HTML Block extensions](https://kramdown.gettalong.org/syntax.html#html-blocks) to mix HTML and Markdown. For example (tested code):

```auto
<div class="customClass" markdown="1">

|a|b
|-|-
|1|2

</div>

```

Producing:

```auto
<div class="customClass">
<table>
...
</table>
</div>

```

If your goal is just to style non-embedded tables without changing markup, then the [CSS `:not()` selector](https://developer.mozilla.org/en-US/docs/Web/CSS/:not) could be used. For example, something like (untested code):

```auto
:not(td) table { color: red }

```

---

<div class="post-metadata">

**Author:** ![vsiege](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vsiege](https://talk.jekyllrb.com/u/vsiege)\
**Post date:** [December 3, 2023, 12:57am UTC](https://talk.jekyllrb.com/t/plugin-wrap-element-with-another-element/8737/3 "2023-12-03T00:57:15Z")

</div>

@chuckhoupt Amazing answers! 😀 I am currently using the the method above that wraps the markdown table with `<div class="customClass" markdown="1">` — which works pretty well.

**Background**  
Without going too off topic here with my CSS+layout…  
I have a bunch of tables in a series of articles, and was trying to avoid rewriting (or having to remember) to wrap each instance in a `<div>`. The wrapping `<div>` ensures users can scroll tables horizontally on **mobile** devices. I use CSS 12-column grid with 16px left & right margins and the wrapping div prevents overflow on the X-axis (keeps everything with the grid [`grid-column: span 12`]).

So my thought was… wouldn’t it be great to find a plugin that just wraps each instance of `<table>` at compilation time? I haven’t dug too far into plugins as of late.

---

<div class="post-metadata">

**Author:** ![chuckhoupt](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/chuckhoupt/32/523_2.png) [@chuckhoupt](https://talk.jekyllrb.com/u/chuckhoupt)\
**Post date:** [December 3, 2023, 10:01pm UTC](https://talk.jekyllrb.com/t/plugin-wrap-element-with-another-element/8737/4 "2023-12-03T22:01:16Z")

</div>

Sometimes one can get tables to scroll properly using variations of the following, but I know the CSS devil is in the details of layouts:

```auto
table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
}

```

---

<div class="post-metadata">

**Author:** ![vsiege](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vsiege](https://talk.jekyllrb.com/u/vsiege)\
**Post date:** [December 4, 2023, 6:07pm UTC](https://talk.jekyllrb.com/t/plugin-wrap-element-with-another-element/8737/5 "2023-12-04T18:07:09Z")

</div>

Thanks. I have the layout working great. The wrapping div ensures the table stays within the 12-column grid and keep a nice 16px margin from the left and right side of the browser on mobile. CSS grid is fantastic. The padding and font-size always get reduced for mobile… but I do not want to keep the table just 100% width… they have many columns. Going to keep this one live for a bit longer before I close it, but I wanted to say thanks again.

PS: Including the final look to showcase the layout of the table and how it functions.

 ![table_Screenshot_20231204_100410](https://canada1.discourse-cdn.com/flex030/uploads/jekyllrb/original/2X/b/b2c78b5dafcf60469e1038b4628b367f9a116139.png)
