Embedding an image within an HTML textbox using the textboxfor method

Looking to elevate the aesthetic of my project, I decided to incorporate a watermark into my @html.textboxfor. However, despite successfully adding it, I encountered an issue with compatibility on IE 8. As a solution, I aim to include an image within the @html.textboxfor to visually indicate to users what should be entered, especially in cases where the watermark fails to display.

Unfortunately, despite multiple attempts using CSS resources found online, I have been unable to achieve the desired result.

Below is a sample representation of the current state. Any insights would be greatly appreciated. Thank you.

Answer №1

There are a variety of ways to achieve this.

1- One method is to create the appearance of an image within an input text field.

2- Another approach is to set a background for the input field.

Approach 1:

Consider the following example:

JS-FIDDLE-DEMO

HTML:

<div class="some-input-parent" style="border: 1px solid #DDD;">
   
      <input class="some-input" style="border: none; "/>   
      <img class="some-input-img"  src="yourImage.png"/>   

</div>

CSS:

.some-input{
width:120px;
}
.some-input-img{
width:15px;
}

.some-input-parent{
  width:140px;
 }

The key is to use a div or span that contains an input field, with the image aligned either left or right (depending on your preference).

The span's width should match the input field's width plus the image's width, along with additional space for borders and padding.



Approach 2:

By setting a background and adjusting its position, you can push it to the right. Add padding to the input field to expand its outer width and prevent text overlap with the image.

JS-FIDDLE-DEMO2

HTML:

<input class="real-input" type="text" />

CSS:

.real-input
{
    background-image: url(youIcon.png");
    background-position: 119px 0;
    background-repeat: no-repeat;
    background-size: 17px auto;
    padding-right: 26px;
    width: 111px;
}

Outcome:

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

Adjusting the flex columns to be mobile-friendly for various screen sizes, I noticed that the HTML text is spilling over into the adjacent section and the columns appear to

Hi everyone, I've been diligently working on my portfolio website and I'm thrilled that it appears responsive on mobile devices, my Macbook Air, and both of my desktop monitors. However, I've encountered an issue on my Lenovo laptop where th ...

Create a left-aligned div that spans the entire width of the screen, adjusting its width based on the screen size and positioning it slightly

I have a parent container with two child elements inside. I want the first child to align to the left side and the second child to align to the right side, but not starting from the exact center point. They should be positioned slightly off-center by -100p ...

Employing CSS for page breaks, while contained within a DIV that is absolutely positioned

I have integrated the ASP.NET AJAX ModalPopupExtender into a webpage. The popup displays details of customer orders that need to be printed, with each order ideally on a separate page. Unfortunately, the CSS page-break-after style doesn't seem to wor ...

How can you create a scenario in Angular Material where items in a toolbar transition to the second line exclusively on mobile screens?

Visit the Angular Material website to see how the toolbar appears on a desktop: https://material.angular.io/ https://i.sstatic.net/KPFMv.png On mobile devices, the menu items Components, CDK, and Guides are displayed on the second line, while github, the ...

What is the best way to add animation to my `<div>` elements when my website is first loaded?

I am looking for a way to enhance the appearance of my <div> contents when my website loads. They should gradually appear one after the other as the website loads. Additionally, the background image should load slowly due to being filtered by the wea ...

Tips for transferring input values between two PHP pages

I need to save various input values in a MySQL database, such as first name, last name, phone number, and mobile number. With a select query, I can retrieve all the entries from the database and display them on a single webpage. When a user clicks on a sp ...

Issues with CSS margins in IE causing bugs?

Currently dealing with a CSS issue that seems to be specific to Internet Explorer. I haven't encountered it in Firefox, Safari, or Opera during testing with IE 11. The problem: I've set the top margin of the website content/stage to around 2%, ...

Achieve maximum height with background images

Having trouble with responsiveness on my box containing a background image. I am aiming for the box to adjust its height based on the background image, as I have set the background-size attribute to auto. html: <div class="index_boxs" id="discover_win ...

'state' has not been defined error (no-undef)

Hey there, I'm currently enrolled in a React course and I've hit a roadblock that I can't seem to overcome. The instructions recommend using the Spread operator '...' instead of 'this.' to select elements from an array. H ...

A guide to spinning an image in every direction with CSS

I'm working on rotating an image in all directions using CSS and Vue.js. To demonstrate this, I have created a CodePen with the necessary code below. <div id="app"> <v-app id="inspire"> <div class="text-xs-center image-rotation" ...

Adjust the content of a table cell using jQuery

<td>09 Mars 2020</td> Using the string above, I have created a div. Here are the Jquery variables I am working with: date1 = 09 Mars 2020 date2 = 18 Mars 2020 My goal is to combine the content from another date with the existing date to ach ...

The drop-down menu in the navbar is always positioned above any fixed elements on the page

I am facing an issue with a drop down menu in a nav-bar. The problem occurs when the drop down menu goes behind a fixed-position div below the nav-bar. I have tried using z-index but it doesn't seem to be working. Setting the position of the dropdown ...

Tips for placing a div within a flex item

Struggling to get it right, here's my query: This is how my DOM looks: <div id="content"> <div class="listitem"> <img class="thumbnail"/> <div class="option_icon"></div> ...

Creating a JavaScript object and retrieving the values of numerous input fields with identical classes

I have encountered an issue that I need assistance with: <input title="1" type="text" class="email"> <input title="2" type="text" class="email"> <input title="3" type="text" class="email"> The HTML code above shows my attempt to extract ...

Is there a way to adjust the dimensions of individual pictures?

Dealing with a variety of images within a resizing container can be tricky. I have a solution for adjusting each image to a specific size without affecting the others. Currently, the container's CSS is set to resize images based on browser size: .con ...

Is there a way for me to move from the initial tab to a different tab within my registration form?

After searching through various resources on stackoverflow, I am still unable to switch from the first tab to another tab. Can someone please point out where the issue lies in my code? You can view the code on JSFIDDLE My files include custom.js, style.c ...

Similar to the :has() in jQuery, the equivalent in CSS

Consider the code snippet below: <div class="section"> <div class="row">...</div> <div class="row"> <!-- bottom margin here needs to be 0 --> <div class="section"> &l ...

Check if the browser version is higher than IE9 or if it is not an IE browser

I am looking to include history and ajaxify only if the browser is ie9 or higher, OR is not ie: <!--[if gte IE 9]> <script type="text/javascript" src="assets/js/plugins/history.js"></script> <script type="text/javascript" src= ...

What is the process for mediating a SWF-Flash file with a PHP mediating, also known as an HTTP-Pseudostreaming script?

I am currently developing a mini context media platform that utilizes HTTP Pseudostreaming. This involves using a PHP script to manage the media file, ensuring proper access and linking to the correct directory. Below are the key components of my code: // ...

Ways to reduce the size of the background image in a select element?

Utilizing the bootstrap form-select select element : <select class="form-select listbox_length_menu_datatable" <option value='5'>5</option> <option value='10'>10</option> <option value ...