Is it possible to display the <input type="text"> and the following <div> element on the same line instead of having space between them?

Could you please advise on how to adjust the layout to eliminate the space between the input type="text" and the following div element? When I set the width of the input type="text element to 180px and the width of the subsequent div to 20px, the div overflows and shifts downward. I am trying to avoid using margin-left: -...px on the child div to align them horizontally (since it requires different values for Firefox and Chrome) but I still want both elements to be perfectly inline without any gaps and spanning edge-to-edge of their parent element in all browsers.

Your suggestions are appreciated...

CSS:

#parentDiv {
  display: block;
  width: 200px;
  height: 20px;
  background-color: rgb(200,200,200);
  border: dotted 2px;
  border-radius: 0px 0px 0px 0px;
}
#parentDiv input[type="text"] {
  display: inline-block;
  width: 175px;
  height: 90%;
  border: none;
  vertical-align: top;
}
#parentDiv div {
  background-color: #57c274;
  display: inline-block;
  cursor: pointer;
  color: #ffffff;
  text-decoration: none;
  height: 100%;
  width: 20px;
  /*margin-left: -6px;*/
  text-align: center;
}
<div id=parentDiv>
  <input type="text">
  <div></div>
</div>

Answer №1

Make a Transformation

<div id='parentDiv'>
  <input type='text' value='' /><div></div>
</div>

Convert

<div id=parentDiv>
  <input type=text>
  <div></div>
</div>

When it comes to HTML, any white spaces or line breaks will condense into a single space. As there is a line break before the <div>, a text node exists in that position.

Answer №2

Just use float on your elements

Then add these styles to ensure consistency across all modern browsers:

#parentDiv {
    width: 200px;                                               /* Set the width to 200px */
    height: 20px;
    background-color: rgb(200,200,200);
    border: dotted 2px;
    border-radius: 0px 0px 0px 0px;
}
#parentDiv input[type="text"] {
    box-sizing:border-box;       /* Respect the designer :) */
    width: 180px;                                               /* Set the width to 180px */
    height: 100%;                /* Adjust height to 100% */
    border: none;
    float:left;                  /* Float left */
}
#parentDiv div {
    background-color: #57c274;
    float:right;                 /* Float right */
    cursor: pointer;
    color: #ffffff;
    text-decoration: none;
    height: 100%;
    width: 20px;                                                /* Set the width to 20px */
    text-align: center;
}

Check out the jsBin demo here

Answer №3

Here is an additional snippet that builds upon the solution provided by PHPglue:

#container {
  display: block;
  width: 200px;
  height: 20px;
  background-color: rgb(200,200,200);
  border: dotted 2px;
  border-radius: 0px 0px 0px 0px;
}
#container input[type="text"] {
  display: inline-block;
  width: 180px;
  height: 90%;
  border: none;
  vertical-align: top;
}
#container div {
  background-color: #57c274;
  display: inline-block;
  cursor: pointer;
  color: #ffffff;
  text-decoration: none;
  height: 100%;
  width: 20px;
  /*margin-left: -6px;*/
  text-align: center;
}
<div id="container">
  <input type="text"><div></div></div>

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

Ajax is transforming a div into a sleek slider

I was able to dynamically load a div using Ajax that has an id of "slider". My goal is to use the jQuery statement $('#slider').orbit(); Given that this content is loaded through Ajax, I'm unsure of the best approach. In previous instances ...

Optimizing Style Inclusion Order with Vue SFC, vue-loader, and webpack

My Vue/SFC/webpack/vue-loader application integrates bootstrap using 'import', with component styles directly included in the SFCs. However, vue-loader always places bootstrap after the component styles, preventing proper overwrite/cascade over b ...

AngularJS synchronous $resource functionality allows for the ability to make parallel API

I'm facing a dilemma because I understand that Javascript isn't designed for synchronous work, especially in the case of AngularJS. However, I find myself in a situation where I require it. The main page on the "www" domain (built with AngularJS ...

Designing a toggle button interface with Material UI

Looking to add a toggle button to my page. Here is an image of what I have in mind: https://i.sstatic.net/tiQAP.png Unable to find any material-ui component or API for this specific toggle button design. Considering building a custom toggle button with CS ...

Conceal a Component within an Embedded Frame

Hey there! I've been attempting to hide a header within an iframe, but it seems like my code isn't doing the trick. Could someone take a look and help me diagnose why the header is still visible? Thanks in advance! <iframe id="booking_iframe" ...

Update the link's name and href using jQuery

i have a link with this href <div id="site_change"> <a id="mysite" href="https://halloweentorino.com/">halloween torino</a> </div> but I want to change the href and the name of the link within it <div id="site_change"> ...

The jQuery text search/filter feature is effective in most cases, but some fields may not be searchable

Check out the live version here My current setup includes a jQuery search feature that filters cards (divs) based on text found in the details field. However, I want to expand this functionality to include filtering by other fields like title, tags, and s ...

When using Node Puppeteer, if the page.on( "request" ) event is triggered, it will throw an error message stating "Request is already being handled!"

I am currently utilizing puppeteer-extra in conjunction with node.js to navigate through multiple URLs. During each iteration, I am attempting to intercept certain types of resources to load and encountering the error below. PS C:\Users\someuser ...

Codeigniter code to retrieve dynamic data in a select box

I am trying to implement a functionality where selecting an option from one dropdown will dynamically populate the options in another dropdown based on the selection. I believe I need to use an onchange function, but I'm unsure how to retrieve data fr ...

Effortless Wordpress AJAX Retrieval

Currently, I am facing an issue with my JavaScript file where I am using AJAX to retrieve a PHP file containing WordPress functions and basic HTML. While the JavaScript file works perfectly in fetching the file using jQuery, it encounters WordPress errors ...

Help needed with parsing nested JSON using the $.each function

Here is a JSON response sample that needs to be parsed in a more generic manner, rather than using transactionList.transaction[0]. "rateType": interestonly, "relationshipId": consumer, "sourceCode": null, "subType": null, "transactionList": { "transac ...

Explore using Laravel for your next search project!

I am working on implementing a search feature for multiple models. After setting up the route and html form, I have successfully retrieved the data in my controller. Now, I am looking to query multiple columns and receive the result as an eloquent or array ...

The CSS filter failed to function properly as the Chrome Inspector is displaying a strik

I recently encountered an issue with my website where I was trying to apply a CSS dropshadow effect to an item, but it just wasn't working. Upon inspecting the code in Chrome Inspector, I noticed a strike through on the rule (refer to screenshot). Eve ...

The presence of element a within the element ul is not permitted in this particular scenario. Additional errors from this section will not be displayed

Having trouble with this code snippet: <nav role="navigation"> <div id="menuToggle"> <input type="checkbox"/> <span></span> <span></span> <span></span> ...

The appended element continues to add infinitely as you scroll, when it should only be appended once

I have successfully added a div to a form after scrolling, but now I am facing an issue where this div keeps appearing indefinitely as the user scrolls. You can view this troublesome div on the following website: . It is the close button that appears at t ...

I have incorporated jquery-1.2.6.js into my project but I am encountering difficulties utilizing the live method

C# foreach (DataRow Row in oDs.Tables[0].Rows) { LitPreferances.Text += "<Li ID=LI_" + Row["pk_Preference_Branch_ID"].ToString() +"_"+ Row["pk_Preference_BranchType_ID"].ToString() +">" + Row["Branch_Name"].ToString() + "&nbsp;&nbsp;< ...

How can I display an image caption within a lightbox using jQuery?

There is a caption under each image. <p> <a href="large.jpg"> <img width="85" height="75" src="thumb.jpg/> </a>Caption</p> I am looking to display the same caption at the bottom of the lightbox. <span id="lightbox-image ...

What is the method to showcase content by category with a button click within the same page using Django?

I am currently working on a portfolio project where I need to categorize the projects based on their categories. I have added buttons for each category and I want to display the contents of each category below the respective button when clicked. For exampl ...

Issues encountered when trying to retrieve data from an Express server

I have set up a NodeJS server with Express.js and I am attempting to send a request to it using fetch. fetch("http://localhost:3001/api", { method: "POST", headers: { "Content-Type": "application/json", ...

JavaScript Transformation of Date Format

var dt="29/05/2013"; //DD/MM/YYYY I am looking to change the format to yyyy/MM/dd; My current approach is: var newdate=dt.getFullYear()+"/"+dt.getMonth()+"/"+dt.getDate(); Is there a more straightforward way to convert it without using substring? No p ...