Css secrets lea verou pdf download
You'll learn how to apply Lea's analytical approach to practically every CSS problem you face to attain DRY, maintainable, flexible, lightweight, and standards-compliant results.
Inspired by her popular talks at over 60 international web development conferences, Lea Verou provides a wealth of information for topics including:. JavaScript is the little scripting language that could. Once used chiefly to add interactivity to web …. React helps you create and work on an app in just a few minutes, but learning ….
Learn algorithms for solving classic computer science problems with this concise guide covering everything from fundamental …. Scott, Shelley Powers.
CSS Secrets features dozens of tips, methods, techniques and practical approaches to help intermediate CSS developers to climb the ladder to advanced skillsets. What makes this book absurdly amazing is the fact that Lea covers all of her tips with prime code examples, ready to be reused whenever you feel ready.
The premise is. The Book starts with how the CSS standards are developed. Then, Lea goes on to reveal 47 CSS secrets spread across various chapters. Although some chapters do require a prerequisite sometimes another chapter from the same book ,the chapters can be read in a. You'll learn how to apply Lea's analytical approach to practically. Last edited by Nikorr. CSS secrets Lea Verou. Edition Notes Includes index. Share this book. The Ersatz Elevator.
We can similarly create stripes with unequal widths, by adjusting the color stop positions Figure 2. Therefore, the following code also creates the exact same gradient we saw in Figure 2. For example, the following snippet will produce horizontal stripes of three colors Figure 2.
Just as many are vertical stripes Figure 2. Thankfully, CSS gradients can help us recre- ate those too, with varying degrees of dificulty. The code for vertical stripes is almost the same, with one main differ- ence: an extra irst argument that speciies the gradient direction.
We could have speciied it for horizontal stripes too, but the default to bottom was exactly what we needed for them. The reason is that we just rotated the gradient inside each tile by 45 degrees, not the repeated background as a whole. Try to remember the bitmap images we usually use to create diagonal stripes, such as the one in Figure 2.
They include four stripes instead of two, so that they tile seamlessly. To understand why this happened, we need to remember the Pythagorean theorem we learned at school about calculating the lengths of the sides of right triangles. In our diagonal stripes, the background size speciies the length of the hypotenuse, but the stripe width is actually the length of the leg. However, unless somebody is pointing a gun at your head threatening to kill you unless you are able to produce diagonal stripes that are exactly 15 pixels wide in which case, you would die anyway, because 2 is not a rational number, so even this is an approximation—though a very high-precision one , I would strongly rec- ommend rounding this unwieldy number, to something like.
If we just try to change the angle of the gradient, the result looks awful check out Figure 2. Thankfully, there is a better way to create diagonal stripes. These work exactly the same way, stripes with one difference: the color stops are repeated indeinitely, until they ill up the whole image. So, for example, this repeating gradient shown in Figure 2.
Due to their repeating nature, it means our whole background can be in the generated gradient image. For comparison, the background we created in Figure 2. Also note that our measurements are now in the gradient color stops instead of background-size. The back- ground size is the initial one, which for gradients is the size of the element.
This means that the lengths are also more straightforward, as they are measured on the gradient line, which is perpendicular to our stripes. No more clunky 2 calculations! However, the biggest beneit is that now we can just change the angle to whatever we want, and it just works without having to think hard and long about how to make a seamless tile. Note that with this method we need four color stops for two stripe colors, regardless of the stripe angle.
This will work as a shortcut to two consec- utive color stops with the same color and different positions, something very commonly needed to create gradient-based patterns. For example, the code for the diagonal stripes in Figure 2.
Unfortunately, at the time of writing, this is not yet supported in any browser. For example, take a look at these stripes: background: repeating-linear-gradient 0deg, b, b px, a 0, a 0px ; You can see in Figure 2.
However, that relationship between the colors is not easy to tell by reading the code. Moreover, if we wanted to change the base color, we would have to make four! Furthermore, as we will see in the next secret, gradient patterns with transparent regions allow us to create very complex patterns by superim- posing multiple different ones. However, stripes are not the be-all and end-all of background patterns or even just geometric patterns. We quite often need many other different types, such as grids, polka dots, checkerboards, and many others.
Thankfully, CSS gradients can help with many of these too. It was included in almost every article, book, and conference talk that mentioned CSS gradients between and and was used by several browser vendors to ine-tune their CSS gradients implement- ations.
However, not every pattern showcased in it would be a good use case for a production website. Some of them are included only to show what is possible, but their code is extremely long and repetitive. For those cases, SVG is a better choice. For some examples of SVG patterns, visit philbit. Perhaps the easiest such pattern is overlaying horizontal and vertical stripes to create various types of grids.
For example, the following code creates the tablecloth-reminiscent gingham pattern shown in Figure 2. This is a great use case for using lengths comprise it transparency shown here as the conventional gray instead of percentages as gradient color stops: checkerboard background: a; background-image: linear-gradient white px, transparent 0 , linear-gradient 0deg, white px, transparent 0 ; background-size: 0px 0px; The result seen on Figure 2.
An SVG would not have been shorter. We can even overlay two grids with different line widths and colors to create a more realistic blueprint grid Figure 2. However, radial gradients can be very useful as well, as they allow us to create circles, ellipses, or parts thereof. The simplest pattern we can create with a radial gradient is an array of dots Figure 2. However, we can combine An array of dots; the repeating tile is two of those gradients and give them different background positions, to shown with dashed lines create a polka dot pattern Figure 2.
Unfortunately, this means that to change the tile size, we need to make four edits. This is on the brink of being unmaintainable, although whether it has crossed the line is debatable. For instance, subtle checkerboards can be an interesting alternative to a bland solid color back- ground. Also, a gray checkerboard pattern is the de facto standard way to depict transparency, which is required in a number of different UIs. Making a checkerboard pattern in CSS is possible, but considerably trickier than one might expect.
It created by repeating an image, the looks like it should be easy to recreate with CSS: we would just create two tile would be the one denoted by the dashed line squares with different background positions, right? Not exactly. Yes, we can technically create squares with CSS gradients, but with no spacing around them, the result will look like a solid color.
However, there is no way to create squares with space around them with one CSS gradient. The trick is to compose the square from two right triangles. We already know how to create right triangles remember our failed attempt at diagonal stripes in Figure 2. Sure, if we tried to compose squares from two triangles like the ones in Figure 2. However, what if we reduce the legs of these 1 triangles to half their original size, so that they occupy 8 of the tile, instead 1 of the current 2?
Then we would end up with something like Figure 2. For example, the hue wheel shown here would be created with the following gradient: background: conic-gradient red, yellow, lime, aqua, blue, fuchsia, red ; Conical gradients are useful for far more things than hue wheels: starbursts, brushed metal effects, and many other kinds of backgrounds, including you guessed it!
They would enable us to create the repeating tile of Figure 2. Notice that this is es- sentially half a checkerboard. We can improve the code a bit by combining the opposite facing triangles i. To change the accent color or the cell size, we need to make it refers to repetitive, unmaintainable code.
At this point, it might be a good idea to use a preprocessor mixin to reduce duplication. An SVG tile for Figure 2. Most of these patterns only use the multiply blending mode, but other values such as overlay, screen, or difference can be very useful too.
Hardly anything in nature ever repeats in identical tiles. Even in repetition, there is always variation and randomness. No two lowers are ever exactly the same. Can we do better?
The solution One irst idea might be to enhance the illusion of randomness by splitting the lat stripe tile into layers: one base color and three layers of stripes, repeating in different intervals.
We can easily achieve this by hardcoding the stripe width in the color stops and using background-size to control their spacing. As you might remember from school, if we have a few what size would our repeated back- ground image have to be to achieve numbers, the minimum number that can contain any of them an integer the same result? It logically follows that to increase perceived randomness, we need to maximize the size of the repeating tile.
Lists of primes up to very large numbers are widely available on the Web. To maximize randomness even further, we can even use prime numbers for the stripe widths. Note that this is not only useful for backgrounds, but also for anything that involves repetition. For example, the irst 10 prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, On the other hand, relatively prime is a relation between numbers, not an attribute of a single number. Relatively prime numbers have no common divisors, but may have many divisors in general e.
Of course, a prime number is relatively prime with any other number. Check out play. Eric Meyer meyerweb. Dudley Storey has also written a very informative piece on this concept demosthenes.
For example, check out Figure 2. In addition, we want the image to resize to cover the entire border area re- gardless of the dimensions of our element. How would we attempt to do something like this with CSS? How could we possibly slice our image via border-image to create the example in Figure 2. If you try it for a bit, you will likely also conclude that this is not possible with border-image.
But then what can we do? This is suboptimal: not only does it mix presentation and styling, but modifying the HTML is simply not an option in certain cases. Can we do this with only one element? The main idea is to use a second background of pure white, covering the stone art image. However, to make the second image show through the border area, we should apply different values of background-clip to them. One last thing is that we can only have a background color on the last layer, so we need to fake the white via a CSS gradient from white to white.
This is how our irst attempt to apply this idea might look: padding: em; border: em solid transparent; background: linear-gradient white, white , url stone-art.
The reason is that the default Bottom: border-image:. To correct this, we image just need to set background-origin to border-box as well: padding: em; border: em solid transparent; background: linear-gradient white, white , url stone-art. For example, take a look at the following code, which generates a vintage envelope themed border: padding: em; border: em solid transparent; background: linear-gradient white, white padding-box, repeating-linear-gradient - deg, red 0, red.
You can easily change the width of TIP! Unlike our stone art border example, this effect is envelope-border-image and ex- doable with border-image too: periment with changing values. Marching ants borders are dashed borders that seem to scroll like marching ants if you imagine that the dashes are ants. These are in- credibly common in GUIs; image editors use them almost always to indicate area selection Figure 2. Obviously, this is not only useful for marching ants, but also for creating all sorts of custom dashed borders, with different color dashes and custom dash-gap width.
Top border clipping, to mimic traditional footnotes However, border-image can also be quite powerful, and even more when used with gradients. For example, assume we want a clipped top border, like the one commonly used in footnotes. All it takes is border-image and a vertical gradient, with the clipping length hardcoded. The border width is controlled by …border-width. In addition, because we speciied ev- erything in ems, the effect will adjust with font-size changes, and be- cause we used currentColor, it will also adapt to color changes as- suming we want the border to be the same color as the text.
Even if we are designing a static website and its exact content is predetermined, we might want to modify it at some point, or it could be displayed in a fallback font with different metrics. However, with our previous code, that the height is smaller than the width; the border-radius circle is shown is not the case. The resulting shape when the width is larger than the height is shown in Figure 3. Can we even use border-radius to make an here with dashed lines ellipse, let alone a lexible one?
This allows us to create elliptical rounding at the corners Figure 3. You can see in Figure 3. When our dimensions vary depending on content, we have a problem. Another lesser known feature of border-radius is that it accepts percentages, not just lengths. The percentage resolves to the corresponding dimension, width for the horizontal radius and height for the vertical one.
This means the same percentage can compute to different horizontal and vertical radii. It seems that corner-radius would have been much more appropriate. The rounding of the inner corner is smaller max 0, border-radius - border-width to be precise. Fortunately, the border-radius syntax is more lexible than that.
You might be surprised to ind that border-radius is actually a short- hand. We can provide different values for each corner, and there are two different ways to do that. If we provide four values, they each apply to one corner, in clockwise order, starting from the top left. If we provide fewer than four values, they are multiplied in the usual CSS way, akin to properties like border-width.
Three values mean the fourth is the same as the second. Two values mean the third is the same as the irst. Figure 3. We can even provide different horizontal and vertical radii for all four corners, by specifying 1—4 values before the slash and 1—4 different values after it. Note that these are expanded into four values individually.
Is it possible to specify such border-radius values that would gen- erate a shape like this? Can you imagine a corner with zero vertical rounding and positive horizontal rounding? Yup, neither could the spec writers. Putting all this together, we can come up with the CSS code for the lexible half ellipse in Figure 3.
Because the percentage will be the same for both horizontal and ver- tical radii of all four corners, no slash notation is needed. Is there a way to only skew the con- tainer shape without skewing the contents? As you can see in Figure 3. Because our content is not contained in the pseudo-element, it is not affected by the transforma- tion. We need our pseudo-element box to remain lexible and automatically inherit the dimensions of its parent, even when they are determined by its contents.
An easy way to do that is to apply position: relative to the parent, position: absolute to the generated content, and set all off- sets to zero so that it stretches horizontally and vertically to the size of its parent. This is how this code would look:. The inished code would look like this and produce exactly them the same visual result as the previous technique:. For example, using a variation of this technique with a rotate transform on a square element would easily give us a diamond rhombus shape.
Can you guess how? For example, when we need multiple dashed borders or multiple borders with spacing and transparency between them. In fact, until recently, it was basically impossible. Therefore, when web designers want to follow this style, they more often than not pre-crop their images via an image editor. Of course, it goes without saying that this is really not a maintainable way to apply any effect and ends up being a mess if one wants to change the image styling in the future.
Surely, these days there must be a better way, right? Actually, there are two! Unless, of course, we were trying to crop the image in an octagon shape, in which case we can stop now and go do something else with our time.
However, we want our image to be as wide as its diagonal, not its side. As the theorem tells. Our inal cropped image Putting it all together, our inal code looks like this:.
The main idea is to use the clip-path property, another feature borrowed from SVG, that these days can be applied to HTML content too at least in supporting browsers with a nice, readable syntax, unlike its SVG counterpart, which is known to have driven people to madness.
Its main caveat is its at the time of writing limited browser support. You might be familiar with clipping paths from image editing apps like Adobe Photoshop. Clipping paths allow us to clip the element in the shape that we please.
We can even use percentages, and they refer to the dimensions of the element. The result is identical to Figure 3. The property is even ani- matable, as long as we animate between the same shape functions polygon , in our case , with the same number of points. Especially lately, with lat design winning over skeuomorphism, there A button with cutout corners, has been an increase in the popularity of this effect.
However, CSS is still not well equipped for creating this effect in an easy, straightforward one-liner. Is there a better way? The solution One solution comes in the form of the omnipotent CSS gradients. The main be looking at my corners, not trick is to take advantage of the fact that gradients can accept an angle reading my text. The text is direction e.
It would corner cut off, through a simple CSS need a transparent color stop for the cutout corner and another color stop gradient in the same position, with the color we want for our background. You can see the result in Figure 3. Technically, a and TIP! We only included it as a fall- for easier debugging.
In practice, both gradients would be back: if CSS gradients are not supported, the second declaration will be the same color. Our irst thought might be something like this: background: a; background: linear-gradient - deg, transparent px, a 0 , linear-gradient deg, transparent px, 0 ; However, as you can see in Figure 3. By default, both gradients occupy the entire element, so they obscure each other. We need to make them smaller, by using background-size to make each gradient occupy only half the element: Hey, focus!
The text is just placeholder! As you can see, although background-size was applied, the gradients are still covering each Hey, focus! The text is other. The reason for this is that we forgot to turn background-repeat just placeholder! Therefore, our back- grounds are still obscuring each other — by repetition this time.
Hey, focus! You will need four gradients, and the code looks reading my text. The text is like this: just placeholder! It requires ive edits to change the background color and four to change the corner size.
A preprocessor mixin could help reduce the repetition. This is due to the fact that we provided default values for the arguments in our SCSS mixin, and yes, these default values can refer to other arguments as well. The only difference is using radial gra- dients instead of linear ones: background: a; Hey, focus!
The text is transparent px, a 0 top left, just placeholder! Just like in the previous technique, the corner size can be controlled through the color stop positions and a mixin would make the code more maintainable here as well. In the common case, where we want the same corner size on all four corners, we need to make four edits to modify it. Similarly, we need to make four edits to modify the background color, ive counting the fallback.
Thankfully, there are a couple different methods we could use, de- pending on our needs. One of them is to use border-image with an inline SVG that generates the corners. The corners would be of length 1, and the straight edges would also be 1. The result zoomed would look like Figure 3. This does not mean 1 pixel; it is referring to the coordinate system of the SVG ile hence the lack of units.
If we had 1 speciied it in percentages, we would need to approximate 3 of the image with something like. The result is shown in Figure 3. As you can see, our cutout corners Hey, focus! We can solve that in two ways: either be looking at my corners, not by specifying a background, or by adding the keyword fill to our reading my text.
In this case, we are going to go with specifying a background, because it will also act as a fallback. But we speciied a px border width! The reason is that with the gradient, the px was px along the gradient line, which is perpendicular to the direction of the gra- dient. Can you see where this is going? Where did our laboriously created cutout corners go? Fear not, young padawan, for our corners are still there.
The text is As you can see in Figure 3. However, we can easily change the corner size in only one be looking at my corners, not place: we just modify the border width.
We can even animate it, because reading my text. The text is border-width is animatable! We can also change the background with just placeholder! In addition, because our background is now independent of the corner effect, we can even specify a gradient on FIGURE 3.
For Our cutout corners with a radial example, check out Figure 3. If border-image is not sup- ported, the fallback is not only the absence of corners. Due to background clipping, it also looks like there is less spacing between the box edge and its content. As a drawback, this increases the number of edits we need to make to change the background color to three.
For example, we still need to have either a solid color background, or a background that is a solid color toward the edges. The amazing thing about CSS clipping paths is that we can mix percentages which refer to the element dimensions with absolute lengths, offering us tremendous lexibility. Check out Figure 3. None of the previous methods can corners, via clip-path do this. In addition, it is also animatable, not only to different corner sizes, but different shapes altogether.
All we need to do is use a different clipping path. Beyond its WETness and its limited browser support, it also has the drawback that it will clip text, if there is no suficient padding, as it just clips the element without distinguishing between its parts. It will be used in conjunction with border-radius to produce cutout corners of different shapes, with their sizes deined in border-radius. The other two can be at any angle. When authors were not em- Trapezoid shapes, faked through borders on pseudo-elements for ulating them through carefully crafted background images, they were usu- clarity, the pseudo-elements are ally created as a rectangle with two triangles on each side, faked through shown here in darker blue borders Figure 3.
It wastes both available pseudo-elements, and is also very inlexible styling-wise. For example, good luck adding a border, a background texture, or some rounding on that tab. Is there a sane, lexible way to make trapezoid tabs? However, think about rotating a rectangle in the physical, three- dimensional world. The two-dimensional image we usually end up seeing is a trapezoid, due to perspective!
Of course, because we applied the 3D transform to our entire element, the text is also distorted. Canceling them on the inner element is technically possible, but very com- plicated. There is still one issue, though. When we apply a transform without setting a transform-origin, the element is rotated in space around its center. Therefore, its dimensions on the 2D projection in our screen change in many ways, as Figure 3.
You can see the difference in Figure 3. To ix this, we might think of applying some extra top Our trapezoid overlaid on its pre- padding. However, the result will then look awful in browsers with no 3D transform version, to highlight the transforms support Figure 3. Instead, we will increase its size via a changes its metrics go through when using transform-origin: bottom; transform as well, so that the entire thing is dropped when 3D transforms are not supported.
With a little experimentation, we ind that some vertical scaling i. However, the superiority of this technique begins to emerge when you start applying some styling to the tabs. For example, take a look at the following code, which is used for styling the tabs in Figure 3.
Furthermore, by merely changing the transform-origin to bottom left or bottom right, we can get left- or right-slanted tabs, respectively!
For an example, see Figure 3. A must-read for web designers and developers By Adrian H. Absolutely fantastic. A must-read for web designers and front-end developers. It's packed with actionable ideas and techniques, many of which I'd never seen before.
Context: I've been a web developer since ish, co-created the Django web framework and have been wrestling with CSS for 15 years or so.
Each chapter presents a specific web design "problem," along with a CSS solution and sometimes multiple solutions.
Some are simple, others are quite complex. Many are quite clever and will make you think of CSS differently. It's well-written and beautifully designed, with full-color pages and tons of informative footnotes. The best part is that Lea talks through her thought process in developing each technique, "teaching a man to fish.
These provided information was really so nice,thanks for giving that post and the more skills to develop after refer that post. Your articles really impressed for me,because of all information so nice. School web design uk.
Rabu, 20 Oktober [M This is why, every time you have downtime, every single time you could take pleasure in reading by soft copy publication CSS Secrets: Better Solutions To Everyday Web Design Problems, By Lea Verou In this practical guide, CSS expert Lea Verou provides 47 undocumented techniques and tips to help intermediate-to advanced CSS developers devise elegant solutions to a wide range of everyday web design problems.
0コメント