What is the best way to align inline-block elements in a straight row?

I am encountering an issue with the code block below:

<div class="1">
  Foo<br>1
</div>
<div class="2">
  Bar<br>2
</div>

Even though both .1 and .2 have styles of

position:relative;display:inline-block
, they are displaying as follows:

     Bar
Foo  2
1

It appears that the content of class .2 is vertically positioned correctly, but the first line of class .1 starts at the same level as the second line of .2, causing the entire div to be pushed down. This behavior is unexpected because .2 comes after .1, leading me to anticipate that .2 should be positioned relative to where .1 is located, not vice versa. This issue persists even when additional divs are added before .1. Is there a way to align them horizontally like this?

Foo Bar
1   2

Answer №1

Just as mentioned by others, 1 and 2 are not suitable for class names; I have updated them to a and b respectively. It is true that your CSS is nested, but you did not select SCSS for your fiddle which makes it invalid CSS. Correct these two issues and everything will work smoothly:

.wrapper { position: relative; display: inline-block; }
.a, .b { position: relative; display: inline-block; }
<div class="wrapper">
  <div class="a">
    Foo<br>1
  </div>
  <div class="b">
    Bar<br>2
  </div>
</div>

Additionally, please ignore suggestions regarding the use of tables and floats. While they are not incorrect, they may not specifically address the issues in your code.

Answer №2

If you desire to have the elements aligned in the manner demonstrated in your example, you have the option of utilizing float or inline-block.

.class1 {
    display: inline-block;
}
.class2 {
    float: left;
}
<div class="class1">Foo
    <br />1</div>
<div class="class1">Bar
    <br />2</div>
<hr />
<div class="class2">Foo
    <br />1</div>
<div class="class2">Bar
    <br />2</div>

It is important to note that you are using invalid classnames according to the specifications outlined here. Simply put, class names need to commence with an underscore (_), a hyphen (-), or a letter (a–z), followed by any combination of hyphens, underscores, letters, or numbers. Additionally, if the initial character is a hyphen, the succeeding character should be a letter or underscore, and the name has to be at least 2 characters in length. Identifiers can also include escaped characters and any ISO 10646 character as a numeric code.

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

Ways to utilize two distinct XPATH values for a single key

When dealing with Xpath for the same object in two different portals, the paths can be: //*[@id="abc"]/fieldset/div/div/div[1]/label //*[@id="xyz"]/fieldset/div[1]/fieldset/div/div/div[1]/label In order to use both values in the same key and have Seleni ...

Implementing a redirection of Dojo Data grid cell values to a different destination

I am working with a Dojo data grid that contains 10 rows (cells) of values. When the first row is clicked, I need it to redirect to another HTML page and carry along the values from the first row. Can you assist me in achieving this functionality? ...

Dynamic row Material-UI input field

Is there a way to create a multiline TextField component that expands to take full height on different devices? I am familiar with fullWidth, but is there an equivalent for setting the height of the component to full on rows depending on the device it is ...

Is it possible to make the body background image adjust its size to the browser window

Looking to change the body background using a jQuery script. The goal is to scale the background image to fit the browser window size. I've come across a script called , but it seems to affect the class img and not the background-img. Any suggestions ...

What materials are required in order to receive messages and information through my Contact page?

Currently, I am pondering the optimal method for gathering information from my Contact page. I have already created a form; however, I'm unsure how to send the gathered data to myself since I am relatively new to Web development. Angular is the framew ...

CSS: The border line with a see-through section

I'm in the process of creating a div that has a unique design like this. https://i.sstatic.net/cHJNY.png .triangle-area { width: 100%; height: 150px; } .triangle1 { width: 100%; height: 50px; border-width: 50px 50px 0 50px; border-col ...

"Rearrange elements on a webpage using CSS and HTML to position one

Can we utilize CSS/HTML to visually move a full width element that is positioned below another one so that it appears above without altering the markup order? <div id="first">first</div> <div id="second">second</div> #first {…} ...

Is Inline Styling the Key to Effective MVC Client-Side Validation?

Having some trouble with my form's client-side validation. I'm using data annotations and models, but the default display is not visually appealing. I've tried applying CSS, but getting tooltip style errors has been a challenge. What I real ...

The email verification process is experiencing technical difficulties

Having trouble displaying error message when emails do not match I am relatively new to PHP and could use some assistance. The $email variable captures the user's email input, while the $VerifyEmail variable does the same for email verification purp ...

JavaScript functions smoothly on Google Chrome but is restricted on Internet Explorer

Experimenting with the code found on this website to conduct some testing. The javascript functions smoothly in Google Chrome and Firefox, but encounters issues in IE. Interestingly, when I run the html file in IE locally, it does work, but a warning pops ...

Is there a way to dynamically update the text in an HTML element with a randomly generated value using JavaScript?

Currently, I am working on a coding project where I am attempting to create a flip box that reveals the name of a superhero from an array when clicked by a user. The code pen link provided showcases my progress so far: https://codepen.io/zakero/pen/YmGmwK. ...

Phonegap application functioning smoothly on computer, encountering issues on mobile device

Hey there! I recently developed a phonegap app that retrieves JSON data from a YQL link and presents it to the user. It works perfectly on Google Chrome desktop, but my client mentioned that it doesn't work on his Android 2.3 device. Could you help me ...

Turn off the CSS hover effect for a custom button if the ng disabled attribute is set to true

My goal is to disable custom buttons, and the disable(pointer) function is working correctly. The issue arises when we try to hover on the button, as the hover effect still works. I therefore need to disable hover as well. I am looking to apply ng-disabl ...

Align the bottom of an image with the top of text to achieve perfect vertical alignment

My goal is to arrange numerous images in a row, with text displayed below each image. Each block of text may consist of multiple lines and should be centered. While the heights of the images may vary, their widths will remain consistent. It is important th ...

The positioning attribute and flexible height in CSS are essential for creating dynamic

My goal is to create a layout with 3 DIVs inside a container, similar to table rows. top {height = 100px} / middle {height = dynamic} / bottom {height = 100px} I am trying to figure out the best approach to make the height of the middle div dynamic whi ...

Getting the ID name from a select tag with JavaScript - A quick guide!

Can you help me retrieve the id name using JavaScript when a selection is made in a select tag? I have tried running this code, but it only alerts undefined. For instance, if I select bbb in the select tag, I should be alerted with 2 Any suggestions on ...

Switch the default blue color of Ngx-Pagination to a different color option

I am currently using the ngx-pagination plugin in my Angular 16 project and I would like to change the default blue color to a different one, is there anyone who can assist me with this? This is the HTML code snippet: <div> <pagination-controls ...

Obtaining the count of a specific column in Angular 6 by grouping objects based on the same value in an array

In TypeScript, I have an array of objects and I am using a foreach loop. The array contains 2 columns with a progress value of 100, while the rest have values less than 100. My goal is to calculate the count of columns with a progress value of 100, which ...

Avoiding additional empty lines between selectors when setting up Stylelint

Can anyone help me with configuring Stylelint options? Specifically, I want to enforce a rule that no empty lines are allowed between selectors in a list of selectors: &:focus, &:hover, &.active { color: #fb3a5e; text-align: left; text-de ...

Do specific elements of typography adjust according to the size of the window?

I'm always amazed by the creative solutions that come out of this community. So I'm reaching out to see if anyone knows a way to achieve something unique with CSS! Specifically, I'm curious if it's possible to make certain horizontal p ...