HTML |定心

时间:2017-02-11 20:23:34

标签: javascript html css

当我使用particles.js时,我无法将图像居中。图像居中,但稍微偏离中心。为什么要这样做,我该如何集中它?

HTML

<!DOCTYPE html>
<html>
<head>
<title>particles.js demo</title>
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div id="particles-js" width='100%'></div>
<script src="http://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js">
</script>
<script src="js/index.js">
</script>
<img id='downarrow' height="75" width="75" src='images/arrowdown.png'>
</body>
</html>

CSS

body {
margin: 0;
height: 2000px;
width: 100%;
}
::-webkit-scrollbar {
display: none;
}
#particles-js {
position: absolute;
width: 100%;
height: 100%;
background-color: #00a4ff;
background-image: url("http://i.imgur.com/yHL4C4u.png");
background-repeat: no-repeat;
background-position: 50% 50%;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

#downarrow {
margin-left: 50%;
}
img {
position: absolute;
}

2 个答案:

答案 0 :(得分:2)

您正在使用left: 50%; - 这会将图片的左边缘放在页面宽度的50%处。如果您希望图像居中,请添加transform: translateX(-50%);

&#13;
&#13;
body {
  margin: 0;
  height: 2000px;
  width: 100%;
}

::-webkit-scrollbar {
  display: none;
}

#particles-js {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #00a4ff;
  background-image: url("http://i.imgur.com/yHL4C4u.png");
  background-repeat: no-repeat;
  background-position: 50% 50%;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

#downarrow {
  margin-left: 50%;
}

img {
  position: absolute;
}
&#13;
<div id="particles-js" width='100%'></div>
<script src="http://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js">
</script>
<script src="js/index.js">
</script>
<img id='downarrow' height="75" width="75" src='images/arrowdown.png'>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用否定margin-left将图片拉回中心:

#downarrow {
    left: 50%;
    margin-left: -37px; /* Half of the image width */
}