Positioning divs at the top

My goal is to design a simple layout with two divs positioned side by side, but they have different heights. I expected the smaller div to align at the top, but it's showing up at the bottom instead. Is this normal behavior within the page flow?

<body>
    <div>
        <div class="left debug-green"></div>
        <div class="right debug-red"></div>
    </div>
</body>


body {
    font-size: 0;
}
.left {
    width: 50%;
    height: 30px;
    display: inline-block;
}
.right {
    width: 50%;
    height: 20px;
    display: inline-block;
}
.debug-red {
    background-color: rgb(255, 0, 0);
}
.debug-green {
    background-color: rgb(0, 255, 0);
}

Take a look at this example on jsFiddle:

http://jsfiddle.net/3nAsx/

Answer №1

insert into the div on the right

align-items: flex-start;

Answer №2

Majority of web browsers display inline-block elements with the default value for vertical-alignment, which happens to be: baseline

Possible values for vertical-align:

    vertical-align: baseline     /* keyword options */
    vertical-align: sub
    vertical-align: super
    vertical-align: text-top
    vertical-align: text-bottom
    vertical-align: middle
    vertical-align: top
    vertical-align: bottom
    vertical-align: 10em          /* <length> values */
    vertical-align: 4px
    vertical-align: 20%           /* <percentage> values */
    vertical-align: inherit

Answer №3

If you switch out the display:inline-block; declarations for float:left;, it can streamline your code since you're already specifying the dimensions. The change will result in a cleaner look and feel. Take a peek at the updated version below after incorporating this adjustment.

Snippet

.left {
    width: 50%;
    height: 30px;
    float:left;
}
.right {
    width: 50%;
    height: 20px;
    float:left;

}

Answer №4

Setting vertical-align to top can be a good choice.

However, if you prefer having one div align on the left and another on the right,

You can achieve this by applying float: left to the left div and float : right to the right div

Answer №5

This method is perfect for concise coding

  <div class="box">
     <div class="left debug-green"></div>
     <div class="right debug-red"></div>
  </div>

   .box div{
     width: 50%;
     height: 30px;
     display: inline-block;
     background-color: green;
     float:left;
     vertical-align:top;
   }

 .right {
     height: 20px !important;
     background-color:red !important;
  }

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

What is the best approach to execute a javascript on an HTML page that is dynamically generated?

Working with jQuery Mobile, I have a situation where I am creating a listView of pages based on events stored in a database. Each event is parsed on my webpage to generate a unique HTML page for each one. Within each page, I include a distinct "subscribe" ...

Are you looking to test your website's performance under limited bandwidth conditions

Similar Query: How can I test a website for low bandwidth? Would it be possible to mimic a slow internet connection to test my website's performance? I need to simulate lower speed conditions in order to observe how the site behaves. ...

How can I transmit information using Json and javascript?

Is there a way to utilize Json to collect data upon button press? I have tried using Javascript to create a function for the button, but I am struggling with integrating Json. Can anyone provide an example of how to achieve this? Below is the HTML code sn ...

Encoding in Spring MVC for HTML with UTF-8 does not properly render Russian characters

I have been experimenting with an idea and have configured the settings as follows: https://i.sstatic.net/LYteF.png and also adjusted the settings to: https://i.sstatic.net/qQXB9.png After all these changes, the outcome is displayed as: https://i.sstatic ...

Tips for ensuring a cloned table header remains fixed when scrolling through a page

Check out my code snippet on Pastebin: http://pastebin.com/pUstKXJz I'm trying to make a sticky header that stays at the top of the page when I scroll past 100 pixels. The current JQuery code I have isn't working properly, so I need help modifyi ...

Receiving a 502 Bad Gateway error when attempting to request a CSS file via HTTPS

After recently adding SSL to some pages on my website, I encountered an issue when trying to call a secured web page <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="https:/ ...

Rearrange two elements by flipping their positions using CSS

I'm working with the following div that contains an input and a label: <div class="js-form-item "> <input type="checkbox" id="checkboxes-11" class="type-checkboxes form-checkbox"> <label for="option-a-checkboxes-11" class=" ...

Video embedded in a plain CSS popup that automatically starts playing even before the popup is triggered

My goal is to incorporate a video into a minimalist CSS modal window, following the guidance provided here. While the setup works well overall, I encounter a issue where the video starts playing before the modal window is opened (I need to maintain the "au ...

Display a hidden table row with an image click using JQuery

I am working with a table that currently has 2 rows. The first row contains an image, while the second row is hidden from view. Using JQuery, my goal is to make this hidden row visible. Below is the structure of the table: <table> <tr> ...

In jQuery, conditionally nest divs within another div based on a specific requirement

There is a container with multiple nested elements that need to be rearranged based on the value of their custom attribute. The goal is to reorder those elements at the end of the container if their 'data-keep-down' attribute is set to true, usin ...

Retrieve the array index of the parent node of the clicked element

I am looking for a way to click on a button, retrieve its parent array number, and then apply a specific style to only one of the many divs with the same class name. Ideally, I would like to accomplish this using vanilla JavaScript. <div class="c ...

What is the secret behind Facebook's ability to keep the chat bar fixed at the bottom of the browser window?

I am curious to learn about the method Facebook uses to keep the chat bar at the bottom of the browser window even when we scroll through the page. ...

Creating a Product Box design with CSS and incorporating visual elements

I have created a simple box design in Photoshop that I want to use to display product information. The box consists of a Header, Body, and Button, each as separate images. How can I assemble these elements using CSS/HTML? I only need the header text at th ...

Forming a space between borders

I'm attempting to create a space within a div's border that can accommodate an image, much like so: Is there a method to achieve this using only CSS? The only solution I have found is: .box { background: url(img.png) bottom left; border ...

Retrieving inner text using HTML Agility Pack

I need assistance with parsing a website I have found here: https://i.sstatic.net/GdFN1.png My goal is to extract information from specific fields that have IDs and classnames: label = node.SelectSingleNode( ".//h3[@c ...

Tips for creating random CSS values for an element generated with javascript?

Apologies for the blunt title, but I couldn't find a more precise way to phrase it. I recently stumbled upon this fantastic slider tutorial. Before implementing it on my own page, I would like to customize it by replacing the small circles at the bott ...

What is the typical method for preserving a specific gap between two lines when the line spacing surpasses the margin?

My workplace provided me with a wireframe that looks like this: https://i.sstatic.net/Qumru.png Description of the image above: Maintain a margin of 10px between the lines and utilize the specified css class .font16. This is what the .font16 class conta ...

The redirection to the webpage is not occurring as expected

I've been attempting to redirect another webpage from the homepage in my node server. However, the redirect isn't working as intended to link to idea.html, where the relevant HTML file is located. Can anyone assist me in identifying any errors in ...

The Textfield component in Material UI now automatically sets the default date to the current date when using the "date" type

I am using Material UI's textfield with the type set to "date" and I'm experiencing an issue where the date defaults to the current date instead of mm/dd/yyyy. Is there a way to prevent this behavior and display mm/dd/yyyy when the user loads the ...

A guide to correcting alignment issues on a flex box containing both images and text

I am encountering a problem with my flexbox setup. It contains multiple products with images and text, but the issue arises when a product has more than one line of text causing the image to be slightly raised compared to others. Any suggestions on how to ...