How do I fix the element to the right of the div?

I have a button that I want to be fixed on the right side of the div, the button toggles its visibility to the left of the div, the problem is that the button loses position after changing the resolution ...

Here is an example

And here's what I've done so far:

  $('.results_toggle').on('click', function() {
    $(this).toggleClass('left_hide');
    $('.left').toggle();
  });
      

    .cont {
      width: 100vw;
    }

    .left {
      position: relative;
      width: 50vw;
      height: 100vh;
      background-color: grey;
      float: left;
      border-left: 2px solid white;
    }

    .right {
      height: 100vh;
      width: 50vw;
      float: left;
    }

    .results_toggle:before {
      content: "\f054";
      font-family: FontAwesome;
      font-style: normal;
      font-weight: normal;
      text-decoration: inherit;
      color: black;
      font-size: 24px;
      padding-right: 0.5em;
      position: absolute;
      top: 14px;
      left: 5px;
    }

    .results_toggle {
      background-color: grey;
      height: 60px;
      width: 30px;
      position: absolute;
      z-index: 106;
      top: 45vh;
      right: 223px;
      border-bottom-right-radius: 110px;
      border-top-right-radius: 110px;
      border-bottom: 0;
    }

    .left_hide {
      left: 0px;
    }
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

      <div class="cont">
        <div class="left">
        </div>
        <div class="results_toggle">
        <!-- the button -->
        </div>
        <div class="right">
        </div>
     </div>
      

Run codeHide result


+3


source to share


5 answers


The simplest solution for this would be to put a radio button on the div .right

and place it on left: 0

so that it is always next to the div .left

, something like this:

<div class="cont">
  <div class="left"></div>
  <div class="right">
    <div class="results_toggle"></div>    
  </div>
</div>

      

.right {
  position: relative; /* add this */
}

.results_toggle {
  /* remove 'right' */
  left: 0; /* add this */
}

      



Working example

The advantage of this method is that it will not be completely changed by any change in screen resolution.

+3


source


Not sure if you meant it, but I just changed the left

button attribute to 50vw

, like your gray square. Here's a violin



edit: another option: position: relative

and float: left

without property left

or right

updated fiddle

+1


source


You are using view units, so their values ​​will change when the viewport size (resolution) is changed.

If you want to arrow

stay in the middle (and so, on the right side of the gray div), you have to center it this way

See snippet below

$('.results_toggle').on('click', function() {

  $(this).toggleClass('left_hide');
  $('.left').toggle();

});
      

.cont {
  width: 100vw;
}

.left {
  position: relative;
  width: 50vw;
  height: 100vh;
  background-color: grey;
  float: left;
  border-left:2px solid white;
}

.right {
  height: 100vh;
  width: 50vw;
  float: left;
}

.results_toggle:before {
      content: "\f054";
    font-family: FontAwesome;
    font-style: normal;
    font-weight: normal;
    text-decoration: inherit;
    color: black;
    font-size: 24px;
    padding-right: 0.5em;
    position: absolute;
    top: 14px;
    left: 5px;
}

.results_toggle {
      background-color: grey;
    height: 60px;
    width: 30px;
    position: absolute;
    z-index: 106;
    top: 50%;
    right:50%;
    transform:translate(100%,-50%);
    border-bottom-right-radius: 110px;
    border-top-right-radius: 110px;
    border-bottom: 0;
}

.left_hide{
  left:0px;
}
      

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cont">
  <div class="left">

  </div>
  <div class="results_toggle">

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

  </div>
</div>
      

Run codeHide result


+1


source


For me, the best approach to aligning elements is to use flexbox attributes. With these attributes, you can put a box-like element in a row, column ... In your case, you have a main rectangle .cont with left and right sides. This is the result of placing the Flexbox:

enter image description here

The main div is represented by a red background. Internally, you have a left div and is right-aligned.

Here's the code to do it:

<html>
 <head>
  <meta charset='utf-8' />
  <style type="text/css">
  .cont
  {
   display: flex;
   align-items: center;
   background-color: red;
   color: white;
  }
  .left
  {
   background-color: blue;
   margin: 5px;
  }
  button
  {
   background-color: green;
  }
  </style>
 </head>
 <body>
   <div class="cont">
    <div class="left">
     <p>Left div</p>
    </div>
    <div class="results_toggle">
      <button>Right button</button>
    </div>
    <div class="right"></div>
   </div>
 </body>
</html>

      

+1


source


This is because you have fixed every property.

You can fix the element to the right of its parent using absolute

and relative

position

. And add width

your child.

Example

.parent{
width:200px;
height:200px;
background-color:#ccc;
position:relative;
}

.child{
position:absolute;
right:0;
top:100px;
transform:translateX(100%) translateY(-50%);
}
      

<div class="parent">
<button class="child">btn</button>
</div>
      

Run codeHide result


0


source







All Articles