# 10 CSS3 Properties You Need to Be Familiar With

This post is part of a series called CSS3 Mastery.
The 30 CSS Selectors You Must Memorize

We've already covered the 30 CSS selectors that we should all memorize, but what about the new CSS3 properties? Though most of them still require a vendor-specific prefix, you can still use them in your projects today. In fact, it's encouraged!

At the conclusion of this article, we'll work on a fun final project.

## 1.border-radius

Easily the most popular CSS3 property in the bunch, border-radius was sort of the flagship CSS3 property. While many designers were still terrified of the idea that a layout could be presented differently from browser to browser, a baby step like rounded corners was an easy way to lure them in!

The irony is that we're all perfectly fine with the idea of providing an alternate viewing experience for mobile browsers. Strangely, however, some don't feel the same way when it comes to desktop browsing.

 1 border-radius: 4px; 

### Circles

Some readers may not be aware that we can also create circles with this property. All you have to do is set the radius to half the width or height of the element.

 1 border-radius: 50px; 

And, if we want to have some fun, we can also take advantage of the Flexible Box Model (detailed in #8) to both vertically and horizontally center the text within the circle. It requires a bit of code, but only because of the need to compensate for various vendors.

 1 display: flex;  2 align-items: center;  3 justify-content: center; 

## 2.box-shadow

Next, we have the ubiquitous box-shadow, which allows you to immediately apply depth to your elements. Just don't be too obnoxious with the values you set!

 1 box-shadow: 1px 1px 3px #292929; 

box-shadow accepts four parameters:

• x offset
• y offset
• blur

Now, what many don't realize is that you can apply multiple box-shadows at a time. This can lead to some really interesting effects. For example, we can use a blue and green shadow to magnify each shadow.

 1 box-shadow: 1px 1px 3px green, -1px -1px 3px blue; 

By applying shadows to the ::before and ::after pseudo-classes, we can create some really interesting perspectives. Here's one to get you started:

#### The HTML

 1 
 2   3 


#### The CSS

 1 .box:after {  2  content: "";  3  position: absolute;  4  z-index: -1; /* hide shadow behind image */  5  box-shadow: 0 15px 20px rgba(0, 0, 0, 0.3);  6  width: 70%;  7  left: 15%; /* one half of the remaining 30% (see width above) */  8  height: 100px;  9  bottom: 0;  10 } 

## 3. text-shadow

Similarly to box-shadow, it must be applied to text, and it receives the same four parameters:

• x-offset
• y-offset
• blur
 1  h1 {  2  text-shadow: 0 1px 0 white;  3  color: #292929;  4  } 

### Text Outlines

Again, much like its sibling, box-shadow, we can apply multiple shadows, by using a comma as the separator. For example, let's say that we want to create an outline effect for the text. While webkit does offer a stroke effect, we can reach more browsers by using the following method (though not quite as pretty):

 1 body { background: white; }  2 3 h1 {  4  text-shadow: 0 1px 0 black, 0 -1px 0 black, 1px 0 0 black, -1px 0 0 black;  5  color: white;  6 } 

## 4. text-stroke

Be careful with this method. It is a non-standard feature. The text-stroke property isn't yet part of the CSS3 spec. However, it is now supported by all major browsers if you use the -webkit- prefix.

 1 h1 {  2  -webkit-text-stroke: 3px black;  3  color: white;  4 } 

### Feature Detection

How can we provide one set of styling for, say, Firefox, and another set for Safari or Chrome? One solution is to use feature detection.

With feature detection, we can use JavaScript to test if a certain property is available. If it's not, we prepare a fallback.

Let's refer back to the text-stroke issue. Let's set a fallback color of black for browsers which don't support this property (all but webkit currently).

 1 var h1 = document.createElement('h1');  2 if ( !( 'webkitTextStroke' in h1.style ) ) {  3  var heading = document.getElementsByTagName('h1')[0];  4  heading.style.color = 'black';  5 } 

First, we create a dummy h1 element. Then, we perform a full cavity search to determine if the -webkit-text-stroke property is available to that element, via the style attribute. If it's not, we'll grab the Hello Readers heading, and set its color from white to black.

Please note that we're being generic here. If you need to replace multiple h1 tags on the page, you'll need to use a while statement to filter through each heading, and update the styling or class name, accordingly.

We're also testing only for webkit, when it's possible that other browsers will eventually support the text-stroke property as well. Keep that in mind.

If you want a more comprehensive feature-detection solution, refer to Modernizr.

## 5. Multiple Backgrounds

The background property has been overhauled to allow for multiple backgrounds in CSS3.

Let's create a silly example, simply as a proof of concept. For lack of any suitable images nearby, I'll use two tutorial images as our backgrounds. Of course, in a real-world application, you might use a texture and, perhaps, a gradient for your backgrounds.

 1 .box {  2 background: url(image/path.jpg) 0 0 no-repeat,  3  url(image2/path.jpg) 100% 0 no-repeat;  4 } 

Above, by using a comma as the separator, we're referencing two separate background images. Notice how, in the first case, it's placed in the top left position (0 0), and, in the second, the top right position (100% 0).

Make sure that you provide a fallback for the browsers which don't provide support for multiple backgrounds. They'll skip over the property entirely, leaving your background blank.

### Compensating for Older Browsers

To add a single background image for older browsers, like IE7, declare the background property twice: first for old browsers, and the second as an override. Alternatively, you could, again, use Modernizr.

 1 .box {  2 /* fallback */  3 background: url(image/path.jpg) no-repeat;  4 5 /* modern browsers */  6 background: url(image/path.jpg) 0 0 no-repeat,  7  url(image2/path.jpg) 100% 0 no-repeat;  8 } 

## 6. background-size

Before modern CSS, we were forced to use sneaky techniques to allow for resizable background images.

 1 background: url(path/to/image.jpg) no-repeat;  2 background-size: 100% 100%; 

The code above will direct the background image to take up all available space. As an example, what if we wanted a particular image to take up the entire background of the body element, regardless of the browser window's width?

 1 body, html { height: 100%; }  2 3 body {  4  background: url(path/to/image.jpg) no-repeat;  5  background-size: 100% 100%;  6 } 

That's all there is to it. The background-size property will accept two parameters: the x and y widths, respectively.

While the latest versions of Chrome and Safari support background-size natively, we still need to use vendor-prefixes for older browsers.

 1 body {  2  background: url(path/to/image.jpg) no-repeat;  3  background-size: 100% 100%;  4 } 

## 7. text-overflow

Originally developed for Internet Explorer, the text-overflow property can accept two values:

• clip
• ellipsis

This property can be used to cut off text that exceeds its container, while still providing a bit of feedback for the user, like an ellipsis.

Did You Know? Internet Explorer has provided support for this property since IE6? They created it!

 1 .box {  2  text-overflow:ellipsis;  3 4  overflow:hidden;  5  white-space:nowrap;  6 7  border: 1px solid black;  8  width: 400px;  9  padding: 20px;  10 11  cursor: pointer;  12 } 

At this point, you might consider showing the the entirety of the text when a user hovers over the box.

 1 #box:hover {  2  white-space: normal;  3  color: rgba(0,0,0,1);  4  background: #e3e3e3;  5  border: 1px solid #666;  6 } 

A bit oddly (unless I'm mistaken), there doesn't seem to be a way to reset the text-overflow property, or "turn it off." To mimic this "off" functionality, on :hover, we can set the white-space property back to normal. This works because text-overflow is dependent upon this to function correctly.

Did You Know? You can also specify your own string, that should be used in place of the ellipsis. Doing so will render the string to represent the clipped text.

## 8. Flexible Box Model

The Flexible Box Model will finally allow us to get away from those mangy floats. Though it takes a bit of work to wrap your head around the new properties, once you do, it all should make perfect sense.

Let's construct a quick demo, and create a simple two-column layout.

 1 
 2 3 
 4   5 6 


Now for the CSS: we first need to instruct the container to be treated as a flexible box. I'll also apply a generic width and height, since we don't have any actual content in play.

 1  #container {  2  width: 600px;  3  height: 450px; /* just for demo */  4 5  background: #e3e3e3;  6  margin: auto;  7 8  display: flex;  9  } 

Next, let's apply, for the demo, unique background colors to the #main div, and the aside.

 1 #main {  2  background: yellow;  3 }  4 5 aside {  6  background: red;  7 } 

At this point, there's not much to look at.

One thing worth noting, though, is that, when set to display: flex mode, the child elements will take up all vertical space; this is the default align-items value: stretch.

Watch what happens when we explicitly state a width on the #main content area.

 1 #main {  2  background: yellow;  3  width: 400px;  4 } 

Well that's a bit better, but we still have this issue where the aside isn't taking up all of the remaining space. We can fix this by using the new box-flex property.

flex instructs the element to take up all available space.

 1  aside {  2  display: block; /* cause is HTML5 element */  3  background: red;  4 5  /* take up all available space */  6  flex: 1;  7  } 

With this property in place, regardless of the width of the #main content area, the aside will consume every spec of available space. Even better, you don't have to worry about those pesky float issues, like elements dropping below the main content area.

We've only scratched the surface here. To learn more about Flexbox, check out our complete guides! This feature is now supported by all major browsers, and you can expect it to work properly on over 99% of the devices.

## 9. resize

The resize property—part of the CSS3 UI module—allows you to specify how a textarea is resized. It is now supported by all major browsers except IE and iOS Safari.

 1  

Note that, by default, webkit browsers and Firefox 4 allow for textareas to be resized, both vertically and horizontally.

 1 textarea {  2  resize: vertical;  3 } 

### Possible Values

• both: Resize vertically and horizontally
• horizontal: Limit resizing to horizontally
• vertical: Limit resizing to vertically
• none: Disable resizing

## 10. Transitions

Perhaps the most exciting addition to CSS3 is the ability to apply animations to elements, without the use of JavaScript.

Let's mimic the common effect where, once you hover a link in a sidebar, the text will slide to the right ever so slightly.

#### The HTML

 1 
 2 
•  3  Hover Over Me  4 
•  5 
•  6  Hover Over Me  7 
•  8 
•  9  Hover Over Me  10 
•  11 
•  12  Hover Over Me  13 
•  14 


#### The CSS

 1  ul a {  2  border-left: 10px orange solid;  3  transition: border-width 0.4s;  4  }  5 6 a:hover {  7  border-width: 20px;  8 } 

transition will accept three parameters:

• The property to transition. (Set this value to all if needed)
• The duration
• The easing type

The reason why we don't directly apply the transition to the hover state of the anchor tag is because, if we did, the animation would only take effect during mouseover. On mouseout, the element would immediately return to its initial state.

Because we've only enhanced the effect, we've done absolutely no harm to older browsers.

## Final Project

Let's combine the bulk of the techniques we've learned in this article and create a neat effect for displaying flip cards.

### Step 1. The Markup

We'll keep it simple; within our .box container, we'll add two divs: one for the front side and the other for the back.

 1   2 3 
 4 
Hello
 5 
World
 6 
 7 8  

### Step 2. Horizontally and Vertically Centered

Next, I want our card to be perfectly centered on the screen. To do so, we'll take advantage of the Flexible Box Model.

As our page will only contain this card, we can effectively use the body element as our wrapper.

 1 body, html { height: 100%; width: 100%; }  2 3 body {  4  display: flex;  5  align-items: center;  6  justify-content: center;  7 } 

### Step 3. Styling the Box

We'll now style our "card."

 1 .box {  2  background: #e3e3e3;  3  border: 1px dashed #666;  4  margin: auto;  5  width: 400px;  6  height: 200px;  7  cursor: pointer;  8  position: relative;  9  transition: all 1s;  10 } 

Note that we've also instructed this element to listen for any changes to the state of the element. When they occur, we'll transition the changes (if possible) over the course of one second (transition: all 1s).

### Step 4. An Effective Shadow

Next, as we learned earlier this article, we'll apply a cool shadow by using the ::after pseudo class.

 1 .box::after {  2  content: "";  3  position: absolute;  4  width: 70%;  5  height: 10px;  6  bottom: 0;  7  left: 15%;  8  z-index: -1;  9  box-shadow: 0 9px 20px rgba(0, 0, 0, 0.4);  10 } 

### Step 5. Styling the Child divs

At the moment, the child divs are still right on top of each other. Let's position them absolutely, and instruct them to take up all available space.

 1 .box > div {  2  position: absolute;  3  width: 100%;  4  height: 100%;  5  top: 0;  6  left: 0;  7  background: #e3e3e3;  8  transition: all 0.5s ease-in-out;  9 10  font: 45px/200px bold helvetica, arial, sans-serif;  11  text-align: center;  12  text-shadow: 0 1px 0 white;  13 } 

### Step 6. Fixing the Front

Refer to the image above; notice how the back side of our card is displaying by default? This is because, due to the fact that the element occurs lower in the markup, it, as a result, receives a higher z-index. Let's fix that.

 1 /* Make sure we see the front side first */  2 .box > div:first-child {  3  position: relative;  4  z-index: 2;  5 } 

### Step 7. Rotating the Card

Now for the fun part; when we hover over the card, it should flip around, and display the back side of the card. To achieve this effect, we use transformations and the rotateY function.

 1 .box:hover {  2  transform: rotateY(180deg);  3 } 

### Step 8. Mirrored Text

Doesn't that look awesome? But, now, the text appears to be mirrored. This, of course, is because we transformed the container. Let's offset this by rotating the child div 180 degrees as well.

 1 .box:hover > div:first-child {  2  opacity: 0;  3 }  4 5 .box:hover div:last-child {  6  transform: rotateY(180deg);  7 } 

And with that last bit of code, we've achieved our neat effect!

## Conclusion

Thank you so much for reading, and I hoped you learned a lot!