MrBuggy MrBuggy - 5 months ago 21
CSS Question

Popup div without resizing the parent

I've got follow code:



angular.module("myApp", []).controller("myController", function($scope) {
$scope.isShown = false;

$scope.togglePopup = function() {
$scope.isShown = !$scope.isShown;
}
});

.wrapper {
display: flex;
flex-direction: column;
width: 100%;
background-color: grey;
}
.inputAddon {
display: flex;
flex-direction: row;
}
input {
width: 75px;
height: 19px;
}
.addon {
width: 25px;
height: 25px;
background-color: green;
}
.popup {
width: 200px;
height: 300px;
border: 1px solid red;
background-color: white;
z-index: 1000;
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController" class="wrapper">
<div class="inputAddon">
<input type="number">
<div class="addon" ng-click="togglePopup()"></div>
</div>
<div class="popup" ng-if="isShown"></div>
</div>





As you can see, when I click on the green div, I would like to popup another div below the input and green div. At the moment this works, but it also resizes the parent div (grey). Who can I overlay it like this without resizing the parent:

enter image description here

I tried it with
z-index
, but it doesn't work. Any ideas?

Thanks and cheers.

Answer

Add position: absolute and top to the .popup:

angular.module("myApp", []).controller("myController", function($scope) {
  $scope.isShown = false;

  $scope.togglePopup = function() {
    $scope.isShown = !$scope.isShown;
  }
});
.wrapper {
  display: flex;
  flex-direction: column;
  width: 100%;
  background-color: grey;
}
.inputAddon {
  display: flex;
  flex-direction: row;
}
input {
  width: 75px;
  height: 19px;
}
.addon {
  width: 25px;
  height: 25px;
  background-color: green;
}
.popup {
  width: 200px;
  height: 300px;
  border: 1px solid red;
  background-color: white;
  z-index: 1000;
  top: 32px;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController" class="wrapper">
  <div class="inputAddon">
    <input type="number">
    <div class="addon" ng-click="togglePopup()"></div>
  </div>
  <div class="popup" ng-if="isShown"></div>
</div>