Imagine you're designing a web page. You want a group of headings to have a large red text, a group of buttons to have a medium white text, and a group of paragraphs to have a small blue text.
Bold choices. But, thanks to CSS classes, you can do exactly that. CSS classes enable you to apply unique style properties to groups of HTML elements to achieve your desired web page appearance.
在这篇文章中,我们将介绍您需要知道的基本术语,例如CSS类,类选择器和CSS特异性。我们还将介绍如何在CSS中创建类并使用它来设计您的网页。
什么是CSS课程?
A CSS class is an attribute used to define a group of HTML elements in order to apply unique styling and formatting to those elements with CSS.
让我们看看CSS课程如何工作的示例。下面,我们有一个简单的HTML页面,带有三个标题(H2元素s) and three paragraphs (p元素)。
请注意,第二个标题,第三标题和最终段落的样式与其他段落的样式不同 - 这是因为这些元素已分配了类bright。Looking at the CSS, we see the。brightselector, which applies its style rules to all elements with the attributeclass =“明亮”。
看到笔css class: heading example克里斯蒂娜·佩里奇(Christina Perricone)(@HubSpot) onCodepen。
You can use CSS classes to group HTML elements and then apply custom styles to them. You can make classes and apply them to text, buttons,spans and divs, tables, images, or just about any other page element you can think of. Let’s now take a closer look at how we can use CSS classes to style page elements.
How to Create a Class in CSS
Let’s try making a CSS class from scratch. Say you want to make a paragraph of text and style certain words for more emphasis. You can do this by creating a CSS class for these special words, then assigning this class to individual words withspantags.
Start by writing out the HTML elements you want to style. In this case, it’s a paragraph of text:
Our marketing software and service platform provide you with the tools you need to engage visitors, convert them to leads, and win them over as customers.
我也放了围绕我们很快使用CSS课程样式的单词标记。
Next, let's add class attributes to thesetags. To do so, add the attributeclass =“名称”到目标元素的开头标签,然后替换姓名with a unique identifier for the class.
In our example, the HTML looks like this:
Our marketing software and service platform provide you with the tools you need to engage visitors, convert them to leads, and win them over as customers.
Here we’ve added two CSS classes to ourspantags:orange-textandblue-text。
Lastly, you need to create rule sets for these classes in CSS. We do this using CSS class selectors and declaration blocks.
CSS中的类选择器是什么?
In CSS, a class selector is formatted as a period (.) character followed by the name of the class. It selects all elements with that class attribute so that unique CSS declarations can be applied to those specific elements without affecting other elements on the page.
In our example, we’ll create declaration blocks for both of our CSS classes with the selectors.Orange-Textand。blue-text:
/* declaration for our first CSS class */
.Orange-Text{
颜色为橙色;
font-weight: bold;
}
/* declaration for our second CSS class */
。blue-text {
color: blue;
font-weight: bold;
}
当我们对HTML和CSS,我们看看our CSS classes target certain elements with our custom styling:
看到笔css class: span example克里斯蒂娜·佩里奇(Christina Perricone)(@HubSpot) onCodepen。
Note that the class attribute doesn't change the content or style of the HTML document by itself. Meaning, simply adding a class attribute to an element without any CSS will not change the appearance or formatting of the element on the front end. You need to assign CSS rules to the class to see any change.
It also helps to create class names that describe the element in the class. In the above example, we used the names.Orange-Textand。blue-text因为我们是creating colorful text。These names are descriptive enough that someone just reading the CSS would understand the purpose of the class.
Class names can be one or multiple words. If your class name is multiple words, use hyphens where you would put spaces. Also, it’s common practice to write class names in lowercase. Some examples of class names include。bright-blueand。fancy-text。
如何使用CSS课程
Now that we understand what a CSS class is and how it appears in the body section of an HTML file, let's take a look at common use cases.
Bootstrap CSSClasses
许多CSS框架大量使用CSS课程。例如,Bootstrap CSS使用类定义页面元素。
Let’s see an example of how Bootstrap uses CSS classes. In Bootstrap CSS,the CSS class。btn可以与HTML element (as well as theand元素)。Bootstrap contains CSS that automatically formats any elements defined with the。btnclass a certain way. Therefore, simply adding the attributeclass=“btn”元素会改变其外观。
更准确地说,添加。btnclass to an element sets the font and font size, and if a visitor clicks on the button text, an outline of a button with rounded edges appears.
Bootstrap also has classes for styling buttons in other ways, such as background color. If we add the class。btn-successor.btn-Dangerthe button will show as green or red respectively.
看到笔css class: bootstrap example克里斯蒂娜·佩里奇(Christina Perricone)(@HubSpot) onCodepen。
借助CSS类,Bootstrap只需添加一个或多个类名称即可快速快速样式的页面元素。CSS类使您能够在编写更少的代码时格式化不同类型的元素。
后代选择器
CSS类的目标是将格式应用于特定元素。为此,后代选择器是您工具箱的绝佳补充。
Descendant selectors let you target elements inside of other elements. For instance, you may have already created a class to define a general style for paragraph or heading text, but want certain words within the paragraph to be styled in their own way.
后代选择器允许您将这些特殊样式添加到特定单词中,而不会影响周围的文本或更改HTML文档。
假设您应用了。blue-text类似于标题,但想更改标题中的单词的颜色。将单词包裹在一个span元素,然后将另一个规则集与后代选择器:之后CSS类Selector之后。blue-text, add a space and then the type selectorspan。
看到笔CSS类:后代选择器示例克里斯蒂娜·佩里奇(Christina Perricone)(@HubSpot) onCodepen。
尽管,小心不要过份。过度使用的descendant selectors can lead you to set up confusing rules that will make changes difficult for you down the line.
Pseudo-Classes
There's more to see on a web page than HTML content. A great deal of information about the user's activity is transmitted while they interact with the page. Some of this information is a reflection of what they're doing.
Consider a link within your content. The user may or may not interact with it. If they do, you can use pseudo-classes to capture temporary user information such as when they hover, click, and follow the link.
Pseudo-classes are identified by a colon followed by the class. They will appear after a CSS selector with no space in between.
Common pseudo-classes for link styling are:
- :关联targets a link that the user hasn't visited.
- :visitedtargets a link that's been visited by the user before.
- :hovertargets a link with the user's cursor over it.
- :积极的targets a link that's being pressed down.
让我们看一个例子。说你想remove the underline仅在所有州的某些链接中。意思是,无论用户是否访问了该链接,徘徊在链接上,或者正在积极按下它,下划线都不会出现在这些特定链接下方。
在这种情况下,您只需将一个类属性添加到要删除下划线的链接中。然后,使用类选择器和四个相应的伪级添加四个规则集。在每个声明块中,您都会设置text-decoration财产为none。
看到笔css class: pseudo classes克里斯蒂娜·佩里奇(Christina Perricone)(@HubSpot) onCodepen。
CSS类与ID选择器
在CSS中,类将多个元素分组在一起,而ID用于识别单个元素。使用类选择器样式的同一类和ID选择器的多个HTML元素来样式,以设计一个唯一的HTML元素。
虽然您可以在HTML页面上具有多个类的实例,但您只能拥有一个实例CSS ID在页面上。要给元素一个ID,请添加属性id =“名称”to its opening tag, and replace姓名with a unique identifying name. In the CSS, the corresponding ID selector begins with a pound sign (#) instead of a period.
Additionally, ID attributes provide the target for URL fragments (such as page anchors), so they should be unique. Fragments help you refer a user to a specific part of a web page — the fragment looks like an ID selector placed at the end of the URL.
CSS特异性
Sometimes, CSS rules conflict. For example, if multiple selectors target the same element in a document, which rules apply? This is determined byCSS specificity。In CSS, different selectors have varying weights. When two or more rules conflict on the same element, the more specific one applies.
This is how the different selectors rank in the specificity hierarchy:
- Inline CSS:内联CSS在HTML元素的开头标签中显示为样式属性。由于该CSS最接近HTML,因此具有最高水平的特异性。
- ID选择器:ID是页面元素独有的,因此非常具体。
- 类选择器,属性选择器和伪级选择器:这三种选择器类型具有相等的特异性。如果将这三种类型应用于同一HTML元素,则在样式表中出现最新的元素将应用并覆盖其余部分。
- 类型选择器:These select all HTML elements that have a given node name and have the syntax元素。这些是元素名称和伪元素。
这是ID选择器发光的地方。因为它们是如此具体,所以几乎所有其他类型的选择器都会失去。另一方面,通用选择器(*)由于其特异性低而每次都会损失。
开始使用CSS类
CSS classes help you customize elements on a web page faster and more easily. Using CSS class selectors allows you to set up rules to format entire classes of HTML elements, specific elements in a class, or single elements across many classes. You can be as creative as you want when designing your site, but remember the goal is to improve your website's user experience.
Editor's note: This post was originally published in February 2020 and has been updated for comprehensiveness.
Originally published Aug 31, 2021 7:00:00 AM, updated August 31 2021
话题:
Bootstrap & CSSDon't forget to share this post!
相关文章
扩展优惠
Sign up for HubSpot's CMS Software
Get it now