Modifying a header's properties by incorporating chosen values from a form

I am tasked with creating an HTML form that includes 3 select attributes (color, shadow, background-color). Depending on the user's selections, these attributes will be applied to a h1 element on the page. I need to achieve this without using JavaScript, only utilizing HTML and CSS. The code I have implemented so far does not seem to be changing the attributes of the h1 element. Can someone please help me find a solution?

h1 {
  margin: 0px;
  padding: 0px;
  margin-top: 100px;
}

table {
  border: 3px double;
  border-collapse: collapse;
}

th {
  border-bottom: 3px double;
}

#text option[value="red"]:checked~h1 {
  color: red;
}

#text option[value="blue"]:checked~h1 {
  color: blue;
}

#shadow option[value="red"]:checked~h1 {
  text-shadow: 2px 2px 5px rgba(255, 0, 0, 0.5);
}

#shadow option[value="blue"]:checked~h1 {
  text-shadow: 2px 2px 5px rgba(0, 0, 255, 0.5);
}

#background option[value="silver"]:checked~h1 {
  background-color: silver;
}

#background option[value="gold"]:checked~h1 {
  background-color: gold;
}
<center>
  <h1>Hello World</h1>
  <table>
    <tr>
      <th colspan="2">parameters</th>
    </tr>
    <tr>
      <td><label for="text">text</label></td>
      <td>
        <select id="text" name="text">
          <option value="red">red</option>
          <option value="blue">blue</option>
        </select>
      </td>
    </tr>
    <tr>
      <td><label for="shadow">shadow</label></td>
      <td>
        <select id="shadow" name="shadow">
          <option value="blue">blue</option>
          <option value="red">red</option>
        </select>
      </td>
    </tr>
    <tr>
      <td><label for="background">background</label></td>
      <td>
        <select id="background" name="background">
          <option value="silver">silver</option>
          <option value="gold">gold</option>
        </select>
      </td>
    </tr>
  </table>
</center>

Answer №1

If you're looking to achieve a specific effect, you can use the :has() selector. With this selector, you can target elements based on their relationship with other elements. Here's an example code snippet that you can customize for your project:

body:has(#color option[value="red"]:checked) h1 {
  color: red;
}

body:has(#color option[value="blue"]:checked) h1 {
  color: blue;
}
<h1>Hello World</h1>
<form>
  <label for="color">Color</label>
  <select id="color">
    <option value="red">Red</option>
    <option value="blue">Blue</option>
  </selec>
</form>

It's important to note that the element you select before :has(...) should be a parent of the <h1> you intend to style.

Additionally, the ~ selector in CSS only targets elements that come after the current one on the same level. Since your <h1> is positioned differently within the DOM structure, using :has() may be more suitable for your needs.

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

Minimizing the use of line breaks in HTML to enhance visual design characteristics

While working with Bootstrap and HTML to construct a website, I have frequently resorted to using line breaks in the code for design purposes. However, I believe there must be a more professional approach to achieve the desired layout. For instance, on ...

both modules loading at the same time in AngularJS

var moduleA=angular.module("MyModuleX",[]); moduleA.controller("MyControllerX",function($scope){ $scope.name = "John X"; }); var moduleB=angular.module("MyModuleY",[]); moduleB.controller("MyControllerY", function($scope) { $scope.name = "Sarah Y" ...

Retrieve 2 data points from an HTML source code using Python

I'm struggling to extract data-b following the term "Grab" from the unmodified webpage code. There are multiple <tr> tags on the page and I need to specifically target those containing "Need" just before </a>. While I initially tried using ...

Looking to design an interactive grid for generating dynamic thumbnails

I am a beginner in the field of web development and I have a desire to create a website for showcasing my portfolio. This website should feature project thumbnails along with brief descriptions, all of which should be displayed dynamically. Although I poss ...

Deactivating Node.js files in vsCode for client-side JavaScript files

I'm facing a major challenge when it comes to coding with JavaScript. I have a JavaScript file that is using Node.js, which means I am unable to manipulate the DOM elements. Take this code snippet for example: var form = document.getElementsByClassNa ...

XMLWorker: unable to align cell vertically properly

Upon reviewing this code snippet: <table> <tr> <td border="1"> <table> <tr><td>Blah</td></tr> <tr><td>Blah</td></tr> ...

What is the best method for sending the styled and checked option via email?

Just finished customizing the checkboxes on my contact form at cleaners.se/home. Here's my question: If I select "telefon," how can I ensure that this option is sent to my email? In Contact Form 7, I used to simply type a shortcode. Is there a simila ...

Launch a bootstrap modal from a different webpage

If you're looking to open multiple modals with different content displayed from HTML files, check out this example below: <div id="how-rtm-works" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" ...

Svelte's feature prevents users from inputting on Mapbox

My goal is to prevent user input when the variable cssDisableUserInput is set to true. Here's what I have within my main tags: <div id=userinput disabled={cssDisableUserInput}> <div id="map"> </div> Within my CSS, I&a ...

Sliding panel, perhaps?

I've been working on a small project and I've managed to create this panel, but I'm looking to change the position of the "tab" to something similar to this Can anyone help me out? I'm still new to all of this~ http://jsfiddle.net/ ...

Tips on how to properly format a DateTime String

I need help with formatting a DateTime string retrieved from an API where it is in the format of YYYY-MM-DDTHH:MM:SS +08:00 and I want to change it to DD-MM-YY HH:MM getDataFromApi(res) { this.timestamp = this.timestamp.items[0].timestamp; console ...

The :focus pseudo-class is failing to target the input element

I'm dealing with a simple input field of type text: <input matInput type="text" placeholder="{{placeholder}}" class="input-field" [ngClass]="{'error': hasErrors}" [readonly]="isReadonly&quo ...

Tips for avoiding duplicate usernames in mySQL when working with PHP

My users register for the database, but how can I prevent duplicate usernames in the database? I want to inform the user if their username already exists. <?php $error = ""; // error $GoodJob = ""; if(isset($_POST[&apos ...

Unable to insert image into div element

Having trouble aligning profile images next to names instead of below the text within the div. Can't seem to figure out how to fix this issue. Just to clarify, I want the images to appear alongside the text on the page (the white image next to ' ...

The pagination feature for Swiper is experiencing issues across both desktop and mobile platforms

I am having an issue with the pagination display in the text. I would like it to appear below the text as a standalone item for better visibility. Another problem arises when viewing the page on mobile devices - the hamburger menu displays behind the slid ...

Retrieve the order in which the class names are displayed within the user interface

In the following code snippet, each div element is assigned a common class name. <div id="category_9" class="list_item" data-item_ids="[38]"</div> <div id="category_2" class="list_item" data-ite ...

What are the steps to vertically aligning rows in bootstrap?

I've been exploring different solutions to vertically center my rows within the container. Adding d-flex and align-items-center did work for centering them vertically but caused the rows to appear side by side. I'm curious if there's an alte ...

Page not found: unique error on alternate route

My website has different paths and pages in hbs format, such as root, help, and 404. The problem arises when I navigate to localhost:3000/wrong, the site displays correctly, but when I try localhost:3000/help/wrong, the CSS is not applied to the 404 page b ...

Troubleshooting: Javascript success callback not executing upon form submission

A snippet of my JavaScript looks like this: $(document).ready(function(){ $("#message").hide(); $("#please_wait_box").hide(); $("#updateinvoice").submit(function(e){ $("#message").hide(); ...

How can we combine Angular Gradients and CSS Webkit Gradients for a modern design

Can a gradient be created that resembles the color picker style shown here? The outer part showing full saturated, 50% brightness values and transitioning towards the inside to 100% brightness. https://i.sstatic.net/ohuF4.jpg ...