Skip to content

Instantly share code, notes, and snippets.

@tomhodgins
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomhodgins/62c378f50988bf2f1a31 to your computer and use it in GitHub Desktop.
Save tomhodgins/62c378f50988bf2f1a31 to your computer and use it in GitHub Desktop.
Element-Query Based Web Layout (as-written)
<!DOCTYPE html><html><head><title>EQCSS Layout Demo</title><meta charset="utf-8"><meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height"><link href="http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet"><link href="http://cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.min.css" rel="stylesheet"></head><body>
<!-- Nav -->
<section id="nav">
<a href="#"><strong>EQCSS Demo</strong></a>
<label for="sidebarToggle">Toggle Sidebar</label>
<script type="text/eqcss">
@element '#nav' and (min-width: 0px) {
body { padding-top: 75px; }
#nav { position: fixed; top: 0; left: 0; background: #fff; color: #333; font-size: 120%; letter-spacing: -.02em; text-align: center; padding: 0 0 10px 0; z-index: 1000; box-shadow: rgba(0,0,0,.3) 0 0 15px; }
#nav a, label { text-decoration: none; color: #000; display: block; padding: 10px 10px 0 10px; }
#nav a:hover, #nav label:hover { color: #fc0; }
}
@element '#nav' and (min-width: 500px) {
body { padding-top: 40px; }
#nav a, label { display; inline-block; }
#nav a { float: left; }
#nav label { float: right; }
}
</script>
</section>
<!-- Header -->
<section id="header">
<div id="headlines">
<h1 class="animated zoomInRight delay-one">Headline 1</h1>
<h2 class="animated zoomInLeft delay-onefifty">Headline 2</h2>
</div>
<img id="header_fg" src="http://placehold.it/1500x500/eee/333&amp;text=%20" class="animated fadeInUp delay-twofifty">
<img id="header_bg" src="http://placehold.it/1500x500/555/eee&amp;text=%20">
<script type="text/eqcss">
@element '#header' and (min-width: 0px) {
#header div { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none; cursor: default; }
#headlines { position: absolute; z-index: 700; width: 100%; text-align: center; top: 50%; margin-top: -6%; }
#headlines h1, #headlines h2 { display: block; font-size: 125%; margin: 0 auto; padding: 0 .25em; line-height: 1.2; letter-spacing: -.05em; }
#headlines h1 { font-size:150%; }
#header img { float: left; width: 100%; height: auto; margin: 0; }
#header #header_fg { position: absolute; top: 0; left: 0; z-index: 500; }
}
@element "#header" and (min-width: 479px) { #headlines { font-size: 10pt; margin-top: -5%; } }
@element "#header" and (min-width: 480px) { #headlines { font-size: 14pt; margin-top: -6%; } }
@element "#header" and (min-width: 650px) { #headlines { font-size: 18pt; } }
@element "#header" and (min-width: 768px) { #headlines { font-size: 24pt; } }
@element "#header" and (min-width: 960px) { #headlines { font-size: 36pt; } }
@element "#headlines h1" and (min-characters: 15) { #headlines h1 { font-size: 250%; } }
@element "#headlines h1" and (min-characters: 30) { #headlines h1 { font-size: 200%; } }
@element "#headlines h1" and (min-characters: 45) { #headlines h1 { font-size: 150%; } }
@element "#headlines h2" and (min-characters: 15) { #headlines h2 { font-size: 100%; } }
@element "#headlines h2" and (min-characters: 30) { #headlines h2 { font-size: 75%; } }
@element "#headlines h2" and (min-characters: 45) { #headlines h2 { font-size: 50%; } }
</script>
</section>
<!-- Main -->
<input type="checkbox" id="sidebarToggle">
<main>
<!-- Sidebar -->
<section id="sidebar">
<aside class="newsletter">
<h3>Email</h3>
Curabitur dictum nibh risus, ac semper!
<form>
<input type="email" placeholder="email" name="email" id="signupEmail">
<input type="submit" value="Sign Up">
</form>
</aside>
<aside class="recent-posts">
<h3>Recent Posts</h3>
<a href="#">Etiam sagittis non tortor eget tempus. Curabitur fermentum quam ut erat lacinia varius.</a>
<a href="#">Vivamus finibus neque id dolor bibendum, sed ullamcorper ipsum mattis.</a>
<a href="#">Duis eros dui, commodo ut nibh non, sodales sodales erat.</a>
</aside>
<script type="text/eqcss">
@element '#sidebar' and (min-width: 0px) {
#sidebarToggle, #sidebar { display:none }
#sidebar { position: absolute; top: 0; right: 0; height: 100%; background: #ccc; color: #333; padding: 15px; height: 100%; }
#sidebar aside + aside { border-top: 1px solid #bbb; margin-top: 10px; padding-top: 10px; }
#sidebar h3 { font-size: 150%; text-transform: uppercase; margin: 0 0 .5em 0; color: #333; font-weight: 300; }
#sidebar .newsletter form { width: 100%; }
#sidebar form input { -webkit-appearance: none; appearance: none; background: #eee; display: block; border: 1px solid #999; border-radius: 3px; width: 100%; padding: 8px; font-size: 100%; margin-top: 5px; }
#sidebar form input[type="email"]:focus { border-color: #f50; }
#sidebar form input[type="submit"] { cursor: pointer; background: #f90; border-color: #f50; color: #f30; font-weight: 600; }
#sidebar form input[type="submit"]:hover { background: #fc0; border-color: #f70; color: #f50; }
#sidebar .recent-posts a { display: block; text-decoration: none; color: #666; margin: 0 0 15px 0; font-size: 90%; line-height: 1.3; }
#sidebar .recent-posts a:hover { color: #333 }
}
@element 'main' and (min-width: 0) and (max-width: 499px) {
#sidebarToggle:checked + main #sidebar { display:block; width: 50%; float: right; }
#sidebarToggle:checked ~ main { padding-right: 52%; }
}
@element 'main' and (min-width: 500px) {
#sidebarToggle:checked + main #sidebar { display:block; width: 31%; float: right; }
#sidebarToggle:checked ~ main { padding-right: 33%; }
}
</script>
</section>
<!-- Intro -->
<section id="intro">
<p>Integer vitae quam id sapien luctus tincidunt a at turpis. Praesent scelerisque tincidunt sapien, quis consectetur metus dapibus eu. Aliquam eu risus purus. Mauris eros turpis, elementum laoreet luctus eget, iaculis eget libero. Phasellus tincidunt rhoncus mauris in euismod</p>
<p>Duis pretium dolor eget tellus vehicula, vitae bibendum nisi suscipit. Fusce dui velit, accumsan sit amet erat quis, mollis finibus lectus. Curabitur vel nulla ut lacus aliquam convallis. Fusce tincidunt, quam ut varius dictum, quam dolor fermentum augue, pulvinar tincidunt erat diam et velit. Fusce varius iaculis tempor. Integer sagittis vulputate sollicitudin. Praesent dapibus augue libero, ultricies sagittis felis mattis tincidunt.</p>
<script type="text/eqcss">
@element '#intro' and (min-width: 0px;) {
#intro p:first-of-type {font-size: 140%; }
#intro p { color: #555; font-weight: 400; font-size: 120%; }
}
</script>
</section>
<!-- Content -->
<section id="content">
<h2>Page Content Headline</h2>
<aside>Written by <strong>Default Author</strong> on <strong>January 1st</strong></aside>
<article>
<p>Nulla non nisl suscipit, maximus odio eget, varius magna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed blandit tellus in tortor vestibulum, in euismod augue lacinia. Ut quis felis sit amet mi vehicula mattis. Aliquam at purus non velit mattis gravida lacinia quis velit. Nunc hendrerit leo eget erat iaculis, non porttitor tellus auctor. Donec blandit ipsum vitae velit consectetur porttitor. Etiam tristique congue convallis. Sed lacus diam, commodo eget ornare sit amet, molestie nec lorem. Vestibulum quis semper orci. Etiam id nisl justo. Quisque molestie, nulla non lobortis sodales, mi sapien ultricies arcu, facilisis faucibus urna lacus a ligula. In turpis orci, eleifend et risus vel, pharetra molestie velit. Sed pellentesque augue in est cursus, sed congue tellus fringilla. Praesent eu orci eget augue congue interdum.</p><p>Maecenas dapibus risus ut enim egestas porttitor. Donec et est pulvinar, aliquet massa sed, vehicula metus. Nam sed auctor mi, id venenatis nulla. Mauris feugiat ultrices nunc, imperdiet sollicitudin odio tempus sed. Curabitur volutpat, mi et cursus lobortis, metus lorem consectetur nisl, sit amet aliquet lectus nisi vel turpis. Proin nec vehicula neque. Maecenas elit leo, feugiat sit amet turpis id, dictum efficitur nisl. Vivamus eu turpis in quam vulputate volutpat vitae id metus. In lectus felis, venenatis vel vestibulum ut, suscipit eu ipsum. Vestibulum pretium nec diam non iaculis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Sed quam augue, laoreet quis sem a, vestibulum laoreet est. Praesent molestie dolor sagittis, cursus turpis nec, laoreet mi.</p>
</article>
<script type="text/eqcss">
@element '#content' and (min-width: 0px) {
#content h2 { font-size: 250%; margin: 0; letter-spacing: -.05em; }
#content aside { font-size: 90%; color: #555; margin: .25em -0; }
#content p { font-size: 120%; color: #333; }
}
</script>
</section>
<!-- Bottom -->
<section id="bottom">
<aside>
<h3>Widget Title</h3>
<div>Nulla non nisl suscipit, maximus odio eget!</div>
<img src="http://placehold.it/250x250/333/999&amp;text=%20">
</aside>
<aside>
<h3>Other Widget</h3>
<div>Maecenas dapibus risus ut enim egestas porttitor. Donec et est pulvinar, aliquet massa sed, vehicula metus.</div>
<img src="http://placehold.it/250x250/999/333&amp;text=%20">
</aside>
<script type="text/eqcss">
@element '#bottom' and (min-width: 0px) {
#bottom aside { display: block; padding: 10px; position: relative; color: #fff; float: left; width: 100%; overflow: hidden; min-height: 100px; margin: 15px 0 0 0;}
#bottom aside img { position: absolute; top: 0; left: 0; width: 100%; height: auto; z-index: 250; }
#bottom h3 { position: relative; z-index: 500; margin: 0; }
#bottom div { position: relative; z-index: 700; }
}
@element '#bottom' and (min-width: 500px) {
#bottom { margin-top: 15px; }
#bottom aside { width: 49%; margin-top: 0; }
#bottom aside + aside { margin-left: 2%; }
}
</script>
</section>
</main>
<!-- Footer -->
<section id="footer">
<input id="col1" type="checkbox"><label for="col1"><span>Column 1</span>
<div>Curabitur dictum nibh risus, ac semper sem ornare eu. Nullam vestibulum sed est non porta.</div>
</label>
<input id="col2" type="checkbox"><label for="col2"><span>Column 2</span>
<div>Curabitur scelerisque id eros a pulvinar. Curabitur vitae aliquam dui, accumsan mattis augue. Phasellus commodo elit maximus justo condimentum.</div>
</label>
<input id="col3" type="checkbox"><label for="col3"><span>Column 3</span>
<div>Id ultrices orci placerat. Pellentesque id finibus magna. Suspendisse lacinia sapien dui, vitae finibus metus eleifend ut. Integer elit ipsum</div>
</label>
<input id="col4" type="checkbox"><label for="col4"><span>Column 4</span>
<div>Etiam sagittis non tortor eget tempus. Curabitur fermentum quam ut erat lacinia varius. Donec in sagittis magna. Nullam eu consequat ligula. Morbi bibendum vestibulum ipsum. Aenean imperdiet leo at nulla molestie cursus.</div>
</label>
<input id="col5" type="checkbox"><label for="col5"><span>Column 5</span>
<div>Morbi metus dolor, dignissim sed imperdiet quis, venenatis a lectus. Praesent libero quam, aliquam ac turpis quis, aliquam congue magna.</div>
</label>
<script type="text/eqcss">
@element '#footer' and (min-width: 0px) {
#footer { background: #111; color: #555; font-weight: 400; padding: 15px; }
#footer label { color: #555; cursor: default; }
#footer label span { cursor: pointer; }
#footer input[type="checkbox"], #footer label div { display: none; }
#footer input[type="checkbox"]:checked + label span { color: #777; }
#footer input[type="checkbox"]:checked + label div { display: block; cursor: text; }
#footer label { width: 100%; padding: 5px;}
#footer label span { font-weight: 700; font-size: 150%; text-align: center; display: block; }
#footer:after { content: 'Copyright © 2015 Company Name'; display: block; width: 100%; padding-top: 15px; float: left; text-align: center; }
}
@element '#footer' and (min-width: 500px) {
#footer label { width: 20%; float: left; padding-bottom: 15px; text-align: left; pointer-events: none; }
#footer label span { font-size: 125%; text-align: left; cursor: default; }
#footer label div { display: block; }
#footer label small { display: block; width: 100%; float: right; }
#footer input[type="checkbox"] + label span { color: #777; }
}
</script>
</section>
<!-- Page Layout -->
<script type="text/eqcss">
@element 'html' and (min-width: 0px) {
* { outline: none; box-sizing: border-box; -moz-box-sizing: border-box; text-rendering: optimizeLegibility; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; font-kerning: auto; }
html { opacity: 1 !important; font-size: 10pt; line-height: 1.4; font-family: 'Open Sans', Roboto, 'Helvetica Neue', 'Myriad Pro', 'Segoe UI', Arial, sans-serif; -webkit-text-size-adjust: 100%; }
body { margin: 0; }
p { margin: 0 0 .6em 0; }
label { -webkit-user-select:none; -moz-user-select:none; -ms-user-select:none; -o-user-select:none; user-select:none; cursor:pointer; }
section, main { width: 100%; float: left; overflow: hidden; position: relative; }
main { padding: 15px 2% 2% 2%; }
}
</script>
<style type="text/css">
html { opacity: 0; }
.delay-one { -webkit-animation-delay: 1s; animation-delay: 1s; }
.delay-onefifty { -webkit-animation-delay: 1.5s; animation-delay: 1.5s; }
.delay-twofifty { -webkit-animation-delay: 2.5s; animation-delay: 2.5s; }
</style>
<!-- EQCSS -->
<script type="text/javascript">function e(e){console.log(e)}EQCSS={code:"",sc:[],qr:[],sl:[],cd:[],ss:[],xhr:new XMLHttpRequest},EQCSS.load=function(){var e=document.querySelectorAll("script[type='text/eqcss']");for(var t=0;t<e.length;t++)e[t].innerHTML!=""&&(this.code+=e[t].innerHTML),e[t].src&&(this.xhr.open("GET",e[t].src,!1),this.xhr.send(null),this.code+=this.xhr.responseText),e[t].type="text/eqcss-read";this.code=this.code.replace(/\s+/g," "),this.code=this.code.replace(/^ | $/g,""),this.code=this.code.replace(/\/\*[\w\W]*?\*\//g,""),this.code=this.code.replace(/ *@element/g,"\n@element"),this.code.replace(/(@element.*(?!@element))/g,function(e,t){EQCSS.qr.push(t)});for(t=0;t<this.qr.length;t++)this.qr[t].replace(/@element ?["']([^"']*)["']/g,function(e,n){EQCSS.sl[t]=n});for(t=0;t<this.qr.length;t++)this.cd[t]=[],this.qr[t].replace(/and ?\( ?([^:]*) ?: ?([^)]*) ?\)/g,function(e,n,r){EQCSS.cd[t].push({measure:n,value:r})});for(t=0;t<this.qr.length;t++)this.qr[t].replace(/{(.*)}/g,function(e,n){EQCSS.ss[t]=n})},EQCSS.apply=function(){var e,t,n,r,i,s,o,u,a;for(e=0;e<EQCSS.qr.length;e++){r=document.querySelectorAll(EQCSS.sl[e]);for(t=0;t<r.length;t++){i="EQCSS_css_block_"+e+"_"+t,s=document.querySelector("#"+i),s||(s=document.createElement("STYLE"),s.id=i,document.querySelector("head").appendChild(s)),s=document.querySelector("#"+i),a=!0;e:for(n=0;n<EQCSS.cd[e].length;n++)switch(EQCSS.cd[e][n].measure){case"min-width":if(EQCSS.cd[e][n].value.indexOf("px")!=-1){o=parseInt(window.getComputedStyle(r[t],null).getPropertyValue("width"));if(!(o>=parseInt(EQCSS.cd[e][n].value))){a=!1;break e}}if(EQCSS.cd[e][n].value.indexOf("%")!=-1){o=parseInt(window.getComputedStyle(r[t],null).getPropertyValue("width")),u=parseInt(window.getComputedStyle(r[t].parentNode,null).getPropertyValue("width"));if(!(u/o<=100/parseInt(EQCSS.cd[e][n].value))){a=!1;break e}}break;case"max-width":if(EQCSS.cd[e][n].value.indexOf("px")!=-1){o=parseInt(window.getComputedStyle(r[t],null).getPropertyValue("width"));if(!(o<=parseInt(EQCSS.cd[e][n].value))){a=!1;break e}}if(EQCSS.cd[e][n].value.indexOf("%")!=-1){o=parseInt(window.getComputedStyle(r[t],null).getPropertyValue("width")),u=parseInt(window.getComputedStyle(r[t].parentNode,null).getPropertyValue("width"));if(!(u/o>=100/parseInt(EQCSS.cd[e][n].value))){a=!1;break e}}break;case"min-height":if(EQCSS.cd[e][n].value.indexOf("px")!=-1){o=parseInt(window.getComputedStyle(r[t],null).getPropertyValue("height"));if(!(o>=parseInt(EQCSS.cd[e][n].value))){a=!1;break e}}if(EQCSS.cd[e][n].value.indexOf("%")!=-1){o=parseInt(window.getComputedStyle(r[t],null).getPropertyValue("height")),u=parseInt(window.getComputedStyle(r[t].parentNode,null).getPropertyValue("height"));if(!(u/o<=100/parseInt(EQCSS.cd[e][n].value))){a=!1;break e}}break;case"max-height":if(EQCSS.cd[e][n].value.indexOf("px")!=-1){o=parseInt(window.getComputedStyle(r[t],null).getPropertyValue("height"));if(!(o<=parseInt(EQCSS.cd[e][n].value))){a=!1;break e}}if(EQCSS.cd[e][n].value.indexOf("%")!=-1){o=parseInt(window.getComputedStyle(r[t],null).getPropertyValue("height")),u=parseInt(window.getComputedStyle(r[t].parentNode,null).getPropertyValue("height"));if(!(u/o>=100/parseInt(EQCSS.cd[e][n].value))){a=!1;break e}}break;case"min-characters":if(!(r[t].innerHTML.length>=parseInt(EQCSS.cd[e][n].value))){a=!1;break e}break;case"max-characters":if(!(r[t].textContent.length<=parseInt(EQCSS.cd[e][n].value))){a=!1;break e}break;case"min-children":if(!(r[t].childNodes.length>=parseInt(EQCSS.cd[e][n].value))){a=!1;break e}break;case"max-children":if(!(r[t].childNodes.length<=parseInt(EQCSS.cd[e][n].value))){a=!1;break e}}if(a===!0)try{s.innerHTML=EQCSS.ss[e]}catch(f){s.ssheet.cssText=EQCSS.ss[e]}else try{s.innerHTML=""}catch(f){s.ssheet.cssText=""}}}},window.addEventListener("load",function(){EQCSS.load(),EQCSS.apply()}),window.addEventListener("resize",function(){EQCSS.apply()});var EQCSS_resizing = false, EQCSS_timeout = true;setInterval(function(){EQCSS.apply();},200);</script></body></html>
@tomhodgins
Copy link
Author

Element Query CSS (EQCSS) has changed the way I'm able to design in-browser!

If you build out the styles for each block of content, starting from the narrowest width that an element could show up in browser, and add responsive styles to the element itself (What would it look like if this element was 100 px wide? What would it look like if this element was 500px wide? What would it look like if this element was 1000px wide? etc) then you can be sure that no matter where those elements show up in your layout, they will look great. You can freely rearrange elements in the layout all you want, and the elements simply adapt to the new space you've given them, and with some knowledge of their contents! Being able to use Element Queries has allowed me to approach in-browser design with the same methodology and process I used when using traditional design tools like Photoshop, Illustrator, or Inkscape for layout.

The added bonus of EQCSS is that you can fine-tune support for elements as well to prevent bugs from happening by applying CSS in situations that regular CSS can't normally reach. We can apply styles based on how many characters of text an element contains, so to accommodate a longer headline without breaking onto multiple lines we can decrease the font size if it contains over 15, or 30 characters of text.

In this demo, the two widgets at the bottom know the width where they should to be side-by-side and when they should be stacked on top of each other, and it doesn't matter whether the sidebar is visible or not, they will stack when they ought to. Using CSS @media queries wouldn't normally be able to target this kind of situation where something changes size on the page after the page loads because there's nothing different about the browser width.

Preview: http://staticresource.com/widget-demo.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment