Tips for centering text within a placeholder

Currently, I have an input field with a placeholder that contains both English and Spanish text. My challenge is aligning the English text to the left and the Spanish text to the right within the placeholder. Here's an example of what I'm currently doing:

Example:

<input type="text" nam="name" placeholder="Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nombre" >

I attempted to use non-breaking spaces (nbsp) in my code, but when viewed on another window or in responsive mode, the text either overflows or does not align properly. Can someone please provide guidance on how to correctly align text within a placeholder to the left and right? Thank you for your assistance.

Answer №1

To achieve this effect, pure HTML and CSS alone are not sufficient. A workaround would involve using JavaScript to mimic a placeholder behavior with CSS. Here is an example:

var input = document.getElementById("name");

input.onblur = function() {
   if(!input.value) input.classList.remove('not-empty');
};

input.onfocus = function() {
   input.classList.add('not-empty');
};
.input-container {
    position: relative;
    display: inline-block;
}

.input-container:before {
    content: "name";
    position: absolute;
    left: 5px;
    top: 0;
    z-index: -1;
}

.input-container:after {
    content: "nombre";
    position: absolute;
    top: 0;
    right: 5px;
    z-index: -1;
}

.input-container input {
    z-index: 1;
    background: transparent;
}

.input-container input.not-empty {
    background: white;
}
<div class="input-container"><input id="name" type="text" name="name"></div>

Answer №2

Shruti

Verify solution

If you are satisfied, please consider giving me a vote as a token of appreciation :)

$(document).ready(function(){
$("input").click(function(){
$("label").hide();
});
$("input").focusout(function(){
        if($(this).val() !=''){

} else{
$("label").show();
}


    });
});
p{position: relative; display: inline-block;}
label[for="english"] { position: absolute;left: 4px;}
label[for="spanish"] {position: absolute; right: 4px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<p>
<label for="english">Name </label>
<label for="spanish">Nombre</label>
<input type="text" nam="name">
</p>

Answer №3

In my opinion, the most efficient method is as follows:

$(document).ready(function() {
  $('input').focus(function() {
    $('label').addClass("focus");
  })
  $('input').blur(function() {
    if(!$(this).val()) {
      $('label').removeClass("focus");
    }
  })
});
.input-group {
  position: relative;
  display: inline-block;
}

label {
  pointer-events: none;
}

label.focus {
  display: none;
}

label.english {
  position: absolute;
  left: 4px;
}

label.spanish {
  position: absolute;
  right: 4px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input-group">
  <label class="english" for="name">Name</label>
  <label class="spanish" for="name">Nombre</label>
  <input id="name" type="text" name="name">
</div>

The Importance of Using Labels
& Additional Resources on Adding Multiple Labels to Input Fields

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

Tips for creating two columns within a Bootstrap row, one with a form and the other with an image, that are both the same height and responsive

I am facing an issue with my bootstrap columns. One column has a form, while the other contains an image. The image section is set to 100% width, making it responsive when the screen size changes. However, the form remains at a fixed height regardless of t ...

Steps for executing ASP code pulled from the database

In my ASP project, I have a single page where clicking on different links retrieves corresponding HTML from the database and loads it. Some links are always present on the page (static HTML), while other divs display dynamic content fetched from the DB. Wi ...

Assistance Required for Troubleshooting Error in Codeigniter Ajax

Hey there, I'm a newcomer to CI and I've been searching the web for a tutorial that actually works, but so far no luck. Can someone please assist me with this code: How can I modify the code to allow for submitting an entry to the database via a ...

When I press the play button, the sound doesn't start playing

This is my script in action: <script> var audioObj = document.createElement("audio"); document.body.appendChild(audioObj); audioObj.src = "http://example.com/audios/Kalimba.mp3"; audioObj.load(); audioObj.volume = 0.3; audioO ...

Customizable form with dynamic content and interactive button within a set location

One distinct challenge not addressed in the similar inquiries below relates to a FORM component with both a variable segment and a fixed footer for submit buttons. The objective is to present: A header (a table set to 100% width including a logo and tex ...

Creating round corners for images with jQuery

To achieve rounded corners on multiple blocks using a single image above, I am opting for the use of images instead of plugins for maximum compatibility. This involves creating wrappers around the blocks to apply the corner images: <div class="wrapper- ...

What is the best way to choose dynamic content?

This might seem like a trivial question, but it's definitely not (well, at least not to me). I understand that when attempting to attach an event to dynamic content, the .on() method must be used. However, I am faced with the challenge of selecting e ...

Using jQuery to access the data attribute values

Within my HTML code, there is a span element: <span class="field" data-fullText="This is a span element">This is a</span> I am trying to access the data-fullText attribute. I attempted two different methods, but unfortunately, they both retur ...

The resource was recognized as a stylesheet but was delivered with the MIME type text/plain in golang

I am encountering an issue while developing my webpage using golang. server file (main.go): package main import ( "net/http" "io/ioutil" "strings" "log" ) type MyHandler struct { } func (this *MyHandler) ServeHTTP(w http.ResponseWriter, ...

Mystery of the Unresponsive MD Ripple Button

While working on creating a CSS button, I wanted to incorporate the Material Design ripple or wave effect into it. I came across a simple script on codepen that works well by adding the class "ripple" to various elements such as divs, buttons, images, and ...

The form contains an HTML table, but unfortunately, the buttons are not functioning as expected

I have encountered an issue with my form and table setup that I cannot seem to resolve. Despite researching similar problems, I have not been able to find a solution. In the code snippet provided below, you can see that when I click on the buttons for De ...

Using jQuery to extract a href URL from a div tag with jQuery

Is it possible to extract the href urls from anchor tags within a div tag? <div id="testing"> <a onclick="http://google.com">google</a> <a href="http://facebook.com">facebook</a> <a onclick="http://gmail.com">gmail< ...

The parseJSON function is not compatible with AJAX requests

I am attempting to use ajax and retrieve JSON data in WordPress. Here is my Ajax code: $.ajax({ type: "POST", dataType : 'html', url: "/wp-content/themes/myproject/ajax/otros_alojamientos.php", da ...

Is less.js capable of compiling CSS code that is compatible with all browsers?

Is there a simple way to convert my Less code into browser-compatible CSS like the example below? #grad1 { background: -webkit-linear-gradient(red, blue); /* For Safari 5.1 to 6.0 */ background: -o-linear-gradient(red, blue); /* For Opera 11.1 to ...

Ways to center align text in a div vertically

I'm working with 2 divs that are floating side by side. The left div contains a part number, which is only one line. The right div holds the product description, which can span multiple lines. I am looking for a way to vertically align the text in t ...

Passing data from PHP to jQuery through Ajax by returning an array

Currently, I am attempting to retrieve a PHP returned array using Ajax. This is the Ajax call that I have set up: https://i.stack.imgur.com/FNIvn.png Below is the PHP code that I am working with: https://i.stack.imgur.com/xe8VK.png The query is execut ...

Fluid layout causing elements in body to be hidden by fixed header

I'm currently tackling a fluid layout challenge, utilizing percentages instead of pixels to achieve it. I've managed to create a fixed heading (#heading) at the top of the page, but now I'm struggling with ensuring that all other elements on ...

What benefits does a dynamic website like this offer compared to one that is purely AJAX-based?

There are two main categories of dynamic websites that I define: The first type utilizes languages like PHP to concatenate an HTML page and send it back to clients. On the other hand, the second type uses an HTML template where all interfaces provide JSO ...

Exploring the font variables in the asset pipeline of Rails 7

Creating a habit of defining a set of root variables is crucial for maintaining consistency throughout an application. :root { --main-font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; --kbd-font-family: Consolas, "Libe ...

Pressing a specific button triggers a broadcast signal, prompting a modal window to pop up and display relevant information

Within my website's HTML page named rollup.html, there exists a button that triggers the opening of a modal. <button id="myBtn" ng-click="printDivModal('rollup-tab')">ModalTest</button> In the accompanying JavaScript file, roll ...