How can you target an element with a specific class only if it is the lone child of its parent?

I need to target an element with a specific class and apply CSS only if it is the sole child within its parent. While jQuery can achieve this easily, I am seeking a pure CSS solution that is compatible across all major browsers. How can I write a selector expression for these elements?

For example:

<div> 
   <span class='a1'/>
</div>

<div> 
   <a>random text</a>
   <span class='a1'/>
</div>

In the above scenario, I want to select the first div containing the .a1 element because it is the only child within its parent.

Answer №1

To experience the power of .a1:only-child styling, take a look at this coding example. Keep in mind that browser support for this CSS3 property may vary depending on your definition of "major browsers", as outlined on this website.

Answer №2

Experiment with this code snippet to ensure compatibility across different web browsers:

.example:first-child:last-child {
    background-color: green;
}

Live Demo 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

I'm looking to have a span and an input side by side, both spanning the full width of the containing div. How can I achieve this?

In my code snippet below: <html> <body> <div style="width: 700px;"> <span>test</span> <input style="width: 100%;"></input> </div> </body> </html> I am facing an issue where the span and input el ...

Adjust the background color of JQuery UI modal dialog overlay in real-time

How can I dynamically change the background color of a page when a modal dialog loads, specifically targeting only one specific modal dialog? Setting the background color with CSS works fine: .ui-widget-overlay { background-color: white; } Here's ...

Utilizing the @page CSS rule in Vue.js: A step-by-step guide

I am facing an issue with printing a specific page in landscape mode. Here is the code snippet I have been using: @page { margin: 0; size: A4 landscape; } The problem is that this style rule affects all pages in my application because all style se ...

Post the HTML5 player on your Facebook feed

Currently, I have a player that exists in both Flash and HTML5 versions. When calling a page through an iframe, the browser and device are detected automatically to load the player accordingly, and this setup is functioning correctly. Prior to implementin ...

ReactJS - Issue displaying the degree symbol passed in as a string

In the process of developing my app, it retrieves data from an external source including... It is currently and &#xb0;F outside. The application then combines these variables with environmental data to form a string. This string is parsed into a comp ...

Tips for resizing an image instead of a line in HTML5 canvas

For instance, we could create a function like this to draw a line: function drawLine(g, n, x1, y1, x2, y2){ g.beginPath(); g.lineWidth = n > 0 ? n : 1; g.strokeStyle = "rgb(0, 128, 32)"; g.moveTo(x1, y1); g.lineTo(x2, y2); g.str ...

Guide on positioning a small image on top of a parent image using CSS

I have an image for a web page design, and I embedded it in an HTML file to display it. Here is the CSS code: .imageMain { position: relative; height: 100vh; /* For 100% screen height */ width: 100vw; /* For 100% screen width */ } img#homeinact ...

Getting the checkbox count value in a treeview upon successful completion of an AJAX request

After successful JSON Ajax response, a treeview structure with checkboxes is displayed. I need to capture the number of checkboxes checked when the save button is clicked. @model MedeilMVC_CLOUD.Models.UserView <script type="text/javascript"> ...

Adjust the Font Size of Bootstrap 3 across different breakpoints

I am currently using Bootstrap 3 and I have a requirement to adjust the font-size at different breakpoints across my website. My goal is to easily modify the font size in one place and have it automatically apply to all Bootstrap components. The desired b ...

javascriptretrieve the second class from the element

HTML Code : <span class="button" onclick="javascript:buttonClicked();">Log In</span> <div class="modal-bg" style="display: none;"> <div class="modal"> <span>Log In<a href="#close" class="close">&# ...

Is there a way to restrict the orientation of a UIWebView using JavaScript or HTML?

Currently, I am working on HTML content for an iPad application. The challenge at hand is locking the UIWebView's content to portrait mode. Despite having already submitted the app to Apple, I have only come across a solution in this question, which s ...

Retrieve all data points if both latitude and longitude are present in the XML dataset

XML data to retrieve details <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <results> <result> <Country_Code>IN</Country_Code> <Country_Name>India</Country_Name> <Region_Nam ...

Ensure that the width of each column in the table remains constant, unaffected by the content within

I am facing a challenge with an html table where the column is set to a fixed width using table-layout: fixed, but it still expands to accommodate text without spaces. Is there a solution for this issue other than wrapping each td content in a div? For re ...

Customized CSS for smartphones and tablets

My CSS code currently looks like this: #FSBDiv-Inner { position: fixed; top: 20%; left: 25%; width: 50%; z-index: 999; display: flex; justify-content: center; background-color: white; } However, for mobile devices, I n ...

Troubleshooting jQuery and CSS problems related to window size discrepancies

My website dynamically calculates the size of each div based on the window size... // Functions to begin $(document).ready(function(){ $('.wrapper').css({'top' : '0px','left' : '0px'}); $(&ap ...

Utilizing Thymeleaf's for each loop to print an ordered list containing an unordered list

Currently, I am working on a project that requires me to display an unordered list within an ordered list. However, the code I have only shows the first list item. Surname name Surname name etc.... The tags "getThemePark" are not empty but for some reaso ...

I am keen on eliminating the lower margin of the PIXI application

Exploring the possibilities of PIXI.js, I am working on creating a new application. However, upon adding the PIXI application to the DOM, I noticed an unwanted margin at the bottom. Here is an image illustrating the issue. Is there a way to remove this bo ...

Is it possible to combine onmouseover and onmousedown events in a web application?

This particular code functions correctly for the onMouseOver and onMouseOut events, although it seems to encounter issues with the onMouseDown event. Below is the HTML5 and JavaScript code that I have written. Any feedback on where I may be going wrong wou ...

The issue with Angular's state.go function is that it is not properly refreshing the page, resulting in

Creating a mobile exam app that features multiple choice questions with previous and next question buttons. The transition between questions is handled using $state.go to load the same HTML page but with new questions and answer choices. // Within a servi ...

Conceal the top row of the second HTML table that does not contain any class or ID attributes

I have a scenario where I am working with two HTML tables within an iframe tag. These tables do not have any class or id attributes specified. My goal is to use jQuery to locate the first row of the second table and hide it. The structure of my tables is a ...