Modify the color of the legend when the input is selected

Can I change the color of legend text with CSS when an input is focused, similar to how fieldsets work?

Fieldsets change color when their respective input fields are focused. I want to achieve the same effect for legends.


                    fieldset {
                        border-radius: 0.6vh;
                        border: 1vh solid black;
                        padding: 1.68vh;
                    }

                    fieldset:focus-within {
                        border-color: blue;
                    }

                    /* legend does not change color when input is focused */

                    legend:focus-within {
                        color: blue;
                    }

                    legend {
                        padding: 0 1.2vh;
                        color: black;
                    }

                    input {
                        border: none;
                        padding: 0.29vh;
                    }

                    input::placeholder {
                        color: black;
                    }

                    input:focus {
                        outline: none;
                    }
                
            

                    <fieldset>
                        <legend>Name</legend>
                        <input type="text" placeholder="Your name">
                    </fieldset>
                
            

Answer №1

To change the color of the legend text when the input field is focused, you must first target the event from the input focus, then use the > selector to style the legend text.

fieldset {
    border-radius: 0.6vh;
    border: 1vh solid black;
    padding: 1.68vh;
}

fieldset:focus-within {
    border-color: blue;
}

/* Change color of legend when input is focused */
fieldset:focus-within > legend {
    color: blue;
}

legend {
    padding: 0 1.2vh;
    color: black;
}

input {
    border: none;
    padding: 0.29vh;
}

input::placeholder {
    color: black;
}

input:focus {
    outline: none;
}
<fieldset>
  <legend>Name</legend>
  <input type="text" placeholder="Your name">
</fieldset>

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 process for reinserting a list item into the nested elements?

Looking for help with manipulating a menu in HTML? I have a menu that I want to modify by removing and adding list items. While I've had success removing items, I'm struggling to properly use the add method. Here's an example of what my menu ...

How do I utilize the Material-UI slider's activated (CSS API) feature to conceal the shadows?

Can someone please explain to me how I can use the activated class to change the style of the thumb and hide its shadows? According to the official website, the activated class is applied to the track and thumb elements to trigger JSS nested styles when a ...

Preloading jQuery Images in Relation to the Present Document

I am looking to create a custom jQuery image preloader that dynamically loads images based on the current document. My plan is to use a jQuery script that combines the current document name and an adjustable imagename/extension. For example, if the curre ...

Utilize dropdown selections to generate a custom function for calculating prices using JavaScript

Currently, I am facing a challenge in writing the calcTotal function as I am uncertain about how to distinguish between the selected pizza and the quantity of pizzas requested. My goal is to assign specific prices to each type of pizza (Cheese $6, Pepperon ...

Tips for creating a layout with two responsive columns side by side: one with text and the other with an image, using Bootstrap 4

How can I create two responsive columns in Bootstrap 4, one with text and the other with an image, similar to the layout shown in the image below? <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" int ...

Move a single <div> element according to the screen resolution

I'm a beginner in coding and struggling with a specific problem. I need to adjust the position of a container based on screen size. For large and mid-sized screens, the container needs to be left-justified with a small amount of padding from the left ...

Enhancing jQuery Rating Plugin

Currently, I am working on customizing the jQuery Bar Rating System Plugin. You can view an example of the plugin by visiting this link: . The rating system on my end will resemble Example D. Rather than having the plugin based on user input, my goal is to ...

Align the text vertically in a Bootstrap button

Can anyone lend a hand with vertically aligning text in a Bootstrap button? When I use the align-middle class, the text is aligned as desired. However, if I use the align-top class, the text remains top-aligned no matter what I do. Any suggestions? Here& ...

The Google reCaptcha reply was "Uncaught (in promise) null"

When using reCaptcha v2, I encountered an issue in the developer console showing Uncaught (in promise) null message regardless of moving the .reset() function. Here is the console output: https://i.stack.imgur.com/l24dC.png This is my code for reCaptcha ...

Using CSS and JavaScript to create a gradient-style opacity effect for smoothly fading out content

Is there a way to achieve a gradient fade effect on the opacity of an iframe and its content? To provide an illustration, think of the bottom of the notification centre in Mountain Lion or iOS. The concept involves having the content within an iframe grad ...

concise stylus CSS selector

Is there a way to condense these stylus selectors for a cleaner look? #map > div.mapboxgl-control-container > div.mapboxgl-ctrl-top-left > div:nth-child(1) > button #map > div.mapboxgl-control-container > div.mapboxgl-ctrl-top-left > ...

The behavior of CSS classes, unique characters, and the usage of class prefixes

I'm dealing with a template that contains some puzzling CSS selectors. The syntax in question is as follows: .\35 grid .\31 1u { width: 91.5%; }, but the '\' character appears as a strange symbol in Notepad++. Can someone ex ...

Positioning Images in Tailwind Modals

I'm currently working on building a modal using Tailwind in Vue, but I've run into some challenges with aligning the elements inside the modal as desired. I've experimented with removing certain Tailwind classes and have tried implementing ...

My table elements are automatically being populated with <tr> elements thanks to Javascript

Upon viewing the screenshot: https://i.sstatic.net/Pwv2v.png it is evident that each element is placed within its own tr. My preference is to have each element contained in a td and then wrap everything within a single tr. Here is the updated HTML stru ...

Preventing opening new tabs when clicking on "_blank" links in Selenium VBA

When using HTML, the code to create a link that opens in a new browser tab is as follows: <a target="_blank" id="mylink" herf="www.google.com">google</a> In VBA, you can use the following code to click on the link using the driver object ...

Enlarge the icon so that it is bigger than the text, but ensure that both the icon and

Can someone please help me figure out how to align the icon with the text on this button? https://i.sstatic.net/Jtc3E.png Here is the current code I have: .btn { -webkit-border-radius: 4; -moz-border-radius: 4; border-radius: 4px; font-family: & ...

Achieving the minimum width of a table column in Material-UI

Currently I am in the process of developing a React website with Material-UI. One query that has come up is whether it's feasible to adjust the column width of a table to perfectly fit the data, along with some extra padding on both ends? Outlined be ...

Creating a replica of Airbnb using Bootstrap 5: Transforming the search bar into a button

I am looking to implement a search bar button similar to the one on airbnb using bootstrap 5, like this example: https://i.sstatic.net/oIIzv.png Here is an example of HTML code that you can use: <button type="button" class="btn btn-ligh ...

"Implementing a fixed header and footer with a scrolling content area within a parent div that has a fixed position for

I'm seeking assistance with some CSS tasks. Specifically, I need to maintain the same functionality in responsive view when utilizing a fixed position in the parent div (Bootstrap 5 is in use). (the main focus of this project is on fullscreen view of ...

How come my gifs appear tiny even though I've adjusted their width to 32% each?

I am trying to display three gifs in a row, each taking up one-third of the width of the page. However, when I preview the page, the gifs appear tiny. I have set the divs to 32% each and the gifs should fill 100% of their respective div. Here is the code s ...