我得到了一个大小未知的元素,其中position = absolute,top = 1000,left = 1000。
现在,该元素的左上角位于(1000,1000),但我希望元素的中心为(1000,1000)。
有没有办法单独使用CSS?
答案 0 :(得分:6)
正如the comments中所指出的,您只需使用transform: translate(-50%,-50%)
即可垂直/水平居中。幸运的是,这种方法适用于动态值,这意味着它可以很好地适用于您的情况,因为您不知道元素的宽度/高度。
例如:
.element {
background: #f00;
width: 100px;
height: 100px;
transform: translate(-50%,-50%);
position: absolute;
top: 50%;
left: 50%;
}

<div class="element"></div>
&#13;
对于它的价值,这里有some alternative methods垂直/水平居中。
答案 1 :(得分:0)
您只需要添加此CSS
.options span {
position: absolute;
line-height: 14px;
transform: translate(-50%,-50%);
position: absolute;
top: 50%;
left: 50%;
}
让我知道您是否需要进一步澄清。
$(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>