What is the best way to conceal content I've created with the :before pseudo selector if the corresponding HTML text element is empty?

Currently, I am working on designing a chat bubble using CSS and HTML.

If the chat bubble does not contain any text, I prefer that it remains hidden from view.

I have managed to make the body of the chat bubble disappear when empty by utilizing the :empty pseudo-class. However, I am facing difficulty in making the triangle part of the chatbox vanish as it is created using the :before pseudo-selector with content: ''.

Below you can find my code snippet. Do you know of any way to hide this little triangle if the h4 element is without content?

/* CSS talk bubble */

.myFeedback h4:empty {
  display: none;
}

.talk-bubble {
  margin-top: 220px;
  left: 390px;
  display: inline-block;
  position: absolute;
  width: 200px;
  height: auto;
  background-color: purple;
}

.round {
  border-radius: 10px;
}

.tri-right.right-in:before {
  content: '';
  position: absolute;
  width: 0;
  height: 0;
  left: auto;
  right: -20px;
  top: 38px;
  bottom: auto;
  border: 12px solid;
  border-color: purple transparent transparent purple;
}


/* talk bubble contents */

.talktext {
  padding: 10px;
  text-align: center;
  line-height: 1.5em;
}

.talktext p {
  /* remove webkit p margins */
  -webkit-margin-before: 0em;
  -webkit-margin-after: 0em;
}


/* end of talk bubble stuff */
<div class="myFeedback">
  <div id='guess-feedback' class="talk-bubble tri-right round right-in">
    <h4 class="talktext"></h4>
  </div>
</div>

Answer №1

/* Create a CSS talk bubble */

h4:empty {
  display: none;
}

.talk-bubble {
  margin-top: 220px;
  left: 390px;
  display: inline-block;
  position: absolute;
    width: 200px;
    height: auto;
    background-color: purple;
}

.round{
  border-radius: 10px;
}

.tri-right.right-in h4:before {
    content: '';
    position: absolute;
    width: 0;
    height: 0;
  left: auto;
    right: -20px;
  top: 38px;
    bottom: auto;
    border: 12px solid;
    border-color: purple transparent transparent purple;
}
/* Add text to the talk bubble */
.talktext{
  padding: 10px;
    text-align: center;
  line-height: 1.5em;
}
.talktext p{
  /* Remove webkit p margins */
  -webkit-margin-before: 0em;
  -webkit-margin-after: 0em;
}

/* End of talk bubble styling */
 <div class="myFeedback">
  <div id='guess-feedback' class="talk-bubble tri-right round right-in">
    <h4 class="talktext"></h4>
  </div>
</div>

 <div class="myFeedback">
  <div id='guess-feedback' class="talk-bubble tri-right round right-in">
    <h4 class="talktext">With Text</h4>
  </div>
</div>

Modify the following code in your CSS

Replace:

.tri-right.right-in:before 

With:

.tri-right.right-in h4:before 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Top method for analyzing HTML code within Android applications

As I work on an application for my parents' restaurant, I encounter a situation where clients can access a private area on the website to view their account status post-login. After successfully logging in, I am presented with the private HTML conten ...

Encourage the user to download and install the ActiveX/.Net Class Library directly from their browser

When I developed a .NET Class Library project that interacts with mshtml.HTMLDocument from JavaScript, everything functioned correctly on my local machine after adjusting permissions in .NET Configuration for Trusted Sites to grant Full access. However, I ...

The process of increasing value through a function in PHP

Looking to create a function that increments a variable when a button is clicked. I have 4 buttons: farm, cave, house, casino The goal is to accumulate the random numbers generated by each button and add them up in the "YOUR GOLD" section. For example, c ...

JQuery HTML not functioning properly with dynamically inserted elements

Initially, I perform this action var columns = row.getElementsByTagName('td'); columns[2].innerHTML ='<Button class="btn btn-success" style="margin-right:4px;" onclick="savecategory(this.parentNode,this)"> ...

activating the submit button depending on the user input

My goal is to create a form with a textarea and a submit button that can be enabled or disabled based on whether there is any input in the textarea. I have confirmed that both the submit button and content are being selected correctly (I'll provide a ...

What is the best way to reposition an element to the end of its flexbox container?

I have a flexbox-container that contains multiple details-elements with flex-wrap behavior. My goal is to have a details-element move below the other elements within the container when it is opened. For example, I want it to go from this initial layout: ...

Troubleshooting jQuery: Unable to refresh the webpage

I have a PHP page that contains a form, checkboxes, and a submit button. I added an if statement to execute a PHP script I created when the button is clicked, which deletes certain values from the selected rows. The button functions properly and changes s ...

In search of a render function that can effectively apply styles to HTML strings using React JS

I have been struggling to convert the result field value, including HTML attributes string, into respective styles for one column in my antd table. Below is the string I am trying to convert: The exhaust air requirements for fume hoods will be based on m ...

Hide and reveal images on hover by using multiple images or links on a single webpage

Despite my efforts, I haven't been able to find exactly what I'm looking for. My current setup involves an image/content slider powered by Awkward Showcase. Within each slide, I want to incorporate a mouse-in/mouse-out effect that switches and hi ...

Positioning a Form with Sliding Out Effects Using CSS

Hello, I recently came across a tutorial online that helped me create a form slide effect on my website. However, the issue I'm facing is that the form is positioned to the left and I would like it to be placed on the right instead. For reference, h ...

What is causing the position-sticky feature in Bootstrap 5 to not function properly?

My attempt at creating responsive card ads with Bootstrap 5 has hit a snag. The sticky-top feature doesn't seem to work properly on the left card. Specifically, I'm aiming to have a sticky sidebar on the left, containing a menu within a grid col ...

scrape particular data from a table using scrapy

Currently, I am utilizing scrapy to extract content from a website specifically located within a particular <td> tag. The guide provided demonstrates downloading all the information, however, I only require the data from one individual <td>. As ...

When I tried to address one issue, my CSS ended up breaking. Any suggestions on how I can resolve the header

Edit: https://i.sstatic.net/kVeHk.jpg Picture 1 code: main.css body { font-family: arial; font-size: 14px; color: #1D2130; background-image: linear-gradient(top, rgb(200,225,245) 15%, rgb(240,240,240) 60%); // more CSS properties h ...

Issue with slideshow counter persisting across multiple arrays

I am experiencing an issue with my simple slideshow type application where it consists of multiple parts on the page. Each slideshow has its own array containing specific information for each slide. The problem arises when I cycle through one slideshow to ...

My ability to click() a button is working fine, but I am unable to view the innerHTML/length. What could be the issue? (NodeJS

Initially, my goal is to verify the existence of the modal and then proceed by clicking "continue". However, I am facing an issue where I can click continue without successfully determining if the modal exists in the first place. This occurs because when I ...

How can CSS grid be utilized to create columns of various widths that wrap onto a new line

Struggling with CSS grid in my new responsive design, particularly regarding wrapping. The issue is with a nav bar containing a header that needs to be aligned left and four buttons that should be aligned right. Currently, using: grid-template-columns: r ...

I'm looking to validate an input tag in HTML5 using JQuery. Can anyone help clarify this process for me?

New to JQuery and struggling with HTML5 input tag validation. On my page, I have an input tag like this: <input id="variazioneAnticipo" class="rightAlligned form-control" style="width:50%" type="number" step="0.01" /> and a button: <button id="va ...

An exclusive execution of the JavaScript function is ensured

I have a JavaScript function that I want to execute 12 times in a row. Here's my approach: Below, you'll find a list of 12 images: <img id="img1" src=""> </img> <img id="img2" src=""> </img> <img id="img3" src=""> ...

When the columns start stacking in Bootstrap, the padding is disabled

As I work on revamping my website, I have decided to incorporate the Bootstrap framework. While I can navigate through HTML coding, I still consider myself a novice when it comes to coding. The current issue I am facing involves the alignment of two colum ...

Can you help me troubleshoot an issue I'm having with a basic HTTP-GET request to a Python script hosted on my

I recently came across a tutorial (Simple URL Example:Get Method section) that caught my interest. You can find the tutorial at this link: https://www.tutorialspoint.com/python/python_cgi_programming.htm After following the instructions in the tutorial, I ...