:using the :empty pseudo-class to style dynamic content

In my research, I came across the sitepoint page and also the quirksmode page discussing the new :empty pseudoclass.

Sitepoint mentioned that the empty style persists even when dynamic content is added, with Firefox being the browser known for behaving this way.

On the other hand, Quirksmode stated that the empty state gets discarded once it's filled in with text or elements. While testing their demo on Chrome 19, everything worked fine for me. This led me to assume that only Firefox was experiencing issues.

However, I encountered a problem within my plugin code where I dynamically populate a list with items. Despite trying to append list items in this fiddle, they don't show up until inspected via the console (strangely appearing when clicking on the <li> in the element tree).

I'm curious as to why this behavior is happening and whether there's a workaround to effectively remove the empty style.

While I realize there are alternative methods to achieve what I'm attempting in the fiddle (currently using one of them), the simplicity of the :empty method appeals to me.

UPDATE:

I've implemented a button to remove items. Ideally, when the last item is removed, the list should disappear - but it still doesn't work. I'll test it in another browser to see if there's any difference.

FIX

A temporary fix or alternative to using :empty and display:none would be adjusting the element to have zero values for width, height, borders, margins, and paddings. Additionally, apply position:absolute to take it out of the flow.

Answer №1

The solution you shared is effective for Firefox 10 and Internet Explorer 9, but it encounters issues in Chrome (version 18 and above).

It remains unclear why this discrepancy exists, especially since the quirksmode page operates smoothly on Chrome.

In regard to implementing the force-discard feature, it appears achievable by adjusting almost any CSS property through code.

You can see an example at this link.

Update:

I stumbled upon a Chrome bug report related to the misbehavior of the :empty selector when combined with display:none.

This issue has reportedly been resolved, but there might be cause to reconsider it.

To demonstrate functionality without utilizing display:none (instead opting for changing background color), check out this test: here.

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

Is there a significant time-saving benefit to utilizing nested rules in CSS, particularly when using HAML or LESS?

Here is how I am currently coding my CSS: Regular CSS: #content { color: white; background: black; } #content a { color: yellow; } #content p { margin: 0; } #content (etc...) #contnet (etc...) #content (etc...) (I always include the parent eve ...

Adjusting the overflow of a parent div based on the position of the div within it by scrolling

I'm trying to create a page with 3 different sections: <div class="container" id="main-container"> <div class="section" id="profile"> Hello World </div> <div class="section" id="projects"> Hello World 2 ...

IE9 Z-index Issue

Currently, I am facing an issue with two images not displaying correctly on a website that I am working on. The problem seems to be specific to IE 9, as other browsers are working fine. The trouble arises when I try to create a slideshow with a shadow back ...

Is there a way to trigger an animation to play in reverse without using a selector and then have it play normally on the :active state?

Trying to achieve the same animation effect using different selectors has been a bit challenging for me. Specifically, when I attempt to utilize the animation without a selector and then with the selector :active, the browser only plays the animation once ...

Checkbox failing to trigger

Check out my code snippet here: http://jsfiddle.net/raficsaza/mgukxouj/ I'm facing an issue with the checkbox in this code. It seems to be stuck at a false value and I can't get it to change. Is there a way to work with the "input" tag instead o ...

Unfold an HTML element and reveal a sneak peek of its content with the aid of JQuery

I need a way to organize and display a set of 5 lines of text vertically. Specifically, I want them arranged like this: 1) Line one 2) Line two 3) Line three 4) Line four 5) Line five However, I have a special requirement where only the first ...

In order to design a v-btn-toggle with vertically aligned buttons, rather than horizontally

I'm currently in the process of developing a quiz using VueJS and Vuetify. My challenge lies in organizing the answer options vertically for the user to select. I attempted to utilize the v-btn-toggle component, but encountered an issue where the butt ...

Using Django variable in li a:nth-child selector produces unexpected results

Currently, I am honing my web programming skills by working on a personal blog in Django. However, I have encountered a hurdle. My intention was to enable the webpage to recognize which menu element is active by passing an argument from views.py to the tem ...

The line spacing is not functioning as expected

I'm looking to vertically align my text with my logo in a seamless manner. However, whenever I adjust the line height, it affects the positioning of my logo as well. Is there a way to only modify the text position while keeping the logo centered? Chec ...

Style Guide for optimum class arrangement in Bootstrap framework

I've come across various Style Guides that discuss declaration and attribute ordering, but surprisingly none that specifically address class ordering. For instance, one developer's code looks like this: <div class="btn btn-primary btn-lg mt- ...

What is the best way to resize images while also maintaining their ability to transition on hover?

Having an interesting dilemma here.. I'm trying to utilize this CSS code: .custom-forums { height: auto; width: 100%; border-image-source:transparent url("../images/forums.png") center top no-repeat; } in combination with: .custom-forum ...

Fixed bar on top, revealed only when the scroll attempts to conceal it

I have placed a menu on the top section of my website, but not at the very top. I would like it to stick to the top of the page when the user scrolls down and disappears from view. However, if the title is still visible, I want the menu to remain below it ...

The function to modify text color in Yii2 using the material bootstrap library is not functioning as intended

My attempt to modify the text color or background of the footer has been unsuccessful after integrating this library. I am unable to alter even a single text color. This is my MaterialAsset: <?php /** * @link http://www.yiiframework.com/ * @copyrigh ...

Identify the CSS Framework being used in conjunction with Selenium

I have developed a program that crawls through various web pages and conducts tests using Selenium. My current task is to identify which CSS Frameworks are utilized on these websites for statistical analysis. Currently, I am using the FireFox Webdriver to ...

Divide a single image into several smaller images and enable clickable links on each separate image, similar to an interactive image map

Challenge: I am faced with the task of splitting a large image (1920px x 1080px) into smaller 40px by 40px images to be displayed on a webpage. These smaller images should come together seamlessly to recreate the original full-size image, with 48 images a ...

Visual button spacing

There is no CSS styling applied to the page. When two image buttons are placed in separate lines of code like this: <asp:ImageButton ID="btnVoteUp" Height="16px" Width="16px" runat="server" ImageUrl="images/thumbs_up.gif" CausesValidation="false" Co ...

The small screen @media query is malfunctioning and not displaying correctly

After creating an HTML file and CSS file, I have encountered a styling issue. When viewing the output on a larger screen, the text "I LOVE CODING, I WILL BECOME AN EXPERT." should be displayed in blue on a red background. However, on smaller screens, it ne ...

Resizing and adaption of Div elements

There's a challenging issue I've been facing and I can't find a way around it. What I'm trying to achieve is a responsive design where both the menu and text resize seamlessly when the resolution changes or when there's a zoom in/ ...

Unable to view indicators and slides are not transitioning in Bootstrap carousel

I am in the process of developing a website with bootstrap, but I am encountering issues with the carousel feature. Despite copying the code directly from their official page, the carousel remains non-functional. The indicators are missing, and the slides ...

Is it necessary to adjust my font stack for optimal viewing on a high DPI tablet or computer screen?

My CSS includes the following font definition: html { font-size: 95%; font-family: Arial, Helvetica, sans-serif } I want my application to display well on PC, iOS, and Android tablets. I am aware that different devices have varying resolutions. C ...