12

I got an element with unknown size with position=absolute, top=1000, left=1000.

Right now, the top left of that element is at position (1000,1000) but I'd want the center of the element to be (1000,1000).

Is there a way to do that with CSS alone?

Anthony N.
  • 295
  • 3
  • 16
RainingChain
  • 7,397
  • 10
  • 36
  • 68

2 Answers2

14

As pointed out in the comments, you can simply use transform: translate(-50%,-50%) in order to center the element vertically/horizontally. Fortunately, this method works for dynamic values, which means that it will work well in your case since you don't know the width/height of the element.

For example:

.element {
  background: #f00;
  width: 100px;
  height: 100px;
  transform: translate(-50%,-50%);
  position: absolute;
  top: 50%;
  left: 50%;
}
<div class="element"></div>

For what it's worth, here are some alternative methods for vertical/horizontal centering.

Community
  • 1
  • 1
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
1

You just need to add this css

.options span {
  position: absolute;
  line-height: 14px;
  transform: translate(-50%,-50%);
  position: absolute;
  top: 50%;
  left: 50%;
}

Let me know if you have any further clearification.

  $(document).ready(function() {
  $('#test').click(function() {
    $(".options:hidden").fadeToggle();
  });
});
body {
  font-family: 'Poor Story', sans-serif;
}

#test {
  cursor: pointer;
  display: block;
  text-align: center;
  position: absolute;
  display: flex;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.options {
  background: #f7f7f5;
  display: none;
  text-align: center;
  vertical-align: middle;
  position: absolute;
  width: 100%;
  left: 50%;
  top: 50%;
  border-radius: 50%;
  border-style: solid;
  border-color: #F3C78D;
  width: 60px;
  height: 60px;
  font-size: 12px;
}

.options span {
  color: #000000;
  text-align: center;
  vertical-align: middle;
  position: absolute;
  width: 100%;
  line-height: 14px;
  transform: translate(-50%,-50%);
  position: absolute;
  top: 50%;
  left: 50%;
}

#option1 {
  transform: translate(-100%, -150%);
}

#option2 {
  transform: translate(-160%, -40%);
}

#option3 {
  transform: translate(-50%, 50%);
}

#option4 {
  transform: translate(60%, -40%);
}

#option5 {
  transform: translate(15%, -150%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap 4.1.x -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
  <link href="https://fonts.googleapis.com/css?family=Poor+Story" rel="stylesheet">

</head>

<body>
  <div class="container">
    <button type="button" class="btn btn-outline-success" id="test">test</button>
    <div class="options" id="option1"><span>Hello<br>World</span></div>
    <div class="options" id="option2"><span>Goodbye</span></div>
    <div class="options" id="option3"><span>How<br>are<br>you?</span></div>
    <div class="options" id="option4"><span>Fine</span></div>
    <div class="options" id="option5"><span>Okay</span></div>
  </div>
</body>
<footer>
  <!-- Bootstrap 4.0 : jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
  <script type="text/javascript" src="index.js"></script>
</footer>

</html>
jaydeep patel
  • 867
  • 4
  • 14