Even though CSS3 is not supported on all browsers yet, many web developer have started to use some of the techniques that it provides. CSS3 has evolved into a technology which, when combined with HTML5 and JavaScript, may end up being a Flash-killer. In this series of articles, we will cover the key additions to CSS3. In the previous article, we learned about border properties with CSS3; and today we will cover the text styling properties that it has to offer.
Advertisement
New Selectors in CSS3
Among other features, CSS3 has also introduced a few new selectors. While many developers overlook selectors and use classes instead; those who understand the importance of selectors really appreciate it.
Selectors allow you to drastically reduce the server-side or Javascript code that is used to style content on the page. Read further to see how.
:nth-child() and :nth-last-child()
This is definitely my favorite selector in CSS. It is based on the :fist-child and :last-child selectors introduced in an earlier version of CSS. This allows you to target a particular child of an element in the page. Moreover, you can pass it ‘odd’ or ‘even’ too. The most evident of examples of this is to style alternate rows of a table differently. So far we needed to use either server-side or javascript code to write the logic for this, but now CSS has become smart enough to understand it.
Here is an example of alternate row styling with CSS3.
You think this is cool? Then what about the fact that you can pass it any formula of the form ‘an+b’ where a and b are integers? Imagine a grid of images where you need to define the right margin of the column to 0? No need to add a special class any more.
:nth-last-child() is similar to nth-child() only it starts counting from the bottom.
Advertisement
Substring Matching Attribute Selectors
The ability to target elements by type, ID and class names wasn’t enough for some people, so they decided to allow targeting elements based on a substring contained in an attribute. Three symbols have been picked from regular expressions to specify the position of the substring.
^ – denoting the beginning of the attribute value.
$ – denoting the end of the attribute value.
* – denoting anywhere in the attribute value.
Here is an example of substring matching attribute selectors.
Old-school typography sometimes liked to style the first letter and first line of chapters differently from the rest of the text. In want to support that in modern day digital typography, CSS3 allows you to target the first letter and first line of an element.
Here is an example of first letter and first line styling.
These are some of the interesting and more widely supported selectors introduced in CSS3. Most if not all of these are supported in Mozilla and Web-kit browsers, however, Opera and Internet Explorer seems to be taking their sweet time to support some of these features. So until they don’t support them, we will have to also use fallback methods so that we can offer the same experience to all users.
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.