Home > Web Front-end > CSS Tutorial > How Can I Create Rounded Outlines for Div Elements Using Box-Shadow?

How Can I Create Rounded Outlines for Div Elements Using Box-Shadow?

Barbara Streisand
Release: 2024-11-28 19:20:16
Original
260 people have browsed it

How Can I Create Rounded Outlines for Div Elements Using Box-Shadow?

Creating Rounded Outline for Div Elements with Box-Shadow

Customizing the outline appearance of a div element can enhance user experience and visual aesthetics. By employing box-shadow property, we can achieve rounded corners similar to the functionality of border-radius for the element's outline.

To illustrate this technique, consider a scenario where you have an input field with rounded borders and desire to modify the color of the outline when it receives focus. The conventional square outline can be unappealing.

Solution:

Instead of attempting to round the outline using conventional methods, leverage the box-shadow property. This solution offers a smoother, more customizable appearance. By fine-tuning the box-shadow's settings, we can create a pseudo-outline that closely mimics a rounded outline:

  input, input:focus {
    border: none;
    border-radius: 2pt;
    box-shadow: 0 0 0 1pt grey;
    outline-color: transparent; /* for high contrast modes */
    transition: .1s;
  }

  /* Smooth outline with box-shadow: */
  .text1:focus {
    box-shadow: 0 0 3pt 2pt cornflowerblue;
  }

  /* Hard "outline" with box-shadow: */
  .text2:focus {
    box-shadow: 0 0 0 2pt red;
  }
Copy after login

In this example, the input element initially has a 2pt border radius, but the box-shadow property adds a 1pt grey shadow around the element. When the element receives focus, the box-shadow increases to 3pt with cornflowerblue color for a smoother outline. Additionally, you can create a more distinct outline by setting the box-shadow to 0 0 0 2pt red.

The above is the detailed content of How Can I Create Rounded Outlines for Div Elements Using Box-Shadow?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template