Home Web Front-end Front-end Q&A How to achieve a simple bubble effect in css

How to achieve a simple bubble effect in css

Apr 21, 2023 am 11:24 AM

Bubbles are a common UI design element that can be used to emphasize information or prompt users, increasing the interactivity and aesthetics of the page. In this article, we will introduce how to use HTML and CSS to implement a simple bubble effect.

Step 1: HTML structure

First, we need to give the bubble a container div, and then place a text content element inside the container. The following is the HTML code:

<div>
  <p>这是气泡内容</p>
</div>

Step 2: CSS style

Next, we need to style the bubble container. In this process, we generally need to consider the following aspects:

  1. Background color and border color
    The color of the bubble needs to be distinguished from the background color of the page, and generally a brighter one will be chosen Color to add visual effect. At the same time, the border color also needs to be set to distinguish it from the background.
  2. Bubble size and position
    The size of the bubble can be adjusted as needed, and is generally slightly larger than the text content. In addition, the position of the bubble needs to consider the alignment relationship with the text content, and is generally placed above or below the content.
  3. Bubble shape
    The shape of the bubble can be adjusted as needed. The more common shapes are rounded rectangle and triangle.

The following is the CSS code, we will explain the details step by step:

.bubble-container {
  position: relative;
  display: inline-block;
  padding: 8px 12px;
  border-radius: 5px;
  background-color: #00BFFF;
  color: #fff;
  font-size: 14px;
  line-height: 1.4;
}

.bubble-container:before {
  content: "";
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-left: -10px;
  border-width: 10px;
  border-style: solid;
  border-color: transparent transparent #00BFFF transparent;
}

First, we set the style of .bubble-container, including background color, font color, font Size etc. In addition, we use the padding attribute to determine the size of the bubble and the distance between the text content.

Then, we use the pseudo-class :before to create the triangular part of the bubble. Specifically, we position the triangle by setting the bottom and left attributes, and then adjust the position through the margin-left attribute. Finally, we use the border-width, border-style, and border-color properties to set the size and color of the triangle.

Step 3: Complete code

Finally, we combine HTML and CSS to get the complete bubble effect code:

HTML code:

<div>
  <p>这是气泡内容</p>
</div>

CSS code:

.bubble-container {
  position: relative;
  display: inline-block;
  padding: 8px 12px;
  border-radius: 5px;
  background-color: #00BFFF;
  color: #fff;
  font-size: 14px;
  line-height: 1.4;
}

.bubble-container:before {
  content: "";
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-left: -10px;
  border-width: 10px;
  border-style: solid;
  border-color: transparent transparent #00BFFF transparent;
}

Through the above code, we can achieve a simple bubble effect, as shown below:

How to achieve a simple bubble effect in css

Conclusion

Through the introduction of this article, I believe you have already understood how to use HTML and CSS to achieve a simple bubble effect. Of course, in addition to the basic rounded rectangle and triangle shapes, you can also use more CSS techniques to create more colorful bubble effects to make the page more vivid and interesting.

The above is the detailed content of How to achieve a simple bubble effect in css. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1596
276
What is the purpose of the rel attribute in a link tag in HTML? What is the purpose of the rel attribute in a link tag in HTML? Aug 03, 2025 pm 04:50 PM

rel="stylesheet"linksCSSfilesforstylingthepage;2.rel="preload"hintstopreloadcriticalresourcesforperformance;3.rel="icon"setsthewebsite’sfavicon;4.rel="alternate"providesalternateversionslikeRSSorprint;5.rel=&qu

What is the purpose of the anchor tag's target attribute in HTML? What is the purpose of the anchor tag's target attribute in HTML? Aug 02, 2025 pm 02:23 PM

ThetargetattributeinanHTMLanchortagspecifieswheretoopenthelinkeddocument.1._selfopensthelinkinthesametab(default).2._blankopensthelinkinanewtaborwindow.3._parentopensthelinkintheparentframe.4._topopensthelinkinthefullwindowbody,removingframes.Forexte

Building Custom, Reusable Hooks in React Building Custom, Reusable Hooks in React Aug 03, 2025 pm 04:51 PM

AgoodcustomhookinReactisareusablefunctionstartingwith"use"thatencapsulatesstatefullogicforsharingacrosscomponents;itshouldsolveacommonproblem,beflexiblethroughparameterslikeuseFetch(url,options),returnaconsistentstructuresuchasanarrayorobje

How to use CSS gradients for backgrounds How to use CSS gradients for backgrounds Aug 17, 2025 am 08:39 AM

CSSgradientsprovidesmoothcolortransitionswithoutimages.1.Lineargradientstransitioncolorsalongastraightlineusingdirectionsliketobottomorangleslike45deg,andsupportmultiplecolorstopsforcomplexeffects.2.Radialgradientsradiatefromacentralpointusingcircleo

How to use del and ins tags in HTML How to use del and ins tags in HTML Aug 12, 2025 am 11:38 AM

Thetagisusedtomarkdeletedtext,optionallywithdatetimeandciteattributestospecifywhenandwhythedeletionoccurred.2.Thetagindicatesinsertedcontent,alsosupportingdatetimeandciteforcontextabouttheaddition.3.Thesetagscanbecombinedtoshowdocumentrevisionsclearl

How to create a responsive testimonial slider with CSS How to create a responsive testimonial slider with CSS Aug 12, 2025 am 09:42 AM

It is feasible to create a responsive automatic carousel slider with pure CSS, just combine HTML structure, Flexbox layout, and CSS animation. 2. First build a semantic HTML container containing multiple recommendation terms, each .item contains reference content and author information. 3. Use the parent container to set display:flex, width:300% (three slides) and apply overflow:hidden to achieve horizontal arrangement. 4. Use @keyframes to define a translateX transformation from 0% to -100%, and combine animation: scroll15slinearinfinite to achieve seamless automatic scrolling. 5. Add media

How can you make an HTML element editable by the user? How can you make an HTML element editable by the user? Aug 11, 2025 pm 05:23 PM

Yes, you can make HTML elements editable by using the contenteditable attribute. The specific method is to add contenteditable="true" to the target element. For example, you can edit this text, and the user can directly click and modify the content. This attribute is suitable for block-level and in-line elements such as div, p, span, h1 to h6. The default value is "true" to be editable, "false" to be non-editable, and "inherit" to inherit the parent element settings. In order to improve accessibility, it is recommended to add tabindex="0&quo

How to use CSS selectors effectively How to use CSS selectors effectively Aug 11, 2025 am 11:12 AM

When using CSS selectors, low-specific selectors should be used first to avoid excessive limitations; 1. Understand the specificity level and use them reasonably in the order of type, class, and ID; 2. Use multi-purpose class names to improve reusability and maintainability; 3. Use attributes and pseudo-class selectors to avoid performance problems; 4. Keep the selector short and clear scope; 5. Use BEM and other naming specifications to improve structural clarity; 6. Avoid the abuse of tag selectors and:nth-child, and give priority to the use of tool classes or modular CSS to ensure that the style is controllable for a long time.

See all articles