Handling spaces between letters and words
1. Spacing between words word-spacing
Sets the spacing between words. You can use positive and negative values. If the meaning is negative, the words can overlap each other.
The value of word-spacing is affected by the value of the text-align property in the case of justified text. Inherited.
Syntax
P (word-spacing: normal;) p (word-spacing: 2px;)
Rice. 1. Spacing between words
2. Letter-spacing
The property sets the distance between letters (tracking amount) and symbols. It can take positive and negative values. It is advisable to use to increase the expressiveness and readability of headings, definitions, etc. Inherited.
Syntax
P (letter-spacing: normal;) p (letter-spacing: 2px;)
Rice. 2. Change the spacing between letters using the letter-spacing property
3. White-space handling
The property handles spaces between words and line breaks within an element. Not inherited.
white space | |
---|---|
Values: | |
normal | Default value. Only one space is inserted between words, additional spaces are discarded. Text wraps only when needed. |
nowrap | Disables line breaks except when applied . |
pre | Spaces in text are not ignored, the browser displays extra spaces and line breaks. |
pre-wrap | Preserves whitespace in text by making line breaks where necessary. |
pre-line | Removes extra spaces except when . |
initial | Sets the value of a property to its default value. |
inherit | Inherits the property value from the parent element. |
Syntax
P (white-space: normal;) p (white-space: nowrap;) p (white-space: pre;) p (white-space: pre-wrap;) p (white-space: pre-line;)
4. Customize tabs tab-size
To change the amount of indentation obtained with the TAB key, use the tab-size property. Property values are ignored when one of the three pre-line , normal , or nowrap white-space property values is set.
Only works for elements
Syntax
Pre (-moz-tab-size: 16;) /* Firefox */ pre (-o-tab-size: 16;) /* Opera 10.6-12.1 */ pre (tab-size: 16;)
It's very easy to set line spacing in CSS. There is a special property for this. But, of course, there are many other options that are universal and can be applied to text.
If no settings are made, the default values are set. You can change this distance if you wish. The value can be either in percentage or in pixels.
Line height
In CSS, line spacing can be shown with the following figure.
The image above shows the parameters with the corresponding distances. The text is in font-size space. Note that the line of text does not start at the base, but slightly higher. The space below is provided for letters that have elements below (g, y, and so on).
Note that the space between the font-size blocks is called leading. In HTML and CSS, this property does not appear in any way, but it is in other graphic and text editors. For example, in Adobe Photoshop.
The figure above shows where in Photoshop you can specify leading. And next to it is the font-size parameter.
Examples of using line-height
In CSS, line spacing can be set to percentages. An illustrative example is shown below.
In the case of a small value, it will be inconvenient for the user of your site to read.
The distance can be changed and the font size. If the difference between the main parameters differs greatly in numbers, then this difference is compensated by an increase in leading.
Design subtleties
In CSS, the spacing between lines can be further adjusted with various indents. Consider an example in the figure.
In the "Element" field in our case there will be text. Padding is the padding inside the object and margin is the padding behind the object. Border is a frame. It can be 0 pixels or it can be 100.
The following image shows all the padding, border, and line-height of text at once.
If your text is small, just one line, or each line is in a separate paragraph, then the distance can be adjusted by indenting between these paragraphs. That is, maring and padding between lines in the same element have no effect. They only create padding around the edges of the object. The object is the entire paragraph, not the lines in it. It is important not to get confused here.
In cases where there are many lines, and all this is located in one object, it is recommended to change the font with the main parameters.
How to increase spacing between CSS lines
The distance between HTML lines can be assigned to some class or for all paragraphs in the text. If you specify like this: p ( line-height:20px; ), then absolutely all paragraphs on the page will be with lines of 20 pixels in size. If you need different sizes in different places, it is recommended to do as follows.
We write styles.
Class1 ( line-height:20px; )
Class2 ( line-height:16px; )
Class3 ( line-height:12px; )
For clarity, let's add a frame so that you can see that this works. In the future, it must be removed.
Please note that in the third case, the strip ran over the text. All due to the fact that it is greater than the line height. Therefore, it is important to ensure that there are no such contradictions. If you make the line height small, reduce the font accordingly.
It is not recommended to make text too small and small spacing between lines. Since no user can safely read all this. His eyes will quickly get tired. Search engines also say that the text should be user friendly.
Moreover, there has recently been a great emphasis on convenience for mobile users. There, the guidelines always say that the font size should be normal, not small. This is especially true for links. With their small size, it will be difficult for the user to use site navigation.
The Google search engine has a special tool that helps in this analysis. It is very handy for webmasters.
Here is an example of the results that might be.
CSS is very necessary for text styling. Any specialist always uses them if he creates a beautiful site. Thanks to these attributes, you can perform any operations with text.
As a rule, most layout designers do not like the default text layout in html. CSS line spacing allows you to customize just about anything imaginable. And if you go deeper, you can do something that you never even thought of.
How to make text line spacing in CSS
It is important to understand that you can set line sizes, indents, and so on for all elements where there is text. For example, all of these attributes are great for:
- paragraph
- lists;
- ;
- tables;
- headings
- and everything else where you place text.
When looking at CSS line spacing, the first things to consider are the following attributes.
In Photoshop, there is the concept of Leading. There is no such attribute in CSS, but thanks to it you can explain the essence. Leading in fact is the distance between the lines.
In CSS, the size of a line is given by the line-height and font-size parameters. As you can see, the figure clearly states that font-size is the size of the font. Not a single letter ever goes beyond these limits.
Line-height is the distance from the middle of the top Leading to the middle of the bottom Leading. All this is indicated by arrows in the figure.
Please note that if you specify a line height less than the font size, then the lines will run into each other. Be sure to keep an eye on this so that this does not happen.
Some people wonder why font-size is stretched lower than the line with letters. Everything is simple. After all, there are letters that are lower than the main level of the line. Below in the figure - a good example, where characters are used, stretched up and down.
As you can see, the letters just occupy the entire font-size range. And the line height is greater, and equal space is added on both sides.
The value of the line-height attribute can be anything:
- pixels;
- inches;
- points;
- interest;
- and others that are used in CSS.
You cannot specify a negative value. For example, if you specify a multiplier equal to one and a half, then you will get the standard one and a half spacing in the text.
If you specify in percentages, then keep in mind that 100% in line-height is equivalent to the font-size value.
In addition, you can specify the value normal or inherit. If you specify normal, then the distance between lines will be calculated automatically by the browser at its discretion. The value may vary depending on the browser. It is recommended to set a specific number so that all users have the same number.
Inherit means that the value will be exactly the same as the parent element.
More detailed design
CSS line spacing isn't really just limited to the above attributes. The distance is also adjusted by the margin and padding attributes. If you specify the attribute exactly as margin="5px", then a distance of 5 pixels will be added on all sides of the line (paragraph).
If you only need to specify above a paragraph, then margin-top can be specified. The translation is very simple. Thus, it will be possible to set indents on each side.
Note that there is also a padding attribute. It works exactly the same as margin. You can set both a general indent and a specific direction (left, right, up, down).
Look carefully at the picture above. Margin is the margin outside the object, and padding is inside.
These attributes are global. They can be applied to everything - pictures, tables, text, links, and so on.
Remember this once and for all. This is a very important point in the design of objects. For example, thanks to margin, you can specify the indents between text and pictures inside it. If these indents are not present, then the text will be placed close to the image.
If you have text in a table then use padding to keep the text from sticking to the edges of the table as it looks ugly. And if you set the indent to at least 5 pixels, it will already look much better. In addition to the height, the line has a width attribute.
Sometimes this is a very useful feature. The figure below shows an example of different text widths.
Design example
When creating a website design, you should always make sure that the text is as readable as possible. If it is difficult for the user to read the text (it is too small or too large), then he will simply close your site.
Try comparing the two paragraphs shown in the figure below. What text do you prefer to read?
CSS line spacing is designed to make text easier to read. Also, keep in mind that different fonts may differ in their initial height.
For clarity, you can check in any editor. Let's compare some fonts.
Please note that all fonts are written in 24 pixels. As you can see, they all differ in height and width of letters. If you compare larger sizes, the difference will be more obvious.
CSS: line spacing in a list
The spacing setting applies to lists as well. In addition, the styles of lists are characterized by the margin and padding described above. Thanks to this, the lists become more beautiful than the standard ones. Using all these attributes, you can set:
- The height of the list items.
- Indents for text on the left.
- Indents for lines above and below.
- The maximum width of each item.
Conclusion
Owning such a set of attributes, you can play with the design of not only lines, but everything you want. These are global parameters that absolutely all elements obey (the exception is font-size, since it is only for text).
When working with a style for text, you can set the required spacing between characters, words, and lines. Such distances are set in any CSS units, be it px , pt , em or something else. Percentages are an exception - they can be used to set the distance between lines (leading), but they do not work when setting the spacing between characters or words.
CSS character spacing: letter-spacing
You can set character spacing using the letter-spacing CSS property. In addition to the usual values (positive and negative), you can also use the values inherit (to inherit the value from the parent) and normal (if you want to return the normal spacing between characters).
An example of writing an intercharacter interval:
P ( letter-spacing: 2em; )
Spacing between words: word-spacing
The CSS word-spacing property differs from the previous one in that it sets the spacing between words, not between characters. This property also has normal and inherit values. You can set negative values. The following is an example of a style entry:
P ( word-spacing: 6px; )
Line spacing: line-height
The CSS line-height property can be used to set the spacing between lines of text. As it was said at the beginning of the topic, to set the leading, in addition to other units of measurement, it is allowed to use percentages. It is also allowed to write the value as a multiplier (numbers greater than 0): to calculate the distance, the browser will multiply the font size by the given number. Negative values don't work. The available values are normal and inherit .
Below is an example of how to make CSS line spacing:
P ( line-height: 180%; )
In the screenshot, you can see what the text looks like with all three properties:
Screenshot: CSS Spacing
Results
When setting the spacing between words, characters, or lines, make sure that the text is easy to read in the end. Such properties must be handled carefully and always used in moderation, without fanaticism, otherwise all textual content threatens to turn into an illegible set of letters.