Question regarding CSS positioning: How do you place an asterisk inside a display block element?

The platform I'm working with is generating code like the one below for forms:

<div class="sf-fieldWrp">
    <label for="Textbox-3">Contact Name</label>
    <input type="text" value="" required="required">
    <p>Your full name please</p>
</div>

I want to add an asterisk next to the labels of required fields. The only indication that a field is required in the code is required="required", without any classes or ids.

Is it feasible to place the asterisk right after the label, specifically after Contact Name?

I attempted using this CSS code, which somewhat works but positions the asterisk before the label since the label itself is a block element. I'm open to changing the label display if needed.

label { display: block }
.sf-fieldWrp { position: relative }
input[required="required"] + p::before {
  content: "*";
    color: red;
    position: absolute;
    top: 0;
    left: 0;
    font-size: 1.1rem;
    font-style: normal;
}

Here is a preview of how it currently looks

Currently, the asterisk appears before the label text, but my goal is to have it appear after the label text (not after the label block element).

How can I ensure the label and input remain on separate lines while positioning the asterisk after the label text?

Answer №1

To apply the :has pseudo-class on the parent element and insert an asterisk using the after pseudo-element in the label, you can follow this CSS code:

.sf-fieldWrp:has(input[required="required"]) label::after {
  content: '*';
  display: inline-block;
  color: red;
}
<div class="sf-fieldWrp">
  <label for="Textbox-3">Contact Name</label>
  <input type="text" value="" required="required">
  <p>Your full name please</p>
</div>

Answer №2

Give this a shot:

label { display: block }
.sf-fieldWrp { position: relative }
label::after {
  content: "*";
    color: red;
    font-size: 1.1rem;
    font-style: normal;
}
<div class="sf-fieldWrp">
    <label for="Textbox-3">Contact Name</label>
    <input type="text" value="" required="required">
    <p>Your full name please</p>
</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

Utilize Bootstrap v4 in styling and aligning buttons for a polished

With Bootstrap v4 dropping the .btn-group-justified class, a solution can be found at https://github.com/twbs/bootstrap/issues/17631 Here's how to justify the buttons: <div class="btn-group btn-group-justified" role="group" aria-label="Justified ...

What is the best way to align the navbar items in the center?

Is there a way to center the navigation bar elements using Bootstrap? HTML <body> <nav class="navbar navbar-inverse navbar-fixed-top " role="navigation"> <div> <ul class="nav navbar-nav"> ...

What is the best way to select an HTML tag element using the :v-deep() selector?

When I utilize the following: ::v-deep img { ... } it functions but triggers a deprecation warning: [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead. How can I achieve the same effect using ...

IE7 causing mispositioning of `<ul>` in jQueryUI Autocomplete results

I have successfully implemented the jQueryUI Autocomplete function and here is how I have customized it: appendTo: "#results", open: function(){ var position = $("#results").position(), left = position.left, top = position.top ...

What is the best way to implement a gradual decrease in padding as the viewport resizes using JavaScript

My goal is to create a responsive design where the padding-left of my box gradually decreases as the website width changes. I want the decrease in padding to stop once it reaches 0. For instance, if the screen size changes by 1px, then the padding-left sh ...

Ways to Adjust the Earth's Position in WebGL Earth

I have been working with this WebGL Earth component for my project, but I am facing difficulty in adjusting the position of the planet on the canvas. My intention was to place the planet at the bottom of the page, but I haven't been able to achieve th ...

Guide to dynamically updating the href of an SVG Image in Angular HTML

I am currently iterating through a list of employee objects, each containing an image URL that I need to incorporate into an SVG - Image element. <div *ngFor ="emp of employees"> <defs> <pattern id = "attachedImage" height ...

Animate the sliding up of divs using Jquery SlideToggle

Is there a way to make this perfect example slide up instead of pushing the 'navs' down? View Example Here Currently, it reveals from top to bottom (pushing the menu down), but I want it to slide up and push the navs up when clicked. The fiddl ...

Ways to shift footer lower if there is a floating element in the div above it

Trying to move my footer to the bottom of the page, regardless of the amount of content above it. After researching online, I came across a solution on this website: However, everything was fine until I added "float: left" to the content div. Suddenly, th ...

Having trouble with my WordPress theme not recognizing my CSS file

My file path is displayed below: D:\web server\blog.dev.cc\wp-content\themes\ablog\css for java script file path D:\web server\blog.dev.cc\wp-content\themes\ablog\assets\js Despite checking ...

What is the best way to ensure the dropdown box in a Bootstrap navbar automatically adjusts its width to fit within the

When I click on the dropdown menu, the box extends beyond the width of the page and a horizontal scroll appears. How can I make the dropdown box automatically adjust its position if it reaches the edge of the page without needing a scroll (as shown in the ...

A dual row navigation layout with the second row featuring equally distributed elements in a neat and organized manner

Looking to create a responsive collapsible navigation with two rows. The first row should contain the logo and language selector, while the second row should have the main links: | | | ...

Adjust the width of the dropdown menu in Twitter Bootstrap's typeahead feature depending on the search

Not entirely certain if this issue is related to jQuery, but there's a strong suspicion that it might be. The problem at hand can best be described by the picture provided: The query output exceeds the width and doesn't resize or scale according ...

Links are unable to function properly outside of the carousel slideshow in Chrome

www.encyclopediasindhiana.org/index2.php I successfully implemented a slideshow using mycarousel with the help of PHP. However, I am facing an issue where the hyperlinks on the left side of the carousel are not working if the image is as large as the heig ...

Is it acceptable to use JavaScript to code positioning if the standard HTML flow is not behaving as expected?

After contemplating for a long time, I have finally mustered the courage to ask this question. The behavior of HTML elements in normal flow has always bewildered me. Controlling them can be quite challenging, especially when dealing with server-side coding ...

Extracting Div Dimensions Based on Screen Size

I've been racking my brain for the past couple of days trying to solve this. Essentially, I am working on a one-page layout where the first block occupies the entire width and height of the screen, while the second block needs to be relative to the fi ...

Develop a versatile currency symbol mixin that can be used in various small text formats

I am currently working on a website where I need to display prices in multiple locations. To achieve this, I have created a sass mixin that is designed to add the desired currency symbol before the price with a smaller font-size. Below are examples of how ...

Highlight the active page or section dynamically using HTML and jQuery - How can it be achieved?

What am I trying to achieve? I am attempting to use jQuery to dynamically highlight the current page from the navigation bar. Am I new to jQuery/HTML? Yes, please excuse my lack of experience in this area. Have I exhausted all resources looking for a sol ...

Incorporating text overlays on images that are compatible with responsive websites is a top priority for me

This is a piece of HTML code that utilizes bootstrap 3 for design purposes. The challenge faced is related to positioning text over an image. When resizing the browser, the alignment of the text with the background gets disturbed. Assistance is needed in r ...

Tips for identifying the presence of a mobile phone notch

My web app, shown in image 1, looks great on most devices. However, when it is launched on a mobile with a notch, like in image 2, a layout problem arises. Using a "safe area" won't work for me because some of my pages need to be fixed at the top, sim ...