CSS has two class selectors at the same time
CSS has two class selectors at the same time
In CSS, we can apply multiple class selectors to HTML elements at the same time, which makes the style more flexible and diverse. By using two class selectors simultaneously, we can achieve more refined style control. Below, we’ll detail how to use two class selectors simultaneously in CSS and provide some sample code to help you understand.
Basic Syntax
In HTML elements, you can specify one or more class selectors using the class attribute, separating multiple class selectors with spaces. In CSS, you can apply styles to an element by referencing two class selectors simultaneously, concatenating the class selector names using the .
notation.
The sample code is as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type='text/css'>
/* styles.css */
.box1 {
background-color: #f0f0f0;
padding: 10px;
}
.box2 {
color: #333;
font-size: 20px;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Having two class selectors at the same time</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="box1 box2">Having two class selectors at the same time</div>
</body>
</html>
Code running results:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type='text/css'>
/* styles.css */
.class1 {
background-color: #f0f0f0;
padding: 10px;
}
.class2 {
color: #333;
font-size: 20px;
}
</style>
<title>Example 1: Two Class Selectors</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="class1 class2">With two class selectors at the same time</div>
</body>
</html>
Code running results:
In this example, the <div>
element has both class1
and class2
class selectors.
Example 2: Using two class selectors and setting different background colors
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type='text/css'>
/* styles.css */
.bg1 {
background-color: #ffcccc;
padding: 10px;
}
.bg2 {
background-color: #ccffcc;
color: #333;
font-size: 20px;
}
</style>
<title>Example 2: Using Two Class Selectors and Setting Different Background Colors</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="bg1 bg2">Using Two Class Selectors</div>
</body>
</html>
Code Running Results:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type='text/css'>
/* styles.css */
.font1 {
font-size: 16px;
padding: 10px;
}
.font2 {
font-size: 24px;
color: #333;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example 3: Using Two Class Selectors and Setting Different Font Sizes</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="font1 font2">With two class selectors at the same time</div>
</body>
</html>
Code running results:
In this example, the <div>
element has both the font1
and font2
class selectors, setting different font sizes.
Example 4: Using Two Class Selectors and Setting Different Border Styles
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type='text/css'>
/* styles.css */
.border1 {
border: 1px solid #333;
padding: 10px;
}
.border2 {
border: 2px dashed #f00;
color: #333;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example 4: Using Two Class Selectors and Setting Different Border Styles</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="border1 border2">Has two class selectors at the same time</div>
</body>
</html>
Code Running Result:
In this example, the <div>
element has two class selectors, border1
and border2
, with different border styles set.
Example 5: Using Two Class Selectors and Setting Different Text Alignment
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type='text/css'>
/* styles.css */
.align1 {
text-align: left;
padding: 10px;
}
.align2 {
text-align: right;
color: #333;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example 5: Using Two Class Selectors and Setting Different Text Alignment</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="align1 align2">Has two class selectors at the same time</div>
</body>
</html>
Code Running Result:
In this example, the <div>
element has two class selectors, align1
and align2
, with different text alignment settings.
Example 6: Using Two Class Selectors and Setting Different Font Colors
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type='text/css'>
/* styles.css */
.color1 {
color: #ff0000;
padding: 10px;
}
.color2 {
color: #00ff00;
font-size: 20px;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example 6: Using Two Class Selectors and Setting Different Font Colors</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="color1 color2">Has two class selectors at the same time</div>
</body>
</html>
Code Running Result:
In this example, the <div>
element has two class selectors, color1
and color2
, and sets different font colors.
Example 7: Using Two Class Selectors and Setting Different Font Styles
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type='text/css'>
/* styles.css */
.style1 {
font-style: italic;
padding: 10px;
}
.style2 {
font-weight: bold;
color: #333;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example 7: Using Two Class Selectors and Setting Different Font Styles</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="style1 style2">With two class selectors at the same time</div>
</body>
</html>
Code running results:
In this example, the <div>
element has two class selectors, style1
and style2
, and sets different font styles.
Example 8: Using Two Class Selectors and Setting Different Background Images
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type='text/css'>
/* styles.css */
.bg-img1 {
background-image: url('geek-docs.com/https://coder-cafe.com/wp-content/uploads/2025/09/image1.jpg');
padding: 10px;
}
.bg-img2 {
background-image: url('geek-docs.com/https://coder-cafe.com/wp-content/uploads/2025/09/image2.jpg');
color: #333;
}
</style>
<meta name="viewport" content="width=device-width, Initial scale = 1.0">
<title>Example 8: Simultaneously have two class selectors and set different background https://coder-cafe.com/wp-content/uploads/2025/09/images</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="bg-img1 bg-img2">Simultaneously have two class selectors</div>
</body>
</html>
Code running results:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type='text/css'>
/* styles.css */
.border-radius1 {
border: 1px solid #333;
border-radius: 5px;
padding: 10px;
}
.border-radius2 {
border: 2px dashed #f00;
border-radius: 10px;
color: #333;
}
</style>
<meta name="viewport" content="width=device-width, Initial scale = 1.0">
<title>Example 9: Using two class selectors and setting different border radius</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="border-radius1 border-radius2">Using two class selectors</div>
</body>
</html>
Code running results:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type='text/css'>
/* styles.css */
.shadow1 {
box-shadow: 5px 5px 5px #888888;
padding: 10px;
}
.shadow2 {
box-shadow: 10px 10px 10px #333333;
color: #333;
}
</style>
<meta name="viewport" content="width=device-width, Initial-scale=1.0">
<title>Example 10: Two class selectors with different shadow effects</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="shadow1 shadow2">Two class selectors</div>
</body>
</html>
Code execution results:
In this example, the <div>
element has two class selectors, shadow1
and shadow2
, with different shadow effects set.
The above is a detailed introduction and sample code for using two class selectors simultaneously in CSS. By applying multiple class selectors simultaneously, we can more flexibly control the style of an element and achieve more diverse design effects.