How to overlay one div over another div

前端 未结 9 1869
旧时难觅i
旧时难觅i 2020-11-21 17:39

I need assistance with overlaying one individual div over another individual div.

My code looks like this:

9条回答
  •  孤街浪徒
    2020-11-21 18:03

    The accepted solution works great, but IMO lacks an explanation as to why it works. The example below is boiled down to the basics and separates the important CSS from the non-relevant styling CSS. As a bonus, I've also included a detailed explanation of how CSS positioning works.

    TLDR; if you only want the code, scroll down to The Result.

    The Problem

    There are two separate, sibling, elements and the goal is to position the second element (with an id of infoi), so it appears within the previous element (the one with a class of navi). The HTML structure cannot be changed.

    Proposed Solution

    To achieve the desired result we're going to move, or position, the second element, which we'll call #infoi so it appears within the first element, which we'll call .navi. Specifically, we want #infoi to be positioned in the top-right corner of .navi.

    CSS Position Required Knowledge

    CSS has several properties for positioning elements. By default, all elements are position: static. This means the element will be positioned according to its order in the HTML structure, with few exceptions.

    The other position values are relative, absolute, sticky, and fixed. By setting an element's position to one of these other values it's now possible to use a combination of the following four properties to position the element:

    • top
    • right
    • bottom
    • left

    In other words, by setting position: absolute, we can add top: 100px to position the element 100 pixels from the top of the page. Conversely, if we set bottom: 100px the element would be positioned 100 pixels from the bottom of the page.

    Here's where many CSS newcomers get lost - position: absolute has a frame of reference. In the example above, the frame of reference is the body element. position: absolute with top: 100px means the element is positioned 100 pixels from the top of the body element.

    The position frame of reference, or position context, can be altered by setting the position of a parent element to any value other than position: static. That is, we can create a new position context by giving a parent element:

    • position: relative;
    • position: absolute;
    • position: sticky;
    • position: fixed;

    For example, if a

    element is given position: relative, any child elements use the
    as their position context. If a child element were given position: absolute and top: 100px, the element would be positioned 100 pixels from the top of the
    element, because the
    is now the position context.

    The other factor to be aware of is stack order - or how elements are stacked in the z-direction. The must-know here is the stack order of elements are, by default, defined by the reverse of their order in the HTML structure. Consider the following example:

    
      
    Bottom
    Top

    In this example, if the two

    elements were positioned in the same place on the page, the
    Top
    element would cover the
    Bottom
    element. Since
    Top
    comes after
    Bottom
    in the HTML structure it has a higher stacking order.

    div {
      position: absolute;
      width: 50%;
      height: 50%;
    }
    
    #bottom {
      top: 0;
      left: 0;
      background-color: blue;
    }
    
    #top {
      top: 25%;
      left: 25%;
      background-color: red;
    }
    Bottom
    Top

    The stacking order can be changed with CSS using the z-index or order properties.

    We can ignore the stacking order in this issue as the natural HTML structure of the elements means the element we want to appear on top comes after the other element.

    So, back to the problem at hand - we'll use position context to solve this issue.

    The Solution

    As stated above, our goal is to position the #infoi element so it appears within the .navi element. To do this, we'll wrap the .navi and #infoi elements in a new element

    so we can create a new position context.

    Then create a new position context by giving .wrapper a position: relative.

    .wrapper {
      position: relative;
    }
    

    With this new position context, we can position #infoi within .wrapper. First, give #infoi a position: absolute, allowing us to position #infoi absolutely in .wrapper.

    Then add top: 0 and right: 0 to position the #infoi element in the top-right corner. Remember, because the #infoi element is using .wrapper as its position context, it will be in the top-right of the .wrapper element.

    #infoi {
      position: absolute;
      top: 0;
      right: 0;
    }
    

    Because .wrapper is merely a container for .navi, positioning #infoi in the top-right corner of .wrapper gives the effect of being positioned in the top-right corner of .navi.

    And there we have it, #infoi now appears to be in the top-right corner of .navi.

    The Result

    The example below is boiled down to the basics, and contains some minimal styling.

    /*
    *  position: relative gives a new position context
    */
    .wrapper {
      position: relative;
    }
    
    /*
    *  The .navi properties are for styling only
    *  These properties can be changed or removed
    */
    .navi {
      background-color: #eaeaea;
      height: 40px;
    }
    
    
    /*
    *  Position the #infoi element in the top-right
    *  of the .wrapper element
    */
    #infoi {
      position: absolute;
      top: 0;
      right: 0;
    
      /*
      *  Styling only, the below can be changed or removed
      *  depending on your use case
      */
      height: 20px;
      padding: 10px 10px;
    }

    An Alternate (Grid) Solution

    Here's an alternate solution using CSS Grid to position the .navi element with the #infoi element in the far right. I've used the verbose grid properties to make it as clear as possible.

    :root {
      --columns: 12;
    }
    
    /*
    *  Setup the wrapper as a Grid element, with 12 columns, 1 row
    */
    .wrapper {
      display: grid;
      grid-template-columns: repeat(var(--columns), 1fr);
      grid-template-rows: 40px;
    }
    
    /*
    *  Position the .navi element to span all columns
    */
    .navi {
      grid-column-start: 1;
      grid-column-end: span var(--columns);
      grid-row-start: 1;
      grid-row-end: 2;
      
      /*
      *  Styling only, the below can be changed or removed
      *  depending on your use case
      */
      background-color: #eaeaea;
    }
    
    
    /*
    *  Position the #infoi element in the last column, and center it
    */
    #infoi {
      grid-column-start: var(--columns);
      grid-column-end: span 1;
      grid-row-start: 1;
      place-self: center;
    }

    An Alternate (No Wrapper) Solution

    In the case we can't edit any HTML, meaning we can't add a wrapper element, we can still achieve the desired effect.

    Instead of using position: absolute on the #infoi element, we'll use position: relative. This allows us to reposition the #infoi element from its default position below the .navi element. With position: relative we can use a negative top value to move it up from its default position, and a left value of 100% minus a few pixels, using left: calc(100% - 52px), to position it near the right-side.

    /*
    *  The .navi properties are for styling only
    *  These properties can be changed or removed
    */
    .navi {
      background-color: #eaeaea;
      height: 40px;
      width: 100%;
    }
    
    
    /*
    *  Position the #infoi element in the top-right
    *  of the .wrapper element
    */
    #infoi {
      position: relative;
      display: inline-block;
      top: -40px;
      left: calc(100% - 52px);
    
      /*
      *  Styling only, the below can be changed or removed
      *  depending on your use case
      */
      height: 20px;
      padding: 10px 10px;
    }
    
    

提交回复
热议问题