w3resource

CSS Properties: How to set vertical align property of an element

Go to Exercise page

Solution:

HTML Code :

 <!DOCTYPE html>
  <html>
  <head>
  <title>How to set vertical align property of an element</title>
  <style type="text/css">
img.top {
    vertical-align: text-top;
}

img.middle {
    vertical-align: middle;
}
img.bottom {
    vertical-align: text-bottom;
}

  </style>
  </head>
  <body>
  <h1><strong>w3resource Tutorial</strong></h1>

<p>An image<img src="//www.w3resource.com/images/w3resource-logo.png" alt="w3resource logo" width="100" height="50" /> with default alignment : w3resource web development tutorial</p> 

<p>An image<img class="top" src="//www.w3resource.com/images/w3resource-logo.png" alt="w3resource logo" width="100" height="50" />   with text-top alignment : w3resource web development tutorial</p> 

<p>An image<img class="middle" src="//www.w3resource.com/images/w3resource-logo.png" alt="w3resource logo" width="100" height="50" /> with text-middle alignment : w3resource web development tutorial</p>

<p>An image<img class="bottom" src="//www.w3resource.com/images/w3resource-logo.png" alt="w3resource logo" width="100" height="50" /> with text-bottom alignment : w3resource web development tutorial</p>

  </body>
  </html>

Live Demo :

See the Pen vertical-align-property-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
YesYesYesYesYes

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

HTML-CSS: Tips of the Day

CSS selector by inline style attribute:

The inline style attribute is no different to any other HTML attribute and can be matched with a substring attribute selector:

div[style*="display:block"]

It is for this very reason however that it's extremely fragile. As attribute selectors don't support regular expressions, you can only perform exact substring matches of the attribute value. For instance, if you have a space somewhere in the attribute value, like this:

<div style='display: block'>...</div>

It won't match until you change your selector to accommodate the space. And then it will stop matching values that don't contain the space, unless you include all the permutations, ad nauseam. But if you're working with a document in which the inline style declarations themselves are unlikely to change at all, you should be fine.

Note also that this is not at all selecting elements by their actual specified, computed or used values as reflected in the DOM. That is not possible with CSS selectors.

Ref: https://bit.ly/3ARh6sF