Ways to incorporate an asterisk into a placeholder using CSS

Is there a way to add an asterisk mark to the placeholder of inputs, similar to this image: https://i.sstatic.net/N6ND0.png

I've scoured the internet but haven't been able to find a solution that works.

My current approach:

Currently, I'm attempting to add it using the after pseudo-element, but it's not showing up.

    input[type=text]::-webkit-input-placeholder:after {
       content: '*';
       color: red; vertical-align: top; font-size: 10px;
    }
    
    input[type=text]:-moz-placeholder:after { /* Firefox 18- */
       content: '*'; 
       color: red; vertical-align: top; font-size: 10px;
    }
    
    input[type=text]::-moz-placeholder:after {  /* Firefox 19+ */
       content: '*';
       color: red; vertical-align: top; font-size: 10px;
    }
    
    input[type=text]:-ms-input-placeholder:after {  
       content: '*';
       color: red; vertical-align: top; font-size: 10px;
    } 
<input type="text" name="your-name" placeholder="Naam">

I don't want to directly insert the symbol into the placeholder text. Instead, I would like to style the symbol differently (for example, blue color while keeping the rest of the text grey).

If anyone can assist me in adding an asterisk to the placeholder, I'd greatly appreciate it.

JSFIDDLE

Answer №1

Why not incorporate the * directly into the placeholder attribute itself?

.asterisk::-webkit-input-placeholder {
    color:    #f00;
}
.asterisk:-moz-placeholder {
   color:    #f00;
   opacity:  1;
}
.asterisk::-moz-placeholder {
   color:    #f00;
   opacity:  1;
}
.asterisk:-ms-input-placeholder {
   color:    #f00;
}
<input type="text" name="your_name" placeholder="*" class="asterisk" />

Note: Pseudo elements cannot be used in replaced html elements


Based on your comment, you can also utilize the required attribute and style them this way:

<input type="text" name="your_name" placeholder="*" required />
[required]::-webkit-input-placeholder {
    color:    #f00;
}

According to your further comments, if you need asterisks for specific inputs, wrap them in a span like shown below:

.input-group{
  position: relative;
}
.input-group::after{
  content: '*';
  position: absolute;
  top: 3px;
  left: 46px;
  color: #f00
}
<span class="input-group">
  <input type="text" name="your_name" placeholder="Name" />
</span>

Answer №2

To enhance responsiveness, you can calculate the length of the text and apply it to your CSS styling.

  const placeholderLength = (placeholder.length) ? placeholder.length : 1 ;
  const placeholderWidth = placeholderLength * 10 + 'px';


 .input-group{
  position: relative;
}
.input-group::after{
  content: '*';
  position: absolute;
  top: 0px;
  left: var(--placeholder-width);
  color: #f00
}

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

Break apart animation similar to the 🎊 emoji using CSS styling

I am attempting to create an animation of a circle splitting open on two sides, similar to the ...

Internet Explorer often fails to interpret html tags correctly

A strange issue is occurring with Internet Explorer on a website, where it seems to be misinterpreting the code. Instead of reading <tag></tag>bla bla</tag><//tag>, it shows something completely off. An example of this problem can b ...

Whenever I adjust the zoom on my HTML page, it either scrolls upwards or downwards

Is there a way to prevent the page from scrolling when using zoom-in or zoom-out? I attempted using overflow: hidden in the body, but it did not work. <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

Utilizing inline javascript to dynamically update the selected option of a radio button

A form on my website includes two radio buttons: one labeled 'trigger_unit' and the other labeled 'normal_unit'. In addition, there is a select drop-down menu with four values and a default NULL option. <select rel="dropdown" name=" ...

Creating a CSS layout with tabs that include a visually appealing right space

My webpage is set up for tabbed browsing, with the tabs displayed as <li>s in block form. The parent div containing the tabs is floated to the right. However, I am encountering an issue where the last tab is not fully aligning with the right side of ...

What is the best way to add padding to this element?

Hey there, I'm currently working on a website and have encountered an issue with the navigation bar. I adjusted some of the links to float to the right, but as a result, they drop downwards, disrupting the vertical center alignment. Adding padding doe ...

Position the flex item adjacent to the initial flex item using wrap mode only if there is extra space

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <titl ...

Loading screen preceding page change

I've been searching everywhere and can't find a good tutorial on how to smoothly transition pages when clicking a link on my website. I want the page to fade out on click, display a preloader, then fade in the new page once the preloader finishes ...

Ways to access the value of an attribute in an AngularJS object

Is there a way to access the value of field.jobtype within a controller? var app=angular.module('myapp',['ui.bootstrap','ui.select']); app.controller('mycontroller',function($scope){ $scope.onStateSelected = func ...

transforming a div container into an inline element using CSS

Hey there! I'm facing an issue with CSS where I am trying to change a div tag into inline. <div id="menu1"> <ul> <a href="#"> <li> one</li> </a> <a href="#"> <li> two</li> </a> <a h ...

Aggregate the data entered into input fields on one page and display them on a separate page

Can you help me with this task? - I have 2 pages set up (page 1 has input fields & page 2 is where the entered data should be displayed) - I want to retrieve all the text input from the first field and insert it into the "content" tag on the second page. ...

Change the country name to all lowercase letters

I'm attempting to retrieve the country flag icon of the Open Weather Map API. For example: The JSON response for the country code from the API request is in uppercase. To obtain the correct icon for the country, I need the country code in lowercase. ...

Elevate your Material UI experience by setting a maximum height for TableBody and enabling vertical scrolling for a seamless

I'm looking to utilize Material UI to set a maximum height of 500px for the TableBody within my Table. If there are rows that exceed this height, I want the TableBody to scroll vertically while keeping the TableHead fixed in place. https://i.sstatic. ...

Can Internet Explorer 11 be reverted to IE 10 on Windows 8.1? If so, what is the process for doing so

Currently using Windows 8.1, but I need to conduct testing on genuine IE 10 instead of emulating IE11. How can I downgrade from IE11 to IE10? ...

Changing select options in AngularJS with dynamic data sources

I'm currently facing a challenge in dynamically updating the options of a select list. I am unsure of what exactly needs to be included in the ng-options parameter to achieve the desired functionality. Below is the JSON data: {    "131":"Activit ...

The mobile version of the navbar is causing me some trouble

I'm facing an issue with my navigation bar. It appears perfectly on desktop and even looks fine when I simulate it on smartphones and tablets using developer tools. But when I actually open the webpage on a mobile device, the navbar icon becomes extre ...

Using jQuery to add HTML elements before the content of a list

I'm facing a challenge in dynamically adding new elements to my list. Through ajax, I retrieve HTML data from my database and aim to iterate through each new <li> element to gradually prepend them to the top of the list. This is the snippet of ...

Ensure the div remains floating to the left even when the browser window is resized

I'm currently working on creating a timeline, but I'm struggling to keep the div aligned horizontally. Within a div container with overflow set to hidden, I have divs floating to the left. Inside these divs are ul elements with fixed widths. My ...

What is the best approach to ensure consistent spacing between columns of the same height in Bootstrap?

I'm currently working on a website project that involves displaying images in a specific grid format. To achieve this, I am utilizing Laravel and Bootstrap. The primary objective is to create a grid layout with spacing that maintains the height of th ...

Transforming web pages containing html, css, and javascript code into a custom node.js application

I am facing an issue where my HTML pages with CSS and js files work perfectly when opened individually in a browser like Chrome. However, when I try to integrate these pages into a Node.js project that I have created, the js file is not being recognized ...