Tweet For Code #2
You don't need a thousand lines of code to make a big difference in any coding language. Oftentimes it's quite the opposite: a few tiny code snippets can do a world of good and accomplish big things. I asked my Twitter followers to tweet to me their favorite tiny snippets of code -- that's a bit difference to try to pack into 140 characters! Here are my favorites from this round!
Comma-separated Lists without Server-Side Logic
Want to make a HTML list of items look like a real, comma-separated list? Check this out:
@davidwalshblog Commas after list items, except the last. No server-side logic required!
ul > li:not(:last-child)::after { content: ","; }
— Stephen Coogan (@coog_ie) February 11, 2014
I implemented this on my upcoming redesign with great effect, and used it on the MDN redesign.
Pure CSS Sliders
The following snippet use max-height to implement CSS-only sliders with overflow hidden:
@davidwalshblog
.foo ul {max-height: 0;}
.foo:hover{
ul {
max-height: 1000px;
transition: .3s ease; }
}
Animate to 'auto' height!
— Tobias (@quagliero) February 11, 2014
I wrote about this a while back -- and awesome technique that too few developers know about.
3-Column Grid
Want a nice 3-column grid generator for your SASS projects?
@davidwalshblog *{box-sizing:border-box}.G{margin-left:-1em}[class*=C]{float:left;padding-left:1em}@each $i in 1,2,3{.C#{$i}{width:33%*$i}}
— Jonathan Bennett (@jonbca7) February 12, 2014
I'm impressed this could fit in a tweet but well done!
JavaScript Var Swap
Ever wanted to swap the contents of two JavaScript vars? Probably not but hey, it's pretty simple:
@davidwalshblog Swap variables in JS: b = [a, a = b][0]
— aksival (@aksival) February 13, 2014
Not the most practical but I expected there to be more code involved.
abbr and attr
Hovering over an abbr element usually gives you the full text representation, but what if you're on mobile? Here's the answer:
@davidwalshblog abbr[title]:after { content: " (" attr(title) ")"; } /* for use on mobiles, in a media query or something */
— Callum Macrae (@callumacrae) February 12, 2014
You could do this with a number of properties, but this is a nice usage.
*waves*
I had no idea what I'd see when I executed this into the JavaScript console, but I'll let it act as the conclusion to this post:
@davidwalshblog ([]+{})[+!+[]+!+[]]+(+(+!+[]+([]+[][+[]])[+!+[]+!+[]+!+[]]+(+!+[])+(+[])+(+[])+(+[]))+[])[7]+([]+[][+[]])[+!+[]+!+[]+!+[]]
— Minko Gechev (@mgechev) February 13, 2014
See you next time! I can't wait to see what you all come up with next!
The first one can be achieved for IE8 too with some adjustments:
For the first tip, the good old (IE7 compliant) sibling selector is also perfect and requires only one rule:
Hi, thank you for your article David,
Raphael : I was thinking about the same thing, but
:before
is not IE7 compliant :phttp://codepen.io/CreativeJuiz/pen/iHyBG
Have a nice day.
How does this work?
b = [a, a = b][0];