CSS Font-WeightIntroductionCSS font weight is a property used in web development to control the thickness and boldness of text within a webpage. It is used to define the weight of the text. The available weight depends on the font family used by the browser. It plays an important role in typography because it gives designers and programmers more control over how text visually appears and emphasizes text elements. Purpose of Font-Weight PropertyThe font-weight property's primary purpose is to set the font's weight, which affects how thick or thin the characters appear on the screen or in print. It is a flexible tool for text formatting because it provides a range of values to manage the boldness level. Depending on the web browser's font family, several font-weight values can be accessible. Different font families have predefined font weights of their own, and although some may offer a greater range of alternatives, others may offer a few options. NOTE: Together with other CSS properties like font size, font family, and font style, the font-weight property is frequently used to provide consistent text design throughout an entire website.Syntax and Property ValuesThe CSS font-weight property contains various property values for controlling the weight and boldness of text and adheres to a specified syntax. Syntax: Property Values:
How to Use Font Weight in CSS1. Applying font weight to Elements:Specific HTML elements can have the font-weight attribute applied in CSS to modify the text's weight and boldness. Doing so allows you to create various text styles and highlight particular passages of your information. Example 1: Font weight for Paragraphs In the example above, the default font weight, which is normal (often 400), will be used for all paragraphs (<p>) within the HTML content. Example 2: Making Headings Bold This CSS rule sets the <h1>, <h2>, and <h3> elements' font-weight bold (often 700), causing these headings to be displayed boldly. Example 3: Custom Font Weight for Specific Class In this case, the font weight will be 600, providing a medium weight text style. Example 4: font-weight with Other Properties The font weight, font size, and font-family properties are used in this example to style the h4> headers. For the chosen elements, this develops a unified and consistent typographic style. 2. Setting Global Font Weight:When using CSS, you can target the body selector to apply a global font weight to the entire document or just a portion. All web page text can have a uniform font weight by applying the font-weight property to the body element. This strategy can be helpful when you want to keep your website's visual design consistent throughout. Example 1: This example applies the font-weight attribute to the body selector with a value of 400, which stands for the standard (normal) font weight. This will guarantee that the font weight of every text element inside the <body> tag is consistent and proper. Example 2: Combining Global Font Weight with Other Selectors: This example uses a global font weight for the entire document while applying various font weights to specific HTML components. The rest of the content will use the global font weight (400), with the headers (<h1>) having a bold font weight of 700 and the paragraphs (<p>) having a softer font-weight of 300. Therefore, you can manage the overall appearance of text on your website by utilizing the body selector to establish a universal font weight. 3. Combining with Other Properties:CSS's font-weight property can be used with other text-related properties to produce comprehensive and aesthetically pleasing typography. Example 1: Combining with font-family Here, all paragraphs (<p>) are given a custom font weight of 300 (a lighter style). Furthermore, we designate a specific font family for paragraphs, which will impact the font weight and overall look of the text. Example 2: Combining with font-size This illustration combines the font weight and size for <h2> headings. The font size is 28 pixels, with a 600 (medium-bold style) font weight. You may produce visually striking and captivating headlines with this combo. Example 3: Combining with line-height For blockquotes in this illustration, the font-weight property is set to bold. In addition, the line height property is changed to 1.5 times the font size. Example 4: Combining with text-decoration For anchor links (<a>), we have specified a custom font weight of 500. The anchor links are also underlined using the text-decoration attribute, helping them stand out and be easier to recognize. Best Practices for Using Font-Weight in CSSAccessibility Considerations:It is crucial to take accessibility into account when using the font-weight property in CSS to make sure that your text is accessible and useable for all users, including those who have visual impairments. Here are some key considerations:
Examples Let's see an example of CSS font weight Using property values:
Next TopicCSS font-stretch
|