Montag, 15. September 2014

Sponsor: Hack Reactor – New Remote Beta Classes




CSS-Tricks





Sponsor: Hack Reactor – New Remote Beta Classes



With the success of their first online cohort, Hack Reactor is happy to announce two new Remote Beta classes. Aspiring engineers no longer need to relocate to San Francisco in order to attend their 12-week immersive JavaScript coding school. Take Hack Reactor’s program from home and gain the skills and confidence needed to build amazing products.


Remote Beta Classes begin October 27th & December 8th with recommended application deadlines of September 29th and November 10th, respectively. Apply today and take their exceptional Remote Beta program from wherever you are!


Direct Link to Article — Permalink



Sponsor: Hack Reactor – New Remote Beta Classes is a post from CSS-Tricks








Mittwoch, 10. September 2014

Give Designers Tools To Get You What You Need




CSS-Tricks





Give Designers Tools To Get You What You Need



Let's say you're having a health back-and-forth between you, a front end developer, and a designer. All the best work comes through collaboration and iteration, right? I believe that. But I also believe there are time traps in there. If you feel you're wasting time by getting peppered with tiny design tweaks, there might be salvation in giving designers tools to do the tweaks and then give you exactly what you need.


Let's see some simple examples.



Positioning Elements in a Space


Let's say you have some absolutely positioned elements. Their exact position is a big part of how the design feels. You could hook up a script to allow those elements to be dragged, and then report their position with the exact CSS you'll need to put them there.


Here I've used David DeSandro's Draggabilly to do that:


See the Pen Draggable Elements that Report their Position in CSS by Chris Coyier (@chriscoyier) on CodePen.


It's a fairly small little script that allows elements to be dragged, and when a drag event stops, it creates the little bit of CSS needed based on that elements ID and top/left position, and puts that in the bottom right corner of the document ready for copy-pasting and providing to you, the front end developer.


Changing Font Stuff


How about little <select> that comes up over paragraphs that allows you to toggle between font families in development?



Or perhaps a range input that controls line-height:



Like this.


Color Pickers


Wouldn't be too difficult to drop in a color picker that shows up around elements you choose and changes backgrounds, fills, borders, strokes, text color, etc.




JSColor


DevTools Training


Of course building your own tools is great because:


They can be designed just how you like them

You can build them to change anything whatsoever

They can be integrated directly onto the design


But... they are a bunch of work as well. You will likely get quite a bit of mileage out of just showing a designer how to use a browsers DevTools to tweak designers. Chances are, they will really take to it.



Have you ever done this?


While I prefaced the article with the idea that this could be time saving for the developer, since you're giving designers tools in which to express exactly what they want, it could backfire. Meaning you could spend a ton of time on these little tools only to see them used very little. Communication, as always, is key.


Have you ever tried doing something like this in your team workflow?




Give Designers Tools To Get You What You Need is a post from CSS-Tricks








Montag, 8. September 2014

Glitch Effect on Text / Images / SVG




CSS-Tricks





Glitch Effect on Text / Images / SVG



Lucas Bebber's Glitch is a super cool effect. It's like you're looking at some text displayed on a progressive scan monitor that has been dropped on the ground one too many times and so the alignment of the pixels is off in weirdly un-even amounts time and space.


It's bonafide CSS trick if there ever was one! It took me a bit to figure out how it was working, so I thought I'd explain. Then I ended up making it work for other kinds of content as well as making it into a group of Sass @mixins to make working with it a bit easier.



See the Pen CSS Glitched Text by Lucas Bebber (@lbebber) on CodePen.


Three Copies of the Text


While the HTML is just:


<div class="glitch" data-text="GLITCH">GLITCH</div> 

Three copies of it are created, via pseudo elements, and they are positioned right on top of each other.


.glitch {
position: relative;
}
.glitch::before,
.glitch::after {
content: attr(data-text);
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

Producing three unique copies that can be controlled individually:



Alter the Copies


Each copy is identical except that:



  • It's offset to the left or right

  • It has a highlight, in the form of a text-shadow


Between the offset and the highlight, it gives it that broken feeling.


.glitch::before {
/* ... anything needed to make it identical */

/* variation */
left: 2px;
text-shadow: -1px 0 red;

/* important: opaque background masks the original */
background: black;
}
.glitch::after {
/* ... anything needed to make it identical */

/* variation */
left: -2px;
text-shadow: -1px 0 blue;

/* important: opaque background masks the original */
background: black;
}

So now the three copies are like this:



Clipping the Copies


You'd only ever see the top copy if left as-is. Probably the ::after version, unless you alter with z-index. But never fear, we're going to be only revealing parts of the top, altered copies with the clip property. This property is apparently deprecated in favor of clip-path, but at the time of this writing, only clip was working for me. I'm sure that will change in time, so we'll have to keep an eye on it, and presumably Autoprefixer will handle it.


The syntax for clip is kinda weird. For the four values, you might expect something like top/left/width/height, or point-at-top-left/point-at-bottom-right. But instead it's like margin and padding: top/right/bottom/left


.glitch::before {
clip: rect(44px, 450px, 56px, 0);
/*
Essentially a box from 0, 44px
to 450px, 56px
*/
}

Here's some example clips on those layers, now with fully opaque backgrounds applied (but still rotated and with extra colors so you can see what's going on):



Animate the Clips


Turns out clip can be animated, so, that clipped box will animate to a new position over time if told to, Here's an example of keyframes:


@keyframes glitch-anim {
0% {
clip: rect(70px, 450px, 76px, 0);
}
20% {
clip: rect(29px, 450px, 16px, 0);
}
40% {
clip: rect(76px, 450px, 3px, 0);
}
60% {
clip: rect(42px, 450px, 78px, 0);
}
80% {
clip: rect(15px, 450px, 13px, 0);
}
100% {
clip: rect(53px, 450px, 5px, 0);
}
}

Notice the left and right values remain the same, it's just the top and bottom that change. And those values are kinda random.


You can generate that pretty easily with Sass, like:


@keyframes glitch-anim {
$steps: 10;
@for $i from 0 through $steps {
#{percentage($i*(1/$steps))} {
clip: rect(random(100)+px, 9999px, random(100)+px, 0);
}
}
}

Because you'd want two sets of randomized clipping positions, you'd make two sets of those @keyframes, and apply them to the copies:


.glitch::before {
...

animation: glitch-anim-1 2s infinite linear alternate-reverse;
}

.glitch::after {
...

animation: glitch-anim-2 2s infinite linear alternate-reverse;
}

There is where we set the speed (number of keyframes also affects speed) as well as making it run infinitely back and forth.


It's pretty fun to watch:



Although it should go without saying a little goes a long way.


Sass @mixins


I thought it would be neat if the technique was more reusable. Basically call a @mixin with parameters to control the effect and get just what you need.


.example-one {
font-size: 100px;
@include textGlitch("example-one", 17, white, black, red, blue, 450, 115);
}

Here's my take on it:


/*
(TEXT) PARAMS
=================
1. Namespace
2. Intensity
3. Text color
4. Background color (flat)
5. Highlight #1 color
6. Highlight #2 color
7. Width (px)
8. Height (px)
*/

@mixin textGlitch($name, $intensity, $textColor, $background, $highlightColor1, $highlightColor2, $width, $height) {

color: $textColor;
position: relative;
$steps: $intensity;

// Ensure the @keyframes are generated at the root level
@at-root {
// We need two different ones
@for $i from 1 through 2 {
@keyframes #{$name}-anim-#{$i} {
@for $i from 0 through $steps {
#{percentage($i*(1/$steps))} {
clip: rect(
random($height)+px,
$width+px,
random($height)+px,
0
);
}
}
}
}
}
&:before,
&:after {
content: attr(data-text);
position: absolute;
top: 0;
left: 0;
width: 100%;
background: $background;
clip: rect(0, 0, 0, 0);
}
&:after {
left: 2px;
text-shadow: -1px 0 $highlightColor1;
animation: #{$name}-anim-1 2s infinite linear alternate-reverse;
}
&:before {
left: -2px;
text-shadow: 2px 0 $highlightColor2;
animation: #{$name}-anim-2 3s infinite linear alternate-reverse;
}

}

There are a million different ways to approach it, this is just one. Totally depends on how much you want the mixin to do for you, how much customizability you want or need, what you want left in the HTML, etc.


I also made two more mixins, one for applying this effect to images, and on for inline SVG. They are different, because they don't use pseudo elements to make the copies, coloring happens in different ways, positioning happens differently, etc. Here's all three of them together.


And a demo:


See the Pen CSS Glitched Text by Chris Coyier (@chriscoyier) on CodePen.




Glitch Effect on Text / Images / SVG is a post from CSS-Tricks








Donnerstag, 4. September 2014

Sponsor: Frontend Masters – Expert Web Development Training




CSS-Tricks





Sponsor: Frontend Masters – Expert Web Development Training



Frontend Masters produces quality video training by industry leaders like Kyle Simpson, Douglas Crockford and Estelle Weyl. I like how these courses are targeted at intermediate and advanced developers:


You've got the basics down and don't want to learn from teachers who just spoon feed you their google results - you want to learn from the masters! Here, you get experts as teachers - people who are actively shaping the industry


Upgrade your JavaScript, CSS and front-end skills with Frontend Masters courses. Check out the complete course list.


Direct Link to Article — Permalink



Sponsor: Frontend Masters – Expert Web Development Training is a post from CSS-Tricks








Mittwoch, 3. September 2014

New Poll: What one specific skill set are you most jealous of?




CSS-Tricks





New Poll: What one specific skill set are you most jealous of?



Rather than ask you what you are already good at, I thought it would be interesting to ask you what do you wish you were good at.


I just picked ten skill sets that all have to do with the web/tech world:




  • Management

  • Marketing

  • Knowing / Practicing the Law

  • Back End Development

  • Front End Development

  • Video / Audio Production

  • Database Administration

  • Server Management

  • Writing / Copywriting

  • Visual / Interaction Design


If you had to just pick one of those that you most wish you were good at (and aren't now), what would it be? Which are you the most jealous of?


Vote in the poll widget in the sidebar (large screen) or further down the ol' tube (small screen).


If you have one that isn't on the list, feel free to share in the comments. But in the interest of a fair data set, we'll leave the poll how it is.




New Poll: What one specific skill set are you most jealous of? is a post from CSS-Tricks








Dienstag, 2. September 2014

Centering in CSS: A Complete Guide




CSS-Tricks





Centering in CSS: A Complete Guide



Centering things in CSS is the poster child of CSS complaining. Why does it have to be so hard? They jeer. I think the issue isn't that it's difficult to do, but in that there so many different ways of doing it, depending on the situation, it's hard to know which to reach for.


So let's make it a decision tree and hopefully make it easier.



I need to center...


Horizontally



Is it inline or inline-* elements (like text or links)?



You can center inline elements horizontally, within a block-level parent element, with just:


.center-children {
text-align: center;
}

See the Pen Centering Inline Elements by Chris Coyier (@chriscoyier) on CodePen.


This will work for inline, inline-block, inline-table, inline-flex, etc.



Is it a block level element?



You can center a block-level element by giving it margin-left and margin-right of auto (and it has a set width, otherwise it would be full width and wouldn't need centering). That's often done with shorthand like this:


.center-me {
margin: 0 auto;
}

See the Pen Centering Single Block Level Element by Chris Coyier (@chriscoyier) on CodePen.


This will work no matter what the width of the block level element you're centering, or the parent.


Note that you can't float an element to the center. There is a trick though.



Is there more than one block level element?



If you have two or more block-level elements that need to be centered horizontally in a row, chances are you'd be better served making them a different display type. Here's an example of making them inline-block and an example of flexbox:


See the Pen Centering Row of Blocks by Chris Coyier (@chriscoyier) on CodePen.


Unless you mean you have multiple block level elements stacked on top of each other, in which case the auto margin technique is still fine:


See the Pen Centering Row of Blocks by Chris Coyier (@chriscoyier) on CodePen.




Vertically



Vertical centering is a bit trickier in CSS.


Is it inline or inline-* elements (like text or links)?



Is it a single line?



Sometimes inline / text elements can appear vertically centered, just because there is equal padding above and below them.


.link {
padding-top: 30px;
padding-bottom: 30px;
}

See the Pen Vertically Centering Text by Chris Coyier (@chriscoyier) on CodePen.


If padding isn't an option for some reason, and you're trying to center some text that you know will not wrap, there is a trick were making the line-height equal to the height will center the text.


.center-text-trick {
height: 100px;
line-height: 100px;
white-space: nowrap;
}

See the Pen Centering a line with line-height by Chris Coyier (@chriscoyier) on CodePen.



Is it multiple lines?



Equal padding on top and bottom can give the centered effect for multiple lines of text too, but if that isn't going to work, perhaps the element the text is in can be a table cell, either literally or made to behave like one with CSS. The vertical-align property handles this, in this case, unlike what it normally does which is handle the alignment of elements aligned on a row. (More on that.)


See the Pen Centering text (kinda) with Padding by Chris Coyier (@chriscoyier) on CodePen.


If something table-like is out, perhaps you could use flexbox? A single flex-child can be made to center in a flex-parent pretty easily.


.flex-center-vertically {
display: flex;
justify-content: center;
flex-direction: column;
height: 400px;
}

See the Pen Vertical Center Multi Lines of Text with Flexbox by Chris Coyier (@chriscoyier) on CodePen.


Remember that it's only really relevant if the parent container has a fixed height (px, %, etc), which is why the container here has a height.


If both of these techniques are out, you could employ the "ghost element" technique, in which a full-height pseudo element is placed inside the container and the text is vertically aligned with that.


.ghost-center {
position: relative;
}
.ghost-center::before {
content: " ";
display: inline-block;
height: 100%;
width: 1%;
vertical-align: middle;
}
.ghost-center p {
display: inline-block;
vertical-align: middle;
}

See the Pen Ghost Centering Multi Line Text by Chris Coyier (@chriscoyier) on CodePen.




Is it a block-level element?



Do you know the height of the element?



It's fairly common to not know the height in web page layout, for lots of reasons: if the width changes, text reflow can change the height. Variance in the styling of text can change the height. Variance in the amount of text can change the height. Elements with a fixed aspect ratio, like images, can change height when resized. Etc.


But if you do know the height, you can center vertically like:


.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
height: 100px;
margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
}

See the Pen Center Block with Fixed Height by Chris Coyier (@chriscoyier) on CodePen.



Is the element of unknown height?



It's still possible to center it by nudging it up half of it's height after bumping it down halfway:


.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}

See the Pen Center Block with Fixed Height by Chris Coyier (@chriscoyier) on CodePen.



Can you use flexbox?



No big surprise, this is a lot easier in flexbox.


.parent {
display: flex;
flex-direction: column;
justify-content: center;
}

See the Pen Center Block with Unknown Height with Flexbox by Chris Coyier (@chriscoyier) on CodePen.





Both Horizontally and Vertically



You can combine the techniques above in any fashion to get perfectly centered elements. But I find this generally falls into three camps:


Is the element of fixed width and height?



Using negative margins equal to half of that width and height, after you've absolutely positioned it at 50% / 50% will center it with great cross browser support:


.parent {
position: relative;
}

.child {
width: 300px;
height: 100px;
padding: 20px;

position: absolute;
top: 50%;
left: 50%;

margin: -70px 0 0 -170px;
}

See the Pen Center Block with Fixed Height and Width by Chris Coyier (@chriscoyier) on CodePen.



Is the element of unknown width and height?



If you don't know the width or height, you can use the transform property and a negative translate of 50% in both directions (it is based on the current width/height of the element) to center:


.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

See the Pen Center Block with Unknown Height and Width by Chris Coyier (@chriscoyier) on CodePen.



Can you use flexbox?



To center in both directions with flexbox, you need to use two centering properties:


.parent {
display: flex;
justify-content: center;
align-items: center;
}

See the Pen Center Block with Unknown Height and Width with Flexbox by Chris Coyier (@chriscoyier) on CodePen.




Conclusion



You can totally center things in CSS.





Centering in CSS: A Complete Guide is a post from CSS-Tricks








Montag, 1. September 2014

Advantages to Native CSS Variables




CSS-Tricks





Advantages to Native CSS Variables



I tend to think variables are best left to a preprocessing step, but there are some distinct things that having them directly in CSS can do:




  1. Daniel Imms:


    [they] can change at runtime, allowing the implementation of things like theming to be done more elegantly.


    i.e. change the value of a variable on a live, rendered web page and see the changes reflected everywhere that uses that variable.



  2. Peter Gasston:


    you can set the value through the setProperty() method on the style object


    Which makes #1 possible and opens up cool possibilities.



  3. They can be scoped to an element. So perhaps you could set variables representing color to theme a site, but override them at a component level to apply different themes in smaller places as needed.




Direct Link to Article — Permalink



Advantages to Native CSS Variables is a post from CSS-Tricks