Div with inline-block elements nested inside

My attempt at creating a grid using display:inline-block with divs was successful when positioning the divs next to each other. However, once I added elements inside the divs, the parent div started moving down.

Here is the code snippet that caused the issue:

<div style="white-space: nowrap;">
  <div *ngFor="let xUnit of xUnits; let i=index"
        style="width: 106px;
              max-width: 106px;
              display:inline-block;
              position: relative;
              height: 50px;
              max-height: 50px;
              padding-right: 5px;
              border: 1px solid #e5e7eb">
      <ng-container *ngFor="let item of getItems(resources[0], xUnit)">
        <div style="height: 25px;
                  position: relative;
                  border-left: 2px solid #FA607E;
                  background: white;
                  margin-left: 5px;
                  display: block;
                  margin-right: 5px;
                  color: #FA607E;
                  font-family: 'Source Sans Pro', sans-serif !important;
                  font-weight: 300;"
            [style.width]="getWidth(item)">
          <span style="padding-left: 5px;">
            Test
          </span>
        </div>
      </ng-container>
    </div>
</div>

Answer №1

One of the common issues with using inline-block is the strange 'bugs' it can create, such as adding an extra 2px of space to elements!

This occurs because the browser now treats your divisions as text and tries to align them based on their baseline.

In many cases, setting vertical-align: top; can resolve this issue.

However, I suggest utilizing flexbox for better options and control over vertical alignment of elements.

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

Fixed position of the element within a parent stacking context

How can I ensure that the #icon appears below the #dropdown in the following example? https://i.stack.imgur.com/GoBcR.png The position: fixed of #container seems to be creating a new stacking context for its children. Changing the position value fixes th ...

How to ensure a list item remains in a fixed position when resizing the window

Imagine a scenario where there is an unsorted list within a resizable div element. The list is set up horizontally and aligned to the right, with one item designated as "special" and given the id="pinned". Using only CSS, can you keep the #pinned item in ...

Creating dynamic captions in an Angular grid is an essential feature for enhancing the

Is there a way in Angular to dynamically update the grid titles based on an array of elements in the model? How can I display them as captions? For instance, imagine we are currently in week 202010. I would like to automatically generate the next five wee ...

Tips for arranging elements vertically in HTML

Recently, I created a code that triggers a function when a button is clicked. This function moves one div above another when the Up button is clicked, and vice versa for the Down button. Do you have any ideas on how to achieve this? http://jsfiddle.net/W ...

The alignment of Vuetify buttons is not consistent in the vertical direction

I'm a beginner with Vue and Vuetify, seeking help on aligning buttons properly. I have a container with v-text-fields and v-combobox centered in the middle. Now I want to add three buttons below this form, but they are not aligned vertically. Each bu ...

Display a modal dialog using HttpInterceptor

@Injectable() export class MyInterceptor implements HttpInterceptor { intercept(req : HttpRequest<any>, next : HttpHandler) : Observable<HttpEvent<any>> { //display a modal dialog to wait for user response before proceeding with t ...

The clip-path in Div: Css is malfunctioning and not displaying correctly

I am trying to create a chat bubble using CSS with rounded corners and a bottom-right arrow. However, I am having trouble getting the arrow to display correctly. https://i.stack.imgur.com/gVXOV.png The CSS styling for the chat bubble is as follows: ...

What methods are recommended for expanding the size of a select/dropdown menu?

Managing a long list of values can be challenging, especially when it continues to grow. Consider the following example: <select> <option value="1">1, some test</option> <option value="2">2, some text</option> <optio ...

Divide words onto separate lines and shift them out of place

I'm tackling the challenge of crafting a basic tooltip without relying on JavaScript. I'm pondering the most effective approach to achieve this. I aim to have the tooltip container accommodate dynamic text seamlessly, adjusting responsively atop ...

What is the best way to change a string into JSON format using JavaScript?

For a recent web project, I utilized the Django framework in conjunction with a MongoDB database. In this project, I implemented a feature where a set of data objects from the database are assigned to a combobox. Interestingly, I incorporated two comboboxe ...

Encountering a database deletion error with Spring Boot Thymeleaf

I'm currently facing an issue where I am attempting to remove an entry from my database. However, when I try to delete it on my html page, I end up being redirected to an error page instead of the intended destination. The entry I am trying to delete ...

"The Toggle Switch/checkbox on Proto.io is stuck and not toggling as

I'm currently struggling to make a toggle switch work on my webpage. Despite trying different approaches such as removing data or not applying it to the input element, the toggle still refuses to function properly. The data-field attribute is supposed ...

Issues with ion-slides functionality in Ionic 3 not functioning properly on web browsers

When using ion-slides in Ionic 3, everything functions correctly on Android and iOS apps; I am able to swipe each row individually while the others remain stationary. However, when attempting to "swipe" using the mouse on a browser, the expected behavior ...

Add a hyperlink alongside every row in the table

My table comprises three columns in each row, and I am looking to include a link next to each row. <table> <th> Name: </th> <th> Price: </th> <th> Description </th> ...

Run the function solely once the asynchronous function has been executed

I need function F1() to wait for function F2() to fully execute and receive the response from a REST call in order to set some data. Here is the code I attempted to use: this.F1().subscribe(result => { this.F2(result); }) F1() { retur ...

Is there a way to transform my existing Angular form layout so that it can effortlessly capture pre-loaded data sourced from an API using reactive form

As I work on creating a form to update user details using Angular, I am looking to enhance the current structure to automatically pre-fill all input fields with data when the page loads. component.ts file constructor( private fb:FormBuilder, private rout ...

An improved approach for implementing ngClass condition in Angular components

Searching for a more concise way to rewrite the following condition: [ngClass]="{ 'class1': image.isAvailable && (image.property !== true && !null), 'class2': image.isAvailable && ...

JQuery is failing to locate elements that have dynamic data titles assigned to them

I've been attempting to locate an element using a jQuery regex comparison in the data title. My situation involves having divs with the class .textbox, each containing a dynamically generated data title. To target specific boxes with that particular d ...

The "ng2-CKEditor" package is experiencing compatibility issues with TypeScript in Angular 2

Currently, I am in the process of setting up CKEditor in my angular2 application. My backend platform is node.js and for this purpose, I am utilizing the ng2-CKEditor npm module. Below, you can find snippets from respective files. index.html:: <html& ...

Executing a child component function once the parent component data is loaded in Angular 5

In my project, I have a parent component called program-page.component where I am invoking a function to fetch some data. ngOnInit() { this.getProgress(); } getFirstProgramItem() { this._contentfulService.getProgramItem(4, 1) .then((programItem) = ...