Alignment in the vertical direction functions with block-level elements

I always thought that vertical-align was only meant for inline and table elements. However, I recently tried using it on a block element and it worked perfectly fine. How is this possible? Does vertical-align actually work for all types of elements or are there some exceptions?

#wrap {
border: 1px solid black;
width: 500px;
height: 500px;
}
#alignTop {
vertical-align: top;
border: 1px solid black;
}
<div id = 'wrap'>
<div id = 'alignTop'> alignTop </div>
</div>

Answer №1

After researching the topic, I found valuable insights on this website:

  • Historically, HTML layout lacked specification for vertical behavior. It primarily focused on width scaling, with height adjusting accordingly. Vertical sizing and layout were extensions of horizontal rules.
  • vertical-align can exhibit diverse behaviors based on its context in coding.

Understanding vertical-align in table cells

In table cells, vertical-align generally aligns content as expected, resembling the outdated valign. In modern browsers, these code snippets produce similar outcomes:

  1. <td valign="middle"> <!-- avoid using valign --> </td>
  2. <td style="vertical-align:middle"> ... </td>
  3. <div style="display:table-cell; vertical-align:middle"> ... </div>

Exploring vertical-align with inline elements

Applying vertical-align to inline elements introduces a different dynamic. It imitates the deprecated align attribute used on <img> elements. Modern browsers treat these code snippets similarly:

  1. <img align="middle" ...>
  2. <img style="vertical-align:middle" ...>
  3. <span style="display:inline-block; vertical-align:middle"> foo<br>bar </span>

Answer №2

Typically, block elements stack on top of each other, which means it does not work as intended.

For more information, refer to W3C

When in a block formatting context, boxes are positioned one after another vertically within a containing block. The vertical space between two sibling boxes is defined by the 'margin' properties. Vertical margins between adjacent block-level boxes in a block formatting context collapse.

You can also verify on W3C that vertical-align does not affect block elements, but is applicable only to inline-level and table-cell elements.

Value:    baseline | sub | super | top | text-top | middle | bottom | text-bottom | <percentage> | <length> | inherit
Initial:      baseline
Applies to:   inline-level and 'table-cell' elements
Inherited:    no
Percentages:      refer to the 'line-height' of the element itself
Media:    visual
Computed value:   for <percentage> and <length> the absolute length, otherwise as specified

Example Code Snippet

#wrap {
  border: 1px solid black;
  width: 500px;
  height: 500px;
}
#alignTop {
  vertical-align: top;
  border: 1px solid black;
}
#alignMiddle {
  vertical-align: middle;
  border: 1px solid black;
}
#alignBottom {
  vertical-align: bottom;
  border: 1px solid black;
}
<div id='wrap'>
  <div id='alignBottom'>alignBottom</div>
  <div id='alignTop'>alignTop</div>
  <div id='alignMiddle'>alignMiddle</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

Adding navigation links to move forward and backward on a webpage in Django:

Currently, I am leveraging Django Python Templates to create a blog with two distinct frames. The code below is used to populate the links on the left side frame: {% for post in posts %} <div class="post"> <ul style="padding: 1px;"& ...

The dimensions of my grid object are constantly being altered by my interactive button

Recently, I started delving into vuejs and vuetify to work on a project management app for school. I encountered a simple problem where adding a button distorted the project bar. I tried nesting the button component within the project bar to rectify this i ...

Ensure that the spacing between rows matches the spacing between columns

I am working on creating a 3x3 grid using Bootstrap. My goal is to have consistent spacing between the rows and columns. How can I achieve this effectively? ...

The <h:outputStylesheet> tag fails to display any content on the HTML page

I am having trouble getting my CSS sheet to load while using JSF2 and PrimeFaces. The CSS file is located at WebContent/resources/css/style.css Here is the xhtml code: <h:head></h:head> <h:body> <h:outputStylesheet name="css/styles. ...

Email Embedder - Incorporating HTML source code into email communications

I've encountered an issue where the CDNs (Bootstrap and FontAwesome) do not work when I embed my HTML source code into an email. Despite using inline styling throughout, the receiver still faces this problem. Any insights on why the CDNs are failing t ...

Struggling to implement a sidebar in HTML using jQuery and running into issues?

I am struggling to create a template that includes a navbar, sidebar, and other elements that can be used across multiple HTML files. Despite trying different approaches, including changing the jQuery version and downloading jQuery, I am unable to make it ...

Ways to customize CSS styles for a single page?

<head> <link href="index.css" rel="stylesheet"> <style> #amor_di_mundo{ color:#ffffff; } </style> </head> <body> <div id='divL'> <div id='amor_di_mundo'>Amor di Mundo</div> <di ...

Is there a way to add styles to template literals?

Currently, I am utilizing template literals in my React project to display data. However, I am looking to style the email portion of the data. Is there a way for me to apply inline styles to this template literal? Any suggestions on how I can achieve thi ...

Tips for personalizing the react-select module

I'm struggling with customizing the appearance of the react-select component. The official documentation lists all the style attributes that can be modified, but I'm having trouble removing the blue borders around the text and container. https:/ ...

Is there a way to prevent automatically scrolling to the top of the page when submitting a form?

Resolved! By enclosing the input tag within an anchor tag, the issue has been resolved. The question may appear confusing at first, so let me provide some clarification. I created a mail form using PHP. This form is located at the bottom of the page. Whe ...

Contrasting float-start and start-0 characteristics

I'm attempting to recreate the layout shown in this image using HTML and Bootstrap 5: https://i.sstatic.net/R6Naf.jpg I'm having trouble with the alignment of the elements. When I use float-start for the image, it aligns properly, but start-0 do ...

Alternative Email for Outlook and Gmail

Hey amazing and intelligent buddies! I've been working on implementing a fallback system for email clients, but despite consulting resources like this HTML Emails: fallback for mso conditional? and other insightful blogs, I still seem to be facing so ...

Labels are overlapping each other and being aligned to the right side

Working with the most up-to-date version of Bootstrap, I am currently designing a search bar featuring a dropdown menu preceding the search button. Upon clicking the dropdown, various controls are revealed. Below is the HTML code for this configuration: ...

Showcasing mysql information within divs rather than tables

Hello! I am currently working on a hotel search website project, where clients can search for hotels. I have successfully developed the backend system for hotels to update their details in the database. However, I am facing challenges with the front-end de ...

How can I extract the string located immediately after the last character using jQuery?

Can you help me extract the text after the last "=" symbol in this string? <div class="main_menu_link" data-ajax-url="http://mytestdomain.com/temp-param-page-2/?wpv_paged_preload_reach=1&wpv_view_count=1&wpv_post_id=720960&wpv_post_search&a ...

CSS slideshow with automatic horizontal image transition that seamlessly restarts

I am facing an issue with my HTML & CSS automatic horizontal slideshow. Everything works fine, except for when the last image finishes sliding and the slideshow loops back to the first image, there is a sudden jump in the appearance of the first image. How ...

Making the browser refresh the CSS automatically once it has been modified

When updating a CSS file on the server, it's common for many client browsers to continue loading pages using the old cached CSS file for an extended period. After exploring various posts and piecing together different ideas, I have devised what appea ...

Automatically selecting a div using jQuery upon loading the page

Hello everyone. I've implemented some jQuery code that allows div elements to slide horizontally when a link is clicked. However, upon page load, no link is clicked and therefore no div is visible (only the ul is visible). I would like to have the div ...

What is the method to integrate HTML tags as native elements within my iOS app?

I am working on creating a custom control that will allow me to convert HTML tags into native tags. For example, when I input the following: label.text = @"&lt;b&gt;&lt;i&gt;Hello World&lt;/i&gt;&lt;/b&gt; The output shoul ...

PHP not displaying value upon submission

Looking for a simple PHP code to post a value and echo it inside an if(isset()) statement when clicking on the submit button, but unfortunately, it's not functioning as expected. <?php if(isset($_GET['q'])) { $q=$_GET[&ap ...