{"id":3277,"date":"2022-04-05T16:00:00","date_gmt":"2022-04-05T16:00:00","guid":{"rendered":"https:\/\/cssdeck.com\/blog\/?p=3277"},"modified":"2022-04-04T09:45:42","modified_gmt":"2022-04-04T09:45:42","slug":"vertical-alignment-of-div-within-a-divs","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/vertical-alignment-of-div-within-a-divs\/","title":{"rendered":"\u00a0Vertical Alignment of Div Within a Divs"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What Exactly Does Vertical Alignment Imply?<\/h2>\n\n\n\n<p>Vertical alignment is the condition or act of arranging objects to be stacked one on top of the other. This concept&#8217;s precise meaning changes depending on the situation. Vertical alignment has applications in a vast number of disciplines and subjects, and its meaning can vary substantially depending on the context.<\/p>\n\n\n\n<p><strong>See Also: <a href=\"https:\/\/cssdeck.com\/blog\/why-digital-marketers-should-learn-basic-css-and-html\/\">Why Digital Marketers Should Learn Basic CSS and HTML<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a &lt;DIV&gt; Tag?<\/h2>\n\n\n\n<p>In HTML, div tags are elements inserted to define parts of a document so that they are identifiable when a unique classification is necessary.&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"333\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-885.png\" alt=\"\" class=\"wp-image-3278\" \/><\/figure><\/div>\n\n\n\n<p>In an HTML document, the &lt;div&gt; element denotes a division or section. The &lt;div&gt; tag serves as a container for HTML components, which may then be decorated using CSS or changed using JavaScript. The class or id attribute can be used to customise the &lt;div&gt; element. The &lt;div&gt; element may include any type of information.<\/p>\n\n\n\n<p><strong>See Also: <a href=\"https:\/\/cssdeck.com\/blog\/understanding-the-difference-between-div-and-span\/\">Understanding the Difference Between Div and Span<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Alignment to Div Within a Div<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Position, Top, Left, and Margin Properties<\/h3>\n\n\n\n<p>If you know the dimensions (width and height) of the divs, you may use the position, top left, and margin attributes to horizontally and vertically center a div within a div on a page.<\/p>\n\n\n\n<p>To begin, in your HTML, enclose a div element in another div element. Use a class name like &#8220;child&#8221; for the inner div and &#8220;parent&#8221; for the outer div.<\/p>\n\n\n\n<p>Then, in your CSS, style the outer div with the class selector .parent. Set the height, width, and color of the backdrop. The color will let you notice how the inner div is centered more clearly. The position attribute should then be set to &#8220;relative.&#8221;<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"363\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-887.png\" alt=\"\" class=\"wp-image-3281\" \/><\/figure><\/div>\n\n\n\n<p>You may now style the inner div using the .child class selector. You may also change the height, width, and background color. Set the position attribute to &#8220;absolute&#8221; after that. Set the top and left attributes at 50% of their original value. Remember that this instructs the browser to align the div&#8217;s left and top edges with the parent container&#8217;s center vertically and horizontally.<\/p>\n\n\n\n<p>If you stop there, you&#8217;ll fall into the identical problem as before: the child div will seem off-center since its corners are aligned in the parent container&#8217;s center. To correctly center the child div, use negative top and left margins. Each value should be half the child element&#8217;s height and width.<\/p>\n\n\n\n<p><strong>See Also: <a href=\"https:\/\/cssdeck.com\/blog\/how-to-link-css-to-html\/\">How To Link CSS To HTML<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Position, Top, Left, and Transform Properties<\/h3>\n\n\n\n<p>Instead of using the margin property, you may use the transform property to vertically and horizontally center a div within a div whose dimensions are unknown. The CSS position, top, and left attributes will still be used.<\/p>\n\n\n\n<p>To make the outer and inner divs in HTML, follow the methods above. Set the outer div&#8217;s height, width, background color, and position to &#8220;relative&#8221; in CSS as well.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"266\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-888.png\" alt=\"\" class=\"wp-image-3306\" \/><\/figure><\/div>\n\n\n\n<p>You&#8217;ll now style the inner div in a similar manner. You may change the background&#8217;s color and position to &#8220;absolute,&#8221; as well as the top and left properties to 50%. The width and height attributes, on the other hand, will not be defined.<\/p>\n\n\n\n<p>You won&#8217;t even specify the margin attribute. Instead, you&#8217;ll use the transform property to position the child div 50% to the right and down from the container&#8217;s boundaries. The child div will be perfectly centered in the parent container as a result of this.<\/p>\n\n\n\n<p><strong>See Also: <a href=\"https:\/\/cssdeck.com\/blog\/css-and-metadata-the-html-viewport-tag\/\">CSS and Metadata: The HTML Viewport Tag<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Flexbox&nbsp;<\/h3>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/CSS_Flexible_Box_Layout\"><strong>Flexbox<\/strong><\/a> allows you to center a div within a div. Flexbox is a nice solution because it&#8217;s responsive and doesn&#8217;t require margin calculations, but it comes with a few more steps to remember.<\/p>\n\n\n\n<p>To use Flexbox to center a div horizontally and vertically, set the HTML and body heights to 100 percent. If you want the parent container to take up the whole viewport, even if it&#8217;s resized, you may set its height to 100 percent. Instead, I&#8217;ll set the parent container to a specified height for this demonstration.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"349\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-889.png\" alt=\"\" class=\"wp-image-3283\" \/><\/figure><\/div>\n\n\n\n<p>Additionally, the parent container must be defined as a flex container. Set the display property to &#8220;flex&#8221; to do this. Then set the properties align-items and justify-content to &#8220;center.&#8221; This instructs the browser to vertically and horizontally center the flex item (the div within the div).<\/p>\n\n\n\n<p><strong>See Also: <a href=\"https:\/\/cssdeck.com\/blog\/using-css-flexbox-in-responsive-design\/\">Using CSS Flexbox in Responsive Design<\/a><\/strong><\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>What Exactly Does Vertical Alignment Imply?<\/p>\n<p>Vertical alignment is the condition or act of arranging objects to be stacked one on top of the other. This concept&#8217;s precise meaning changes depending on the situation. Vertical alignment has applications in a vast [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/vertical-alignment-of-div-within-a-divs\/\" title=\"Click to read '\u00a0Vertical Alignment of Div Within a Divs'\">Read Article<\/a><\/p>\n","protected":false},"author":21,"featured_media":3284,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[41,181,8,187],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3277"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=3277"}],"version-history":[{"count":2,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3277\/revisions"}],"predecessor-version":[{"id":3307,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3277\/revisions\/3307"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media\/3284"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=3277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=3277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=3277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}