Adjust the style of one element when another element is in focus

How can I get this to function properly? HTML

#nav input[type=text]:focus ~ #two {
   color: red;
}
<div id=nav>
  <input type="text" placeholder="Search" name="search">
</div>
<div id="two">Two jfjsoijfoisoivnosnovdnsnnoivnoinsionvonoiniso</div>

I believe the issue may be related to the individual div elements.

Answer №1

#nav:focus-within ~ #two {
  color: red;
}
<div id="header">
  <input type="text" placeholder="Search here" name="search" />
</div>
<div id="content">Content goes here</div>

This styling can be applied using the :focus-within pseudo-class.

Answer №2

Unfortunately, it is not possible to do so unless the element is a sibling or child of the :focus.

To achieve this effect, you can either utilize Javascript or place the div inside the nav element, although this may not align with your desired DOM structure.

#nav input[type=text]:focus~#two {
  color: red;
}
<div id=nav>
  <input type="text" placeholder="Search" name="search">
  <div id="two">Two jfjsoijfoisoivnosnovdnsnnoivnoinsionvonoiniso</div>
</div>

Javascript Solution :

var navbar = document.getElementById('nav')

navbar.addEventListener('focusin', toggleClass);
navbar.addEventListener('focusout', toggleClass);

function toggleClass() {
  document.querySelector('.two').classList.toggle('redColor');
}
.redColor {
  color: red;
}
<div id=nav>
  <input type="text" placeholder="Search" name="search">
</div>
<div class="two">Two jfjsoijfoisoivnosnovdnsnnoivnoinsionvonoiniso</div>

Answer №3

Cascading Style Sheets (CSS) does not have the ability to analyze the Document Object Model (DOM) tree like jQuery can, making it unable to parse parent elements.

The `~` operator will only function properly if your element is located at the same depth in the DOM.

<div id=nav>
    <input type="text" placeholder="Search" name="search">
    <div id="two">Two jfjsoijfoisoivnosnovdnsnnoivnoinsionvonoiniso</div>
</div>

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

What is the best way to create a flawless circular image with a card in Bootstrap 5?

For my project, I incorporated Bootstrap version 5 cards. I attempted to create a perfect circle image by utilizing the Bootstrap class "rounded-circle", however, the resulting image ended up appearing oval in shape. Below is the code snippet: <img src ...

The html element is failing to adjust its height to match the entirety of the browser window, even when set to

After inspecting my HTML element, I noticed that it isn't occupying 100% of the browser view, even though I have set the height to 100% in my CSS file. I tested it in both Chrome and Firefox, and the issue persists. I suspect that the browser is addin ...

Tips for ensuring the footer remains at the bottom of the page

I'm struggling with positioning my footer at the bottom of the page. No matter what I try, it just doesn't look right. Here's the code snippet I've been working with: <div id="footer"> <div class="container"> <div ...

Issue arises when CSS selectors do not function properly with absolutely positioned divs

I am experiencing an issue with my CSS code that is functional on CodePen but not on my actual website. I am attempting to target the .appraisals class for manipulating the background color of an opaque screen slider containing information. Despite using a ...

Unable to generate a navigation panel using HTML/CSS and jQuery

I recently completed the basic courses in HTML/CSS, JavaScript, jQuery, and PHP on Codecademy. I'm currently working on creating a website using HTML/CSS and jQuery in Codecademy's codebits. However, I'm facing some issues with my navigation ...

The counter variable does not function properly within a setInterval function

I have encountered an issue with my scroll-counter variable inside the setInterval function. It increments by 1 each time the setInterval function runs, which is supposed to happen 20 times before stopping. However, I noticed that the value of this variabl ...

Trouble arises when attempting to include numerous Visualforce components on a single page

When using a visualforce component multiple times on a page, the hover function does not open any of the dialogs. The problem seems to be related to the ids but I'm unsure about how to resolve it. Below is an example of the code for the visualforce c ...

Get key values in Javascript/JSON by the maximum value of the key in the property

The code presented here filters JSON data to extract all "CDNAME" values associated with the key-value pair "AREAID":"area4". These values are then displayed within a div bordered in blue. If the area identifier is changed to area3, the corresponding maxim ...

Is there a way to make my Bootstrap carousel display three cards at a time in a continuous cycle?

I currently have a Bootstrap carousel implemented on my website, but I am encountering an issue where only one card is displayed at a time within the carousel. I am looking to modify the carousel so that it displays 3 cards simultaneously. Additionally, I ...

How can we align images above one another in a responsive manner using Bootstrap 3?

Looking for assistance with aligning and overlapping images on top of another image in Bootstrap while maintaining responsiveness. I've attempted the following: HTML: <div class="container"> <div class="row"> <div class="col-lg-12"> ...

issues with responsive mobile navigation

Greetings! I've been diligently working on a website project for a client, but I have a nagging feeling that I may have overlooked something important. If you'd like to take a look at the small design mockup preview I'm providing for the cl ...

Cause a bootstrap column to have overflowing content rather than expanding

I've searched extensively for the solution to my problem, but haven't found a satisfactory answer yet. My goal is to create a location finder with a map on the right and a list on the left. With over 18,000 locations to search through, the list ...

Performing Jquery functions on several elements at once

Looking at the code snippet below, there are two buttons and an input in each container. The input calculates and adds up the number of clicks on the 2 buttons within the same container. However, it currently only works for the first container. How can thi ...

Does CSS in the current IE9 beta allow for text shadows to be implemented?

Text shadows look great in Firefox and Chrome. For example, I have a basic website for streaming videos. Unfortunately, there are no shadows in IE9 for me. This is my CSS code: p { color: #000; text-shadow: 0px 1px 1px #fff; padding-bottom: 1em; } ...

Align FontAwesome icons of various sizes vertically in a bullet-point list

Our website is built using Bootstrap v4.6.2 and FontAwesome 5.15.4. The following HTML code snippet displays an unordered list with FontAwesome icons of different sizes (0.8em vs 0.44em). <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/ ...

Open the HTML document and access the file contents

I'm having trouble reading a text file from my computer onto a website. It works fine in Internet Explorer, but won't work in Chrome. I don't have much experience with HTML, so any help would be much appreciated! <html> <body> ...

When attempting to change the text in the textarea by selecting a different option from the dropdown list, the text is not updating

I am facing an issue with three multi-select dropdown lists. When a user selects options from these lists and then presses the submit button, the selected text should display in a textarea. However, if the user manually changes some text in the textarea ...

Email Embedder - Incorporating HTML source code into email communications

I've encountered an issue where the CDNs (Bootstrap and FontAwesome) do not work when I embed my HTML source code into an email. Despite using inline styling throughout, the receiver still faces this problem. Any insights on why the CDNs are failing t ...

Turn off HTML display for Internet Explorer users

I am looking to implement code that will block certain pages for Internet Explorer users, making it accessible only for Google Chrome and Firefox users. Do you have any suggestions on how I can achieve this or if there are existing solutions available? I& ...

A demonstration of VueJS Nested Builder functionality

I am currently exploring VueJS and working on a project to create a simple page builder application that allows users to add sections and subsections. I am seeking guidance on how to properly set up the Vue data property for this functionality. Any advice ...