Arrange the divs closely together in the HTML code

I'm running into a problem with my html code. I have elements for "Title", "Firstname" and "Surname", each followed by an input box that users need to fill out. These input boxes are defined as fields in my form generator (e.g., [en_title]). The issue is, when I write the html code to display these elements, they end up stacked on top of each other instead of beside one another. If I try using float in the div style, they do line up next to each other but the input boxes shrink in size. In the initial layout, the input boxes maintain their original size. Can someone please provide a solution to ensure that the elements are aligned next to each other without the input boxes shrinking? Thank you.

<div>
  Title <br />
  [en_title]
</div>
<div>
  Firstname <br />
  [en_fname]
</div>
<div>
  Surname <br />
  [en_sname]
</div>

Answer №1

Replace the float property with display:inline-block;

div{
display:inline-block;
}
<div>
Title <br />
<input type="text"/>
</div>
<div>
Firstname <br />
<input type="text"/>
</div>
<div>
Surname <br />
<input type="text"/>
</div>

Answer №2

If you have three div elements that you want to place next to each other horizontally, with the container div's width set to 100%, each inner div should have a width of 33.33333% to achieve this layout, similar to how Bootstrap works.

Here is an example of how you can accomplish this:

See the code snippet below for reference:

.your-block {
    float: left;
    width: 33.33333%;
}
<div class="your-block">
    <label for="title">Title </label> <br />
    <input id="title" type="text"/>
</div>
<div class="your-block">
    <label for="firstname">First Name </label> <br />
    <input id="firstname" type="text"/>
</div>
<div class="your-block">
    <label for="surname">Surname </label> <br />
    <input id="surname" type="text"/>
</div>

Answer №3

Ensuring that you utilize width and incorporate position:absolute will allow you to freely position your control using margin-left:10px or margin-top:10px according to your specific requirements. Additionally, sharing your CSS code would be beneficial for further assistance.

Answer №4

An alternative approach: Avoid using excessive div elements and opt for labels and input fields instead:

HTML:

<form>
  <label> Name <input type="text"> </label>
  <label> Email <input type="email"> </label>
</form>

CSS:

form{display: flex}

This method allows for cleaner HTML code while achieving the desired layout. For a comprehensive guide on utilizing flexbox, refer to css-tricks: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

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

I'm looking for a way to incorporate JavaScript code within an iframe tag. Specifically, I'm attempting to embed code into a Wix website using the "Embed HTML

Trying to execute the code below on a Wix website using "Embed HTML", but IFRAME is blocking scripts. Seeking help to embed custom HTML with JavaScript on platforms like Wix.com or Wordpress.com, as the embedded code is not functioning due to IFRAME restri ...

Show dropdown menu on bootstrap responsive table

I've encountered an issue with a bootstrap3 responsive table that includes dropdown selections in each row. When the dropdown is clicked, users have to scroll to view all the options, especially on smaller screens where scrolling becomes impossible. ...

Is there a way to align the image to the left within the div contained in my header section?

I am facing an issue with positioning the logo in the header section of my website. It appears correctly on the left side for tablet and mobile versions, but on desktop, it does not align to the corner as desired. I tried adding a right margin, but I belie ...

Issue with fancyBox not functioning properly when integrated with a caption script

Hey there! I've been working with both jQuery's fancyBox for displaying image/video galleries and the Capty script for showing image titles on images. However, I've run into a snag - when the title is displayed on the image, fancyBox st ...

Adjust Font Size inside Circular Shape using CSS/jQuery

I have been searching for different libraries that can resize text based on the size of a container. However, my container is circular and I am facing difficulty in preventing the text from overflowing beyond the border. Below you will find a Fiddle conta ...

Adjust the checkmark color of MUI Checkbox

When using Material UI's MUI library for React, I encountered an issue with the checkbox checkmark color. By default, the checkmark takes on the background color of the container element, which is great for light backgrounds but not ideal for dark one ...

Retrieving Form Validation Error Value in AngularJS

Incorporating AngularJS 1.2 RC2 and utilizing Bootstrap for CSS, I have constructed a straightforward form as shown below: <form class="form-horizontal" name="form" novalidate> <label class="col-lg-2 control-label" for="name">Name</labe ...

The browser is displaying the scene, but the Three.js Geometry is not visible

After following a tutorial and successfully getting the entire scene to work within the body tag, I decided to move it inside a div. However, now only the scene itself is rendered. Despite not receiving any errors and ensuring everything is accessible thro ...

A space has been included before the CSS tag in the document for docx4j/Js

I am looking for a way to convert an HTML formatted string into a docx file. Currently, I am using Jsoup to clean up the HTML and then docx4j to parse the XHTML into a docx format. However, I encountered an issue with colors since they are not supported b ...

Guidance on marking a menu item as selected when choosing its sub-item

I've been tasked with creating a menu and I want to ensure that the menu item stays selected when its subchild is selected. Below is a snippet from my CSS file: #wrapper { width:100%; height:500px; } h2 { color:#787878; } // more ...

Unable to utilize navigator.camera.getPicture in iOS while using PhoneGap Adobe Build

I've spent hours searching, trying to troubleshoot my PhoneGap app (compiled by Adobe PhoneGap Build) and I suspect there's something crucial about PhoneGap that I'm missing. I've added the following lines to the config.xml file: <f ...

Flex wrap is causing additional space within the layout

When using flex-wrap, if there are more textBoxes than the available width in the container, they shift to the next line. This is fine, but it leaves too much space between the button and the textBox. This spacing issue can make the layout look odd. I hav ...

Guide to utilizing the app.locals parameter in JavaScript?

Currently I am in the process of developing a node.js application. In my app.js file, I have loaded a JSON file as app.locals.parameter and now I am attempting to utilize it in my index.hbs. Let's assume: app.locals.componentData = require('./m ...

Auto-fill feature malfunctioning

Currently, I have implemented jQuery Autocomplete to retrieve data from a database and display it in the search box. Everything is working perfectly fine, except for one issue - when hovering over the results in the dropdown, instead of displaying the resu ...

I wish to adjust the font size as well as resize the table elements simultaneously

Adjusting the height and width of the table should automatically adjust the font size as well. <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI Resizable - Default functiona ...

Transforming the navigation menu using CSS, HTML, and jQuery

One challenge I am facing involves creating a menu similar to the one on http://edition.cnn.com/. I want the clicked button in the menu to receive focus, while the others lose it. Despite trying various methods, I have not been successful. Can someone off ...

Creating a CSS Grid system with a set 100 grid cells, while allowing for dynamic changes in the number of rows and columns

My goal is to design a CSS grid consisting of exactly 100 cells. The number of columns and rows is not crucial, as long as there are always 100 cells in total. I'm feeling a bit confused about how to accomplish this using the properties: grid-template ...

Comparison between padding on parent element and margin on child element

When trying to organize logical divs within a container with appropriate spacing, there are two main approaches to consider: Setting the padding property of the container Setting the margin property of the inner div Sometimes, multiple elements need to ...

Can someone provide details on how to reset the auto-scroll timer on my content slideshow after each click?

I'm currently building a content slideshow and you can find it at Below is the javascript code: jQuery(document).ready(function($) { setInterval(function() { moveRight(); }, 8000); var slideCount = $('#slider-isp ul li').length; ...

Customize the background in a blogger template without using the body.background property

I am using a custom Galauness template on my blogger site (http://nethoroskop.blogspot.com), but I want to add my own image as the background. Can anyone guide me on how to do this? The HTML code does not include any reference to body.background. body{ ba ...