Why Aren't Static Positioned Divs Aligning at the Parent's Top?

I am facing an issue where I have 3 divs with content inside them, and the two smaller ones are aligning at the bottom of the larger div.

Despite no presence of margin or padding, this alignment mystery has left me puzzled.

body {
  width: 100%
}
.container {
  margin: 0 auto;
  text-align: center;
}
.box {
  display: inline-block;
  width: 25%;
  height: 100%;
  border: 3px solid black;
  padding: 2%;
}
<div class="container">
  <div class="box blue">
    <h1>Box 1</h1>
    <p>Blub</p>
    <div class="remainder"></div>
  </div>
  <div class="box green">
    <h1>Box 2</h1>
    <p>I'm working on different ideas for 100% height without using Flexbox, Tables, or a set height on the parent container.</p>
    <hr />
    <p>That's not my question though, why are boxes 1 &amp; 2 floating so far down?</p>
  </div>
  <div class="box yellow">
    <h1>Box 3</h1>
    <p>Here's some more content</p>
    <div class="remainder"></div>
  </div>
</div>

Answer №1

The reason for this behavior is due to the usage of the vertical-align property within the .box class being defined as bottom. By declaring elements as inline-block, it enables the vertical align property to impact their positioning.

Important to note: Although you didn't set the vertical align property explicitly, it is actually being inherited from another rule. Experiment by setting it directly in order to observe the variation in outcomes.

.box {
    vertical-align: top;
    /* ... */
}

Answer №2

Inline-level elements determine their vertical alignment through the vertical-align property.

.box {
    vertical-align: /* value */;
}

Here are some examples:

  • vertical-align: top

    body {
      width: 100%
    }
    .container {
      margin: 0 auto;
      text-align: center;
    }
    .box {
      display: inline-block;
      width: 25%;
      height: 100%;
      border: 3px solid black;
      padding: 2%;
      vertical-align: top;
    }
    <div class="container">
      <div class="box blue">
        <h1>Box 1</h1>
        <p>Blub</p>
        <div class="remainder"></div>
      </div>
      <div class="box green">
        <h1>Box 2</h1>
        <p>I'm working on different ideas for 100% height without using Flexbox, Tables, or a set height on the parent container.</p>
        <hr />
        <p>That's not my question though, why are boxes 1 &amp; 2 floating so far down?</p>
      </div>
      <div class="box yellow">
        <h1>Box 3</h1>
        <p>Here's some more content</p>
        <div class="remainder"></div>
      </div>
    </div>

  • vertical-align: middle

    body {
      width: 100%
    }
    .container {
      margin: 0 auto;
      text-align: center;
    }
    .box {
      display: inline-block;
      width: 25%;
      height: 100%;
      border: 3px solid black;
      padding: 2%;
      vertical-align: middle;
    }
    <div class="container">
      <div class="box blue">
        <h1>Box 1</h1>
        <p>Blub</p>
        <div class="remainder"></div>
      </div>
      <div class="box green">
        <h1>Box 2</h1>
        <p>I'm working on different ideas for 100% height without using Flexbox, Tables, or a set height on the parent container.</p>
        <hr />
        <p>That's not my question though, why are boxes 1 &amp; 2 floating so far down?</p>
      </div>
      <div class="box yellow">
        <h1>Box 3</h1>
        <p>Here's some more content</p>
        <div class="remainder"></div>
      </div>
    </div>

  • vertical-align: bottom

    body {
      width: 100%
    }
    .container {
      margin: 0 auto;
      text-align: center;
    }
    .box {
      display: inline-block;
      width: 25%;
      height: 100%;
      border: 3px solid black;
      padding: 2%;
      vertical-align: bottom;
    }
    <div class="container">
      <div class="box blue">
        <h1>Box 1</h1>
        <p>Blub</p>
        <div class="remainder"></div>
      </div>
      <div class="box green">
        <h1>Box 2</h1>
        <p>I'm working on different ideas for 100% height without using Flexbox, Tables, or a set height on the parent container.</p>
        <hr />
        <p>That's not my question though, why are boxes 1 &amp; 2 floating so far down?</p>
      </div>
      <div class="box yellow">
        <h1>Box 3</h1>
        <p>Here's some more content</p>
        <div class="remainder"></div>
      </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

I possess 9 captivating visuals that gracefully unveil their larger counterparts upon being clicked. Curiously, this enchanting feature seems to encounter a perplexing issue within the realm of web browsing

<style type="text/javascript" src="jquery-1.11.0.min.js"></style> <style type="text/javascript" src="myCode.js"></style> </body> //jquery is within my site directory on my desktop $(document).ready(function(){ //note: $("#ar ...

Eliminating excess space beneath nested images while adjusting the size without distorting the image

Struggling with getting an image to scale properly inside a nested div in a fluid layout. Looking for advice on how to prevent the image from becoming smaller than its parent div. EDIT: Here is an updated CodePen link demonstrating that using min-height s ...

Strange spacing appears after image tags

My efforts to remove the space (red) between the <img> and the <div> have been unsuccessful. I have minimized it as much as possible. After researching similar threads, it seems that whitespace or newlines between inline-box elements can cause ...

Challenges arise when utilizing CSS3 animations in conjunction with transitions triggered by toggling a JavaScript class

Struggling to activate an animation while updating a class using JavaScript for a PhoneGap app. Planning on utilizing -webkit- prefixes for compatibility. However, the animations are currently unresponsive in Chrome during testing, both when applied to th ...

Assistance with Ajax for content loading

Greetings, I am encountering an issue with the following code snippet (located in a js file named ajax.js) $(function(){ $("#loading").hide(); $("ul#nav a").click(function(){ page = "content/"+$(this).attr('href') ...

Simultaneously Implementing JQuery's fadeIn and Load Functions

Currently, I am utilizing the Jquery load function to refresh specific parts of a webpage. The page loaded by the load function only displays content in the div if certain database parameters are satisfied. However, my issue is that when new content is ren ...

Creating a responsive container in Bootstrap that adjusts height dynamically, featuring a text input box positioned at the bottom

I am currently using Bootstrap 4 to design a desktop chatbot inspired by Google's Bard. My goal is to have a container that spans the height of the screen, with the text input located at the bottom of the container. Additionally, I want the container ...

The justify-content property aligns single-line text horizontally, but its alignment may not be consistent when the text expands over

Seeking a deeper understanding of flexbox alignment, I decided to experiment with aligning content using only flexbox properties. One puzzling observation is how "justify-content" centers items with single lines of text (flex-item-1 and flex-item-5), but n ...

What is the purpose of Firebug adding <tbody> elements to <table> tags?

While analyzing the HTML source code, I noticed that the <tbody> tag is missing. However, when inspecting the code using Firebug in the HTML tab, the <tbody> tag suddenly appears. What could be the reason behind this discrepancy? ...

Encountering an issue with npm installation following a recent node version upgrade

Having trouble installing Sass on Node version v16.14.0. I keep receiving this error message: https://i.stack.imgur.com/6KNcF.png ...

Import Information into Popup Window

When a user clicks on the "view" button, only the details of the corresponding challenge should be displayed: Currently, clicking on the "view" button loads all the challenges. This is because in my view-one-challenge.component.html, I have coded it as fo ...

Tips for keeping Sub Menu visible at all times

Is there a way to keep the sub menu always visible on the homepage of my website's navigation? Thank you for your help! Check out my website here ...

Achieve full width with flexbox column wrap while minimizing the height

How can I arrange elements in columns within a container with a fixed width and variable height, when the number of elements is unknown? My challenge is that I do not know the maximum width of the child elements, preventing me from setting specific column ...

Utilizing Selenium to extract data from a table and displaying the results

I am looking to scrape tables from a specific website, but I am new to automation with Selenium. Here is the code snippet that I have come up with after doing some research: from selenium.webdriver import Firefox from selenium.webdriver.common.by import By ...

Adapting Bootstrap components based on the screen size: Viewport-driven

Check out our Custom Sizing Solution, designed to offer sizing options tailored to different devices and viewports. Here's what we can do: For small devices, the width will be set at 100% For larger devices, the width will be adjusted to 75% For ext ...

Is there a simpler method to access the source element for an event?

I'm just starting to learn JavaScript and jQuery, and right now I have the following code in my HTML: <a id="tog_table0" href="javascript:toggle_table('#tog_table0', '#hideable_table0');">show</a> After that, I hav ...

The `appendTo` function in Ajax is used to swap out the current element

I have been working on sending form data to a Servlet using JQuery and then receiving the response from the same JQuery. Check out the code snippet below. <%-- Document : index Created on : Feb 23, 2015, 8:18:52 PM Author : Yohan --% ...

Do we always need to incorporate components in Vue.js, even if there are no plans for reuse?

I've been pondering this question for some time now: is it necessary for every component to be reusable? Consider a scenario where we have HTML, CSS, and JavaScript that cannot be reused. For instance, a CRUD table designed specifically for managing u ...

Request financial data from AlphaVantage using PHP

I'm currently working on utilizing the alphavantage API to retrieve currency exchange information. In order to obtain the desired data, I am using the following query URI: https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_cu ...

Support for Arabic in database, PHP, and JavaScript

After inputting an Arabic comment into a textarea on the site, it displays correctly as "عربي" and is successfully sent to the database. However, upon refreshing the page, the text appears garbled: "عربÙ�". I attempted to directly enter s ...