
- CSS - Home
- CSS - Roadmap
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Types
- CSS - Measurement Units
- CSS - Selectors
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS - Cascading
- CSS - Universal Selectors
- CSS - ID Selectors
- CSS - Group Selectors
- CSS - Class Selectors
- CSS - Child Selectors
- CSS - Element Selectors
- CSS - Descendant Selectors
- CSS - General Sibling Selectors
- CSS - Adjacent Sibling Selectors
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Advanced Features
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS Interview Questions
- CSS Online Quiz
- CSS Online Test
- CSS Mock Test
- CSS - Quick Guide
- CSS - Cheatsheet
- CSS - Properties References
- CSS - Functions References
- CSS - Color References
- CSS - Web Browser References
- CSS - Web Safe Fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - Focus Effects
CSS focus effects are used to make form elements like input fields, buttons, and links more dynamic and engaging for users interacting with the webpage.
The :focus pseudo-class in CSS is used to target an element when it receives focus (by clicking on it or by pressing tab). Its purpose is to apply styles or trigger specific behaviors to enhance the user experience or provide additional visual feedback.
The :focus is a tool to make interactive elements more dynamic and engaging, especially when users navigate using the keyboard.
Table of Contents
What is Focus Pseudo-Class?
- In CSS, the pseudo-class :focus is a type of selector used to target and style an element when it gains focus, usually through keyboard navigation or mouse interaction.
- Focus effects are mostly used with interactive elements like form fields, buttons, etc., to provide users a clear indication of the focused element.
- Focus effects are useful to add a dynamic and engaging look to a website and improve accessibility.
CSS Focus Effect on Input Field
Here is an example of styling input fields when they receive focus:
Example
<!DOCTYPE html> <html> <head> <style> .input-field { width: 80%; height: 30px; padding: 5px; margin: 10px 0; background-color: #f0f0f0; border: 1px solid black; font-size: 16px; outline: none; } .input-field:focus { border: 3px solid darkblue; padding: 10px; } </style> </head> <body> <div class="main"> <input type="text" class="input-field" placeholder="Enter your name" tabindex="0"> <input type="text" class="input-field" placeholder="Enter your email" tabindex="1"> <input type="text" class="input-field" placeholder="Enter your password" tabindex="2"> </div> </body> </html>
CSS Button With Focus Effect
Here is an example of styling a button in a focused state.
Example
<!DOCTYPE html> <html> <head> <style> button { padding: 10px 20px; margin: 10px; background-color: #ffffff; color: #228B22; border: 2px solid #228B22; border-radius: 5px; text-decoration: none; font-size: 16px; font-weight: bold; transition: background-color 0.3s, color 0.3s; } button:focus { background-color: #FFCC33; color: #ffffff; outline: none; transform: scale(1.2); } </style> </head> <body> <button> Focus on me! </button> </body> </html>
CSS Border With Focus Effect
Here is an example that shows how the border is changing when the element receives focus:
Example
<!DOCTYPE html> <html> <head> <style> div { padding: 10px 20px; margin: 10px; background-color: #228B22; color: #ffffff; border: 2px solid #228B22; border-radius: 5px; text-decoration: none; font-size: 16px; font-weight: bold; transition: all 0.3s; } div:focus { border-radius: 20px; outline: none; } </style> </head> <body> <div tabindex="0"> Focus on me! </div> </body> </html>
CSS Box-Shadow With Focus Effect
Here is an example, where box-shadow is added when the div receives focus:
Example
<!DOCTYPE html> <html> <head> <style> div { padding: 10px 20px; margin: 10px; background-color: #228B22; color: #ffffff; border: 2px solid #228B22; border-radius: 5px; text-decoration: none; font-size: 16px; font-weight: bold; transition: all 0.3s; } div:focus { box-shadow: 20px 20px 10px grey; outline: none; } </style> </head> <body> <div tabindex="0"> Focus on me! </div> </body> </html>
CSS Styling on Focusing
Here is an example, where a shadow effect is given to a button on focus:
Example
<!DOCTYPE html> <html> <head> <style> body { height: 300px; overflow: hidden; display: grid; justify-content: center; align-items: center; } .glow { padding: 10px; width: 250px; height: 50px; border: none; outline: none; color: #fff; background: #111; cursor: pointer; position: relative; z-index: 0; border-radius: 20px; } .glow:before { content: ''; background: linear-gradient(60deg, #ff0000, #ff7300, #fffb00, #48ff00, #00ffd5, #002bff, #7a00ff, #ff00c8, #ff0000); position: absolute; top: -4px; left:-4px; background-size: 400%; z-index: -1; filter: blur(15px); width: calc(100% + 6px); height: calc(100% + 6px); animation: glowing 20s linear infinite; opacity: 0; transition: opacity .3s ease-in-out; border-radius: 10px; } .glow:focus:before { opacity: 1; } .glow:after { z-index: -1; content: ''; position: absolute; width: 100%; height: 100%; background: #111; left: 0; top: 0; border-radius: 10px; } @keyframes glowing { 0% { background-position: 0 0; } 50% { background-position: 400% 0; } 100% { background-position: 0 0; } } </style> </head> <body> <button class="glow" type="button" tabindex="0"> FOCUS ON ME! </button> </body> </html>