Home >Web Front-end >CSS Tutorial >Tips and methods to use CSS to achieve image bubble effects
Techniques and methods of using CSS to achieve image bubble effects
In web design, adding special effects to images is one of the important means to improve user experience. Among them, picture bubble effects can add interest and interactivity to pictures, making web content more attractive. This article will share some tips and methods for using CSS to achieve image bubble effects, with specific code examples.
<div class="container"> <img src="image.jpg" alt="Image"> <div class="bubble"></div> </div>
.container { position: relative; display: inline-block; } .bubble { position: absolute; top: -30px; left: 50%; transform: translateX(-50%); width: 100px; height: 30px; background-color: #fff; border-radius: 15px; border: 1px solid #ccc; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); }
In this example, we set relative positioning to the parent element containing the image, and added a div# with the
bubble class name inside it. The ## element serves as a pseudo-class element. By setting
position: absolute;, the pseudo-class element is positioned relative to the parent element, and through the
top,
left and
transform attributes to adjust its position. At the same time, we also set its background color, border, rounded corners and shadow styles to achieve the effect of bubbles.
<svg width="150px" height="150px" viewBox="0 0 150 150"> <path fill="#fff" d="M50 100C50 100 0 120 0 140C0 160 30 160 50 140C70 160 100 160 100 140C100 120 50 100 50 100Z" /> </svg>
.bubble { /* 省略其他样式 */ opacity: 0; transition: opacity 0.3s ease; } .container:hover .bubble { opacity: 1; }
opacity property to 0. Then, use the CSS transition effect attribute
transition to gradually change the
opacity attribute value of the bubble within 0.3 seconds to achieve a gradual animation effect. Finally, by setting the
.container:hover .bubble selector, when the mouse hovers over the container containing the image, change the
opacity property value of the bubble to 1 to make it appear come out.
The above is the detailed content of Tips and methods to use CSS to achieve image bubble effects. For more information, please follow other related articles on the PHP Chinese website!