From 088c87bcb58be576308da503d4f11a68843c5013 Mon Sep 17 00:00:00 2001 From: Bradley Taunt Date: Mon, 1 Jul 2024 16:23:43 -0400 Subject: Initial new commit --- _posts/2018-12-19-jsincss-parent-selector.md | 80 ++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 _posts/2018-12-19-jsincss-parent-selector.md (limited to '_posts/2018-12-19-jsincss-parent-selector.md') diff --git a/_posts/2018-12-19-jsincss-parent-selector.md b/_posts/2018-12-19-jsincss-parent-selector.md new file mode 100644 index 0000000..f7244ba --- /dev/null +++ b/_posts/2018-12-19-jsincss-parent-selector.md @@ -0,0 +1,80 @@ +--- +layout: post +title: "Using Parent Selectors in CSS" +date: 2018-12-19 +--- + + +I recently saw a Twitter thread posted by Tommy Hodgins 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 jsincss-parent-selector and qaffeine + +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: + + + + + + + + +
+
+

This is a header

+
+
+ + + + + +### 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 video covering this topic if you prefer to follow along. -- cgit v1.2.3-54-g00ecf