The difference between -webkit-box-shadow and box-shadow in CSS

WBOY
Release: 2023-08-26 23:57:02
forward
1572 people have browsed it

CSS 中 -webkit-box-shadow 和 box-shadow 的区别

As we all know, CSS provides us with a wide range of properties and pseudo-classes that enable developers to add desired styles to elements. One of these properties is the BoxShadow property; it allows us to add a shadow-like effect around an element.

Box-shadow property

Box Shadow is a CSS property used to create an outer or inner shadow effect on an element. It applies one or more shadows to an element, each specified by an X and Y offset from the element, a blur radius, a diffuse radius, a color, and an opacity value.

The box-shadow property can accept multiple values, separated by commas; each value defines a shadow effect. A box shadow without any offset will make it look like a flat shape, just like when printed on paper.

Assuming that the element we want to apply box-shadow to specifies some form of borderradius, the effect of box-shadow will also have a curved border like that element. Multiple box shadows are ordered on the z-axis in the same order as multiple text shadows.

We can use - to specify a box shadow for an element -

  • Two values - Whenever we use the box-shadow property with two values, they will be used as the values ​​for the X and Y offset.

  • Three values - The first two values ​​act as X and Y offset values, while the third value is used for the blur radius effect.

  • Four values - The fourth value is considered as the value of the diffusion radius, and the remaining values ​​are the values ​​of the X offset, Y offset, and blur radius respectively.

  • Inset - It is an optional value and its presence biases the frame's shadow to one side. If we don't specify this, the shadow will appear to be raised above, like a drop shadow

  • Color - This is another optional value that sets the color of the shadow. If not specified, the color defaults to the element's current color.

Its initial value is none and applies to all elements. The animation type of the shadow list can be used for animation, but it cannot be inherited.

Example

An example of using the box-shadow property in CSS is given below.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Box Shadow</title>
   <style>
      blockquote {
         padding: 20px;
         box-shadow: inset 0 -3em 3em rgba(0, 0, 0, 0.1), 0 0 0 2px rgb(255, 255, 255), 0.3em 0.3em 1em rgba(0, 0, 0, 0.3);
      }
   </style>
</head>
<body>
   <blockquote>
      <q>
         This is an example of box shadow effect on elements <br />
         Another temporary line for extra text
      </q>
      <p>— Example of Box Shadow</p>
   </blockquote>
</body>
</html>
Copy after login

Now that we understand the box Shadow property, we'll look at what "webkit" in CSS is and why we need it. Later we will discuss webkit box Shadow.

What is webkit?

Webkit is Apple's web browser engine used by nearly all macOS applications. There are many other web browser engines, such as Gecko for Firefox, Blink for edge, and many more. So, the question arises, why do we need them.

The -webkit prefix on a CSS selector indicates properties that are only processed by that engine, similar to the -moz property. By specifying this, we are basically telling the browser to only use it when using a specific browser engine, otherwise leave it as is. It's cumbersome to use; that's why many developers want it discontinued as soon as possible.

Webkit-box-shadow property in CSS

Like the box-shadow property, the webkit-box-shadow property also adds a shadow-like effect to the frame of the element to which it is applied. However, it is important to note that its implementation is specific to browsers such as Chrome or Apple Safari.

Possible values ​​that can be assigned to this attribute are -

  • X-offset - It specifies the horizontal offset or distance to the element.

  • Y Offset - This also specifies an offset or distance, but in a vertical direction

  • Blur - It is a length value, if it is large, the blur effect created will be large, so the shadow effect will be larger, and vice versa.

Example

An example of using web kit-box-shadow in CSS is given below.

<!DOCTYPE html>
<html>
<head>
   <style>
      .BoxShadow {
         color: blue;
         border: solid 1px blue;
         margin: 1.5rem 3rem;
         -webkit-box-shadow: 5px 10px 18px red;
      }
   </style>
</head>
<body>
   <div class="BoxShadow">
      <h1>Sample text</h1>
      <p>Some more random text</p>
   </div>
</body>
</html>
Copy after login

The difference between box-shadow and webkit-box-shadow

Now that we understand these two properties, let's list the differences between them.

  • The box-shadow property is universally implemented, while "webkitbox-shadow" on the other hand only works in browsers using a specific web browser engine, i.e. Safari or Google Chrome.

  • The box Shadow property allows us to style the shadow effect in all recent versions, but if we have to work on older versions of browsers, we have to use webkit-box-shadow.

in conclusion

To summarize, the main difference between -webkit-box-shadow and box-shadow in CSS is that -webkit-box-shadow is the vendor prefix for the box-shadow property introduced by Webkit browsers. The box-shadow property allows you to apply a drop shadow effect to an element without using an image or other external resource. The -webkit-box-shadow attribute has been deprecated and replaced with standard box-shadow syntax. Because most modern browsers support it. In summary, both properties are used to create shadows on elements, but only one of them should be used as the other will be deprecated over time.

The above is the detailed content of The difference between -webkit-box-shadow and box-shadow in CSS. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template