Skip to main content

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!

Comments

Recommended for You

Where does Google get it's live traffic data from?

Referring to a post that I wrote earlier, Google’s - Live traffic Layer , ever wondered how Google collected this data? I was wondering the other day, how Google received live data to display it on their maps as a layer! I looked up the web and found something very interesting and am sharing the same with you all. As we all know, the traffic layer is available most accurately in several states in USA. Most major metro areas in the US have sensors embedded in their highways. These sensors track real time traffic data. Easy to miss at high speeds (hopefully anyway, traffic permitting), more commonly noticed may be the similar sensors that often exist at many busy intersections that help the traffic lights most efficiently let the most amount of people through. The information from these tracking sensors is reported back to the Department of Transportation (DOT). The DOT uses this data to update some of the digital signs that report traffic conditions in many metro areas. They als

Playing with the markers and info window bubbles...

    In the last few posts, we have seen some marker examples and some information window examples. Now, lets do something interesting combining these two things. Just writing that "This is an info window" in the information bubble is not very interesting! And I know this...Have gone through the same phase!     So, today we will do something interesting! We will display the latitude- longitude co-ordinates of the point that the user clicks on the map! Doing this is not at all complex! Copy paste the following code and you will see for yourself a map coming to life!     The output of the above code looks as seen in the result section above! If you have any queries regarding the above code please comment on the blog post or feel free to contact me at my mail ID .

The bitter divorce of PSD and HTML

    Today's article is an interesting post that I read. The original post in Portuguese and authored by Fabricio Teixeira  can be found at arquiteturadeinformacao  (Now don't ask me pronounce this =)).     Some are calling it the death of PSD  but I prefer calling it a "divorce". PSD and HTML are both healthy and living strong, just that they do not live together anymore. "PSD to HTML", which for years was the most accurate and sometimes the only right path to web design process, seems like has its days counted.     Firstly you draw a page in Photoshop; impeccable layout, representing exactly how the web pages would appear when opened in a browser. After a sign-off on this picture (PSD) from the client the front end developer transforms these pictures into HTML, CSS and Javascript. The assets are cut, one by one, exported from the PSD and integrated into the HTML. Plugins and new tools are created in the process and some companies even charge upto $1

Ground Truth - How Google Builds Maps

    Todays's article is cross posted from The Atlantic 's Tech section. The article was posted by Alexis Madrigal who is a senior editor at The Atlantic , where he oversees the Technology channel. So, thanks to The Atlantic and Alexis Madrigal, we will have an exclusive look inside Ground Truth , the secretive program to build the world's best accurate maps.     Behind every Google Map, there is a much more complex map that's the key to your queries but hidden from your view. The deep map contains the logic of places: their no-left-turns and freeway on-ramps, speed limits and traffic conditions. This is the data that you're drawing from when you ask Google to navigate you from point A to point B -- and last week, Google showed me the internal map and demonstrated how it was built. It's the first time the company has let anyone watch how the project it calls GT, or "Ground Truth," actually works.     Google opened up at a key moment in its evo

Geodesic Polyline

    Today we will have a look at a very interesting polyline example - "The geodesic polyline". Now the first question that will pop is "What is geodesic?". Mathematically, geodesic means the shortest line between two points on a mathematically defined surface, as a straight line on a plain or an arc of a great circle or sphere.     The next question after reading the above definition is clearly, "Why do we need geodesic polylines?" and that would be followed up with "What is this Great Circle?". We will discuss this first, before we move on to the actual example today. The example is very very similar to the normal polyline example, with just a small change.     Having said so, I will now try to explain why we need a geodesic polyline? The shortest distance between two locations on the earth is rarely a straight line as the earth is roughly spherical in nature. So any two points on the earth, even if they are very close lie on a curve and n