CSS group selector applies the same style to multiple elements at a time. The names of elements can be separated by commas. Group selector keeps CSS concise and avoids redundancy.
Syntax
The syntax for the CSS group selector is as follows:
span, #para, .myClass {
background-color: #04af2f;
color: white;
}
</style></head><body><p id="para">This is a paragraph element.</p><div class="myClass">This is a div element.</div><br><span>This is a span element.</span></body></html></pre>
Group Selectors Example with Pseudo-classes
In this example, we have used pseudo classes with group selectors. CSS :active pseudo-class changes the text color of the link and button on clicking while CSS :hover pseudo-class changes the background-color of the div and p element.
.myDiv:hover, #para:hover {
background-color: #031926;
color: white;
}
</style></head><body><div class="myDiv">
Hover over me
</div><p id="para">This is a paragraph.</p><a class="link" href="#">Click on this link.</a><br><br><button id="btn">Click me</button></body></html></pre>
CSS ID selector selects a single element with a particular value for the id attribute. An id in CSS is denoted by the “#” (hash) symbol. The same class can be applied to multiple elements, but an id is unique for an element.
Syntax
The syntax for the CSS id selector is as follows:
#id{/* property: value; */color: #04af2f
}
ID Selector for Styling a div Element
CSS ID selector can be used to select various elements such as div, p , section, etc.
Example
In this example, we have used an ID selector to style the div element. We have used background-color, color, padding, border, and text-align properties to style and center the text in the div element.
#myId {
background-color: #04af2f;
color: white;
font-size: 20px;
font-family: Verdana, sans-serif;
padding: 10px;
border: 1px solid black;
text-align: center;
}
</style></head><body><div id="myId">
This is a div element having various styles.
</div></body></html></pre>
ID Selector with Pseudo-Classes
CSS ID selector can be used with pseudo classes to add interactivity to the element such as changing text color on hovering any link.
Example
In this example, we have used hover pseudo-class to change the link text color upon hovering over it.
#myId:hover {
color: #031926;
}
</style></head><body><a href="#" id="myId">
Hover over this link to change its color.
</a></body></html></pre>
Using ID Selector with Forms
CSS ID selector can be used with forms to design the form elements.
Example
In this example, we have used an id selector on form elements like buttons and input fields. We have designed the input field and changed the appearance of the submit button.
CSS ID selector can be used with CSS animations for creating smooth animation effects.
Example
In this example, we have used an id selector to animate a bouncing ball. CSS keyframes specify the frame sequence and translateY() function of the transform property to control the bouncing of the ball.
CSS universal selector is a special selector that selects all the elements in an HTML document. It is denoted by an asterisk mark (*). Universal selector is used to set a similar style for the entire document.
Syntax
The syntax for the CSS universal selector is as follows:
*{margin: 0;padding: 0;}
Using Universal Selector Setting Background Color
To set the background color and text color of the web page, universal selector is used:
Example
Let us see an example to set the background color and text color of the HTML document.
Cascading in CSS refers to a process where a browser determines which rule to apply on any element when various conflicting rules exist. The cascade follows a structured order to specify which style rule will take precedence over other style rules.
Key Factors of CSS Cascading
The key factors for deciding which CSS rule will take priority are mentioned below:
1. Specificity
Specificity refers to, how specific rules have been defined. For example: rules defined using the ID selector take priority over rules defined using the class selector if both selectors are used on the same element.
The styles marked with !important have higher priority over other rules. For example, if both inline and internal css, are used on an element. Then the rule that is marked as !important will take priority.
CSS specificity plays an important role in deciding which CSS rule to apply to an element when multiple CSS rules are defined for the same element. The more specific the CSS rule is, the higher priority that CSS rule will get. A point system is used to calculate the CSS specificity. The point system for selectors is mentioned below:
Inline CSS: 1000 points
ID Selector: 100 points
Classes, Attributes, and Pseudo-Classes: 10 points
Elements and Pseudo-Elements: 1 point
/*Lowest Priority*/p{color: red;}/*1 point*//*Higher Priority than Element Selector*/.myId{color: blue;}/*10 points*//*Second Highest Priority*/#myClass{color: green;}/*100 points*//*Highest Priority*/
<p style="color: #04af2f;">
Welcome to Tutorials Point
</p>
CSS Rule Overriding
CSS Rule overriding occurs when multiple styles are applied to the same element. CSS uses specificity, importance, and source order to decide which rule should be applied.
Example
In this example, we have used two element selectors on the p element. Based on source order the latter used style has been applied in the last paragraph.
p {
color: aqua;
}
#txt {
color: red;
}
.txt {
color: #04af2f;
}
p {
color: blueviolet;
}
</style></head><body><p id="txt">Welcome to Tutorials Point</p><p class="txt">
This is CSS example for Overriding
rule in CSS.
</p><p>This is third paragraph.</p></body></html></pre>
CSS user-select property determines whether text can be selected by the user, with no effect on content loaded as part of a browser’s user interface (chrome) other than textboxes.
While user-select is not inherited, its initial “auto” value often makes it behave as if it is inherited. WebKit/Chromium-based browsers implement the property as inherited, which violates the specification and will cause problems. Chromium has been resolving these problems to align with the specified behavior.
Possible Values
none − The element’s and sub-elements’ text is not selectable, but these elements may be present in the Selection object.
auto − The auto value is determined as follows:
The value used for the ::before and ::after pseudo-elements is none.
For editable elements, the used value is contain.
If the parent of this element has a user-select value all, then the used value is all.
If the parent of this element has a user-select value none, then the used value is none.
The used value is text.
text − The user can select the text.
contain − Allows the selection to begin within the element, but contains the selection to the boundaries of that element.
all − The content of the element must be selected atomically: If a selection contains part of an element, it must also include all of its descendants. If a double-click or context-click happens in a sub-element, the highest ancestor with this value will be chosen.
Applies To
All elements.
Syntax
user-select: none | auto | text | contain | all;
CSS user-select – none Value
The following example demonstrates the user-select: none property preventing users from selecting the text −
<html><head><style>
.text-none {
-webkit-user-select: none;
user-select: none;
}
</style></head><body><p>This text should be selectable.</p><p class="text-none">This text cannot be selected.</p></body></html>
CSS user-select – auto Value
The following example demonstrates the user-select: auto property used to select the text −
<html><head><style>
p {
-webkit-user-select: auto;
user-select: auto;
}
</style></head><body><p>This text should be selectable.</p></body></html>
CSS user-select – text Value
The following example demonstrates the user-select: text property allows the users to select the text −
<html><head><style>
p {
-webkit-user-select: text;
user-select: text;
}
</style></head><body><p>This text should be selectable.</p></body></html>
CSS user-select – all Value
The following example demonstrates the user-select: all property allows the users to select the text within a single click −
<html><head><style>
p {
-webkit-user-select: all;
user-select: all;
}
</style></head><body><p>This text can be selected with a single click.</p></body></html>
CSS user-select – contain Value
The following example demonstrates the user-select: contain property allows the users to select the text within the paragraph’s boundaries −
CSS accent-color property determines the accent color that can be applied to user-interface controls such as radio button, checkboxes, buttons etc. This property gives the flexibility to assign color of user’s choice to the user-interface control.
Syntax
accent-color: auto | color | initial | inherit;
Value
Description
auto
The browser selects the accent color. Default value.
color
It specifies the color to be used. Different formats (rgb, hex, color-name, etc) can be used.
initial
This sets the property to its initial value
inherit
This inherits the property from the parent element
Examples of CSS Accent Color Property
Below examples will explain the accent-color property with different values.
Setting Default Color
To allow the browser to give the color to the user-interface controls, the auto value can be used. This is shown in the example below.
To apply color of our choice to the user-interface controls, we can specify the color in different formats. This is shown in the example below. Three different formats have been used - color name, color rgb value and color hexadecimal value.
The CSS shorthand property offset makes it easier for an element to animate along a certain path.
It has many characteristics that together comprise an offset transform.
With this transform, a specified point inside the element (offset-anchor) is aligned to a certain path position (offset-position) at various points along the route (offset-distance).
It also allows the element to be optionally rotated (offset-rotate) to follow the path’s direction.
Constituent Properties
The offset property is a shorthand for the following CSS properties:
offset-anchor
offset-distance
offset-path
offset-position
offset-rotate
Possible Values
The following list of values is accepted by offset shorthand property.
offset-anchor – Defines a point within the element that aligns with an offset position on the path.
offset-path – Defines the path along which the element is animated.
offset-distance – Defines how far along the path the element is placed.
offset-rotate – Optionally rotates the element to align with the direction of the path.
auto – All properties are reset to their default values.
CSS min-inline-size property sets the minimum inline size of an element. The direction of the inline is determined by the writing-mode property. The property has no effect, if the content fits well within the inline size of the element.
Syntax
min-inline-size: auto | length | percentage | initial | inherit;
Property Values
Value
Description
auto
No width limit is set with this value. Default.
length
It sets the min-inline-size of the element using length units (e.g. px, em, rem etc.)
percentage
It sets the min-inline-size of the element using percentage value relative to the size of the containing element.
initial
It sets the property to its default value.
inherit
It inherits the property from the parent element.
Examples of CSS Min Inline Size Property
The following examples explain the min-inline-size property with different values.
Min Inline Size Property with Auto Value
To not set any specific limit on the inline-size of an element, we use the auto value. The size of the element depends on the length of the content. This is shown in the following example.
To set the inline size of an element, we can specify the size using length units (e.g. px, em, rem etc.). The specified size will be applied to the element. If the content is greater than the size of the element, the element will grow to accomodate the content. This is shown in the following example.
To set the inline size of an element, we can specify the size using percentage value (e.g. 10%) relative to the size of the containing element. The specified size will be applied to the element. If the content is greater than the size of the element, the element will grow to accomodate the content. This is shown in the following example.
TutorialsPoint offers extensive online courses.
</p></div></div><br/><h4>
min-inline-size: 75% (of the
size of the containing element)
The min-inline-size property can be used in combination with the writing-mode property which determines the inline direction. In horizontal-mode, the inline-size grows from left to right. In vertical-mode, the inline-size grows from top to bottom (or bottom to top). This is shown in the following example.
CSS max-inline-size property sets the maximum inline size of an element. The direction of the inline is determined by the writing-mode property. The property has no effect, if the content fits well within the inline size of the element.
Syntax
max-inline-size: auto | length | percentage | initial | inherit;
Property Values
Value
Description
auto
No width limit is set with this value. Default.
length
It sets the max-inline-size of the element using length units (e.g. px, em, rem etc.)
percentage
It sets the max-inline-size of the element using percentage value relative to the size of the containing element.
initial
It sets the property to its default value.
inherit
It inherits the property from the parent element.
Examples of CSS Max Inline Size Property
The following examples explain the max-inline-size property with different values.
Max Inline Size Property with Auto Value
To not set any specific limit on the inline-size of an element, we use the auto value. The size of the element depends on the length of the content. This is shown in the following example.
TutorialsPoint offers comprehensive online
tutorials across various subjects, including
programming, web development, and data science.
It provides accessible, step-by-step guides,
practical examples, and interactive learning
resources for learners of all levels.
</p></div></body></html></pre>
Max Inline Size Property with Length Values
To set the inline size of an element, we can specify the size using length units (e.g. px, em, rem etc.). The specified size will be applied to the element. If the content is greater than the size of the element, it will overflow. This is shown in the following example.
TutorialsPoint offers comprehensive online
tutorials across various subjects, including
programming, web development, and data science.
It provides accessible, step-by-step guides,
practical examples, and interactive learning
resources for learners of all levels.
</p></div><br/><h4>
max-inline-size: 6.5em
</h4><div class="container size2"><p>
TutorialsPoint offers comprehensive online
tutorials across various subjects, including
programming, web development, and data science.
It provides accessible, step-by-step guides,
practical examples, and interactive learning
resources for learners of all levels.
</p></div></body></html></pre>
Max Inline Size Property with Percentage Values
To set the inline size of an element, we can specify the size using percentage value (e.g. 10%) relative to the size of the containing element. The specified size will be applied to the element. If the content is greater than the size of the element, it will overflow. This is shown in the following example.
TutorialsPoint offers comprehensive online
tutorials across various subjects, including
programming, web development, and data science.
It provides accessible, step-by-step guides,
practical examples, and interactive learning
resources for learners of all levels.
</p></div></div><br/><h4>
max-inline-size: 55% (of the size
of the containing element)
TutorialsPoint offers comprehensive online
tutorials across various subjects, including
programming, web development, and data science.
It provides accessible, step-by-step guides,
practical examples, and interactive learning
resources for learners of all levels.
</p></div></div></body></html></pre>
Max Inline Size Property with Writing Mode
The max-inline-size property can be used in combination with the writing-mode property which determines the inline direction. In horizontal-mode, the inline-size grows from left to right. In vertical-mode, the inline-size grows from top to bottom (or bottom to top). This is shown in the following example.
TutorialsPoint offers comprehensive online
tutorials across various subjects, including
programming, web development, and data science.
It provides accessible, step-by-step guides,
practical examples, and interactive learning
resources for learners of all levels.
</p></div></div><br/><h4>
max-inline-size: 65% (of the size of the
containing element); writing-mode: vertical-rl;
TutorialsPoint offers comprehensive online
tutorials across various subjects, including
programming, web development, and data science.
It provides accessible, step-by-step guides,
practical examples, and interactive learning
resources for learners of all levels.
</p></div></div></body></html></pre>
It darkens colors by multiplying the background and foreground colors.
screen
It lightens colors by inverting, multiplying, and inverting again.
overlay
It combines multiply and screen, preserving highlights and shadows.
darken
It retains the darker color from overlapping elements.
lighten
It retains the lighter color from overlapping elements.
color-dodge
It brightens the background by decreasing the color of the element.
color-burn
It darkens the background by increasing the color of the element.
difference
It subtracts the colors to create a high-contrast effect.
exclusion
It is similar to difference, but with softer contrast effects.
hue
It preserves luminance and saturation, altering only the hue.
saturation
It Preserves luminance and hue, altering only the saturation.
color
It combines hue and saturation of the element with luminance.
luminosity
It preserves hue and saturation, altering only the luminosity.
Examples of CSS Mix Blend Mode Property
The following examples explain the mix-blend-mode property with different values.
Mix Blend Mode Property with Normal Value
To prevent an element from interacting with other layers, we use the normal value. The element is rendered as is without any interaction with underlying layers. This is shown in the following example.
To multiply the background and foreground colors, resulting in a darker color, we use the multiply value. It results in rich shadows. This is shown in the following example.
To brighten the colors by inverting both layers, multiplying them, and then inverting the result, we use the screen value. It produces a brightening effect. This is shown in the following example.
To enhances contrast by darkening dark areas and lightening light areas, we use the overlay value. It combines multiply and screen preserving highlights and shadows. This is shown in the following example.
To subtract the colors of the overlapping layers, resulting in high-contrast effect, we use the difference value. This is shown in the following example.
To subtract the colors of the overlapping layers, resulting in a soft contrast effect, we use the exclusion value. This is shown in the following example.
To preserve the luminance and saturation of the background while altering only the hue of the element, we use the hue value. This is shown in the following example.