Achieve proper alignment between a label and textbox using CSS styling

While the title may lead you to believe this is just another CSS issue, it's actually a bit different. I have a textbox with a placeholder property and a applied CSS class. Now, I need to add a label to signify it as a mandatory field. The problem arises because of the CSS - I can't seem to position the label in the top right corner of the textbox.

Here's the code snippet:

<div id="formwrap">
        <div class="formarea">
            <p class="sub_headline">
                User Details</p>

            <asp:TextBox ID="txtFname" class="input" placeholder="First name" runat="server"></asp:TextBox>
            <asp:Label runat="server" Text="*" ForeColor="Red"></asp:Label><br />
            <asp:RequiredFieldValidator runat="server" ErrorMessage="Please Enter First name" ControlToValidate="txtFname" ></asp:RequiredFieldValidator>


</div>
</div>

CSS snippet:

.input
{
    font-family: 'Lato' , sans-serif;
    border: 1px solid #CCCCCC;
    width: 65%;
    height: 20px;
    margin: 14px 94px 0 0;
    font-size: 16px;
    font-weight: lighter;
    padding: 4px;
    color: #E96151;
    float: left;
}

Any thoughts on how I can resolve this?

Appreciate your help!

Answer №1

To make the textbox work, simply eliminate the margin.

.input
{
    font-family: 'Lato' , sans-serif;
    border: 1px solid #CCCCCC;
    width: 65%;
    height: 20px;
    /*margin: 14px 94px 0 0;*/
    font-size: 16px;
    font-weight: lighter;
    padding: 4px;
    color: #E96151;
    float: left;
}

Take a look at this DEMO with html controls. If you prefer not to edit the css class, you can override by adding style="margin:0;" directly in the Textbox.

Suggestion: The placeholder attribute only functions for html input elements. You may instead use the following code for better performance.

<asp:TextBox ID="txtFname" class="input" Text="First Name"
       onblur="if (this.value == '') {this.value = 'First Name';}" onfocus="if (this.value == 'First Name') {this.value = '';}" runat="server"></asp:TextBox>

Answer №2

To tackle this issue, consider utilizing an HTML table to design the user interface for the application.

Begin by creating an HTML table and allocating one column for a textbox and another column for a label [Mandatory *] placed next to the textbox, as shown below:

<table class="style1">
    <tr>
        <td>
            <input type="text" id="textbox1" />
        </td>
        <td>
            <label for="lblMandatory">* Mandatory</label>
        </td>
    </tr>
</table>

Answer №3

To ensure proper display of each element, you will need to create a separate parent container for each one.

<div id="formwrap">
    <div class="formarea">
        <p class="sub_headline">
            User Details</p>

        <asp:TextBox ID="txtFname" class="input" placeholder="First name" runat="server"></asp:TextBox>
        <div>
          <asp:Label runat="server" Text="*" ForeColor="Red"></asp:Label><br />
        </div>
        <div>
            <asp:RequiredFieldValidator runat="server" ErrorMessage="Please Enter First name" ControlToValidate="txtFname" ></asp:RequiredFieldValidator>
        </div>
     </div>  
</div>

Then, remember to set the style for vertical alignment as needed.

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

Are checkboxes embedded within labels?

There are two common ways that people code checkboxes on the web, and I'm wondering which one is considered correct. <input id="a" type="checkbox"/><label for="a">checkbox</label> <label for="b"><input id="b" type="checkbo ...

Different styles of Unicode arrowheads found on different versions of Android devices

Here is the HTML/CSS code I'm using to create a "Play" button with an arrowhead symbol. The code utilizes ▶ and &9658; to achieve this functionality. <div class='audio-container'> <p style="text-indent:0em;"> <aud ...

allowing absolutely positioned region to expand

For further information, please visit this page: test page In the process of designing a website, I have implemented a sidebar featuring an accordion style vertical navigation bar. The sidebar is set to be absolutely positioned in relation to its containi ...

Inserting multiple rows of data into a MySQL database in a single page using only one query in PHP

This snippet shows a MySQL query being used to update and insert data into a database: if ($_POST["ok"] == "OK") { $updateSQL = sprintf("UPDATE attend SET at_status=%s, at_remarks=%s WHERE at_tt_idx=%s", GetSQLValueString ...

Repopulate data with jQuery and JavaScript

I have a div element as shown below in an HTML page: Whenever I click on any of the div elements, I save the text within the span tag to a database. Upon returning to the same page, I retrieve the saved text and try to repopulate the clicked view based on ...

Employing a lexicon of hexadecimal color code values to harmonize CSS styles

I have a unique requirement where I need to utilize a dictionary of HTML color codes and then apply those colors as styles. It's an interesting challenge! Here is an example of how my color dictionary looks like: const colorCodes = { red: ...

Question about authenticating users in ASP.NET

I've got this code snippet on an aspx page: protected void Page_Load(object sender, EventArgs e) { if (User.Identity.IsAuthenticated) lblAuthentication.Text = "Logged in user: " + User.Identity.Name; el ...

Ways to create a fixed button positioned statically at the bottom of a page

Currently, I am utilizing tailwind CSS to create a webpage with Next and Back buttons for navigation. However, an issue arises when there is minimal content on the page as the button adheres to the top. For visual reference, please view the image linked be ...

Adjustment of navigation as window size changes

Are there any efficient methods to prevent navigation from moving while resizing the window? Currently, I am using the following code which is clearly incorrect as it will cause proportions to change. Should I consider using a fixed width instead? #main ...

CSS - Utilizing Flexbox for creating inline lists that adjust based on text length

I have a set of radio buttons arranged like this: https://i.sstatic.net/l4uhp.png I want to utilize flexbox to display them horizontally when there is sufficient space, similar to this: https://i.sstatic.net/IGUAc.png However, the current setup uses a ...

Update the color of a span in JQuery when its value equals 0

Currently, my goal is to update the color of a span tag only if its value is 0. I attempted to achieve this with the following code: $('span.number:contains("0")').css('color', '#C1C1C1'); However, this code also changes the ...

The validators on the deployment server have ceased to function

It's truly puzzling and inexplicable. In my ASP.NET 2.0 application, I have a form that utilizes various client-side validators including Custom, Regular Expression, and Required Field. The functionality of my app requires me to toggle the enable/disa ...

The nested div escapes from its parent div despite the application of clearfix and the correct positioning of relative and absolute elements

Hey there, I have a question about using relative and absolute positioning along with applying clearfix to the main container in my code. The layout is quite simple - a nav-bar with a drop-down menu at the top, followed by a hero image spanning the screen, ...

Exploring nested selection with css and utilizing the ::focus pseudo class

Is it possible, using CSS, to target the helpTextInvalid class when the div element with the selectize-input class is in focus? <html> <head> <body> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/lib ...

Integrating autoprefix-cli into ANT build

I've been attempting to integrate autoprefix-cli into my ANT build. The code snippet below shows what I've tried so far. <target name="auto"> <apply executable="autoprefixer-cli.bat" verbose="true" force="true" failonerror="true"> ...

Design interactive horizontal buttons containing images and corresponding labels using HTML5

I want to create three horizontal buttons with images and labels underneath using HTML5. I attempted to follow this example: tutorial, but I need a different image for each button. When I give each button an ID and add #id {background-image:url} to the CS ...

Chrome may clip the gradient radius when setting the focal point outside of the SVG gradient

I just joined this community and I might have some questions that could be considered too basic. Recently, I've been trying to understand SVG and came across something that left me puzzled. Take a look at this: <svg xmlns="http://www.w3.org/20 ...

Unusual Happenings with jQuery Draggable

Currently, I am experimenting with jQuery draggable to move individual letters of the word "Hello" around on the page. However, I have come across a frustrating issue. When I drag the letter H towards the right and it gets near the letters E, L, L, or O, ...

What sets apart transactions in SQL Server from those in ADO.NET?

Can you explain the difference between transactions in SQL Server and implementing transactions in ADO.NET? I am interested in understanding this from a performance standpoint. If I were to use transactions (Begin End Trans) compared to using SqlTransacti ...

Connecting pages through index.html within the hosting file manager directory

I have just started my coding journey and recently signed up for ipage hosting. After uploading my .html files to a directory named "Website", I encountered an issue while trying to link multiple pages to the index.html home page. I tried using the code ...