{"id":301081,"date":"2020-01-06T08:05:47","date_gmt":"2020-01-06T15:05:47","guid":{"rendered":"https:\/\/css-tricks.com\/?p=301081"},"modified":"2020-01-06T08:05:47","modified_gmt":"2020-01-06T15:05:47","slug":"how-to-stack-elements-in-css","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/how-to-stack-elements-in-css\/","title":{"rendered":"How to Stack Elements in CSS"},"content":{"rendered":"

If you want to create fantastic and unique visual experiences on the web, you will eventually need two elements to overlap or exist in the same place. You may even just need them to be positioned near or next to each other. Let’s go over two different ways to accomplish this, one with the position property and one with CSS Grid.<\/p>\n

<\/p>\n

Method 1: Using the Position Property<\/h3>\n

You may already know that position: absolute;<\/code> will place something absolutely on the page wherever you want it to be. In this case, we\u2019re absolutely positioning the child to the top-left of the page. No matter where the parent is, the child will be placed in that corner, absolutely.<\/p>\n

.child {\r\n  ...\r\n  position: absolute;\r\n  top: 0;\r\n  left: 0;\r\n}<\/code><\/pre>\n

\n See the Pen
\n CSS Stacking, Absolute 1<\/a> by Sarah Drasner (
@sdras<\/a>)
\n on
CodePen<\/a>.<\/span>\n<\/p>\n

But this is very brittle! What if you were to place something on the page and then something else comes along after it? Maybe you have an icon within a navigation that you always want in the top-left corner, but a third party comes in and puts in a banner ad. (I\u2019m not advocating for banner ads, but they do exist.) This pushes the navigation down and now the icon is out of place.<\/p>\n

Or, let\u2019s say you want to make a self-contained component that you can use in multiple places. You need it to be reusable and work within its own context, no matter where you use it.<\/p>\n

If we put position: relative;<\/code> on the parent element, anything inside of it with position: absolute;<\/code> will be placed absolutely, relative to that containing unit!<\/p>\n

.child {\r\n  \/* ... *\/\r\n  position: absolute;\r\n  top: 0;\r\n  left: 0;\r\n}\r\n\r\n.parent {\r\n  position: relative;\r\n}<\/code><\/pre>\n

\n See the Pen
\n CSS Stacking, Absolute 2<\/a> by Sarah Drasner (
@sdras<\/a>)
\n on
CodePen<\/a>.<\/span>\n<\/p>\n

Nice.<\/p>\n

We can use this same premise if we wanted to stack two elements on top of each other. Here, we\u2019ll have two child elements stacked on top of one another and set apart by 150 pixels. We’ll see that they’re now contained in that same parent and stay positioned inside it.<\/p>\n

<div class=\"parent\">\r\n  <h2>Parent<\/h2>\r\n\r\n  <div class=\"child child-1\">\r\n    <h2>Child 1<\/h2>\r\n  <\/div>\r\n\r\n  <div class=\"child child-2\">\r\n    <h2>Child 2<\/h2>\r\n  <\/div>\r\n<\/div><\/code><\/pre>\n
.child {\r\n  position: absolute;\r\n  top: 0;\r\n}\r\n\r\n.child-1 {\r\n  left: 0;\r\n}\r\n\r\n.child-2 {\r\n  left: 150px;\r\n}\r\n\r\n.parent {\r\n  position: relative;\r\n}<\/code><\/pre>\n

\n See the Pen
\n CSS Stacking, Absolute 3<\/a> by Sarah Drasner (
@sdras<\/a>)
\n on
CodePen<\/a>.<\/span>\n<\/p>\n

This is a little old school, but I\u2019ve been using it for years and I still reach for it. It works consistently across browsers and can help you achieve even the strangest and unique placements.<\/p>\n

Method 2: Using CSS Grid<\/h3>\n

Another nice way of overlapping elements, stacking them, or modifying their placement is CSS Grid, depending on how far back you need to support (which you can check with caniuse<\/a>).<\/p>\n

We can place something where we need it in the container like this:<\/p>\n

.parent {\r\n  display: grid;\r\n  grid-template-columns: 250px 1fr;\r\n  grid-template-rows: 150px 1fr;\r\n}\r\n\r\n.child {\r\n  grid-area: 1 \/ 1 \/ 2 \/ 2;\r\n}<\/code><\/pre>\n

\n See the Pen
\n CSS Stacking, Grid 1<\/a> by Sarah Drasner (
@sdras<\/a>)
\n on
CodePen<\/a>.<\/span>\n<\/p>\n

And if one element should stack on the other, we can put them in the exact same grid area. Let’s also offset them slightly by using a margin.<\/p>\n

.parent {\r\n  display: grid;\r\n  grid-template-columns: 250px 1fr;\r\n  grid-template-rows: 150px 1fr;\r\n}\r\n\r\n.child {\r\n  grid-area: 1 \/ 1 \/ 2 \/ 2;\r\n}\r\n\r\n.child-2 {\r\n  margin-left: 200px;\r\n}<\/code><\/pre>\n

\n See the Pen
\n CSS Stacking, Grid 2<\/a> by Sarah Drasner (
@sdras<\/a>)
\n on
CodePen<\/a>.<\/span>\n<\/p>\n

If you find this technique difficult to visualize, I\u2019ve created a CSS Grid Generator<\/a> that hopefully helps see things more clearly. <\/p>\n


\n

There are so many places to use these techniques! You can stack, layer and offset elements. You can make navigations, footers. You can create just about any type of layout where you want to have more fine-grain control of how elements are placed on a page.<\/p>\n","protected":false},"excerpt":{"rendered":"

If you want to create fantastic and unique visual experiences on the web, you will eventually need two elements to overlap or exist in the same place. You may even just need them to be positioned near or next to each other. Let’s go over two different ways to accomplish this, one with the position […]<\/p>\n","protected":false},"author":7699,"featured_media":301146,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[4],"tags":[9704,686,1200,9705,9703],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/12\/cube-pattern-svg.png?fit=2400%2C1200&ssl=1","jetpack-related-posts":[{"id":331673,"url":"https:\/\/css-tricks.com\/a-utility-class-for-covering-elements\/","url_meta":{"origin":301081,"position":0},"title":"A Utility Class for Covering Elements","date":"December 26, 2020","format":false,"excerpt":"Big ol' same to Michelle Barker here: Here\u2019s something I find myself needing to do again and again in CSS: completely covering one element with another. It\u2019s the same CSS every time: the first element (the one that needs to be covered) has\u00a0position: relative\u00a0applied to it. The second has\u00a0position: absolute\u00a0and\u2026","rel":"","context":"In "Link"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/12\/a-utility-class-for-covering-elements-01.jpg?fit=1200%2C527&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":325791,"url":"https:\/\/css-tricks.com\/grid-auto-flow-css-grid-flex-direction-flexbox\/","url_meta":{"origin":301081,"position":1},"title":"grid-auto-flow : CSS Grid :: flex-direction : Flexbox","date":"November 18, 2020","format":false,"excerpt":"When setting a parent element to display: flex, its child elements align left-to-right like this: CodePen Embed Fallback Now, one of the neat things we can do with flexbox is change the direction so that child elements are stacked vertically on top of each other in a column. We can\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":181375,"url":"https:\/\/css-tricks.com\/centering-css-complete-guide\/","url_meta":{"origin":301081,"position":2},"title":"Centering in CSS: A Complete Guide","date":"September 2, 2014","format":false,"excerpt":"Centering things in CSS is the poster child of CSS complaining. Why does it have to be so hard? They jeer. I think the issue isn't that it's difficult to do, but in that there so many different ways of doing it, depending on the situation, it's hard to know\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2014\/09\/guide-centering.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":353700,"url":"https:\/\/css-tricks.com\/less-absolute-positioning-with-modern-css\/","url_meta":{"origin":301081,"position":3},"title":"Less Absolute Positioning With Modern CSS","date":"October 13, 2021","format":false,"excerpt":"Ahmad Shadeed blogs the sentiment that we might not need to lean on position: absolute as much as we might have in the past. For one thing: stacking elements. For example, if you have a stack of elements that should all go on top of each other... .stack { display:\u2026","rel":"","context":"In "Link"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/10\/position-absolute-code.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":334958,"url":"https:\/\/css-tricks.com\/how-to-map-mouse-position-in-css\/","url_meta":{"origin":301081,"position":4},"title":"How to Map Mouse Position in CSS","date":"March 1, 2021","format":false,"excerpt":"Let\u2019s look at how to get the user\u2019s mouse position and map it into CSS custom properties: --positionX and --positionY. We could do this in JavaScript. If we did, we could do things like make make an element draggable or move a background. But actually, we can still do similar\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/02\/css-mouse-cursor-mapping.gif?fit=900%2C450&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":306022,"url":"https:\/\/css-tricks.com\/a-grid-of-logos-in-squares\/","url_meta":{"origin":301081,"position":5},"title":"A Grid of Logos in Squares","date":"April 6, 2020","format":false,"excerpt":"Let's build a literal grid of squares, and we'll put the logos of some magazines centered inside each square. I imagine plenty of you have had to build a logo grid before. You can probably already picture it: an area of a site that lists the donors, sponsors, or that\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/04\/grid-of-logos.png?fit=1200%2C902&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"featured_media_src_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/12\/cube-pattern-svg.png?fit=1024%2C512&ssl=1","_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/301081"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/7699"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=301081"}],"version-history":[{"count":7,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/301081\/revisions"}],"predecessor-version":[{"id":301243,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/301081\/revisions\/301243"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media\/301146"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=301081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=301081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=301081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}