CSS caret-shape Property
The caret-shape property controls the shape of the text insertion caret displayed in editable content.
The insertion caret indicates where the next character will appear when typing. It is commonly displayed as a thin vertical line in text inputs, textareas, and editable elements. The caret-shape property allows developers to request a vertical bar, block, or underscore instead of the browser's default shape.
This property is part of CSS Basic User Interface Module Level 4 and has limited browser availability. Unsupported browsers continue using their normal insertion caret shape.
caret-shape Syntax
Use caret-shape with one of its predefined keyword values.
selector {
caret-shape: block;
}
The general syntax is:
selector {
caret-shape: auto | bar | block | underscore;
}
Automatic Shape
input {
caret-shape: auto;
}
Bar Shape
input {
caret-shape: bar;
}
Block Shape
input {
caret-shape: block;
}
Underscore Shape
input {
caret-shape: underscore;
}
CSS-Wide Keywords
input {
caret-shape: inherit;
}
input {
caret-shape: initial;
}
input {
caret-shape: unset;
}
input {
caret-shape: revert;
}
input {
caret-shape: revert-layer;
}
The property accepts one keyword per declaration. It does not accept a length, percentage, or custom shape definition.
Property Values
The caret-shape property defines four primary values.
| Value | Description |
|---|---|
auto |
Allows the browser to choose the caret shape. This is the initial value. |
bar |
Requests a thin vertical bar-shaped caret. |
block |
Requests a rectangular block-shaped caret covering a character position. |
underscore |
Requests a horizontal underscore-shaped caret beneath the character position. |
CSS-Wide Keywords
| Value | Description |
|---|---|
inherit |
Uses the computed value from the parent element. |
initial |
Resets the property to auto. |
unset |
Uses the inherited value because the property is inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | Yes |
| Applies to | Elements that can display a text insertion caret |
| Computed value | Specified keyword |
| Animation type | Discrete |
| Percentages | Not applicable |
| Shorthand | caret |
| Primary use | Customizing the text insertion caret shape |
The requested shape may not appear if the browser does not support caret-shape.
How It Works
The insertion caret appears when an editable element receives focus and the user places the text insertion point.
Step 1: Create a Text Input
<input type="text" class="custom-input" placeholder="Enter text">
Step 2: Request a Caret Shape
.custom-input {
caret-shape: block;
}
In supporting browsers, the insertion caret appears as a rectangular block instead of the default thin line.
Step 3: Set a Caret Color
.custom-input {
caret-color: #1769aa;
caret-shape: block;
}
The caret uses the specified color and requested shape where both properties are supported.
Step 4: Apply to Multiple Controls
input,
textarea {
caret-color: #1769aa;
caret-shape: bar;
}
Step 5: Provide a Compatible Fallback
.custom-input {
caret-color: #1769aa;
caret-shape: block;
}
Browsers that do not support caret-shape can still apply the widely supported caret-color declaration.
The property changes the caret's appearance without changing the actual text or insertion position.
Automatic Caret Shape
The auto value allows the browser to determine the insertion caret's shape.
input {
caret-shape: auto;
}
Default Behavior
Most browsers display a thin vertical bar in ordinary text inputs, although the exact shape may depend on the browser, operating system, and editing environment.
Automatic Shape in a Textarea
textarea {
caret-shape: auto;
}
Automatic Shape in Editable Content
[contenteditable="true"] {
caret-shape: auto;
}
Restore the Default Shape
.custom-input {
caret-shape: block;
}
.default-input {
caret-shape: auto;
}
When to Use auto
Use auto when you want the browser to manage the caret's shape or when overriding an earlier custom declaration.
Because auto is the initial value, it normally does not need to be declared.
Bar Caret Shape
The bar value requests a thin vertical line at the text insertion position.
input {
caret-shape: bar;
}
Bar with Custom Color
input {
caret-color: #1769aa;
caret-shape: bar;
}
Bar in a Textarea
textarea {
caret-color: #008855;
caret-shape: bar;
}
Bar in Editable Content
.editable-box {
caret-color: #1769aa;
caret-shape: bar;
}
When to Use bar
The bar shape is appropriate for conventional text editing interfaces where users expect a narrow insertion indicator.
Because browsers commonly use a vertical bar by default, explicitly setting bar may produce little or no visible difference.
Block Caret Shape
The block value requests a rectangular caret that occupies a character position.
input {
caret-shape: block;
}
Block with Custom Color
input {
caret-color: #008855;
caret-shape: block;
}
Terminal-Style Input
.terminal-input {
color: #00cc66;
background-color: #111111;
font-family: monospace;
caret-color: #00cc66;
caret-shape: block;
}
Block in Editable Content
.code-editor {
color: #eeeeee;
background-color: #222222;
font-family: monospace;
caret-color: #66ccff;
caret-shape: block;
}
When to Use block
The block shape is useful for terminal-style interfaces and specialized text editors that benefit from a larger insertion indicator.
The browser determines how the block is rendered over or around the character position. Its appearance can vary between implementations.
Underscore Caret Shape
The underscore value requests a horizontal caret positioned beneath the character insertion point.
input {
caret-shape: underscore;
}
Underscore with Custom Color
input {
caret-color: #9933cc;
caret-shape: underscore;
}
Terminal-Style Underscore
.terminal-input {
color: #00cc66;
background-color: #111111;
font-family: monospace;
caret-color: #00cc66;
caret-shape: underscore;
}
Underscore in a Textarea
textarea {
caret-color: #1769aa;
caret-shape: underscore;
}
When to Use underscore
The underscore shape can be useful in interfaces that imitate traditional terminals or text editors.
Browser support is limited, so the caret may continue appearing as a vertical bar instead of an underscore.
Combining with caret-color
The caret-color property controls the insertion caret's color independently of its shape.
Blue Bar Caret
.blue-caret {
caret-color: #1769aa;
caret-shape: bar;
}
Green Block Caret
.green-caret {
caret-color: #008855;
caret-shape: block;
}
Purple Underscore Caret
.purple-caret {
caret-color: #9933cc;
caret-shape: underscore;
}
Use the Text Color
input {
color: #1769aa;
caret-color: currentColor;
caret-shape: block;
}
Compatible Fallback
input {
caret-color: #1769aa;
caret-shape: block;
}
If the browser does not support caret-shape, it can still display the custom caret color while retaining its default shape.
This makes caret-color a useful companion property when experimenting with custom caret shapes.
Using the caret Shorthand
The caret shorthand combines caret-color, caret-shape, and caret-animation.
Separate Longhand Properties
input {
caret-color: #1769aa;
caret-shape: block;
caret-animation: auto;
}
Combined Shorthand
input {
caret: #1769aa block auto;
}
Bar Caret
input {
caret: #1769aa bar;
}
Block Caret
input {
caret: #008855 block;
}
Underscore Caret
input {
caret: #9933cc underscore;
}
Longhand Override
input {
caret: #1769aa bar;
caret-shape: block;
}
In browsers supporting the shorthand, the later longhand changes the shape without replacing the caret color.
Shorthand Compatibility
The complete caret shorthand has limited browser support. Use individual longhand properties when you need more predictable compatibility.
Remember that shorthand declarations reset omitted subproperties to their initial values.
Form Controls
The caret-shape property is relevant to form controls that display a text insertion caret.
Text Input
input[type="text"] {
caret-shape: block;
}
Email Input
input[type="email"] {
caret-shape: bar;
}
Search Input
input[type="search"] {
caret-shape: underscore;
}
Password Input
input[type="password"] {
caret-shape: bar;
}
Textarea
textarea {
caret-shape: block;
}
Combined Form Controls
input,
textarea {
caret-color: #1769aa;
caret-shape: bar;
}
Focused Controls
input:focus,
textarea:focus {
caret-shape: block;
}
The property does not create a text insertion caret in buttons, checkboxes, or other controls that do not support text editing.
For standard forms, the browser's default caret shape is usually sufficient.
Editable Content
The caret-shape property can be applied to elements made editable with the HTML contenteditable attribute.
Editable HTML Element
<div class="editable-box" contenteditable="true">
Click here and edit this text.
</div>
Custom Caret Shape
.editable-box {
padding: 12px;
border: 1px solid #cccccc;
caret-color: #1769aa;
caret-shape: block;
}
Editable Code Area
<div class="code-editor" contenteditable="true" spellcheck="false">
Edit this sample text.
</div>
.code-editor {
padding: 15px;
color: #eeeeee;
background-color: #222222;
font-family: monospace;
caret-color: #66ccff;
caret-shape: block;
}
Apply to All Editable Elements
[contenteditable="true"] {
caret-shape: bar;
}
Focus Styling
.editable-box:focus-visible {
outline: 2px solid #1769aa;
outline-offset: 2px;
}
The focus outline identifies the active editing area, while the caret identifies where typed text will appear.
Custom caret shapes should not interfere with normal keyboard navigation, text editing, or selection behavior.
Accessibility
The insertion caret is an important visual indicator for users entering or editing text.
Maintain a Visible Caret
input {
color: #222222;
background-color: #ffffff;
caret-color: #1769aa;
caret-shape: bar;
}
Choose a Clear Shape
The requested caret shape should remain easy to identify against the background and surrounding text.
Avoid Obscuring Text
Block-shaped carets can cover a character position. Check that the insertion point remains understandable when editing existing text.
Preserve Focus Indicators
input:focus-visible {
outline: 2px solid #1769aa;
outline-offset: 2px;
}
Respect Platform Conventions
Users may be accustomed to the insertion caret provided by their browser or operating system. Unusual shapes should serve a clear purpose.
Provide a Fallback
input {
caret-color: #1769aa;
caret-shape: block;
}
When caret-shape is unsupported, the browser can continue displaying its default caret shape.
Test Keyboard Editing
Check text entry, arrow-key navigation, text selection, and focus visibility when using custom caret styling.
For ordinary form controls, the browser's default insertion caret is generally the most familiar and dependable option.
Practical Uses
The caret-shape property is most useful for specialized text editing interfaces.
Standard Text Input
.standard-input {
caret-color: #1769aa;
caret-shape: bar;
}
Terminal-Style Input
.terminal-input {
color: #00cc66;
background-color: #111111;
font-family: monospace;
caret-color: #00cc66;
caret-shape: block;
}
Underscore Caret
.underscore-input {
caret-color: #9933cc;
caret-shape: underscore;
}
Dark-Themed Editor
.dark-editor {
color: #eeeeee;
background-color: #222222;
caret-color: #66ccff;
caret-shape: block;
}
Editable Text Area
.editable-area {
caret-color: #1769aa;
caret-shape: bar;
}
Restore the Default Shape
.default-input {
caret-shape: auto;
}
Progressive Enhancement
.custom-input {
caret-color: #1769aa;
}
@supports (caret-shape: block) {
.custom-input {
caret-shape: block;
}
}
The custom shape is applied only when the browser recognizes the declaration.
caret-shape Example
The following example requests three different caret shapes. Click inside each field to see the insertion caret.
Because caret-shape has limited browser support, the fields may display the same default caret shape even though different values are specified.
<div class="caret-shape-demo">
<label for="bar-caret">Bar Caret</label>
<input type="text" id="bar-caret" class="bar-caret" placeholder="Click to type">
<label for="block-caret">Block Caret</label>
<input type="text" id="block-caret" class="block-caret" placeholder="Click to type">
<label for="underscore-caret">Underscore Caret</label>
<input type="text" id="underscore-caret" class="underscore-caret" placeholder="Click to type">
</div>
.caret-shape-demo label {
display: block;
margin-bottom: 6px;
font-weight: bold;
}
.caret-shape-demo input {
display: block;
width: 100%;
box-sizing: border-box;
margin-bottom: 15px;
padding: 10px;
border: 1px solid #cccccc;
border-radius: 4px;
color: #333333;
background-color: #ffffff;
}
.caret-shape-demo .bar-caret {
caret-color: #1769aa;
caret-shape: bar;
}
.caret-shape-demo .block-caret {
caret-color: #008855;
caret-shape: block;
}
.caret-shape-demo .underscore-caret {
caret-color: #9933cc;
caret-shape: underscore;
}
.caret-shape-demo input:focus-visible {
outline: 2px solid #1769aa;
outline-offset: 2px;
}
Each field requests a different caret shape and color. In supporting browsers, the insertion caret changes shape when the field receives focus.
In browsers without caret-shape support, the caret colors may still change while the caret shape remains unchanged.
Important Considerations
Keep the following points in mind when using caret-shape:
- Purpose: Controls the shape of the text insertion caret.
- Initial value:
auto. - Inheritance: The property is inherited.
- Automatic shape:
autoallows the browser to choose the caret shape. - Bar shape:
barrequests a thin vertical caret. - Block shape:
blockrequests a rectangular caret. - Underscore shape:
underscorerequests a horizontal caret beneath the insertion position. - Editable content: The property is relevant to elements that display an insertion caret.
- Color: Use
caret-colorto customize the caret's color. - Animation: Use
caret-animationto control automatic blinking where supported. - Shorthand: The property is included in the
caretshorthand. - Browser support: The property has limited availability.
- Fallback: Unsupported browsers use their normal caret shape.
- Feature detection: Use
@supportswhen the custom shape is an optional enhancement. - Accessibility: Keep the insertion caret visible and easy to identify.
- Focus: The caret does not replace a visible keyboard focus indicator.
- Mouse pointer: The property does not change the mouse cursor.
- Text selection: The property does not change the appearance of selected text.
- Testing: Check the result in the browsers and operating systems your website supports.
Use caret-shape as a progressive enhancement for specialized editing interfaces, while preserving a usable default insertion caret in unsupported browsers.
Browser Support
The caret-shape property has limited availability. Some browsers may not recognize the property and will continue displaying their default insertion caret shape.
Support is less widespread than for caret-color, which can be used independently to customize the caret's color.
Feature Detection
@supports (caret-shape: block) {
.custom-input {
caret-shape: block;
}
}
Compatible Fallback
.custom-input {
caret-color: #1769aa;
}
@supports (caret-shape: block) {
.custom-input {
caret-shape: block;
}
}
The caret color remains available in browsers supporting caret-color, even when the requested shape is unavailable.
Testing Recommendation
Test each shape in the target browsers. Recognition of the property does not necessarily guarantee identical visual rendering across platforms.
For current compatibility details, visit Can I Use? and search for caret-shape.
Specifications
The caret-shape property is defined in CSS Basic User Interface Module Level 4.
CSS Basic User Interface Module Level 4
The specification defines the available caret shapes and how they apply to text insertion indicators.
