Achieve a multi-column layout on a webpage by ensuring two HTML elements remain on the same line

Trying to tackle this CSS challenge, but it's definitely been a head-scratcher for me.

I'm aiming to place two elements on the same line:

  • Textbox
  • Search icon image

The search icon should maintain its size regardless of width, while the textbox should occupy 100% of the available space (while still positioning the icon right next to it).

The issue I'm facing is depicted in this visual (the alignment is off due to the textbox taking up 100% width):

This is my current markup:

<div class="SearchBox">
    <div class="searchDiv">
        <div class="searchDivFullSpan">
            <asp:TextBox runat="server" ID="SearchOnGoogleBox" Height="34px"></asp:TextBox>
        </div>
        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Resources/Images/SearchIcon.png" OnClick="SearchBtn_Click" ToolTip="Search for content" Height="48px" OnClientClick="_gaq.push(['_trackEvent', 'Global_master', 'Search for content']);" />
    </div>
</div>

This is the accompanying CSS:

.SearchInputBox {
    border: 1px solid #C6D1AD;
    font-size: 20pt;
    background-color: #FAFAFA;
}

.searchDiv {
    padding-top: 10px;
    padding-left: 10px;
    padding-right: 10px;
}

.searchDivFullSpan {
    display: inline;
}

.searchDivFullSpan input[type=text] {
    width: 100%;
}

.searchDiv input[type="text"] {
    background-color: #f3f3e9;
    -moz-border-radius: 5px;
    border-radius: 5px;
}

Any helpful clues or suggestions would be greatly appreciated. Thank you!

Answer №1

If you want to improve the layout, consider using display table-cell instead of inline. You'll need to create a new container div and set both searchDiv and searchDivFullSpan to have a width of 100%. Make sure that the child divs of searchDiv are also set to display table-cell.

.SearchInputBox {
  border: 1px solid #C6D1AD;
  font-size: 20pt;
  background-color: #FAFAFA;
}

.searchDiv {
  padding-top: 10px;
  padding-left: 10px;
  padding-right: 10px;
}

.searchDiv div {
  display: table-cell;
}

.searchDivFullSpan {
  width: 100%;
}

.searchDivFullSpan input[type=text] {
  width: 100%;
}

.searchDiv input[type="text"] {
  background-color: #f3f3e9;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
<div class="SearchBox">
  <div class="searchDiv">
    <div class="searchDivFullSpan">
      <asp:TextBox runat="server" ID="SearchOnGoogleBox" Height="34px"></asp:TextBox>
    </div>
    <div class="searchImageDiv">
      <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Resources/Images/SearchIcon.png" OnClick="SearchBtn_Click" ToolTip="Søg efter et emne" Height="48px" OnClientClick="_gaq.push(['_trackEvent', 'Global_master', 'Search for content']);" />
    </div>

  </div>

</div>

<div class="SearchBox">
     <div class="searchDiv">
        <div class="searchDivFullSpan">
             <asp:TextBox runat="server" ID="SearchOnGoogleBox" Height="34px"></asp:TextBox>
         </div>
         <div class="searchImageDiv">
             <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Resources/Images/SearchIcon.png" OnClick="SearchBtn_Click" ToolTip="Søg efter et emne" Height="48px" OnClientClick="_gaq.push(['_trackEvent', 'Global_master', 'Search for content']);" />
          </div>

      </div>

</div>



<style>

    .SearchInputBox {
    border: 1px solid #C6D1AD;
    font-size: 20pt;

    background-color: #FAFAFA;
}

.searchDiv {
    padding-top: 10px;
padding-left: 10px;
padding-right: 10px;
}

.searchDiv div {
  display:table-cell;
}

    .searchDivFullSpan {
    width:100%;
    }

.searchDivFullSpan input[type=text] {
    width: 100%;
}

.searchDiv input[type="text"] {
    background-color: #f3f3e9;
    -moz-border-radius: 5px;
    border-radius: 5px;
}
</style>

Answer №2

Currently I am unable to test this, but have you thought about trying something similar to the following:

 .searchDiv{
       padding-left: 25px; //Set this equal to the width of your image plus a little extra
       position:relative;
  }

  .searchDivFullSpan input[type=text] {
        width: 100%;
  }

  input[type="image"]{
      width: 20px; //Adjust this as necessary, keeping it consistent with the above value
      position:absolute; 
      top:10px;  //Make adjustments to these two properties as needed
      left:5px;

  }

Answer №3

Consider this example with Bootstrap 3 for some guidance:

<div class="form-inline" style="display: block;">
  <input type="text" id="txt-field-1" class="form-control" size="4" max="4" min="4">
  <input type="text" id="txt-field-2" class="form-control" size="3" max="3">
  <button class="btn btn-info form-control" type="button" id="search-btn">
      <i class="fa fa-search"></i>
  </button>
</div>

To ensure elements remain side by side, add the class "form-inline" to the container Div. Use display:block if you want them to always stay next to each other regardless of screen resolution.

Answer №4

Give this a shot:

CSS CODE:

.SearchBtn{
    display:inline-block;
    float:right;
    bottom:24px;
    position:relative;
}
.SearchInputBox {
    border: 1px solid #C6D1AD;
    font-size: 20pt;

    background-color: #FAFAFA;
}

.searchDiv {
    padding-top: 10px;
padding-left: 10px;
padding-right: 10px;
}

.searchDivFullSpan {
    display: inline;
}

.searchDivFullSpan input[type=text] {
    width: 100%;
}

.searchDiv input[type="text"] {
    background-color: #f3f3e9;
    -moz-border-radius: 5px;
    border-radius: 5px;
}

HTML CODE:

<div class="SearchBox">
   <div class="searchDiv">
      <div class="searchDivFullSpan">
         <input type="text" id="SearchOnGoogleBox"/>
      </div>
      <input type="button" value="Click" class="SearchBtn"/>
   </div>
</div>

Check out the jsfiddle here!

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

What is the best way to horizontally align my menu items within a Bootstrap dropdown?

I am trying to modify a bootstrap nav menu so that the menu items become center aligned in the dropdown menu when the screen size is below 768px. Currently, the menu items are either staying floated or right aligned, and I can't seem to get them cente ...

Arranging Elements Individually Using Bootstrap 4 in a Row or Column

Utilizing Bootstrap 4, I am designing a row with two items placed in a column. The first item should always be aligned to the left of the row, while the second item is centered within the row. I have successfully achieved this layout using custom CSS. Is i ...

What is the best way to incorporate a percentage-based width scrollbar?

Help needed: I want to implement a scroll feature for the parent div but here's the catch - the width of the text inside the div needs to be in percentage. So, if there is a long name, it should scroll within the parent div. HTML: <div id="list"& ...

How is the color of the browser tab determined in Bootstrap?

I've been searching through Bootstrap documentation and Stack Overflow, but I haven't been able to find the answer I'm looking for. It seems like the main theme color, displaying differently in various browsers by tinting the tab bar. In S ...

Interactive grid feature created with HTML Canvas

Imagine my surprise when I discovered that the latest version of Google Spreadsheets is now using a canvas tag to render the spreadsheet grid, unlike the traditional <table><tr><td> method used in the past. In the previous version, only ...

The Firebase JQuery .on method is incrementally updating individual values in an array instead of updating them all simultaneously

I am trying to update the values of the orders placed by users on the Corporate's page without a refresh. I have implemented the jQuery .on method for this purpose. However, the values are being returned one by one from the array created for the order ...

The Hover Effect on HTML Element Not Working on One Side Specifically

Scenario: Currently, I am working on creating a popover (tooltip) using Bootstrap 3.3.7. In order to achieve this, I have structured my HTML file as follows: popover { margin-left: 100%; padding: 5px 11px; border: solid 1px; border-radius: 25px ...

Having trouble connecting my jQuery file to my HTML page

I am struggling to connect my jquery file to my HTML document. It seems like such a simple task, yet it is causing me quite a headache. Below is my HTML code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <meta ...

Encountering issues with submitting a request when using $.ajax GET function

I'm encountering an issue with the code functionality. The problem arises when a user clicks on the "Email page" link, as it should open in a fancybox with an address input and a submit button. However, upon clicking the submit button, nothing seems t ...

Running a Node.js child process upon clicking an HTML button

I need to create a basic webpage where clicking a button will open the command prompt. This is my goal. Below are the HTML and Node.js code snippets. test.html <html> <head> </head> <body> <p>guru</p> <form a ...

What is the best way to ensure the alignment of list content and numbering remains separate in HTML while using list-style-position set to inside?

Below is a list with customized styling: ol { background: #ff9999; padding: 20px; list-style-position: inside; } ol li { background: #ffe5e5; padding: 5px; } <ol> <li>Coffee</li> <li>Tea</li> <li>Coca ...

I am experiencing an issue where my Next.js application, developed using Tauri, is not displaying the

My next.js app has a file located at /app/not-found.tsx which functions perfectly when running: cargo tauri dev However, when I compile the app using: cargo tauri build or cargo tauri build --debug And I navigate to a non-existent page, instead of getti ...

Updating the background color and adjusting the text input color in the upcoming user interface

import {Input} from "@nextui-org/react"; <Input label="Email" radius='sm' /> https://i.sstatic.net/gprRpEIz.png I am looking for a way to customize the input text color and the label color separatel ...

Show the cell data when the checkbox next to it is selected

I have a specific table setup and I am trying to display the content of the table cell if its corresponding checkbox is checked upon clicking a button. For example: If Row2 is checked, an alert box should display Row2 Below is my code snippet, Java ...

Retrieve the current font style with Kendo UI Editor

Recently, I've been facing some challenges while working with the Kendo UI Editor. Specifically, I'm struggling to retrieve the current font style. My goal is to use JavaScript or jQuery to obtain the following values: Values I am looking for: ...

What could be causing my difficulty in locating an HTML element using jQuery/JS string interpolation?

In my project, I have a set of div blocks each identified by a unique numerical id. For instance: https://i.sstatic.net/lYod8.png One of the tasks in my project is to select the 29th element following a specific chosen element. For example, if I choose t ...

Guide on implementing automatic callbacks with AJAX

I am facing an issue with my index page that has one input tag and one division. There is an ajax function that retrieves data from the server, but I am encountering a problem. When I enter a single letter, it returns 3 records. Then, if I add another lett ...

determine the selection based on its numerical index

I'm looking to leverage the robot framework for automating user actions involving hovering over a bar chart. Is there a method to identify the bar chart by its index? Or what would be the appropriate selector for using the robot framework to hover o ...

Automatically select and pre-fill a list item based on its value using jQuery

I'm a beginner in JQuery. In my application I have the following: Here is my HTML code: <ul id="list"> <li> <a class="selected" value="0" href="#" id="sel">ALL</a> </li> <li> <a hre ...

Having trouble locating and interacting with the final li item in a ul list using Selenium Webdriver in C#

I am working with a chunk of code that resembles the following structure and my objective is to extract the text content (inner html; which should be 4) from the span element located within the button element under the last li. <ul class="xyz" ...