Positioning two blocks in absolute position for desktop and relative position for mobile using CSS or Jquery

user1502071

I am trying to perform the below task as shown in the image. I tried with css coding and failed to this. Is it possible with CSS, if not how to achieve this task with jquery.

For desktop : I am trying Block 1 and block 2 are in absolute position to container, while block 2 position relative to block 1 with height auto. Block 3 position relative to container.

For Mobile In the mobile view I want only block 1 in the absolute bottom position of the container and block 2 and block 3 position relative to container as shown in the picture.

I want to perform this task using CSS

<style>
.container{
    width:100%;
    min-height:768px;
    height:auto;
    margin:0;
    padding:0;
    top:0;
    left:0;
    position:absolute;
    display:block;
    background:#666;
}
.b-50{
    width:50%;
}
.pinned1{
    background:#888;
    float:right;
    display:block;
    padding:20px;
    height:auto;
    color:#FFF;
}
.pinned2{
    background:#888;
    position:relative;
    display:block;
    float:right;
    color:#FFF;
    padding:20px;
    height:auto;
}
.block3{
    display:block;
    height:auto;
    position:relative;
    margin-top:auto;
    top:100%;
}
@media all and (max-width: 767px){
.b-50{
    width:100%;
}
.pinned2{
    position:absolute;
    top:100%;
}

}
</style>
<div class="container">
          <div class="b-50 pinned1">
              <h1>Block 1</h1>
          </div>    
          <div class="b-50 pinned2">
              <h2>Block 2</h2>
          </div>
</div>
<div class="block3">
    <h3>Block 3</h3>
</div>

Demo JSFIDDLDE

roo2

a pure CSS solution is a tough problem because you want to make it appear as if the nesting of elements has changed!

It can be accomplished by relying on the fact that floating elements dont affect their parents height unless there is a clear:both element below them (the clearfix ). We then make the parent ignore the height of the second child on the mobile site.

<div class="container">
   <div class="box1"> </div>    

   <div class="clear-mobile"></div>

   <div class="box2"> </div>    

   <div class="clear-desktop></div>
</div>

css:

/* for desktop */
.clear-desktop{
    clear: both;   
}

@(max-width: 767px)

  .box2{
    margin-top: 40px;
  }
  .clear-mobile{
    clear: both;
  }
  .clear-desktop{
    clear: none;   /* now container will ignore .box2's height! */
  }
}

posible with css only!

http://jsfiddle.net/b5Kyy/2/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Position absolute but relative to parent

From Dev

How to position a div to the bottom of the container, without using absolute positioning?

From Dev

Positioning two blocks in absolute position for desktop and relative position for mobile using CSS or Jquery

From Dev

CSS positioning independently of other overlapping elements (without position: absolute)

From Dev

Position relative element in front of absolute element CSS

From Dev

Absolute position relative to an image

From Dev

How to add !important in position:absolute using jQuery.css() function?

From Dev

Positioning absolute and relative elements with css

From Dev

css add two div with absolute position in container

From Dev

CSS position:absolute not covering position:relative text and borders

From Dev

Position element without using absolute positioning

From Dev

positioning two sibling elements to be at same top without using position absolute(2)

From Dev

CSS: absolute position and relative position

From Dev

css position an absolute div in relative with a slideable menu

From Dev

Position absolute and relative on one element?

From Dev

Difference and impact of position:relative, position:absolute, and float

From Dev

CSS drop down menue using absolute positioning with a left:auto position

From Dev

Things disappearing with position:absolute but not position:relative?

From Dev

position absolute within position relative not working correctly

From Dev

How to add !important in position:absolute using jQuery.css() function?

From Dev

How to position a label using css relative to an absolute positioned input box

From Dev

HTML CSS Div overlapping absolute & relative position

From Dev

CSS position:absolute not covering position:relative text and borders

From Dev

CSS Absolute Position Not Positioning a Button

From Dev

CSS Position element relative to two other

From Dev

CSS: Relative position in absolute position in a relatively positioned div

From Dev

How to use absolute position as relative?

From Dev

CSS relative vs absolute position

From Dev

advantage of using absolute over relative position?

Related Related

  1. 1

    Position absolute but relative to parent

  2. 2

    How to position a div to the bottom of the container, without using absolute positioning?

  3. 3

    Positioning two blocks in absolute position for desktop and relative position for mobile using CSS or Jquery

  4. 4

    CSS positioning independently of other overlapping elements (without position: absolute)

  5. 5

    Position relative element in front of absolute element CSS

  6. 6

    Absolute position relative to an image

  7. 7

    How to add !important in position:absolute using jQuery.css() function?

  8. 8

    Positioning absolute and relative elements with css

  9. 9

    css add two div with absolute position in container

  10. 10

    CSS position:absolute not covering position:relative text and borders

  11. 11

    Position element without using absolute positioning

  12. 12

    positioning two sibling elements to be at same top without using position absolute(2)

  13. 13

    CSS: absolute position and relative position

  14. 14

    css position an absolute div in relative with a slideable menu

  15. 15

    Position absolute and relative on one element?

  16. 16

    Difference and impact of position:relative, position:absolute, and float

  17. 17

    CSS drop down menue using absolute positioning with a left:auto position

  18. 18

    Things disappearing with position:absolute but not position:relative?

  19. 19

    position absolute within position relative not working correctly

  20. 20

    How to add !important in position:absolute using jQuery.css() function?

  21. 21

    How to position a label using css relative to an absolute positioned input box

  22. 22

    HTML CSS Div overlapping absolute & relative position

  23. 23

    CSS position:absolute not covering position:relative text and borders

  24. 24

    CSS Absolute Position Not Positioning a Button

  25. 25

    CSS Position element relative to two other

  26. 26

    CSS: Relative position in absolute position in a relatively positioned div

  27. 27

    How to use absolute position as relative?

  28. 28

    CSS relative vs absolute position

  29. 29

    advantage of using absolute over relative position?

HotTag

Archive