Post pobrano z: ANAKIN![]()
Archiwum kategorii: CSS
Jardan Furniture
Post pobrano z: Jardan Furniture![]()
Do you put anything in your code specifically for “Find in Project”?
Post pobrano z: Do you put anything in your code specifically for “Find in Project”?
During a having a team meeting the other day, a code formatting idea came up that I thought was super interesting. It had to do with formatting code in a such a way that made it easier to find what you were looking for later with any code editors „Find in Project” feature.
Here’s what it was.
When declaring a function in JavaScript, put a space after the function name and before the opening parenthesis, like…
function doSomething () {
}
That space between doSomething and () is perfectly fine. Then when you call the function, don’t use a space, like this:
doSomething();
It’s just a syntax convention.
But now, „Find in Project” is more useful. If we want to quickly find where that function was defined, we can search for „doSomething ()”, and if we want to find instances of where it is used, we can look for „doSomething()”.
You could extend the idea to classes or whatever too:
class myThing () {
constructor () {
}
doThing () {
}
}
let example = new myThing();
example.doThing();
I’d say that’s worth doing.
It reminds me of when I need to where a Ruby method is definied, I can always search for „def foo” since that „def ” is required for creating the method.
I can imagine a scenario where it might be useful to define base classes in CSS like .module { but then if it’s a variation or nested or used again for any other reason… .module{.
Here’s a classic:
// TODO: 😅
— Charlotte Dann (@charlotte_dann) September 10, 2017
Leaving TODO comments throughout your code base turns a „Find in Project” for „TODO” an instant list of things you need to work on. Surely that could be even more nuanced, incorporating things like priority levels and assignments. Perhaps even emoji’s to help like Charlotte suggested!
Here’s an interesting one:
Use long classes/variables, mark future refactor points with comments. What are you looking for?
— Chris Pauley (@ChrisPauley) September 10, 2017
I like the idea of „long names”. I suppose the longer and more descriptively you name things, the easier they are to search for and get specific results (avoiding repetitive matches like you would with very simple names).
Comments are a great place to leave things to search for. I often use a convention where I put my own name in the comment if I want people to know it was me leaving the comment, but it’s not necessarily for me.
.thing {
overflow: hidden; /* [Chris]: ugkghk this fixed a layout thing but I didn't have time to figure out exactly why. */
}
A smattering of responses:
At top of each template I add "<!– Start [name of template] –>", at bottom, same but "End." Helps FIP and in browser-rendered code.
— Dale Henry Geist (@dalehenrygeist) September 10, 2017
I use consistent and natural language to name things, so when documentation goes stale, at least the code itself is still easy to stab at.
— John James Jacoby (@JJJ) September 10, 2017
Not in the code itself but in Sublime settings folder_exclude_patterns you can pass in an array of folders to exclude from all searches
— Ian C Woodward (@IanCWoodward) September 10, 2017
// TODO 😕
// FIXME 😲
// WT* 😤💣— Kamran Ahmed (@kamranahmedse) September 10, 2017
ctags is a lifesaver.
— Harry Roberts (@csswizardry) September 10, 2017
Naming conventions mate! BEM-esque those classes up `–thing` is always easier to find than `thing`
— James Bosworth (@fltwhte) September 11, 2017
Do you put anything in your code specifically for “Find in Project”? is a post from CSS-Tricks
Custom Elements Everywhere
Post pobrano z: Custom Elements Everywhere
Custom Elements Everywhere is a site created by Rob Dodson. It displays the results of a set of tests that check JS frameworks that use Custom Elements and Shadow DOM for interoperability issues.
It could look like a report card at first glance, but the description at the top of the site nicely sums up the goal of comparing frameworks:
This project runs a suite of tests against each framework to identify interoperability issues, and highlight potential fixes already implemented in other frameworks. If frameworks agree on how they will communicate with Custom Elements, it makes developers’ jobs easier; they can author their elements to meet these expectations.
Nice! Consensus and consistency are exactly what Custom Elements needs in light of the official spec being in working draft and the surge in JS frameworks using them.
Direct Link to Article — Permalink
Custom Elements Everywhere is a post from CSS-Tricks
Breaking the Grid
Post pobrano z: Breaking the Grid
If you thought CSS Grid solves issues where overflowed content escaping the confines of a horizontal layout, then think again. Dave Rupert writes up two ways he unintentionally broke outside the grid and how he wrangled things back into place.
As a Front-End developer nothing bothers me more than seeing an unexpected horizontal scrollbar on a website. While building out a checkout layout with CSS Grid I was surprised to find something mysterious was breaking the container. I thought Grid sort of auto-solved sizing.
Eventually I found two ways to break CSS Grid. As it would happen, I was doing both in the same layout.
Turns out these special cases boil down to:
- Using
overflow-xon an grid element - Using grid on form controls (or, more specifically, replaced elements)
Dave’s solution is a set of CSS rules affectionately named Fit Grid, which is a helper class that effectively removes and replaces the automated min-width: auto property assigned to grid items. This is a super helpful resource, though he admits it toes the line of „Clearfix 2.0” territory.
Direct Link to Article — Permalink
Breaking the Grid is a post from CSS-Tricks
Building a design system for HealthCare.gov
Post pobrano z: Building a design system for HealthCare.gov
Sawyer Hollenshead has written up his thoughts about how he collaborated with the designers and developers on the HealthCare.gov project.
In this post, I’d like to share some of the bigger technical decisions we made while building that design system. Fortunately, a lot of smart folks have already put a lot of thought into the best approaches for building scalable, developer-friendly, and flexible design systems. This post will also shine a light on those resources we used to help steer the technical direction.
There’s a lot going on in here, from guidelines on code architecture and documentation to build systems and versioning. In other words, there’s a lot of great detail on the inner workings of a massive public project that many of us are at least outwardly familiar with.
Interesting to note that this project is an offshoot of the United States Design Systems project, but tailored specifically for the Centers of Medicare & Medicaid Services, which oversees HealthCare.gov.
Direct Link to Article — Permalink
Building a design system for HealthCare.gov is a post from CSS-Tricks
When Design Becomes Part of the Code Workflow
Post pobrano z: When Design Becomes Part of the Code Workflow
I recently did an experiment where I created the same vector illustration in three different applications, exported the illustration as SVG in each application, then wrote a post comparing the exported code.
While I loved the banter and insights that came in the comments, I was surprised that the bulk of conversation was centered on the file size of the compiled SVG.
I wasn’t surprised because performance and SVG do not go hand-in-hand or that performance isn’t the sort of thing we generally care about in the front-end community. I was surprised because my personal takeaway from the experiment was a reminder that SVG code is code at the end of the day and that the way we create SVG in applications is now more a part of the front-end workflow than perhaps it has been in the past.
I still believe that is the key point from the post and wanted to write a follow-up that not only more clearly articulates it, but also details how we may need to change the way we think about design deliverables for projects that use SVG.
The gap between design and code is getting narrower
We already know this and have extolled the virtues of designers who know how to code. However, what the SVG experiment revealed to me is that those virtues are no longer so much an ideal as much as they are a growing necessity.
If a project calls for SVG and a designer has been tasked with creating illustrations and providing design assets for development, then the designer is no longer handing over a static file, but a snippet of code and, depending on the scope of the project, that code may very well be inlined or injected directly into the HTML document.
Sure, we can intervene and check the code that is provided. We may even run it through a tool like SVGOMG or have automated tasks that help clean and optimize the code before it gets served to production. That is all great, but does not change the fact that what we were delivered in the first place was a piece of code and that there is now an additional consideration in our workflow to code review a design asset.
That’s a significant change. It’s not a bad change or even true in all scenarios, but it is a significant one for no reason more than it requires a change in how we think about, request, and handle design deliverables on a project.
A new era of design etiquette is upon us
I was one of many, many fans of the Photoshop Etiquette site when I learned about it. It not only struck about a dozen nerves that rang true to my own experiences working with other designers on web projects, but forced me to re-examine and improve my own design practices for the benefit of working within teams. Tips like nicely organized layers with a consistent naming convention make a world of difference when a file is handed off from one person to another, much like nicely documented CSS that uses consistent naming conventions and is generous with comments.
SVG makes these tips much more about necessity than etiquette. Again, now that we have a design deliverable that becomes code, the decisions a designer makes—from configuring an artboard to how the layers are grouped and named—all influence how the SVG code is compiled and ultimately used in production.
Perhaps it’s time for an offshoot of Photoshop Etiquette that is more squarely focused on SVG design deliverables using illustration applications.
Applications are super smart, but still need human intervention
My favorite comment from the previous post was a manually coded rendition of the SVG illustration. The code was much cleaner and way more efficient than any of the versions generated by the applications being compared.
Whether or not it was the point of the comment, what I love most about it is how it proves we cannot always take what applications give us for granted. It’s freaking amazing that an application like Sketch can take a series of shapes I draw on a screen and turn them into valid and working code, but is it the best code for the situation? It could be. Then again, the commenter proved that it could be done better if the goal was a smaller file size and more readable code.
All three of the applications I tested are remarkably smart, incredibly useful, and have unique strengths that make each one a legitimate and indispensable tool in anyone’s web development arsenal. The point here is not to distrust them or stay away from using them.
The point is that they are only as smart as the people using them. If we give them bad shapes and disorganized layers, then we can likely expect less-than-optimal code in return. I would go so far as to say that my method for creating the illustration in the experiment likely influenced the final output in all three cases and may not have given the applications the best shot for generating stellar code.
Either way, it took a human reviewing that generated code and optimizing it by hand to make the point.
Wrapping Up
I want to give a big ol’ thanks to everyone who commented on the previous post. What started as a simple personal curiosity became a more nuanced experiment and I was stoked to see it spark healthy debate and insightful ideas. It was those comments and some ensuing offline conversations that made me think deeper about about the the hand-off between design and development which ultimately wound up being the key takeaway from the entire exercise.
When Design Becomes Part of the Code Workflow is a post from CSS-Tricks
JavaScript Scope and Closures
Post pobrano z: JavaScript Scope and Closures
Scopes and closures are important in JavaScript. But, they were confusing for me when I first started. Here’s an explanation of scopes and closures to help you understand what they are.
Let’s start with scopes.
Scope
A scope in JavaScript defines what variables you have access to. There are two kinds of scope – global scope and local scope.
Global scope
If a variable is declared outside all functions or curly braces ({}), it is said to be defined in the global scope.
This is true only with JavaScript in web browsers. You declare global variables in Node.js differently, but we won’t go into Node.js in this article.
const globalVariable = 'some value'
Once you’ve declared a global variable, you can use that variable anywhere in your code, even in functions.
const hello = 'Hello CSS-Tricks Reader!'
function sayHello () {
console.log(hello)
}
console.log(hello) // 'Hello CSS-Tricks Reader!'
sayHello() // 'Hello CSS-Tricks Reader!'
Although you can declare variables in the global scope, it is advised not to. This is because there is a chance of naming collisions, where two or more variables are named the same. If you declared your variables with const or let, you would receive an error whenever you a name collision happens. This is undesirable.
// Don't do this!
let thing = 'something'
let thing = 'something else' // Error, thing has already been declared
If you declare your variables with var, your second variable overwrites the first one after it is declared. This also undesirable as you make your code hard to debug.
// Don't do this!
var thing = 'something'
var thing = 'something else' // perhaps somewhere totally different in your code
console.log(thing) // 'something else'
So, you should always declare local variables, not global variables.
Local Scope
Variables that are usable only in a specific part of your code are considered to be in a local scope. These variables are also called local variables.
In JavaScript, there are two kinds of local scope: function scope and block scope.
Let’s talk about function scopes first.
Function scope
When you declare a variable in a function, you can access this variable only within the function. You can’t get this variable once you get out of it.
In the example below, the variable hello is in the sayHello scope:
function sayHello () {
const hello = 'Hello CSS-Tricks Reader!'
console.log(hello)
}
sayHello() // 'Hello CSS-Tricks Reader!'
console.log(hello) // Error, hello is not defined
Block scope
When you declare a variable with const or let within a curly brace ({}), you can access this variable only within that curly brace.
In the example below, you can see that hello is scoped to the curly brace:
{
const hello = 'Hello CSS-Tricks Reader!'
console.log(hello) // 'Hello CSS-Tricks Reader!'
}
console.log(hello) // Error, hello is not defined
The block scope is a subset of a function scope since functions need to be declared with curly braces (unless you’re using arrow functions with an implicit return).
Function hoisting and scopes
Functions, when declared with a function declaration, are always hoisted to the top of the current scope. So, these two are equivalent:
// This is the same as the one below
sayHello()
function sayHello () {
console.log('Hello CSS-Tricks Reader!')
}
// This is the same as the code above
function sayHello () {
console.log('Hello CSS-Tricks Reader!')
}
sayHello()
When declared with a function expression, functions are not hoisted to the top of the current scope.
sayHello() // Error, sayHello is not defined
const sayHello = function () {
console.log(aFunction)
}
Because of these two variations, function hoisting can potentially be confusing, and should not be used. Always declare your functions before you use them.
Functions do not have access to each other’s scopes
Functions do not have access to each other’s scopes when you define them separately, even though one function may be used in another.
In this example below, second does not have access to firstFunctionVariable.
function first () {
const firstFunctionVariable = `I'm part of first`
}
function second () {
first()
console.log(firstFunctionVariable) // Error, firstFunctionVariable is not defined
}
Nested scopes
When a function is defined in another function, the inner function has access to the outer function’s variables. This behavior is called lexical scoping.
However, the outer function does not have access to the inner function’s variables.
function outerFunction () {
const outer = `I'm the outer function!`
function innerFunction() {
const inner = `I'm the inner function!`
console.log(outer) // I'm the outer function!
}
console.log(inner) // Error, inner is not defined
}
To visualize how scopes work, you can imagine one-way glass. You can see the outside, but people from the outside cannot see you.

If you have scopes within scopes, visualize multiple layers of one-way glass.

After understanding everything about scopes so far, you’re well primed to figure out what closures are.
Closures
Whenever you create a function within another function, you have created a closure. The inner function is the closure. This closure is usually returned so you can use the outer function’s variables at a later time.
function outerFunction () {
const outer = `I see the outer variable!`
function innerFunction() {
console.log(outer)
}
return innerFunction
}
outerFunction()() // I see the outer variable!
Since the inner function is returned, you can also shorten the code a little by writing a return statement while declaring the function.
function outerFunction () {
const outer = `I see the outer variable!`
return function innerFunction() {
console.log(outer)
}
}
outerFunction()() // I see the outer variable!
Since closures have access to the variables in the outer function, they are usually used for two things:
- To control side effects
- To create private variables
Controlling side effects with closures
Side effects happen when you do something in aside from returning a value from a function. Many things can be side effects, like an Ajax request, a timeout or even a console.log statement:
function (x) {
console.log('A console.log is a side effect!')
}
When you use closures to control side effects, you’re usually concerned with ones that can mess up your code flow like Ajax or timeouts.
Let’s go through this with an example to make things clearer.
Let’s say you want to make a cake for your friend’s birthday. This cake would take a second to make, so you wrote a function that logs made a cake after one second.
I’m using ES6 arrow functions here to make the example shorter, and easier to understand.
function makeCake() {
setTimeout(_ => console.log(`Made a cake`, 1000)
)
}
As you can see, this cake making function has a side effect: a timeout.
Let’s further say you want your friend to choose a flavor for the cake. To do so, you can write add a flavor to your makeCake function.
function makeCake(flavor) {
setTimeout(_ => console.log(`Made a ${flavor} cake!`, 1000))
}
When you run the function, notice the cake gets made immediately after one second.
makeCake('banana')
// Made a banana cake!
The problem here is that you don’t want to make the cake immediately after knowing the flavor. You want to make it later when the time is right.
To solve this problem, you can write a prepareCake function that stores your flavor. Then, return the makeCake closure within prepareCake.
From this point on, you can call the returned function whenever you want to, and the cake will be made within a second.
function prepareCake (flavor) {
return function () {
setTimeout(_ => console.log(`Made a ${flavor} cake!`, 1000))
}
}
const makeCakeLater = prepareCake('banana')
// And later in your code...
makeCakeLater()
// Made a banana cake!
That’s how closures are used to reduce side effects – you create a function that activates the inner closure at your whim.
Private variables with closures
As you know by now, variables created in a function cannot be accessed outside the function. Since they can’t be accessed, they are also called private variables.
However, sometimes you need to access such a private variable. You can do so with the help of closures.
function secret (secretCode) {
return {
saySecretCode () {
console.log(secretCode)
}
}
}
const theSecret = secret('CSS Tricks is amazing')
theSecret.saySecretCode()
// 'CSS Tricks is amazing'
saySecretCode in this example above is the only function (a closure) that exposes the secretCode outside the original secret function. As such, it is also called a privileged function.
Debugging scopes with DevTools
Chrome and Firefox’s DevTools make it simple for you to debug variables you can access in the current scope. There are two ways to use this functionality.
The first way is to add the debugger keyword in your code. This causes JavaScript execution in browsers to pause so you can debug.
Here’s an example with the prepareCake:
function prepareCake (flavor) {
// Adding debugger
debugger
return function () {
setTimeout(_ => console.log(`Made a ${flavor} cake!`, 1000))
}
}
const makeCakeLater = prepareCake('banana')
If you open your DevTools and navigate to the Sources tab in Chrome (or Debugger tab in Firefox), you would see the variables available to you.

You can also shift the debugger keyword into the closure. Notice how the scope variables changes this time:
function prepareCake (flavor) {
return function () {
// Adding debugger
debugger
setTimeout(_ => console.log(`Made a ${flavor} cake!`, 1000))
}
}
const makeCakeLater = prepareCake('banana')

The second way to use this debugging functionality is to add a breakpoint to your code directly in the sources (or debugger) tab by clicking on the line number.

Wrapping up
Scopes and closures aren’t incredibly hard to understand. They’re pretty simple once you know how to see them through a one-way glass.
When you declare a variable in a function, you can only access it in the function. These variables are said to be scoped to the function.
If you define any inner function within another function, this inner function is called a closure. It retains access to the variables created in the outer function.
Feel free to pop by and ask any questions you have. I’ll get back to you as soon as I can.
If you liked this article, you may also like other front-end-related articles I write on my blog and my newsletter. I also have a brand new (and free!) email course: JavaScript Roadmap.
JavaScript Scope and Closures is a post from CSS-Tricks
Managing CSS & JS in an HTTP/2 World
Post pobrano z: Managing CSS & JS in an HTTP/2 World
Trevor Davis on how we’ll link up CSS when we go all-in on HTTP/2:
This is the opposite of what we have done as best practice for years now. But in order to take advantage of multiplexing, it’s best to break up your CSS into smaller files so that only the necessary CSS is loaded on each page. An example page markup would look something like this:
<link href="stylesheets/modules/text-block/index.css" rel="stylesheet">
<div class="text-block">
...
</div>
<link href="stylesheets/modules/two-column-block/index.css" rel="stylesheet">
<div class="two-column-block">
...
</div>
This idea shares some DNA with Critical CSS. Loading CSS with <link> is blocking, so load as little of it as you can right away and load the rest of it as you need it. There is no penalty for loading the stylesheets individually because of HTTP/2 multiplexing, and loading them right before the HTML that uses them actually takes advantage of the blocking by not allowing the rendering of the HTML before the CSS for it is gotten. Plus you’ll be able to break cache on these smaller bits of CSS as needed, just bear in mind it might not compress as well.
The thing is… for any browser that doesn’t support HTTP/2 (e.g. IE 10, Opera mobile/mini, UC browser), while this technique will still work, it will be pretty bad for performance. This will be an easier call to make on projects that don’t need to support those browsers for whatever reason.
Direct Link to Article — Permalink
Managing CSS & JS in an HTTP/2 World is a post from CSS-Tricks
Form Validation with Web Audio
Post pobrano z: Form Validation with Web Audio
I’ve been thinking about sound on websites for a while now.
When we talk about using sound on websites, most of us grimace and think of the old days, when blaring background music played when the website loaded.
Today this isn’t and needn’t be a thing. We can get clever with sound. We have the Web Audio API now and it gives us a great deal of control over how we design sound to be used within our web applications.
In this article, we’ll experiment with just one simple example: a form.
What if when you were filling out a form it gave you auditory feedback as well as visual feedback. I can see your grimacing faces! But give me a moment.
We already have a lot of auditory feedback within the digital products we use. The keyboard on a phone produces a tapping sound. Even if you have „message received” sounds turned off, you’re more than likely able to hear your phone vibrate. My MacBook makes a sound when I restart it and so do games consoles. Auditory feedback is everywhere and pretty well integrated, to the point that we don’t really think about it. When was the last time you grimaced at the microwave when it pinged? I bet you’re glad you didn’t have to watch it to know when it was done.
As I’m writing this article my computer just pinged. One of my open tabs sent me a useful notification. My point being sound can be helpful. We may not all need to know with our ears whether we’ve filled a form incorrectly, there may be plenty of people out there that do find it beneficial.
So I’m going to try it!
Why now? We have the capabilities at our finger tips now. I already mentioned the Web Audio API, we can use this to create/load and play sounds. Add this to HTML form validating capabilities and we should be all set to go.
Let’s start with a small form.
Here’s a simple sign up form.
See the Pen Simple Form by Chris Coyier (@chriscoyier) on CodePen.
We can wire up a form like this with really robust validation.
With everything we learned from Chris Ferdinandi’s guide to form validation, here’s a version of that form with validation:
See the Pen Simple Form with Validation by Chris Coyier (@chriscoyier) on CodePen.
Getting The Sounds Ready
We don’t want awful obtrusive sounds, but we do want those sounds to represent success and failure. One simple way to do this would be to have a higher, brighter sounds which go up for success and lower, more distorted sounds that go down for failure. This still gives us very broad options to choose from but is a general sound design pattern.
With the Web Audio API, we can create sounds right in the browser. Here are examples of little functions that play positive and negative sounds:
See the Pen Created Sounds by Chris Coyier (@chriscoyier) on CodePen.
Those are examples of creating sound with the oscillator, which is kinda cool because it doesn’t require any web requests. You’re literally coding the sounds. It’s a bit like the SVG of the sound world. It can be fun, but it can be a lot of work and a lot of code.
While I was playing around with this idea, FaceBook released their SoundKit, which is:
To help designers explore how sound can impact their designs, Facebook Design created a collection of interaction sounds for prototypes.

Here’s an example of selecting a few sounds from that and playing them:
See the Pen Playing Sound Files by Chris Coyier (@chriscoyier) on CodePen.
Another way would be to fetch the sound file and use the audioBufferSourceNode. As we’re using small files there isn’t much overhead here, but, the demo above does fetch the file over the network everytime it is played. If we put the sound in a buffer, we wouldn’t have to do that.
Figuring Out When to Play the Sounds
This experiment of adding sounds to a form brings up a lot of questions around the UX of using sound within an interface.
So far, we have two sounds, a positive/success sound and a negative/fail sound. It makes sense that we’d play these sounds to alert the user of these scenarios. But when exactly?
Here’s some food for thought:
- Do we play sound for everyone, or is it an opt-in scenario? opt-out? Are there APIs or media queries we can use to inform the default?
- Do we play success and fail sounds upon form submission or is it at the individual input level? Or maybe even groups/fieldsets/pages?
- If we’re playing sounds for each input, when do we do that? On
blur? - Do we play sounds on every blur? Is there different logic for success and fail sounds, like only one fail sound until it’s fixed?
There aren’t any extremely established best practices for this stuff. The best we can do is make tasteful choices and do user research. Which is to say, the examples in this post are ideas, not gospel.
Demo
Here’s one!
And here’s a video, with sound, of it working:
Voice
Greg Genovese has an article all about form validation and screen readers. „Readers” being relevant here, as that’s all about audio! There is a lot to be done with aria roles and moving focus and such so that errors are clear and it’s clear how to fix them.
The Web Audio API could play a role here as well, or more likely, the Web Speech API. Audio feedback for form validation need not be limited to screen reader software. It certainly would be interesting to experiment with reading out actual error messages, perhaps in conjunction with other sounds like we’ve experimented with here.
Thoughts
All of this is what I call Sound Design in Web Design. It’s not merely just playing music and sounds, it’s giving the sound scape thought and undertaking some planning and designing like you would with any other aspect of what you design and build.
There is loads more to be said on this topic and absolutely more ways in which you can use sound in your designs. Let’s talk about it!
Form Validation with Web Audio is a post from CSS-Tricks