With the latest release of CSS3 has come a lot of power. In this article we look at several quick styling methods you can use to create CSS3 effects. Before we begin, here is a cheat sheet which lists the various functions you can do with CSS3.
Generally, I recommend making use of programs that have built CSS3 into their interface, but for this article we will look at several CSS3 coding snippets which you can use with your layouts.
Prior to the latest CSS updates, if you wanted certain special effects you would have to create those in image manipulation or illustration programs and then bring the finished result into your layout.
CSS3 has made many of those obsolete and allows you to create special effects using code. Some examples where you can do this are with images, rounded corners, image layer effects, shadowed text, buttons, and gradients. We will look at a few of those here.
Images
With images there are several ways you can use them, by positioning, sizing, repeating, etc. Here is some code:
<style>
body
{
background:url(blue_square.gif);
background-repeat:no-repeat;
}
</style>
Note that in this code, there’s no mention of the image size, so it will appear as is when rendered in your browser.
If you want to change that, you can do so by pixel size or by percentage:
background-size:120px 50px;
or
background-size:85% 55%;
One of the things you’ll notice in the above code is the no-repeat property, which will bring up only one image in your browser. If you want the image to tile across the screen the code would read something like this:
<style>
body
{
background: url(red_circle.gif);
background-repeat: repeat-x;
}
</style>
And here is the result.
If you want several individual images to appear, one after the other, the code would look like this:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title> New Document </title>
</head>
<body>
<style>
.three_horiz {
display: block;
min-width: 350px;
padding: 0;
}
.three_horiz img {
float: left;
margin: 0;
padding: 0;
}
</style>
<div class=”three_horiz“>
<img src=”blue_square.gif” />
<img src=”red_circle.gif” />
<img src=”yellow_star.gif” />
</div>
</body>
</html>
Here is the result.
Now we will go one step further and create a layered image with the above shapes. In this case, we set a number to each element (z-index). The way this works is that elements with higher numbers will overlap elements with a lower number. Here is the code:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
The last effect we will do is to add transparency to the images with the opacity element. In this case I have applied opacity to the red circle and the green start. The blue square is a solid color. Here is the code:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
Formerly an effect you would need to create in program like Photoshop, rounded corners can be quickly created using CSS3 only. Here is a simple example:
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Untitled Document</title>
<style>
div
{
border:2px dashed blue;
padding:20px 20px;
background:#eeeeee;
width:200px;
border-radius:10px;
}
</style>
<body>
<div>Here is a simple example of the border-radius property in action.</div>
</body>
</html>
This is the result. Of course, you can do much more than this, including changing the color of each side of the box individually, changing the radius for each corner or adding a drop shadow to the entire box. Here is the code to add a color to each side of the box:
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Untitled Document</title>
<style>
div
{
border:2px solid blue;
padding:20px 20px;
background:#eeeeee;
width:200px;
border-radius:25px;
border-top-color:#FF0000;
border-bottom-color:#00FF00;
border-left-color:#00FFFF;
border-right-color:#FF00FF;
}
</style>
<body>
<div>Here is a simple example of the border-radius property in action.</div>
</body>
</html>
And the result.
Here’s the code for the colored sides and for a different radius for each corner:
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Untitled Document</title>
<style>
div
{
border:2px solid blue;
padding:20px 20px;
background:#eeeeee;
width:200px;
border-top-color:#FF0000;
border-bottom-color:#00FF00;
border-left-color:#00FFFF;
border-right-color:#FF00FF;
border-top-left-radius:10px;
border-bottom-left-radius:20px;
border-top-right-radius:30px;
border-bottom-right-radius:10px;
}
</style>
<body>
<div>Here is a simple example of the border-radius property in action.</div>
</body>
</html>
And the final image.
Now, here is the code for the above image using the box-shadow property.
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Untitled Document</title>
<style>
div
{
border:2px solid blue;
padding:20px 20px;
background:#eeeeee;
width:200px;
border-top-color:#FF0000;
border-bottom-color:#00FF00;
border-left-color:#00FFFF;
border-right-color:#FF00FF;
border-top-left-radius:10px;
border-bottom-left-radius:20px;
border-top-right-radius:30px;
border-bottom-right-radius:10px;
box-shadow: 15px 15px 8px #333333;
}
</style>
<body>
<div>Here is a simple example of the border-radius property in action.</div>
</body>
</html>
And the final result.
Conclusion
As you can see, CSS3 offers many different styling options. In the next article we will look at buttons, gradients and a few software programs to make your life easier and speed up your workflow.
Are you a game developer looking to jump into the futuristic world of virtual reality? If so, you will likely need to use VR game development platforms and tools to help you along the way, and here is a list of some of the best of the bunch to get you started. After reading this […]
While it is possible to create websites in the modern era using content management systems (CMS) like WordPress and website builder platforms like Shopify, it never hurts to learn the cornerstones of those web development tools, especially if you ever want to add your own custom functionality to your websites. What’s more, not every […]
The images in a web page make the page much more attractive, descriptive, and help improve user experience. In this web development tutorial, we will talk about how to work with images in HTML, and, specifically, background images, inline images, image insertion paths, and the most important attributes of the HTML tag. We will […]
Since the Internet first broke on to the scene in the late 80s and 90s, it has undergone a couple of significant changes, encapsulated by Web 1.0 and 2.0. Now, we are entering into the third version of the Web known as Semantic Web or Web 3.0. In this web development tutorial, we will explore […]
The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.
Advertiser Disclosure: Some of the products that appear on
this site are from companies from which TechnologyAdvice
receives compensation. This compensation may impact how and
where products appear on this site including, for example,
the order in which they appear. TechnologyAdvice does not
include all companies or all types of products available in
the marketplace.