Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position right div over the left in mobile view

Tags:

html

css

position

Is it possible to stack right side div over the left sided div in mobile view with the help of CSS? The default behavior is the right sided div floats under the left sided div.

CSS:

.left {
    position: relative;
    float: left;
    background: #F48024;
    width:576px;
    height: 324px;
}
.right {
    position: relative;
    float: left;
    background: #EFF0F1;
    width:576px;
    height: 324px;
}

HTML:

<div class="main">
    <div class="left"></div>
    <div class="right"></div>
</div>

Trying to achieve 3rd layout of this diagram.

enter image description here

like image 547
Kunj Avatar asked Feb 09 '18 07:02

Kunj


People also ask

How do I move a div to the right side of the screen?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

How do I move a div to the right without floating?

If you need to align it to the right, then set margin-left: auto and margin-right: 0; . Basically, to align a block element to one side horizontally, set the margin of that side to 0 and the opposite side to auto.

How do I position one div over another?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

How do you position a div to the right in a div?

if a div is fixed or absolute you can use right: 0; to get the div to the right.


2 Answers

You can achieve this by using flex box! Change Your css to this:

.main{
    display:flex;
    flex-wrap:wrap;
    justify-content: center;
}

.left {
    position: relative;
    background: #F48024;
    width:576px;
    height: 324px;
}

.right {
    position: relative;
    background: #EFF0F1;
    width:576px;
    height: 324px;
}

@media screen and (min-width:1152px){
  .main {
      justify-content: space-between;
  }

  .left {
      order:2;
  }

  .right {
      order:1;
  }
}

order property determines which element stacks first. You can read more about flex box here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

like image 70
dshukertjr Avatar answered Sep 21 '22 14:09

dshukertjr


This may serve as a quick fix:

@media screen and (max-width:480px){
    .main {
        display: flex;
        flex-direction: column-reverse;
    }
}

Note:

You may have to use other flex related css props too to align and justify the content with in the div props like justify-content and align-items.

But if you have many div elements, all of them will be reversed.

div-n
...
div-2
div-1
like image 45
pranay kumar Avatar answered Sep 19 '22 14:09

pranay kumar