Looking for assistance with CSS to properly align a dozen items

UPDATE: See the current layout in this JFiddle based on feedback received from others, still a few tweaks needed.

In my setup, there are 12 divs housed within one container div. These 12 divs consist of 6 text components and 6 corresponding input fields.

This is how I envision the structure...

[Text1    Input Item1       Text2   Input Item2       Text3   Input Item3]
[Text4    Input Item4       Text5   Input Item5       Text6   Input Item6]

How can I achieve this desired layout using CSS? Whenever I attempt it, the boxes get misaligned and end up in wrong rows. Any assistance with this would be greatly appreciated.

<div class="container">
     <div class="text1"></div>
     <div class="InputItem1"></div>

     <div class="text2"></div>
     <div class="InputItem2"></div>

     <div class="text3"></div>
     <div class="InputItem3"></div>

     <div class="text4"></div>
     <div class="InputItem4"></div>

     <div class="text5"></div>
     <div class="InputItem5"></div>

     <div class="text6"></div>
     <div class="InputItem6"></div>
</div>

CSS code:

.container{width:100%;}

.text1{width:15%;float:left}
.InputItem1{width:15%;float:left}       

.text2{width:15%;float:center}
.InputItem2{width:15%;float:center}       

.text3{width:15%;float:right}
.InputItem3{width:15%;float:right}       

.text4{width:15%;float:left}
.InputItem4{width:15%;float:left}       

.text5{width:15%;float:center}
.InputItem5{width:15%;float:center}       

.text6{width:15%;float:right}
.InputItem6{width:15%;float:right}       

Answer №1

Check out this useful example on jsFiddle

HTML Code:

<div class="container" name="container">
     <div class="textitem" name="text1">t</div>
     <div class="inputitem" name="InputItem1">i</div>

     <div class="textitem" name="text2">t</div>
     <div class="inputitem"  name="InputItem2">i</div>

     <div class="textitem" name="text3">t</div>
     <div class="inputitem"  name="InputItem3">i</div>

     <div class="textitem clear" name="text4">t</div>
     <div class="inputitem"  name="InputItem4">i</div>

     <div class="textitem" name="text5">t</div>
     <div class="inputitem"  name="InputItem5">i</div>

     <div class="textitem" name="text6">t</div>
     <div class="inputitem"  name="InputItem6">i</div>
</div>​

CSS Styles:

.container{width:100%;overflow:auto;}
div {
    border:1px solid #999;
}

.inputitem,.textitem {
    float:left;  
    width:15%;    
}

Please note that the borders are added to enhance the visibility of the div elements.

Answer №2

It has been rightly pointed out by @GionaF that you need to specify the class attribute instead of the name attribute for your CSS to take effect.

<div class="wrapper">
     <div class="content1"></div>
     <div class="InputSection1"></div>

     <div class="content2"></div>
     <div class="InputSection2"></div>

     <div class="content3"></div>
     <div class="InputSection3"></div>

     <div class="content4"></div>
     <div class="InputSection4"></div>

     <div class="content5"></div>
     <div class="InputSection5"></div>

     <div class="content6"></div>
     <div class="InputSection6"></div>
</div>

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

Issues with the menu pop-up functionality arise when utilizing the CSS class .active

When I have this code, the ".active" function doesn't work when clicked. However, if I change the div class from "top-menu-popup" to "top-menu-popup active" when opening the page, the menu is already activated. <div class="top-menu-popup"> ...

Create a text container that adjusts its size based on the content within

My goal is to create a unique display using CSS: I want to add various texts and lines to fill in the white space on the left and right sides. I have made some progress with this http://jsfiddle.net/g5jtm/, but I am facing a few issues: The text width ...

What could be the reason behind Flex making several images decrease in size?

I'm trying to set up a carousel of images with Tailwind CSS, where 5 identically sized images are displayed next to each other and can be scrolled left and right. When I imported the images without using Flex, they fit the max-width that I had specifi ...

td having no line breaks causing text to extend beyond the cell

I have a challenge with using the nowrap property on td elements to ensure proper formatting of my tables. In the first table, everything wraps nicely, but in the second table, the content in the first "td" exceeds its designated space due to length. How c ...

Modifying the WP FlexSlider plugin to eliminate the previous and next side panels

Is there a way to either reduce the width of the slider's prev/next sides or remove these panels while keeping the arrows to cycle through slides? These elements appear to be within ul.flex-direction-nav. I attempted using ul.flex-direction-nav { dis ...

Why does CSS respect height:auto for relative positioned parent elements but not width:auto?

Although CSS position properties may seem simple at first, when building a layout, tons of weird edge cases can come out of nowhere. One thing that I always overlooked was using height: auto or width: auto. To gain a better understanding, I stumbled upon ...

How can I improve a gif's background for optimal viewing on mobile devices?

Hey there! I'm facing an issue with a gif running as the background on my website. On my phone, the whole screen isn't filled up and there's just a black space halfway down. You can check it out at . Do you know how to make the gif take up t ...

When scrolling, the body content covers up the navbar

Whenever I try to scroll the page, the body contents start overlapping with the navbar. I have content over the navbar that should disappear when scrolling, and it is working correctly. The navbar should be fixed while scrolling, and this functionality ...

Having trouble getting my list items to display on individual lines within the foreach loop. It just doesn't seem to be working as expected

In the event listener, I need to ensure that my list items within the forEach loop are not displaying on separate lines. This issue is causing a problem in a lengthy section of code. The goal is to update questions when an answer is clicked from a list. B ...

How can you align square cells within a container using CSS grids while maintaining a consistent grid gap?

Our objective is to perfectly align square cells with the edges of their container while maintaining a consistent gap between cells in each row and column. Although this Codepen solution is close, there are two key issues: (1) vertical spacing doesn' ...

Is there a way to have my MUI Typography component display a unique image cursor when hovered over?

After some testing, I found that setting the cursor to cursor: pointer works perfectly fine. However, my goal is to use a custom image as a cursor. The image is saved in both my src and public folders, but I seem to be struggling with the syntax when using ...

What would you name this particular element?

I want to create a unique slider design but I'm unsure where to begin or how to search for information because I don't know the correct terminology. Does this type of slider have a specific name? I heard about Marquee, but that seems like someth ...

Are we utilizing this JavaScript function properly for recycling it?

Two functions have been implemented successfully. One function adds the autoplay attribute to a video DOM element if the user is at a specific section on the page. The other function smoothly slides in elements with a transition effect. The only limitatio ...

Role-based dynamic layout

I am currently working on a website that requires user login functionality. To achieve this, I am utilizing materialise-css and Angularjs for the front-end development, while the back-end is powered by Java-Hibernate (as Spring is not an option in my case) ...

The addition of one-page navigation in Angular 7 seems to be experiencing some glitches

I recently added a new menu option to my Angular 7 project, but it's not functioning correctly. I used Bootstrap 4 and followed this example from here. Can anyone help me figure out the correct way to implement this? Check out the sample on StackBlit ...

Designing versatile buttons with HTML

When accessing the website on a phone, I have trouble seeing and tapping on these two buttons because they are too far apart. I would like to change it so that once a file is selected, the 'choose file' button will be replaced by the upload butto ...

When switching between tabs on Twitter Bootstrap, the page automatically scrolls to the top

I am encountering an issue with my tabs setup. Each tab has a different height and there is a Row on top of the tabs. You can see the problem in action on this JSFiddle. Whenever I switch from a tab with greater height to a tab with less height, the page ...

Banner Text Alignment Issue: Uneven Left Alignment

Struggling to achieve consistent text alignment on a ribbon banner? Check out the issue I'm facing and see if you have any suggestions on fixing it: Link to Codepen Main issues: The first line of text appears slightly more indented on the left side ...

Prevent floating labels from reverting to their initial position

Issue with Form Labels I am currently in the process of creating a login form that utilizes labels as placeholders. The reason for this choice is because the labels will need to be translated and our JavaScript cannot target the placeholder text or our de ...

Extract CSS from Chrome developer tools and convert it into a JavaScript object

Recently, we have started incorporating styles into our React components using the makeStyles hook from Material-UI. Instead of traditional CSS, we are now using JavaScript objects to define styles. An example of this is shown below: const useStyles = ma ...