Thursday, March 6, 2014

CSS Text and Font

    In this post today, we will take a look at some of the most interesting CSS text and font properties listed below.
  • text-transform
  • white-space
  • word-break
  • word-spacing
  • word-wrap
  • font-variant

Text-transform property:

    Let's begin with the text-transform property. The filler text used in all the examples below has been taken from Andy Matthew's filler text generator which is a humorous replacement to the traditional boring lorem ipsum.

    The text-transform property will turn your text to uppercase, lowercase and also will capitalize the first character of every word. So now you don't need any javascript to do your bidding. This transformation will be done irrespective of the special characters preceding and following the text characters. The following example will make things much more clear and editable of course =)

White-space property:

    The next property to explore is the white-space property. The white-space CSS property is more of a replacement to the "pre" tag in HTML with some added advantages as well. The various values of the white-space property like the nowrap, pre, pre-line and pre-wrap will handle the white spaces and new lines differently. The following example will clearly define the value that this property gets to the table.

    As you can see in the example above, "nowrap" will not allow text or other inline elements to break naturally to the next line, but instead will push the boundaries of its parent until the line is completed, putting everything on a single line.

    The value pre works exactly as you would expect: The same as content wrapped inside the pre tags. All spaces and line breaks are honored, creating output in the browser that is virtually identical to what is in the markup. Also just like the pre tag, if everything in the markup is on a single line, the line will push the boundaries of its parent to keep everything in the output on a single line.

    The value pre-line will work exactly as normal with just one change. Line breaks in the HTML will be honored. So, if you have multiple spaces between elements, those will be ignored as usual, but if in your markup you drop to a new line, the new line will also occur in the output.

    The last value pre-wrap will work exactly as pre except that it will naturally wrap lines according to the boundaries of its parent. So it will not force everything on a single line to imitate what's in the markuo, but it will honor multiple spaces and explicit line breaks.

Word-break property:

    Let's explore the word-break property now. This property has 1 main value that we will discuss - the break-all value. Let us take a look at the example below.

    In the example above, I have put a border and width on the paragraph to demonstrate the usage of the word-break property. As you will notice in the first para, line breaks in text can only occur in certain spaces, like when there is literally a space or a hyphen. However, when you set the word-break property to break-all line breaks will occur between any character, as seen in the second para in the example above.

Word-spacing property:

    The word-spacing property helps to add space between words in a paragraph. The syntax is pretty simple and is as follows.

word-spacing: 10px;

Word-wrap property:

    The word-wrap property has a very useful value break-word. This value forces long words or sentences with no logical space or hyphen to wrap to the next line. The following example will make this much more clear.

    Note: If you have followed the post right upto this point, then you must be wondering that word-wrap: break-word and word-break: break-all are doing the exactly same thing. Aren't they? Well, these 2 things are slightly different and you can check the details on this post here.

Font-variant property:

    In font-variant there is one important value small-caps. In a small-caps font all the lowercase characters are converted to uppercase characters. However the uppercase characters are a font size smaller than the original uppercase character in the text. The following example will make things much more clear.

    This concludes our article today. Do let me know your thoughts on it. Do drop a line if this has helped you or even if you have found a mistake or would like to suggest something to better this post. Till the next post, happy coding!

If this post has helped you, leave a comment or show your love by liking the Spatial Unlimited Facebook page. You could even consider buying me a coffe! Till next time; happy coding!