{"id":16,"date":"2015-05-18T20:12:51","date_gmt":"2015-05-19T01:12:51","guid":{"rendered":"http:\/\/theshrouded.com\/wp\/?p=16"},"modified":"2022-08-22T20:20:53","modified_gmt":"2022-08-23T01:20:53","slug":"html5-css-crossview","status":"publish","type":"post","link":"https:\/\/theshrouded.com\/wp\/?p=16","title":{"rendered":"HTML5 CSS Crossview"},"content":{"rendered":"\n<p>A couple weeks ago while browsing reddit I stumbled across a very cool Android UI button that I thought was genius.&nbsp;<a href=\"https:\/\/github.com\/cdflynn\/crossview\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/cdflynn\/crossview&nbsp;<\/a> In particular this element would be great for lists where you want to add or remove options, like a choose your toppings list for a burger (e.g. add or remove toppings).<\/p>\n\n\n\n<p>I thought that this would be a great time to learn about CSS transitions and make a version of the element in HTML5 and CSS with as little JavaScript as I possibly could. My first few attempts managed to get the cross to appear and then when applying<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\ntranform: rotate(45deg);\n<\/pre><\/div>\n\n\n<p>I got a nasty little issue because the center of the 2 elements that I was rotating were in different places and it just didn&#8217;t work at all. I contacted my CSS guru friend over at <a href=\"http:\/\/scottlittle.me\">scottlittle.me<\/a> and asked for some advice. He pointed me towards using a label and attaching it to a checkbox to handle the toggle states. To handle the centering of the cross we figured out a method of using ::before and ::after pseudo elements.<\/p>\n\n\n\n<p>It took me a little while to work out the details but I managed to come up with something that worked well. Using a combination of the label, :before and :after pseudo selectors in addition to a :checked selector I was able to make the whole element work without any JavaScript. An added bonus to the methods I found was that it scales very well simply by changing the width and height of the label. I was hoping to be able to wrap the whole thing up in some easy to use package but when I looked at Web Components support on&nbsp;<a href=\"http:\/\/caniuse.com\/#search=components\" target=\"_blank\" rel=\"noopener noreferrer\">Can I Use<\/a>, things looked quite abysmal. I may eventually look to wrap this up as a Polymer&nbsp;Element once Polymer hits 1.0 (breaking changes are expected between 0.9 and 1.0).<\/p>\n\n\n\n<p>Here is the final product:\n<br>\n<style>\ninput.add-remove[type=checkbox]{\n  display:none;\n}\n\ninput.add-remove[type=checkbox] + label{\n  width:40px;\n  height:40px;\n  display:inline-block;\n  position:relative;\n  z-index:1;\n}\n\ninput.add-remove[type=checkbox] + label:before {\n  content:\"\";\n  left: 50%;\n  width: 10%;\n  margin-left: -5%;\n  position:absolute;\n  z-index:-1;\n  height: 100%;\n  color: #ccc;\n  background:red;\n  font-style: italic;\n  -webkit-transition:-webkit-transform .3s;\n  transition:transform .3s;\n} \n\ninput.add-remove[type=checkbox] + label:after {\n  content:\"\";\n  position:absolute;\n  top: 50%;\n  height: 10%;\n  margin-top: -5%;\n  width: 100%;\n  background:red;\n  z-index:-1;\n  color: #ccc;\n  font-style: italic;\n  -webkit-transition:-webkit-transform .3s;\n  transition:transform .3s;\n}\n\ninput.add-remove[type=checkbox]:checked + label {\n  color: #f00;\n  font-style: normal;\n  z-index:1;\n}\n\ninput.add-remove[type=checkbox]:checked + label:before {\n  -webkit-transform:rotate(-45deg);\n  -ms-transform:rotate(-45deg);\n  transform:rotate(-45deg);\n} \n\ninput.add-remove[type=checkbox]:checked + label:after {\n  -webkit-transform:rotate(135deg);\n  -ms-transform:rotate(135deg);\n  transform:rotate(135deg);\n}\n<\/style>\n<div>\n<input id=\"example\" class=\"add-remove\" type=\"checkbox\"><label for=\"example\">\n<\/div>\n<br>\nGo ahead and click on it to go between adding or removing an option. And since this is just a hidden checkbox plus a label it will submit just like a checkbox!<br>I don&#8217;t have IE9 to run this on but looking at caniuse.com and MDN, this should support everything going back to IE9 (plus I added the necessary vendor prefixes for Safari and IE)<\/p>\n\n\n\n<p>*Feature image taken from cdflynn&#8217;s github, linked above.<\/p>\n\n\n\n<p>Here&#8217;s the css I used:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\ninput.add-remove&#x5B;type=checkbox]{\n  display:none;\n}\n\ninput.add-remove&#x5B;type=checkbox] + label{\n  width:40px;\n  height:40px;\n  display:inline-block;\n  position:relative;\n  z-index:1;\n}\n\ninput.add-remove&#x5B;type=checkbox] + label:before {\n  content:\"\";\n  left: 50%;\n  width: 10%;\n  margin-left: -5%;\n  position:absolute;\n  z-index:-1;\n  height: 100%;\n  color: #ccc;\n  background:red;\n  font-style: italic;\n  -webkit-transition:-webkit-transform .3s;\n  transition:transform .3s;\n} \n\ninput.add-remove&#x5B;type=checkbox] + label:after {\n  content:\"\";\n  position:absolute;\n  top: 50%;\n  height: 10%;\n  margin-top: -5%;\n  width: 100%;\n  background:red;\n  z-index:-1;\n  color: #ccc;\n  font-style: italic;\n  -webkit-transition:-webkit-transform .3s;\n  transition:transform .3s;\n}\n\ninput.add-remove&#x5B;type=checkbox]:checked + label {\n  color: #f00;\n  font-style: normal;\n  z-index:1;\n}\n\ninput.add-remove&#x5B;type=checkbox]:checked + label:before {\n  -webkit-transform:rotate(-45deg);\n  -ms-transform:rotate(-45deg);\n  transform:rotate(-45deg);\n} \n\ninput.add-remove&#x5B;type=checkbox]:checked + label:after {\n  -webkit-transform:rotate(135deg);\n  -ms-transform:rotate(135deg);\n  transform:rotate(135deg);\n}\n<\/pre><\/div>\n\n\n<p>You can get this and the alternate version (remove-add and add-remove) together from my Github project for this element <a href=\"https:\/\/github.com\/sebsebmc\/add-remove\" target=\"_blank\" rel=\"noopener noreferrer\">here.<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A couple weeks ago while browsing reddit I stumbled across a very cool Android UI button that I thought was genius.&nbsp;https:\/\/github.com\/cdflynn\/crossview&nbsp; In particular this element would be great for lists where you want to add or remove options, like a choose your toppings list for a burger (e.g. add or remove toppings). I thought that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":18,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16"}],"version-history":[{"count":23,"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions\/106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=\/wp\/v2\/media\/18"}],"wp:attachment":[{"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theshrouded.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}