The alignment of image and text next to each other is not functioning as intended

I've been attempting to display an image and text next to each other, but I'm encountering some issues. The current output looks like the figure below: https://i.stack.imgur.com/WxxrS.jpg

The desired layout is as shown in the following link:

https://i.stack.imgur.com/IKNbq.jpg

Here is the code snippet I am using:

<div class="form widget widget-large">

    <form method="POST">
        @csrf

        <img src="https://www.pngitem.com/pimgs/m/325-3256412_buy-shopping-cart-add-product-ecommerce-icon-png.png"
            style="width : 30%; display: inline-block; vertical-align: top;">

        <div>
            <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
        </div>
        <div>
            <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
        </div>
        <div>
            <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
        </div>
        <div>
            <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
        </div>

    </form>

</div>

Despite following tutorials on aligning images and text side by side, it's still not working as expected. Can anyone point out what might be causing this issue?

Appreciate your help in advance.

Answer №1

To enhance the current code, incorporating inline-block on the divs with the name: elements is a simple solution. By utilizing this CSS property, you can instruct the browser to maintain elements on the same line rather than starting a new line automatically.

Below is an example snippet illustrating this concept:

<div class="form widget widget-large">

    <form method="POST">
        @csrf

        <img src="https://www.pngitem.com/pimgs/m/325-3256412_buy-shopping-cart-add-product-ecommerce-icon-png.png"
            style="width : 30%; display: inline-block; vertical-align: top;">

        <div style="display:inline-block;">
            <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
        </div>
        <div style="display:inline-block;">
            <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
        </div>
        <div style="display:inline-block;">
            <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
        </div>
        <div style="display:inline-block;">
            <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
        </div>

    </form>

</div>

While this adjustment addresses the immediate formatting concern, additional modifications may be needed. Additionally, the presence of label Name suggests the need for an associated input element, which could potentially be added in future iterations.

Answer №2

If you want to organize your content into two columns, consider splitting the areas left and right. You can achieve this by setting the left column with display:inline-block; and float:left. These CSS properties will align the column to the left, allowing space for another column on the right.

Once you have created the left and right columns, remember to apply these styles to the right column as well. Since the element is a block-level element, using display:inline-block; will prevent it from extending across the entire row.

.left, .right {
  display:inline-block;
  float:left;
  width: 50%;
}

img {
  width:100%;
  height:auto;
  display: inline-block; 
  vertical-align: top;
}
<div class="form widget widget-large">

    <form method="POST">
        <div class='left'>
        <img src="https://www.pngitem.com/pimgs/m/325-3256412_buy-shopping-cart-add-product-ecommerce-icon-png.png"
            >
        </div>

        <div class='right'>
          <div>
              <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
          </div>
          <div>
              <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
          </div>
          <div>
              <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
          </div>
          <div>
              <label for="Name" class="widget-title" style="width: 200px; display: inline-block;">Name:</label>
          </div>
        </div>

    </form>

</div>

Another Approach

Instead of manually handling layout with CSS, consider utilizing grid systems like Bootstrap's grid system. It simplifies creating multiple columns and offers flexibility in managing layouts.

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

Basic form submission versus using an Ajax loader mechanism

Handling forms can be done in various ways. One way is to submit the form and retrieve variables in PHP, while another method involves sending data with AJAX. During this process, a dialog can be displayed with information about processing the data, along ...

Attempting to design a uniform question mark enclosed within a circle using CSS

I'm attempting to use CSS to create a glyph of a question mark inside a circle, similar to the copyright symbol ©. a::before { content: '?'; font-size: 60%; font-family: sans-serif; vertical-align: middle; font-weig ...

Unable to retrieve the parent element using jQuery

I am facing an issue with my html structure that is generated dynamically through a foreach loop. I have attempted to remove the entire <a> element by accessing it from its ACTIVE HYPERLINK. However, all my efforts seem to be in vain as I am unable t ...

How can I prevent scrolling while a loader is displayed during page loading?

How can I disable scrollability on my personal website when the page is loading with the loader wrapper? I have tried using overflow: hidden; in the CSS but it didn't work. The loader is set to display for 2.5 seconds. Any suggestions on how to achiev ...

Angular is sending a parameter with a null value, which is not supposed to happen

My filtering system used to work well with a code that displayed items of specific status. However, I had to modify it to match a select input requirement. <ul id="dropdown-basic" *dropdownMenu class="dropdown-menu" role="menu" aria-labelledby="button- ...

IE9 causing trouble with CSS vertical alignment

I'm trying to center a button with text using Cuffon for the font, but I can't get it to align vertically and horizontally. The text-align property is working fine, but vertical align isn't. Here's the code for my div block: btndownlo ...

Angular 6 and Bootstrap 4 Collaborate for a Dynamic Multi-Level NavBar

(UPDATE: Issue Resolved - I discovered that I needed to include the JavaScript within $(document).ready(function()), which was previously missing. The example below worked perfectly for me.) I am attempting to implement a Multi-Level Navbar with Angular 6 ...

error in URI (not a valid URI):

Encountering an issue with a URI that is triggering a bad URI error. http://localhost:3000/api/v1/company_donations.json?token=foo&donation={&amount=101}&comment=Ordered The goal is to have the URL carry 2 attributes Token Donation obje ...

Ways to emphasize the currently active tabs on a navigation bar

How can I highlight the active tabs and shift it when clicking on another link? $(function() { $('.nav ul li').on('click', function() { $(this).parent().find('li.active').removeClass('active'); $(this).a ...

Is it possible to dynamically change HTML content by utilizing a JSON file?

Looking to implement a JavaScript loop (using jQuery) that can dynamically populate the HTML file with content from a JSON file based on matching <div> ids to the JSON "id" values. The solution should be scalable and able to handle any number of < ...

There seems to be a clash between Modal Semantic UI React and Bootstrap causing issues

I created a project using .net core MVC. I have integrated the semantic-ui-react modal by importing its library and linking the CSS for it to function properly. However, I encountered an issue where the bootstrap CSS was conflicting with the CDN for semant ...

The grid is intended to be positioned horizontally, but is currently being shown vertically

I need help fixing the layout of my items. They are currently displaying vertically instead of in a horizontal row. Any suggestions on how to correct this? https://i.stack.imgur.com/CQ4yG.jpg <div class="col-lg-12 col-sm-12 portfolio-item"> <d ...

Is your animation glitching due to axis restrictions?

I decided to create my own version of the Google Maps icon using Photoshop, and now I want to replace the current image with my duplicate when the link that wraps around both is hovered over. To achieve this, I used the position: absolute property to layer ...

Is it possible to update the value of an element using JavaScript?

My goal is to manipulate the content of a specific element on a third-party web page using a JavaScript Add-on in order to display a clickable hyperlink. I have already identified the link that I want to interact with on the page, and I believe document.g ...

The name 'Landbot' cannot be located. Have you meant to type '_landbot' instead?

I'm currently in the process of integrating Landbot into my React.js application with TypeScript. I'm following this [doc] 1. However, I'm facing an issue where the code inside useEffect (new Landbot.Container) is causing an error. 'C ...

Vuetify's scrolling list feature allows for smooth navigation through a list

Check out my Vuetify code snippet that utilizes a list: <v-list> <v-list-tile v-for="user in users" :key="user.id" avatar @click="" > <v-list-tile-content> < ...

Fade in/out overlay effect when clicking on a content block

I've hit a roadblock while trying to implement overlay fading in and out to cover a block created by some JavaScript code. Here is a link to the project. When you click on any of the continents, a series of blocks with country flags will appear. You& ...

Improving Page Load Speed with HTML Caching: Strategies for Enhancing Performance when over half of the data transferred is for navigation menus

I manage a complex and expansive website that contains a significant amount of repetitive HTML elements such as the navigation menu and top ribbon. Loading a single page on my site can be resource-intensive, with up to 300KB of data required, half of whic ...

A problem encountered in specific JavaScript code

As a newcomer to JavaScript, I have encountered an issue while trying to run this script: <html> <head> <title>Exploring javascript functionalities</title> </head> <body> <p id="demo">I ...

Tips for effectively utilizing the Angular ngIf directive for toggling the visibility of elements

<div *ngFor = "let element of myElements, let i=index" [ngClass]="{'selected':element[i] == i}"> <li> Name: {{element.element.name}}</li> <li> Description: {{element.element.description}}</li ...