How can we ensure that the borders of a text field automatically adjust to fit the dimensions of the text field using CSS?

I've encountered an issue with the borders of text fields while working on a project involving CSS/HTML. The problem is that in the browser, the borders appear shorter than the text fields.

Initially, I attempted to adjust the border size to match the text field, which seemed to work. However, upon testing it on a larger screen using Internet Explorer (IE), the same problem reappeared.

Below is a snippet of my code:

<tr>
  <td>First Name</td>
  <td>
    <div style="border:4px #26a570 solid;
            background-color:white; 
            width:37.5%; 
            height:15%">
      <input type="text" name="txtFirst" id="txtFirst" />
    </div>
  </td>
</tr>

I am looking for a solution where the border automatically scales to match the size of the text box across all browsers and screens.

Answer №1

To start, as mentioned by @Nimsrules, place the border around the input field. Next, utilize a viewport to ensure scalability. The viewport should be included in the head section of your code. Instead of using percentages, consider utilizing vw for width and vh for height.

For example:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<tr>
    <td>First Name
</td>
    <td>
<div> 
    <input type="text" name="txtFirst" id="txtFirst" style="border:4px #26a570 solid;background-color:white; width:37.5vw; height:15vh"/>
</div>
</td>
  </tr>

Check out this Fiddle

For more information on viewports, you can visit this link.

It's advisable to avoid inline CSS and opt for a separate CSS file instead. This resource provides insights on why inline CSS is not recommended: this article.

Answer №2

To fix the issue, adjust the styling on the input element.

The reason it's not working is because you also have set width:37.5%; on the parent element.

<table>

<tr>
  <td>First Name
</td>
  <td>
<div> 
  <input style="width: 200px; border:4px #26a570 solid;background-color:white; height:15%" type="text" name="txtFirst" id="txtFirst" />
</div>
</td>
</tr>
</table>

Answer №3

Applying a border to the input field can be tested on Internet Explorer.

If it doesn't seem to be working, consider removing the current border CSS properties, such as

border-color: grey;

and

border-style: solid;

Instead of using a single tag for styling, giving this method a try might yield better results.

Fingers crossed for success!

Answer №4

Greetings! It's not recommended to use inline styles as they can make editing a hassle. It's better to create a class and define the styles in your CSS file instead :)

Take a look at your HTML structure below:

<table>
  <tr>
    <td>First Name</td>
    <td>
      <div> 
          <input class="input-wrapper" type="text" name="FirstName" />
      </div>
    </td>
  </tr>
</table>

Here are the corresponding CSS styles:

input.input-wrapper{
  border:5px solid green;
}

And lastly, see the difference with an INLINE style included:

<input style="border:5px solid green;" type="text" name="FirstName" />

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

scrollable list using bootstrap

<div class="panel panel-primary" id="result_panel"> <div class="panel-heading"><h3 class="panel-title">List of Results</h3> </div> <div class="panel-body"> <ul class="list-group"> &l ...

Transforming a multi-row table into a list of dictionaries using Beautifulsoup

I'm facing an issue with converting an HTML table into a list of dictionaries. The table has limited attributes, and I need to ensure accurate parsing. Here's the relevant section: <div class="table-container"> <table> ...

The HTML element failed to be inserted

Currently, I am involved in a project based on .NET Core for my organization. This project entails loading work orders from our SQL database using Entity Framework and then filtering them to display as markers on a map via the Google Maps API for our insta ...

Placing text alongside an image at the top of the page

Here's the HTML code generated by JSF: <div align="center"> <img src="images/background_image.jpg" height="200px" width="30%" style="vertical-align: top"/> <span style=""> Welcome abc, <input type= ...

Bootstrap slider aligned to the right side

I'm currently utilizing bootstrap 3 for my website. However, I encountered an issue with the slider when viewing the page on a mobile device as the image was displaying outside the viewport, shifted to the right. Additionally, in Firefox, the slider ...

Adjusting the height of a Vue component to 100% triggers a change in height whenever a re-render occurs

In my Vue component, there is a class called "tab-body-wrapper" that serves the purpose of displaying the "slot" for the active tab. However, I encountered an issue while troubleshooting where the height of the ".tab-body-wrapper" component reduces during ...

Basic HTML code that displays either one or two columns based on the width of the screen

My monitoring website displays dynamic rrd graphs in png format with fixed dimensions. I am looking to adjust the layout based on browser window size - showing the graphs in 1 column if the width is below a certain threshold, and in 2 columns if it exceed ...

The function of style.marginRight differs from that of style.marginLeft

One function aligns content to the left while the other doesn't align it to the right function openNavLeft() { document.getElementById("mySidenavLeft").style.width = "100vw"; document.getElementById("main").style.marginLeft = "100vw"; } function ...

The lifecycle of XMLHTTPRequest objects in JavaScript - from creation to destruction

After years of working with traditional compiled object-oriented languages like C++ and .NET programming, I decided to dip my toes into JavaScript for a new project. As I was experimenting with AJAX, I stumbled upon a perplexing aspect related to how objec ...

Is there a way for me to duplicate a complex element multiple times within the same page?

As an illustration, let's say I have a social tab located in my header that I would like to duplicate in the footer. This tab is comprised of various buttons with SVG images on top, event listeners linked to button IDs, and CSS styling. One option is ...

Elevate Your Design with Bootstrap 4 - Rearrange Rows with Ease

I have been browsing the Bootstrap documentation, but I am unable to locate the specific class needed to achieve a certain effect. Is there a method for shifting or offsetting a div Row if the parent row has a column that pushes down the bottom div? Can th ...

Transferring Data through the POST Method

Is there a way for me to send just one variable using the post method? I have retrieved the $row[id] from the database and need to include it in the form submission. We know how to send user input using dfs, but what about sending the specific $row[id] v ...

What is the best way to position a div at the bottom of the main div?

How can I position a div with the class "boxLinks" at the bottom of the main box with the class "main-box"? Here's my idea: The main class has a width of 1200px; within this main class, there are 5 divs with each div having a width of 25%. .main- ...

Once AJAX/GET has been utilized, further execution of jq functions such as .click may not be

I'm facing an issue with AJAX/GET code. After reloading data from a PHP file using jQuery, I have another block of code that my problem lies in the fact that after reloading data from MySQL/PHP server while the page refreshes, I can't use functio ...

Using Javascript to dynamically add and remove elements on click

Whenever I click on a link, I am able to generate input, label, and text. However, I want to be able to delete these elements with the next click event on the same link: I've tried updating my code like this: var addAnswer = (function() { var la ...

Customize Your Wordpress Category Title with a Background Image

Within my wordpress template, there is a module known as new_boxs. The code snippet below shows that this module displays on the page six times, with two columns wide and three rows down: foreach($data_cat as $cat){ ?> <div class="span6"> ...

Deciphering the :host attribute in CSS: A guide

When using Selenium to query the CSS properties of an element, I noticed that even though the actual background color is white, the returned value for background-color is always #000000. This issue arises in an Ionic-built app. Upon inspecting with Chrome ...

Is there a method, like using jQuery, to insert `<br>` tags into each line of a dropdown select list?

I need help with implementing a select tool in a limited horizontal space with each line containing a lot of information. I attempted to embed a tag into the line but it didn't work. Are there any solutions available that could provide a simple fix ...

How can I use JQuery to iterate through Object data and dynamically create tr and td elements?

In previous projects, I utilized Vanilla JS to iterate through Ajax return data and construct tables. However, for this current project, I have decided to switch over to JQuery. The main reason behind this decision is that some of the td elements require s ...

Angular ensures that the fixed display element matches the size of its neighboring sibling

I have a unique challenge where I want to fix a div to the bottom of the screen, but its width should always match the content it scrolls past. Visualize the scenario in this image: The issue arises when setting the div's width as a percentage of the ...