How can I add a black-colored name and a red-colored star to the Placeholder Star as a required field?

I'm looking to customize the placeholder text in an input field. I want the main placeholder text to be in black and have a red star indicating it's a required field. However, my attempt to set the color of the star specifically to red using `::-WebKit-input-placeholder:after` didn't work as expected. Can someone suggest another method to achieve this color customization?

I've already tried using the following code snippet, but it didn't give me the desired result:

::-WebKit-input-placeholder:after {
    content: "*";
    color: red;
}

For demonstration purposes, you can check out the image by visiting the link below: https://i.sstatic.net/Go2Co.jpg

Answer №1

It will definitely meet your needs. I have recently written the code for a single field.

.form-group{position:relative;}
.form-group input{position:relative;z-index:9;background:transparent;border:1px solid #aaa;padding: 5px}
.form-group label{position:absolute;left:5px;top:5px;z-index:1;color:#ccc;}
.form-group label::after{content:"*";color:red;}
input[required]:valid + label{display: none;}
<div class="form-group">
  <input type="text"  name="name" required="required" />
  <label>Enter first name</label>
</div>

Answer №2

There is no one-size-fits-all solution.

One approach is to utilize background images, but this requires manually setting the star position for each field:

input::placeholder {
  background-image: url('images/some-red-star.png') no-repeat;
  background-position-x: 50px;
}

If manually placing stars is not feasible, you can create a faux HTML placeholder by inserting a div below each input and styling it to mimic a placeholder:

<input required="required">
<div class="placeholder">name<span>*</span>

.placeholder { 
  position: ... 
  pointer-events: none;
  user-select: none;
}
.placeholder > span { color: red; }
input:valid ~ .placeholder {
  display: none;
}

Answer №3

Give this a try, I believe it could be helpful to you

input {
 width: 200px;
 padding: 8px;
}

input[required] + label {
  position: relative;
  color: #000;
  font-size: 16px;
  left: -210px;
}

input[required] + label:after {
  content: '*';
  color: red;
}

.btn {
  width: auto;
  margin: 10px 0;
}
<form>
  <input type="text" id="box1" required="required" />
    <label for="name">Enter First Name</label><br/><br/>
  <input type="text" id="box1" required="required" />
    <label for="name">Enter Last Name</label><br/>
  <input class="btn" type="submit" />
</form>

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

Has the HTML attribute 'step' stopped functioning properly?

I'm currently working on an Angularjs project that primarily uses material design. I am trying to set up a timepicker using the input control with the type=time attribute. However, I want to restrict the user to select times only within a 30-minute ra ...

Looking for a more efficient method to pass components with hooks? Look no further, as I have a solution ready for

I'm having trouble articulating this query without it becoming multiple issues, leading to closure. Here is my approach to passing components with hooks and rendering them based on user input. I've stored the components as objects in an array an ...

Using JavaScript, add a complex JSON record to a JSON variable by pushing it

I have been attempting to insert a complex JSON data record into a JSON variable using the following code: var marks=[]; var studentData="student1":[{ "term1":[ {"LifeSkills":[{"obtained":"17","grade":"A","gp":"5"}]}, {"Work":[{"obtained":"13"," ...

Switch up image transitions using Javascript

I have already completed the css and html structure for my project. However, I am encountering issues with the JavaScript functionality. I really like the image effect on this website: (the blue one). I have attempted to replicate it here: , but have bee ...

Avoiding special characters in URLs

Is there a way to properly escape the & (ampersand) in a URL using jQuery? I have attempted the following methods: .replace("/&/g", "&amp;") .replace("/&/g", "%26") .replace("/&/g", "\&") Unfortunately, none of these are y ...

Utilizing useState for React Data Picker

I recently attempted to implement the React Data Picker in my React project using npmjs. However, I encountered an issue when trying to import useState from import React, { useState } from "react"; The error message displayed: 'useState&a ...

verify that the div has finished loading with ajax

I am working on a project where I have a div with the id #pageToLoad. This div loads a form from formPage.php. I want to ensure that the page is fully loaded in the div so that if any errors occur, I can reset the form using: $("#form").reset(); Can some ...

Updating Sencha list itemTpl on the fly

Is there a way to dynamically change the itemTpl in a Sencha list to adjust the visibility of a column based on certain conditions? Your assistance would be greatly appreciated. ...

A guide on showcasing the compilation results of PHP code directly on the web browser

How can I compile PHP code on an HTML Button click and display the compilation output in a browser? The code snippet below works well in the terminal but doesn't show anything in the browser. I am currently using XAMPP server on a Windows machine. &l ...

Show subscriptions retrieved from an Ajax .post request within a modal popup

I have a modal on my index.php page that loads when a button is clicked. I want to display data retrieved from an ajax post to a PHP file that returns information about subscriptions. If a user is subscribed to a particular subscription, there should be an ...

Exploring ways to traverse a JSON encoded object in PHP with the help of JavaScript

I am facing an issue while trying to access my data from PHP. I am using the following code: echo json_encode($rows); When I comment out datatype: 'json', I can see a normally encoded JSON string. But when I use it, the alert shows me an array ...

Looking for a way to focus on an element similar to `event.target.focus()`

I am in the process of developing a dynamic list component that generates an empty text field every time an item is added to the list. The structure of the component is shown below: const DynamicInputList = ({ inputs, onChangeArray, label, name, errors }) ...

What methods can be employed to execute a intricate substitution utilizing both javascript and jQuery?

Within the source code of my web pages, I have elements that resemble the following: <div id="opt_3" >A)</div> <div id="opt_2" >B)</div> <div id="opt_4" >C)</div> <div id="opt_5" >D)</div> <div id="op ...

Connecting Vue component data to external state sources

I am facing a challenge with integrating a Vue component into a large legacy system that is not based on Vue. This component retrieves data through AJAX requests and displays information based on an array of database record IDs, typically passed at page lo ...

The function is not explicitly declared within the instance, yet it is being cited during the rendering process in a .vue

import PageNav from '@/components/PageNav.vue'; import PageFooter from '@/components/PageFooter.vue'; export default { name: 'Groups', components: { PageNav, PageFooter, }, data() { return { groups: ...

What's the best way to add margin or padding to an SVG image within a v-app-bar

Seeking assistance with a seemingly simple question that can help enhance my understanding as I dive into using vue/vuetify for a new front end project. I am starting fresh and utilizing v-app-bar. Below is a snippet from my app.vue file: <template> ...

"Utilizing an AJAX request to dynamically fill form fields based on a database query as the selected value

I've searched through the questions here but haven't found a precise answer to my query :( However, I have managed to find something. I have a form select field that I populate from a database query. <select style="width:100%;" class="quform ...

Creating a striking two-tone border pattern in an HTML document

Looking to create a design with a straight line that features two colors and a slanted line in between. Here is an example of the desired result: https://i.stack.imgur.com/9ys6e.png ...

Tips for uploading files in asp.net using an ajax call

I have developed a small asp.net web forms application for managing emails. The interface allows users to input mandatory information such as sender, recipient, and subject. I am now looking to implement file attachments in the emails using the asp.net fil ...

Unlock the power of TypeScript's inheritance by utilizing static methods for type

In my TypeScript project, I have two classes: BaseModel and HotelModel. The HotelModel extends the BaseModel class, which provides static methods like findById, all, etc. export default class BaseModel { private collection:string _id:string | undefine ...