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

Having trouble with moving the svg:svg element?

I'm struggling to move an svg element that is nested within another svg. I am trying to directly manipulate the x and y values, but I keep encountering a "read-only" error. I attempted to use transform instead, but it doesn't seem to have any eff ...

Creating HTML tabs using jQuery with input[type="radio"] tutorial for beginners

I'm having trouble creating a tab with input[type="radio"] using Jquery. The tab works fine on the first click, but it doesn't work on the second click. I can't figure out where the issue is in the Jquery code. Can someone assist m ...

Show the json data on a PHP table

I've been attempting to showcase JSON content in a PHP table, but I keep encountering errors. It seems like there are some syntax issues that I can't quite pinpoint. Any suggestions on what needs to be modified? PS. I'm using the Slim frame ...

Having trouble with the CSS drop down menu in Internet Explorer 10?

Can you assist me with a challenge I am facing? I am currently working on creating a dropdown menu using only HTML and CSS. However, I have encountered an issue where the hover function is not functioning properly in IE10, although it works perfectly fine ...

Tips for dividing the WordPress header navigation horizontally using separate PHP files

I am trying to customize my WordPress menu navigation by splitting it into two sections: left and right. I have added the menu function in the "functions.php" file to create the menu from the backend, and the frontend modifications will be done in the "hea ...

When using the Column width property in React Material UI, it may not automatically expand

I've been playing around with React Material UI Tables and I'm having trouble making the columns auto-expand without wrapping. I tried setting the 'width: auto' property in the <Table size="small" style={{ width: auto}}> ...

When a user chooses an item, the ui-select dropdown appears hidden behind additional fields on the screen

In my AngularJS application, I am utilizing ui-select within a table that repeats rows using ng-repeat. The following code snippet displays how ui-select is implemented: <ui-select name="{{'selProperty' + $index}}" ng-model="thing.property" ...

Angular package that includes horizontal scrolling functionality with arrow buttons

Currently seeking an Angular 2-6 package featuring a horizontal image scroll with arrows, similar to the functionality on Airbnb: Any suggestions or recommendations are greatly appreciated – thank you! ...

Selecting Messages in a Scala Play Application

After running the sample code in play 2.2 "comuputer-database-jpa", I came across a specific part in the Conf/Messages file: # Messages computers.list.title={0,choice,0#No computers|1#One computer|1<{0,number,integer} computers} found I found this co ...

Retrieve JSON data by making a POST request to a website's API

Can you help me retrieve Json data from a website API using JavaScript? I'm trying to fetch quotes from the following website: for my quotes generator page. While I have some understanding of making GET requests, it seems that this API requires POST ...

The oversized image is refusing to scale down to fit within the confines of the CSS grid container

I am currently facing a challenge with creating a responsive image within a grid layout using CSS. The image is not resizing properly when the browser window changes size and is extending beyond its container borders. I have experimented with various techn ...

What is the best way to divide an image into smaller rectangles for use on iPhone, Android, or Rails platforms

Can you help me develop an app for iPhone and Android that turns personalized photos into jigsaw puzzles? I am aware that similar apps already exist. What would be the most effective method to divide a customized image into smaller rectangles? I came acro ...

Issue with CSS pseudo-elements :before and :after in Internet Explorer

I am attempting to create a heading with lines on both sides, but I am facing an issue where it only seems to work on the left side in IE. Check out the code here h2 { position: relative; overflow: hidden; text-align: center; } h2:before, h2:afte ...

Execution of Ajax call fails to occur synchronously

I have created a unique weather website that utilizes the flickr API as well as the yahoo API to gather weather data. However, I am facing an issue where the ajax call from the yahoo API does not retrieve the necessary data in time for the content of the p ...

What's the reason why Angular stops flickering with the use of "/" in href?

Recently, I've come across a peculiar issue in one of my web applications while using Angular's routeProvider as a template engine. The app functions properly, but the behavior seems inexplicable to me. The problem arises during page transitions ...

Updating an SVG after dynamically adding a group with javascript: a step-by-step guide

Currently, I am working on a circuit builder project using SVG for the components. In my HTML structure, I have an empty SVG tag that is scaled to full width and height. When I drag components from the toolbar into the canvas (screen), my JavaScript functi ...

Unable to get compatibility mode IE to work as intended

I am facing an issue where I need to showcase a webpage in compatibility mode. Despite using the code below: <meta http-equiv="X-UA-Compatible" content="IE=11"> It seems like this code only functions if compatibility mode is already enabled. In tha ...

Achieve center alignment for two column divs without altering the HTML code using only CSS

Recently, I stumbled upon a basic HTML code that I am unable to modify due to restrictions: <div id="container"> <div id="a"> Title col </div> <div id="b"> Left col </div> <div id="c"> ...

What is the best way to bring the global stylesheet into a Vue component?

Embarking on my first VueJS project, I decided to create a global stylesheet to maintain consistent styles across different components. After installing the SCSS loader and setting up my stylesheets, I encountered an issue. $mainFont: 'PoppinsRegular, ...

Guide on adjusting the CSS styling of elements in real-time from the backend using a user customization panel to modify the appearance of various web pages

Imagine a scenario where we have a website consisting of multiple pages, including a user account page. The user has the ability to modify the color, font size, and style of certain elements on other pages for their own viewing preferences. How can this fu ...