aboutsummaryrefslogtreecommitdiff
path: root/posts/jsincss-parent-selector.md
diff options
context:
space:
mode:
authorBradley Taunt <bt@btxx.org>2024-06-06 08:05:12 -0400
committerBradley Taunt <bt@btxx.org>2024-06-06 08:05:12 -0400
commit6b742c459266b18e2b375b35205ce8a6c02f0452 (patch)
treeb16fbb9a045e33dd6c97eb5ab72e6ff4d9237ea3 /posts/jsincss-parent-selector.md
Initial commit
Diffstat (limited to 'posts/jsincss-parent-selector.md')
-rw-r--r--posts/jsincss-parent-selector.md77
1 files changed, 77 insertions, 0 deletions
diff --git a/posts/jsincss-parent-selector.md b/posts/jsincss-parent-selector.md
new file mode 100644
index 0000000..f6176d8
--- /dev/null
+++ b/posts/jsincss-parent-selector.md
@@ -0,0 +1,77 @@
+# Using Parent Selectors in CSS
+
+2018-12-19
+
+I recently saw a Twitter thread posted by <a href="https://twitter.com/innovati/status/1068998114491678720">Tommy Hodgins</a> on implementing highly requested styling features in CSS with only a minimal amount of JavaScript. Many of his examples are great, but the `parent` selector instantly peaked my interest.
+
+Being able to target an element's parent always becomes a minor annoyance (since vanilla CSS does not support it) - so you always end up having to do something a little ugly like:
+
+
+ var el = document.getElementById('custom-div');
+ var parent = el.closest(selectors);
+
+
+And then add any custom styling to the parent element directly in JavaScript - or toggle a class which opens a whole other can of worms.
+
+## Save the day with <a href="https://www.npmjs.com/package/jsincss-parent-selector">jsincss-parent-selector</a> and <a href="https://github.com/tomhodgins/qaffeine">qaffeine</a>
+
+By using the `jsincss-parent-selector` and `qaffeine` plugins we can target an element's parent in CSS without breaking a sweat. Let's break it down:
+
+### Import the packages
+
+
+ npm install jsincss-parent-selector qaffeine
+
+
+### HTML (ex. index.html)
+
+Now we add our very simple HTML skeleton:
+
+
+ <!doctype html>
+ <html>
+ <head>
+ <link rel="stylesheet" href="output.css">
+ </head>
+ <body>
+ <header>
+ <main>
+ <h2>This is a header</h2>
+ </main>
+ </header>
+ </body>
+ <script src=output.js></script>
+ </html>
+
+
+### JavaScript (ex. input.js)
+
+
+ const qaffeine = require('qaffeine')
+ const parent = require('jsincss-parent-selector')
+
+ qaffeine(
+ {
+ stylesheet: {},
+ rules: {
+ parent
+ }
+ },
+ 'input.css',
+ 'output.js',
+ 'output.css'
+ )
+
+
+### CSS (ex. input.css)
+
+
+ header {
+ display: block;
+ }
+ main[--js-parent] {
+ background: blue;
+ }
+
+
+Then simply run `node` against your `js` file. That's it! I would also suggest checking out Tommy's <a href="https://www.youtube.com/watch?v=rG8cLe7VbW0">video covering this topic</a> if you prefer to follow along.