Modify the CSS styling of a sibling element when our element contains an <a> tag

I recently acquired a log in module for DotNetNuke and I am interested in customizing its appearance. Here is the html structure generated when a user logs in to my site:

<div id="Login">
<div id="HelloLogin">Hello</div>
<a href="http://localhost:6111/en-us/userprofile.aspx">mosijava</a>
</div>

However, when the user logs out, the html changes as follows with the absence of the <a> tag:

<div id="Login">
<div id="HelloLogin">Hello</div>
guest
</div>

My goal is to apply a css style to the div with ID="HelloLogin" only when a user is logged in, indicated by the presence of the <a> tag in the html.

Is there a way to achieve this using pure css?

I attempted to use the following selector but it did not yield the desired results

#login a:before{
color:red;
}

Answer №1

Unfortunately, CSS does not have a previous sibling selector, so you cannot achieve what you attempted to do. The :before pseudo-element behaves differently than anticipated.

In this particular situation, however, you can utilize CSS3's :not() and :only-child pseudo-classes with the IDs to only match when #HelloLogin is not the sole child element in #Login (note that inline text, such as "guest," does not count as children/siblings in CSS):

#Login #HelloLogin:not(:only-child) {
    color: red;
}

The downside is that versions of IE prior to 9 do not support :not() or :only-child, leaving no alternative solution in pure CSS.

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

Problem with mobile-first flexbox navigation

I attempted to replicate the fantastic flexbox tutorials by Wes Bos. I aimed to adapt a particular tutorial he presented on creating a responsive flexbox menu. However, my approach was to design the menu with a mobile-first perspective, utilizing media que ...

Using the <mark> tag in CSS for creating padding

Struggling to utilize the padding feature in CSS effectively with the HTML below, as it only pads the first word on the left side: The current CSS code I have that's not working correctly: mark { background-color: #89ce40; color: white; opac ...

Tips for aligning content vertically in bootstrap 4

Looking to vertically center content in Bootstrap 4? <div class="container"> <div class="content"> <div class="row"> <div class="col-6 align-middle" ...

Utilizing Express for hosting public static resources

I am having trouble configuring my Express server to serve files from the public folder. Despite trying various file path combinations, Pug does not seem to cooperate with Node.js. While I can view the HTML content of the index, the CSS and music files fai ...

Altering the color scheme of a specific column within a stacked bar chart using C3.js

I'm currently facing a challenge with highlighting specific values in a c3.js stacked bar chart. While I was able to change the color of an individual bar in a non-stacked bar following this example, I'm struggling to determine how to identify th ...

CSS - when "start" is not 1, an alternative to the "nth-child" selector

Imagine a scenario where you have a list of questions that start at 0 and you want to apply CSS styling based on their value. For instance, consider the following list... <b>How are you feeling?</b> <ol start="0"> <li>Great< ...

What might be causing my CSS animations to fail on Safari for iPhone when they work correctly on all other devices?

I am currently working on developing a website using HTML and CSS (without JavaScript), and I have implemented a keyframes animation in the navigation menu that triggers on :hover and :active states to display a sub-menu. The Issue: The animation function ...

Changing the background color of a PHP input based on the webpage being viewed - here's how!

I'm in the process of creating a website where each page will have its own unique background color. Additionally, I am using a PHP input for both the header and footer sections, which need to change their background colors based on the specific webpa ...

Eliminate the inclusion of CSS files in the header section of Magento

My Magento store seems to be loading an excessive amount of CSS files: https://i.sstatic.net/geBtq.jpg I am looking to streamline this by combining all these files into just 1 or 2 CSS files. While I am aware of the merge option in Magento, it unfortunate ...

What is the best way to utilize the @Import CSS rule in my code?

After using CSS for a considerable amount of time, I have successfully utilized the @import rule in the past. However, I am currently encountering an issue with it on a new project. Is there anyone who can offer assistance? Here is the code: @import url(&a ...

Wordpress Debacle: Bootstrap Columns Clash

Currently, I am in the process of designing a unique theme. In my code, I have implemented a condition that generates an additional div with the class "row" after every three posts or columns are created. Everything seems to be working smoothly, except f ...

Tab panels are shown by Bootstrap 5 tabs

I am using Bootstrap 5 to design a layout that changes based on the width of the screen. When the screen width is greater than or equal to 992 pixels, a table is displayed in the sidebar and some text is shown in the main content area. However, if the scre ...

Utilizing Xpath and CssSelectors for identical tags, attributes, and elements

Creating Selenium tests for multiple buttons with identical code: <div class="item"> <div class="head"> <a class="title caps_style primary-color-text"href="https://www.www.www/link/link-link-complex-api-testing" target="_blank">Ra ...

What is the best way to center the 'email promo' text in the top navigation bar alongside the image and other text links?

Struggling with learning CSS and HTML, particularly when it comes to positioning elements on the page. I'm having trouble vertically aligning the elements inside the top nav bar and can't seem to figure out what I'm doing wrong. Any insights ...

When using mobile browsers and the screen width is at its maximum

I am in desperate need of help with this issue, can someone please provide guidance? I am currently working on optimizing a webpage for mobile devices using CSS media queries. In the <head> section, I have included the following: <meta name="Ha ...

Avoid activating the hover state

Is there a way to prevent a button, div, or li from hovering if it already has an active status using CSS? #list-of-tests .item-test:hover:not(.active) { background-color: #4d5f75; border: solid 1px #4d5f75; } #list-of-tests .item-test:active { ...

Displaying items side by side instead of stacking them on top of each other can be achieved by utilizing the <ul><li>

My webpage displays items vertically, but I need them to be horizontal. Which CSS element should I use to achieve this? This is the code snippet: <ul class="jobs"> <li> <img height="80px" src="http://pbs.twimg.com/media ...

Designing the Background with HTML and CSS

Can someone assist me with customizing the background of my website? I would like to achieve the following specifications: Implementing different gradient backgrounds on the left and right sides of the website, ensuring compatibility with all Internet Ex ...

The drawback of using a datepicker within an Angular input field

When attempting to open the Angular Material datepicker, it was appearing above the input field. How can I make it open below the input field instead? <mat-form-field appearance="outline" class="w-100 date-class"> < ...

Angular material chips showcase a row of five chips

I am working with the basic mat-chips from angular material and I want to arrange them in rows of five without increasing their size: Chip1 Chip2 Chip3 Chip4 Chip5 ..................(space left) Chip6 Chip7 Chip8 Chip9 Chip10 ...............(space le ...