What is the best way to position an image next to an input element?

How can I position submit.png next to the input element in my code?

<div class="input-box" style="height: 40px; width: 500px;">
            <img src="img/delete.png" width="25px" height="25px;" style="position: absolute; right: 0;">
            <input id="answer" type="text" style="border: none; border-bottom: 2px solid silver; height: 100%; width: 100%; font-size: 17px; background-color: #ffffff00; outline: none; color: white; font-weight: bold; text-align: center;" required readonly oninput="let p=this.selectionStart;this.value=this.value.toUpperCase();this.setSelectionRange(p, p);">
            <img src="img/submit.png" width="25px" height="25px">
        </div>

I have tried using display: inline-block and float: right attributes as suggested in other articles, but it did not work. Can someone please assist me with this issue?

Please help me

Answer №1

Although I'm not entirely certain if I grasped the question correctly, allow me to offer a solution utilizing the display: flex property.

<div class="input-box" style="height: 40px; width: 500px; display: flex;">
    <img src="https://picsum.photos/200" width="25px" height="25px;" style="position: absolute; right: 0;">
    <!-- INPUT FIELD -->
    <input id="answer" type="text" style="border: none; border-bottom: 2px solid silver; height: 200px; width: 200px; font-size: 17px; border: 1px solid red; background-color: #ffffff00; outline: none; color: white; font-weight: bold; text-align: center;" required readonly oninput="let p=this.selectionStart;this.value=this.value.toUpperCase();this.setSelectionRange(p, p);">
    <!-- SUBMIT -->
    <img src="https://picsum.photos/200" width="25px" height="25px">
</div>

If this solution doesn't meet your requirements, please let me know so that I can further assist you.

Here is a helpful guide on "Flex" for reference: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Answer №2

To arrange all elements in a div side by side horizontally, utilize the following CSS:

.container{
    display: flex;
 }

For vertical alignment at the center, apply the style shown below:

.container{
    align-items:center;
 }

Answer №3

After setting the container's display to flex and adjusting the order of the items according to your specifications, I have also made sure that the input covers the entire width of the container. This will allow you to easily add margins to the items as needed. If you have any questions or need further assistance, please don't hesitate to reach out.

.input-box{
  display: flex;
  align-items: flex-end;
}

.input-box input{
  flex-grow: 1;
}

.input-box img:first-child{
  order: 3;
}

.input-box img:last-child{
  order: 2;
}
<div class="input-box" style="height: 40px; width: 500px;">
  <img src="img/delete.png" width="25px" height="25px;">
  <input id="answer" type="text" style="border: none; border-bottom: 2px solid silver; font-size: 17px; background-color: #ffffff00; outline: none; color: white; font-weight: bold; text-align: center;" required readonly oninput="let p=this.selectionStart;this.value=this.value.toUpperCase();this.setSelectionRange(p, p);">
  <img src="img/submit.png" width="25px" height="25px">
</div>

Answer №4

After some trial and error, I decided to implement a table layout for the solution I was working on.

<table style="border-spacing: 30px;">
            <tr>
                <td style="height: 40px; width: 500px;">
                    <img id="backspace" src="img/delete.png" width="25px" height="25px;" style="position: absolute; right: 95px; cursor: pointer;">
                    <input id="answer" type="text" style="border: none; border-bottom: 2px solid silver; height: 100%; width: 100%; font-size: 17px; background-color: #ffffff00; outline: none; color: white; font-weight: bold; text-align: center;" required readonly oninput="let p=this.selectionStart;this.value=this.value.toUpperCase();this.setSelectionRange(p, p);">
                </td>
                <td>
                    <img id="submit" src="img/submit.png" width="35px" height="35px" style="cursor: pointer;">
                </td>
            </tr>
        </table>

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

Is there a way to showcase an HTML body in the Gmail app on iOS?

I am facing a challenge while trying to incorporate HTML formatting in the body of an email shared through Gmail using UIActivityViewController on iOS. Despite my efforts, the Gmail application fails to render the HTML content properly and only displays pl ...

The results from various <div> elements are displayed based on the selection made from the dropdown menu

<body> <label for="country">Country : </label> <select id="country"> <option>Please select</option> <option name="CountryRevenue">Revenue</option> <option name="CountryQuantity">Quantity ...

Interactive pop-up window featuring conversational chat boxes

Trying to create a comment box within a Modal dialog box that is half of the width. The comments in this box should be read-only and created using div tags. Attempted reducing the width and using col-xs-6, but ending up with columns spanning the entire w ...

Content positioned beside an image with the help of Bootstrap 4 framework

How can I align text to the right of an image, like a table layout? I experimented with grid layout using <div class="span2"> and <div class="span10"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bo ...

When viewing my website on a mobile device in landscape mode, the CSS divs sized at 20% do not match the size of the div set at

Currently, I am working on a mobile application that is HTML5 cross-platform compatible. To design the app, I am utilizing Bootstrap 3, although it seems unrelated to the specific issue I am facing. Here's the problem at hand: Within my application, ...

Processing PHP forms with dynamic form inputs

Have a form with the capability to add multiple input fields for ordering pictures by picture number. A customer could potentially order anywhere from 1 to 100 pictures. How should I handle this in PHP? Creating up to 100 $_POST[] variables for each poss ...

Enable a single column to scroll until the content is fully displayed, and then stay in a fixed position

My HTML content consists of two columns, with the first column being a sidebar that should have limited content. The second column holds the main content and can span several pages. The desired functionality is for the first column to scroll until the end ...

SQL query for finding the number of shared nodes between two given nodes

How can we determine the common nodes between two specific nodes in SQL, using the following example: Count the occurrences of <li> tags between <h2 id="vgn">VGN A </h2> and <h2 id="vgn">VGN </h2>, as well as the total number ...

pressure exerted on the body

Recently, I've been working on a project for the website . One of the main issues I've encountered is that the <body> element is consistently 20px lower than it should be at the top. This has led to visible problems with the background grad ...

Execute a JavaScript function when an HTML list element is clicked

As a beginner in web development, I have a question that might seem obvious to some. I want to create a menu where each item, when clicked, triggers a JavaScript function with the item's ID as an argument. I plan to display the menu items in an unorde ...

What is the process for manually looping an HTML5 video on iOS 4.3/5?

I'm facing an issue with a video that has specific segments I need to be looped. The problem arises on iOS 5, where after updating videoElem.currentTime for the third time, mobile Safari stops recognizing this attribute correctly. Interestingly, on i ...

Is there a way to ensure consistent heights for various elements within a column, even if their heights are

Currently, I am utilizing the flex property to create uniform columns and vh to ensure they have the same height. This setup is functioning properly, but within each column, there could be a varying number of items. I am interested in having the elements w ...

Avoid the CSS ::before content from being highlighted

I have implemented the ::before pseudo-element for my ordered and unordered lists in my application. However, I am facing an issue where only the ordered lists are highlightable, and I am unable to change the highlight color to match the overall look of my ...

Looping through color transitions upon hover using CSS

I am trying to create a color transition effect on hover, where the background changes from yellow to red and then back to yellow in a loop. I'm having trouble figuring out how to make this transition repeat continuously. Do I need to incorporate Java ...

The link that has been clicked on should remain in an active state

Is there a way to make the link that is clicked on active? I have attempted various scripts but have had no luck in getting the desired effect. Can anyone identify what might be causing the issue? $("a").click(function () { if ($(this).hasClass("acti ...

Is there a way to access comprehensive data pertaining to an ID through Ajax?

I need help with an Ajax call. Below is the code I currently have: $.ajax({ async: true, url: 'test/', type: 'POST', datatype: 'text json', data: { id: id, }, success: function(data) { // Retrieve the da ...

Fetching large images with "fill" layout in NextJS

I am currently using Cloudinary to serve correctly sized images, however, it appears that NextJS image is always fetching with a width of 3840, instead of the fixed value. <Image src={loaderUrl(logoImage)} alt="" role="presen ...

Footer remains fixed and scrolls into the body when the window is resized

Having trouble with the footer's responsiveness when resizing the desktop window. It looks fine in developer tools, but in different browsers, the footer placement gets messed up and scrolls over the rest of the content. Any insights on what might be ...

App.controller attributes in HTML tags are not receiving the value of the HTML variable

In the process of developing a simple student-teacher web application, I am utilizing AngularJS to handle the front-end. Within my JavaScript file, there are two app controllers - one for fetching student data and another for retrieving subjects assigned t ...

Issues with properly functioning collapse button in Bootstrap 5.1.3 Navigation Bar

When I initially click on the collapsed nav-bar button, it expands to show the nav-bar contents. However, upon clicking the button again, it fails to collapse. Below is the corresponding HTML code snippet. *<!-- CSS Stylesheet -->* <link href= ...