{"id":753,"date":"2017-10-07T04:18:40","date_gmt":"2017-10-07T04:18:40","guid":{"rendered":"http:\/\/frowningbear.com\/blog\/?p=753"},"modified":"2018-05-29T09:43:08","modified_gmt":"2018-05-29T09:43:08","slug":"css-centering","status":"publish","type":"post","link":"https:\/\/frowningbear.com\/codebase\/2017\/10\/07\/css-centering\/","title":{"rendered":"CSS Centering"},"content":{"rendered":"<h2>Horizontal centering.<\/h2>\n<p>When the element to be centered is an inline element we use <code>text-align<\/code> center on its parent.<\/p>\n<p>When the element is a block level element we give it a width and set the left and right margins to a value of auto.<\/p>\n<h2>Vertical centering<\/h2>\n<p>The values for <code>vertical-align<\/code> have meaning with respect to a parent inline element.<\/p>\n<p><!--more--><\/p>\n<p>With a line of text the values are relative to the <code>line-height<\/code>.<br \/>\nWith a table cell the values are relative to the table-height-algorithm, which usually means the height of the row.<\/p>\n<p>Unfortunately <code>vertical-align<\/code> doesn\u2019t apply to block-level elements like a paragraph inside a div.<\/p>\n<h3>Line-Height Method<\/h3>\n<p>Set a <code>line-height<\/code> on the child element containing the text larger than its <code>font-size<\/code>.<\/p>\n<p>By default equal space will be given above and below the text and so the text will sit in the vertical center.<\/p>\n<p>Only works for vertically centering a <strong>single<\/strong> line of text.<\/p>\n<p>It&#8217;s often recommended to also set the height on the element with the same value given to the line-height. However this doesn&#8217;t seem to be necessary.<\/p>\n<p>Be careful when using the shortcut for the font property. This method relies on you setting the <code>line-height<\/code> as a value greater than the <code>font-size<\/code>. When you use the font shortcut any property you don\u2019t specifically set gets set to its default value. With <code>line-height<\/code> that default is 1. If you use the font shortcut, just make sure to explicitly set the <code>line-height<\/code> inside.<\/p>\n<p>To vertically center an image with this approach, set the <code>line-height<\/code> (taller than the image) on the parent instead and add <code>vertical align: middle<\/code> to the child image.<\/p>\n<h3>Equal Top and Bottom Padding<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Horizontal centering. When the element to be centered is an inline element we use text-align center on its parent. When the element is a block level element we give it a width and set the left and right margins to a value of auto. Vertical centering The values for vertical-align have meaning with respect to &hellip; <a href=\"https:\/\/frowningbear.com\/codebase\/2017\/10\/07\/css-centering\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;CSS Centering&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-753","post","type-post","status-publish","format-standard","hentry","category-css"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/comments?post=753"}],"version-history":[{"count":1,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"predecessor-version":[{"id":1259,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/753\/revisions\/1259"}],"wp:attachment":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}