Inline “required” asterisk in Laravel form label

2019-07-19 08:27发布

问题:

I'm trying to add a red asterisk for my required fields in Laravel, but I'm not sure how to add them inline with the label.

What I'm doing currently is

{{ Form::label('took_act_or_sat' , 'Did you or will you take the SAT or ACT?' ) }} <span style="color: red">*</span>

but when the label wraps to a second line, for some reason the asterisk is being bumped an extra line down.

So I guess the first part of my question is: Can you add inline styling to a form label in Laravel?

And the second part is: Is there another way I should be going about adding the asterisk (or other marker) so that this wouldn't be an issue?

If it's relevant, I'm also using Bootstrap for the majority of my styling if that also affects anything.

回答1:

Add a class to your label with the third parameter

{{ Form::label('took_act_or_sat' , 'Did you or will you take the SAT or ACT?', array('class' => 'required') ) }}

And add asterisk with css

<style>
    .required:after{ 
        content:'*'; 
        color:red; 
        padding-left:5px;
    }
</style>


回答2:

There are multiple ways to achieve this. if you want to achieve required result without changing your HTML code then you can add some style to your span elemnt as follows

Code

{{ Form::label('took_act_or_sat' , 'Did you or will you take the SAT or ACT?' ) }} <span style="color: red; display:block; float:right">*</span>


回答3:

Try this

<label for="took_act_or_sat">Did you or will you take the SAT or ACT? <span style="color: red">*</span></label>


回答4:

{{ Form::label('took_act_or_sat' , 'Did you or will you take the SAT or ACT? <span style="color: red">*</span>','',false ) }} 

All you need is false as parameter.