Defining classes in css

Содержание
  1. HTML class Attribute
  2. Using The class Attribute
  3. Example
  4. London
  5. Paris
  6. Tokyo
  7. Example
  8. My Important Heading
  9. The Syntax For Class
  10. Example
  11. Multiple Classes
  12. Example
  13. London
  14. Different Elements Can Share Same Class Different HTML elements can point to the same class name. In the following example, both and point to the «city» class and will share the same style: Example Use of The class Attribute in JavaScript The class name can also be used by JavaScript to perform certain tasks for specific elements. JavaScript can access elements with a specific class name with the getElementsByClassName() method: Example Click on a button to hide all elements with the class name «city»: Don’t worry if you don’t understand the code in the example above. You will learn more about JavaScript in our HTML JavaScript chapter, or you can study our JavaScript Tutorial. Chapter Summary The HTML class attribute specifies one or more class names for an element Classes are used by CSS and JavaScript to select and access specific elements The class attribute can be used on any HTML element The class name is case sensitive Different HTML elements can point to the same class name JavaScript can access elements with a specific class name with the getElementsByClassName() method Источник Classes in CSS CSS classes are used to select elements from the webpage and add style to them. Classes are used to add style to multiple elements at once. The CSS classes allow us to define multiple CSS properties in a single block and when that class is added to any element then all the specified CSS property is reflected in the HTML element. Classes are defined either in the style tag or in the external stylesheet. Once a class is defined it can be used as many times as you want to use. Defining a class Classes are defined using a dot (.) followed by the class name. For example, if we want to define a class named myclass then we will write it as .myclass . CSS properties for that class will be written inside the curly braces. Note : The class name should not start with a number and should not contain spaces or special characters except the hyphen (-). Adding class to the element Once a class is defined, it can be added to any element using the class attribute in the HTML element. For example, if we want to add the class class-1 to the p element then we will write it as

    Paragraph with multiple class

    A working example for CSS class. Note : A class can be used multiple times in a single HTML document. And also an element can have multiple classes. Источник How To Create Classes With CSS In this tutorial, you will create a CSS class selector, which will allow you to apply CSS rules only to HTML elements that are assigned the class. CSS class selectors are useful when you want to apply different style rules for different instances of the same HTML element. Prerequisites To follow this tutorial, make sure you have set up the necessary files and folders as instructed in a previous tutorial in this series How To Set Up You CSS and HTML Practice Project. How CSS Class Selectors Work A CSS class selector allows you to assign style rules to HTML elements that you designate with that class rather than all instances of a certain element. Unlike HTML elements (such as , or ), whose names are predetermined, class names are chosen by the developer when they create the class. Class names are always preceded by a . , which can help you distinguish between tag selectors and class selectors in CSS files. A CSS rule for a class selector is written in the same way as a rule for a tag selector, with the exception of the . prepended to the class name: To use a class when adding HTML content to your webpage, you must specify it in the opening tag of an HTML element using the class attribute in your HTML document like so: h1 class=".red-text">Content.element> Creating a CSS Class Using a Class Selector Let’s begin exploring CSS classes in practice. Erase everything in your styles.css file and add the following code snippet to specify a rule for the class red-text : After adding the code snippet to your styles.css file, save the file. Return to your index.html and erase everything but the first line of code that links to your CSS stylesheet. Then add the following HTML code snippet: p class="red-text">Here is the first sample of paragraph text.p> Note that the class name is not prepended here with a . as it is when being used as a selector for a CSS rule. Your entire index.html file should have the following contents: . . . link rel="stylesheet" href="css/styles.css"> p> In this code snippet you have added text using the HTML tag. But you have also specified the red-text class by adding the highlighted class attribute class=»red-text» inside the opening HTML tag. Save your index.html file and load it in the browser. (For instructions on loading an HTML file, please visit our tutorial step How To View An Offline HTML File In Your Browser). You should receive a webpage with red text: Let’s add an additional CSS class to explore styling different pieces of text content with different classes. Add the following code snippet to your styles.css file (after your CSS rule for “red-text”): .yellow-background-text background-color: yellow; > This CSS rule declares that the class yellow-background-text is assigned the yellow value for the background-color property. Any HTML text element assigned this class will have a yellow background. Note that the use of the word text in the class yellow-background-*text* is for human readability purposes only. You do not need to include the word text in your class names for classes assigned to HTML text. To apply this new CSS class, return to your index.html file and add the following line of code to the bottom: p class="yellow-background-text"> Here is the second sample of paragraph text.p> In this code snippet, you have added some text content with the element and specified the yellow-background-text class. Save the file and reload it in your browser. You should have a webpage with two different sentences, the first one red and the second one with a yellow background: Note that you can add more than one class to an HTML tag. Try adding both classes to a single text element by adding the following line to your index.html file: p class="red-text yellow-background-text">Here is a third sample of text.p> Note that the class names are only separated by a space. Save the file and reload it in the browser. You should receive something like this: Your third line of text should now be styled according to the property values set in the red-text class and the yellow-background-text class and have a red font and yellow background. Adding CSS Classes to Images CSS classes can also be applied to other HTML elements, such as images. To explore using CSS classes for images, erase the content in your styles.css file and add the following code snippet: .black-img border: 5px dotted black; border-radius: 10%; > .yellow-img border: 25px solid yellow; border-radius: 50%; > .red-img border: 15px double red; > Here you have created CSS rules for three different classes that can be applied to the HTML tag. Before you move on, let’s briefly study what we’ve declared in each ruleset: The first CSS rule declares that the class black-img should have a black , dotted border five pixels wide and a border-radius sized at 10%, which gives the element rounded corners. The second CSS rule declares that the class yellow-img should have a yellow , solid border 25 pixels wide and a border-radius sized at 50%, which gives the element a circular shape. The third CSS rule declares that the class red-img should have a red , double border 15 pixels wide. You have not set a border-radius, so the border will conform to the element’s shape. Save the styles.css file. Then erase everything from your index.html file (except for the first line of code: ) and add the following code snippet: img src="https://css.sammy-codes.com/images/small-profile.jpeg" class="black-img"> img src="https://css.sammy-codes.com/images/small-profile.jpeg" class="yellow-img"> img src="https://css.sammy-codes.com/images/small-profile.jpeg" class="red-img"> Each of these three lines of HTML code add an image to the HTML document and assign it one of the three classes you just added to the styles.css file. Note that you are sourcing the image from an online location. You can also use your own image by specifying the file path as instructed in our tutorial How To Add Images To Your Webpage With HTML. Save your index.html file and load it in the browser. You should receive something like this: Your webpage should now display three images, each styled with the different specifications of their assigned class. To continue exploring CSS classes, trying creating new classes with different rulesets and applying them to different types of HTML content. Note that properties and values specified in class declaration blocks will only work on elements that they are intended for. For example, a font-color declaration will not change the color of an image border. Likewise, a height declaration will not change the size of the font. Conclusion You have now explored how to create classes, assign them specific property values, and apply them to text and image content. You will return to using classes when you begin building the website in the second half of this tutorial series. In the next tutorial, you will create CSS ID selectors, which work similarly as class selectors with the exception of some unique features. Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Tutorial Series: How To Build a Website With CSS This tutorial is part of a series on creating and customizing this website with CSS, a stylesheet language used to control the presentation of websites. You may follow the entire series to recreate the demonstration website and gain familiarity with CSS or use the methods described here for other CSS website projects. Before proceeding, we recommend that you have some knowledge of HTML, the standard markup language used to display documents in a web browser. If you don’t have familiarity with HTML, you can follow the first ten tutorials of our series How To Build a Website With HTML before starting this series. Источник
  15. Different Elements Can Share Same Class Different HTML elements can point to the same class name. In the following example, both and point to the «city» class and will share the same style: Example Use of The class Attribute in JavaScript The class name can also be used by JavaScript to perform certain tasks for specific elements. JavaScript can access elements with a specific class name with the getElementsByClassName() method: Example Click on a button to hide all elements with the class name «city»: Don’t worry if you don’t understand the code in the example above. You will learn more about JavaScript in our HTML JavaScript chapter, or you can study our JavaScript Tutorial. Chapter Summary The HTML class attribute specifies one or more class names for an element Classes are used by CSS and JavaScript to select and access specific elements The class attribute can be used on any HTML element The class name is case sensitive Different HTML elements can point to the same class name JavaScript can access elements with a specific class name with the getElementsByClassName() method Источник Classes in CSS CSS classes are used to select elements from the webpage and add style to them. Classes are used to add style to multiple elements at once. The CSS classes allow us to define multiple CSS properties in a single block and when that class is added to any element then all the specified CSS property is reflected in the HTML element. Classes are defined either in the style tag or in the external stylesheet. Once a class is defined it can be used as many times as you want to use. Defining a class Classes are defined using a dot (.) followed by the class name. For example, if we want to define a class named myclass then we will write it as .myclass . CSS properties for that class will be written inside the curly braces. Note : The class name should not start with a number and should not contain spaces or special characters except the hyphen (-). Adding class to the element Once a class is defined, it can be added to any element using the class attribute in the HTML element. For example, if we want to add the class class-1 to the p element then we will write it as

    Paragraph with multiple class

    A working example for CSS class. Note : A class can be used multiple times in a single HTML document. And also an element can have multiple classes. Источник How To Create Classes With CSS In this tutorial, you will create a CSS class selector, which will allow you to apply CSS rules only to HTML elements that are assigned the class. CSS class selectors are useful when you want to apply different style rules for different instances of the same HTML element. Prerequisites To follow this tutorial, make sure you have set up the necessary files and folders as instructed in a previous tutorial in this series How To Set Up You CSS and HTML Practice Project. How CSS Class Selectors Work A CSS class selector allows you to assign style rules to HTML elements that you designate with that class rather than all instances of a certain element. Unlike HTML elements (such as , or ), whose names are predetermined, class names are chosen by the developer when they create the class. Class names are always preceded by a . , which can help you distinguish between tag selectors and class selectors in CSS files. A CSS rule for a class selector is written in the same way as a rule for a tag selector, with the exception of the . prepended to the class name: To use a class when adding HTML content to your webpage, you must specify it in the opening tag of an HTML element using the class attribute in your HTML document like so: h1 class=".red-text">Content.element> Creating a CSS Class Using a Class Selector Let’s begin exploring CSS classes in practice. Erase everything in your styles.css file and add the following code snippet to specify a rule for the class red-text : After adding the code snippet to your styles.css file, save the file. Return to your index.html and erase everything but the first line of code that links to your CSS stylesheet. Then add the following HTML code snippet: p class="red-text">Here is the first sample of paragraph text.p> Note that the class name is not prepended here with a . as it is when being used as a selector for a CSS rule. Your entire index.html file should have the following contents: . . . link rel="stylesheet" href="css/styles.css"> p> In this code snippet you have added text using the HTML tag. But you have also specified the red-text class by adding the highlighted class attribute class=»red-text» inside the opening HTML tag. Save your index.html file and load it in the browser. (For instructions on loading an HTML file, please visit our tutorial step How To View An Offline HTML File In Your Browser). You should receive a webpage with red text: Let’s add an additional CSS class to explore styling different pieces of text content with different classes. Add the following code snippet to your styles.css file (after your CSS rule for “red-text”): .yellow-background-text background-color: yellow; > This CSS rule declares that the class yellow-background-text is assigned the yellow value for the background-color property. Any HTML text element assigned this class will have a yellow background. Note that the use of the word text in the class yellow-background-*text* is for human readability purposes only. You do not need to include the word text in your class names for classes assigned to HTML text. To apply this new CSS class, return to your index.html file and add the following line of code to the bottom: p class="yellow-background-text"> Here is the second sample of paragraph text.p> In this code snippet, you have added some text content with the element and specified the yellow-background-text class. Save the file and reload it in your browser. You should have a webpage with two different sentences, the first one red and the second one with a yellow background: Note that you can add more than one class to an HTML tag. Try adding both classes to a single text element by adding the following line to your index.html file: p class="red-text yellow-background-text">Here is a third sample of text.p> Note that the class names are only separated by a space. Save the file and reload it in the browser. You should receive something like this: Your third line of text should now be styled according to the property values set in the red-text class and the yellow-background-text class and have a red font and yellow background. Adding CSS Classes to Images CSS classes can also be applied to other HTML elements, such as images. To explore using CSS classes for images, erase the content in your styles.css file and add the following code snippet: .black-img border: 5px dotted black; border-radius: 10%; > .yellow-img border: 25px solid yellow; border-radius: 50%; > .red-img border: 15px double red; > Here you have created CSS rules for three different classes that can be applied to the HTML tag. Before you move on, let’s briefly study what we’ve declared in each ruleset: The first CSS rule declares that the class black-img should have a black , dotted border five pixels wide and a border-radius sized at 10%, which gives the element rounded corners. The second CSS rule declares that the class yellow-img should have a yellow , solid border 25 pixels wide and a border-radius sized at 50%, which gives the element a circular shape. The third CSS rule declares that the class red-img should have a red , double border 15 pixels wide. You have not set a border-radius, so the border will conform to the element’s shape. Save the styles.css file. Then erase everything from your index.html file (except for the first line of code: ) and add the following code snippet: img src="https://css.sammy-codes.com/images/small-profile.jpeg" class="black-img"> img src="https://css.sammy-codes.com/images/small-profile.jpeg" class="yellow-img"> img src="https://css.sammy-codes.com/images/small-profile.jpeg" class="red-img"> Each of these three lines of HTML code add an image to the HTML document and assign it one of the three classes you just added to the styles.css file. Note that you are sourcing the image from an online location. You can also use your own image by specifying the file path as instructed in our tutorial How To Add Images To Your Webpage With HTML. Save your index.html file and load it in the browser. You should receive something like this: Your webpage should now display three images, each styled with the different specifications of their assigned class. To continue exploring CSS classes, trying creating new classes with different rulesets and applying them to different types of HTML content. Note that properties and values specified in class declaration blocks will only work on elements that they are intended for. For example, a font-color declaration will not change the color of an image border. Likewise, a height declaration will not change the size of the font. Conclusion You have now explored how to create classes, assign them specific property values, and apply them to text and image content. You will return to using classes when you begin building the website in the second half of this tutorial series. In the next tutorial, you will create CSS ID selectors, which work similarly as class selectors with the exception of some unique features. Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Tutorial Series: How To Build a Website With CSS This tutorial is part of a series on creating and customizing this website with CSS, a stylesheet language used to control the presentation of websites. You may follow the entire series to recreate the demonstration website and gain familiarity with CSS or use the methods described here for other CSS website projects. Before proceeding, we recommend that you have some knowledge of HTML, the standard markup language used to display documents in a web browser. If you don’t have familiarity with HTML, you can follow the first ten tutorials of our series How To Build a Website With HTML before starting this series. Источник
  16. Different Elements Can Share Same Class
  17. Example
  18. Use of The class Attribute in JavaScript
  19. Example
  20. Chapter Summary
  21. Classes in CSS
  22. Defining a class
  23. Adding class to the element
  24. How To Create Classes With CSS
  25. Prerequisites
  26. How CSS Class Selectors Work
  27. Creating a CSS Class Using a Class Selector
  28. Adding CSS Classes to Images
  29. Conclusion
  30. Tutorial Series: How To Build a Website With CSS

HTML class Attribute

The HTML class attribute is used to specify a class for an HTML element.

Multiple HTML elements can share the same class.

Using The class Attribute

The class attribute is often used to point to a class name in a style sheet. It can also be used by a JavaScript to access and manipulate elements with the specific class name.

In the following example we have three elements with a class attribute with the value of «city». All of the three elements will be styled equally according to the .city style definition in the head section:

Example

London

London is the capital of England.

Paris

Paris is the capital of France.

Tokyo

Tokyo is the capital of Japan.

In the following example we have two elements with a class attribute with the value of «note». Both elements will be styled equally according to the .note style definition in the head section:

Example

My Important Heading

This is some important text.

Tip: The class attribute can be used on any HTML element.

Note: The class name is case sensitive!

Tip: You can learn much more about CSS in our CSS Tutorial.

The Syntax For Class

To create a class; write a period (.) character, followed by a class name. Then, define the CSS properties within curly braces <>:

Example

Create a class named «city»:

London is the capital of England.

Paris is the capital of France.

Tokyo is the capital of Japan.

Multiple Classes

HTML elements can belong to more than one class.

To define multiple classes, separate the class names with a space, e.g. . The element will be styled according to all the classes specified.

In the following example, the first element belongs to both the city class and also to the main class, and will get the CSS styles from both of the classes:

Example

London

Different Elements Can Share Same Class

Different HTML elements can point to the same class name.

In the following example, both and

point to the «city» class and will share the same style:

Example

Use of The class Attribute in JavaScript

The class name can also be used by JavaScript to perform certain tasks for specific elements.

JavaScript can access elements with a specific class name with the getElementsByClassName() method:

Example

Click on a button to hide all elements with the class name «city»:

Don’t worry if you don’t understand the code in the example above.

You will learn more about JavaScript in our HTML JavaScript chapter, or you can study our JavaScript Tutorial.

Chapter Summary

  • The HTML class attribute specifies one or more class names for an element
  • Classes are used by CSS and JavaScript to select and access specific elements
  • The class attribute can be used on any HTML element
  • The class name is case sensitive
  • Different HTML elements can point to the same class name
  • JavaScript can access elements with a specific class name with the getElementsByClassName() method

Источник

Classes in CSS

CSS classes are used to select elements from the webpage and add style to them. Classes are used to add style to multiple elements at once.

The CSS classes allow us to define multiple CSS properties in a single block and when that class is added to any element then all the specified CSS property is reflected in the HTML element.

Classes are defined either in the style tag or in the external stylesheet.

Once a class is defined it can be used as many times as you want to use.

classes in CSS

Defining a class

Classes are defined using a dot (.) followed by the class name.

For example, if we want to define a class named myclass then we will write it as .myclass .

CSS properties for that class will be written inside the curly braces.

Note : The class name should not start with a number and should not contain spaces or special characters except the hyphen (-).

Adding class to the element

Once a class is defined, it can be added to any element using the class attribute in the HTML element.

For example, if we want to add the class class-1 to the p element then we will write it as

.

Multiple classes can be added to an element. like shown below.

Paragraph with multiple class

A working example for CSS class.

Note : A class can be used multiple times in a single HTML document. And also an element can have multiple classes.

Источник

How To Create Classes With CSS

In this tutorial, you will create a CSS class selector, which will allow you to apply CSS rules only to HTML elements that are assigned the class. CSS class selectors are useful when you want to apply different style rules for different instances of the same HTML element.

Prerequisites

To follow this tutorial, make sure you have set up the necessary files and folders as instructed in a previous tutorial in this series How To Set Up You CSS and HTML Practice Project.

How CSS Class Selectors Work

A CSS class selector allows you to assign style rules to HTML elements that you designate with that class rather than all instances of a certain element. Unlike HTML elements (such as

, or ), whose names are predetermined, class names are chosen by the developer when they create the class. Class names are always preceded by a . , which can help you distinguish between tag selectors and class selectors in CSS files.

A CSS rule for a class selector is written in the same way as a rule for a tag selector, with the exception of the . prepended to the class name:

To use a class when adding HTML content to your webpage, you must specify it in the opening tag of an HTML element using the class attribute in your HTML document like so:

h1 class=".red-text">Content.element> 

Creating a CSS Class Using a Class Selector

Let’s begin exploring CSS classes in practice. Erase everything in your styles.css file and add the following code snippet to specify a rule for the class red-text :

After adding the code snippet to your styles.css file, save the file.

Return to your index.html and erase everything but the first line of code that links to your CSS stylesheet. Then add the following HTML code snippet:

p class="red-text">Here is the first sample of paragraph text.p> 

Note that the class name is not prepended here with a . as it is when being used as a selector for a CSS rule. Your entire index.html file should have the following contents:

. . . link rel="stylesheet" href="css/styles.css"> p> 

In this code snippet you have added text using the HTML

tag. But you have also specified the red-text class by adding the highlighted class attribute class=»red-text» inside the opening HTML tag.

Save your index.html file and load it in the browser. (For instructions on loading an HTML file, please visit our tutorial step How To View An Offline HTML File In Your Browser).

You should receive a webpage with red text:

Webpage output with red paragraph text

Let’s add an additional CSS class to explore styling different pieces of

text content with different classes. Add the following code snippet to your styles.css file (after your CSS rule for “red-text”):

.yellow-background-text  background-color: yellow; > 

This CSS rule declares that the class yellow-background-text is assigned the yellow value for the background-color property. Any HTML text element assigned this class will have a yellow background. Note that the use of the word text in the class yellow-background-*text* is for human readability purposes only. You do not need to include the word text in your class names for classes assigned to HTML text.

To apply this new CSS class, return to your index.html file and add the following line of code to the bottom:

p class="yellow-background-text"> Here is the second sample of paragraph text.p> 

In this code snippet, you have added some text content with the

element and specified the yellow-background-text class. Save the file and reload it in your browser. You should have a webpage with two different sentences, the first one red and the second one with a yellow background:

Webpage with text styled by two classes

Note that you can add more than one class to an HTML tag. Try adding both classes to a single text element by adding the following line to your index.html file:

p class="red-text yellow-background-text">Here is a third sample of text.p> 

Note that the class names are only separated by a space. Save the file and reload it in the browser. You should receive something like this:

IWebpage with text styled by three classes

Your third line of text should now be styled according to the property values set in the red-text class and the yellow-background-text class and have a red font and yellow background.

Adding CSS Classes to Images

CSS classes can also be applied to other HTML elements, such as images. To explore using CSS classes for images, erase the content in your styles.css file and add the following code snippet:

.black-img  border: 5px dotted black; border-radius: 10%; > .yellow-img  border: 25px solid yellow; border-radius: 50%; > .red-img  border: 15px double red; > 

Here you have created CSS rules for three different classes that can be applied to the HTML tag. Before you move on, let’s briefly study what we’ve declared in each ruleset:

  • The first CSS rule declares that the class black-img should have a black , dotted border five pixels wide and a border-radius sized at 10%, which gives the element rounded corners.
  • The second CSS rule declares that the class yellow-img should have a yellow , solid border 25 pixels wide and a border-radius sized at 50%, which gives the element a circular shape.
  • The third CSS rule declares that the class red-img should have a red , double border 15 pixels wide. You have not set a border-radius, so the border will conform to the element’s shape.

Save the styles.css file. Then erase everything from your index.html file (except for the first line of code: ) and add the following code snippet:

img src="https://css.sammy-codes.com/images/small-profile.jpeg" class="black-img"> img src="https://css.sammy-codes.com/images/small-profile.jpeg" class="yellow-img"> img src="https://css.sammy-codes.com/images/small-profile.jpeg" class="red-img"> 

Each of these three lines of HTML code add an image to the HTML document and assign it one of the three classes you just added to the styles.css file. Note that you are sourcing the image from an online location. You can also use your own image by specifying the file path as instructed in our tutorial How To Add Images To Your Webpage With HTML.

Save your index.html file and load it in the browser. You should receive something like this:

Webpage with images styled with three classes

Your webpage should now display three images, each styled with the different specifications of their assigned class.

To continue exploring CSS classes, trying creating new classes with different rulesets and applying them to different types of HTML content. Note that properties and values specified in class declaration blocks will only work on elements that they are intended for. For example, a font-color declaration will not change the color of an image border. Likewise, a height declaration will not change the size of the font.

Conclusion

You have now explored how to create classes, assign them specific property values, and apply them to text and image content. You will return to using classes when you begin building the website in the second half of this tutorial series.

In the next tutorial, you will create CSS ID selectors, which work similarly as class selectors with the exception of some unique features.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Tutorial Series: How To Build a Website With CSS

This tutorial is part of a series on creating and customizing this website with CSS, a stylesheet language used to control the presentation of websites. You may follow the entire series to recreate the demonstration website and gain familiarity with CSS or use the methods described here for other CSS website projects.

Before proceeding, we recommend that you have some knowledge of HTML, the standard markup language used to display documents in a web browser. If you don’t have familiarity with HTML, you can follow the first ten tutorials of our series How To Build a Website With HTML before starting this series.

Источник

Читайте также:  Raspberry pi apache2 php
Оцените статью