简体   繁体   中英

Vertically align an asterisk (*) with a label in an HTML Form

On my form, I put an asterisk (*) behind a label to mark it as important.

The problem is that it vertically aligned as top position by default. I am hoping that there's some way through which I can vertically align it as middle.

This is how it looks:在此处输入图片说明

Notice the * to be top aligned.

I tried this, but it doesn't work

.imp {
        display: inline-table;
        vertical-align: middle;
}

Use vertical-align: sub .

 .verified span{ vertical-align:sub; }
 <p class="verified"><span>*</span> verified</p>

The vertical-align: sub method also expands the container height. Since you also tagged html for the question, you can use the html entity for native middle asterisk.

&lowast;

Demo:

Here is the default asterisk (*) . Here is the middle asterisk (∗) .

试试这个: http : //jsfiddle.net/yce0mbux/1/

<span style="line-height:30px;vertical-align:middle" >*</span><label>Some text</label>

use pseudo element before

 .imp { display: block; line-height: 26px; } .imp:before { content: '*'; display: inline-block; vertical-align: middle; }
 <div class="imp">Mandatory</div>

If you don't have a nested child element, add one like so:

<div>
    <span id="one">
        *
    </span>
     some text
</div>

You need to define both height and line-height to achieve the effect that you want.

#one{
    vertical-align: middle;
    display: inline-block;
    line-height: 20px;
    height: 15px;
}

Example

Try this method:

.your-label {
 display: inline-table;
 vertical-align: middle;
 position: relative;
 padding-left: 15px; /* Adjust as needed to make space for the asterisks */
 /* You need a given dimension to prevent overlap */
      }


.your-label:before {
 position: absolute;
 content: '*';
 left: 0; /* Adjust as needed */
 top: 3px; /* Adjust as needed */
}

See working example here

Note: Inserting as shown above, gives you the independence to position as needed.

Use relative instead of absolute to get a better alignment.

.required::before {
    position: relative;
    content: '*';
    left: 0;
    top: 0;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM