我有一个div是我网站上的一个页面,我正试图将这些字体很棒的图标集中在它上面。但由于某种原因,他们不会居中。我已经搜索了一个解决方案但到目前为止还没有工作。
点击“关于”按钮,看看我在说什么。
$(function() {
$('#fadeContent').click(function(){
var bodyContent = $('#bodyContent')
bodyContent.fadeIn(400);
bodyContent.click(function() {
$(this).fadeOut(400);
})
});
});
body {
background-color: black;
font-family: "Source Sans Pro", sans-serif;
color: #ccc;
z-index: -100;
background-color:black;
}
#bodyContent{
display:none;
position:absolute;
color: white;
width:100%;
height:100%;
background:#454545;
z-index: 100;
}
.fa-times{
position: absolute;
top: 0px;
right: 0px;
font-size: 3em;
margin-right: 2%;
margin-top: 1.5%;
}
.fa-twitter,
.fa-github,
.fa-steam {
display: inline-block;
position: relative;
text-align: center;
font-size: 2.5em;
padding: .5em;
border: 3px solid white;
margin-top: 20%;
margin-left: 1.5%;
margin-right: 1.5%;
}
.fa-times:hover{
color: #e84d4d;
cursor: pointer;
}
#home {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 20%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
.about {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 37%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
#projects {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 54%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
#contact {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 71%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
}
#home:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
.about:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#projects:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#contact:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
@keyframes fadein {
from {
opacity:0;
}
to {
opacity:1;
}
}
@-webkit-keyframes fadein { /* Safari and Chrome */
from {
opacity:0;
}
to {
opacity:1;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home</title>
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro|Play" rel="stylesheet">
<link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css'>
<link rel='stylesheet prefetch' href='http://rawgit.com/fralec/elegantShareJS/master/css/elegant.css'>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<button id="home">Home</button>
<button id="fadeContent" class="about">About</button>
<button id="projects">Projects</button>
<button id="contact">Contact</button>
<div id="bodyContent">
<i class="fa fa-times" aria-hidden="true"></i>
<i class="fa fa-twitter" aria-hidden="true"></i>
<i class="fa fa-github" aria-hidden="true"></i>
<i class="fa fa-steam" aria-hidden="true"></i>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
<script type="text/javascript" src="js/elegant.js"></script>
<script type="text/javascript" src="js/transition.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/background.js"></script>
</body>
</html>
答案 0 :(得分:1)
在CSS中将NF
添加到0
。
答案 1 :(得分:0)
试试这个:
#bodyContent {
text-align: center;
}
$(function() {
$('#fadeContent').click(function() {
var bodyContent = $('#bodyContent')
bodyContent.fadeIn(400);
bodyContent.click(function() {
$(this).fadeOut(400);
})
});
});
body {
background-color: black;
font-family: "Source Sans Pro", sans-serif;
color: #ccc;
z-index: -100;
background-color: black;
}
#bodyContent {
display: none;
position: absolute;
color: white;
width: 100%;
height: 100%;
background: #454545;
z-index: 100;
text-align: center;
}
.fa-times {
position: absolute;
top: 0px;
right: 0px;
font-size: 3em;
margin-right: 2%;
margin-top: 1.5%;
}
.fa-twitter,
.fa-github,
.fa-steam {
display: inline-block;
position: relative;
text-align: center;
font-size: 2.5em;
padding: .5em;
border: 3px solid white;
margin-top: 20%;
margin-left: 1.5%;
margin-right: 1.5%;
}
.fa-times:hover {
color: #e84d4d;
cursor: pointer;
}
#home {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 20%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s;
/* Firefox */
-webkit-animation: fadein 2s;
/* Safari and Chrome */
}
.about {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 37%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s;
/* Firefox */
-webkit-animation: fadein 2s;
/* Safari and Chrome */
}
#projects {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 54%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s;
/* Firefox */
-webkit-animation: fadein 2s;
/* Safari and Chrome */
}
#contact {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 71%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s;
/* Firefox */
-webkit-animation: fadein 2s;
/* Safari and Chrome */
}
#home:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
.about:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#projects:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#contact:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
@keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@-webkit-keyframes fadein {
/* Safari and Chrome */
from {
opacity: 0;
}
to {
opacity: 1;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home</title>
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro|Play" rel="stylesheet">
<link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css'>
<link rel='stylesheet prefetch' href='http://rawgit.com/fralec/elegantShareJS/master/css/elegant.css'>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<button id="home">Home</button>
<button id="fadeContent" class="about">About</button>
<button id="projects">Projects</button>
<button id="contact">Contact</button>
<div id="bodyContent">
<i class="fa fa-times" aria-hidden="true"></i>
<i class="fa fa-twitter" aria-hidden="true"></i>
<i class="fa fa-github" aria-hidden="true"></i>
<i class="fa fa-steam" aria-hidden="true"></i>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
<script type="text/javascript" src="js/elegant.js"></script>
<script type="text/javascript" src="js/transition.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/background.js"></script>
</body>
</html>
答案 2 :(得分:0)
如果您可以在#bodyContent
div中添加内容包装器,则可以使用flexbox轻松解决该问题。
这是修改过的代码,我减少了图标上的边距,将它们包裹在一个div中(高度适合容器),项目对齐到中心,内容对齐到中心。这将使图标位于#bodyContent
的绝对中间位置。
$(function() {
$('#fadeContent').click(function() {
var bodyContent = $('#bodyContent')
bodyContent.fadeIn(400);
bodyContent.click(function() {
$(this).fadeOut(400);
})
});
});
body {
background-color: black;
font-family: "Source Sans Pro", sans-serif;
color: #ccc;
z-index: -100;
background-color: black;
}
#bodyContent {
display: none;
position: absolute;
color: white;
width: 100%;
height: 100%;
background: #454545;
z-index: 100;
}
.fa-times {
position: absolute;
top: 0px;
right: 0px;
font-size: 3em;
margin-right: 2%;
margin-top: 1.5%;
}
.fa-twitter,
.fa-github,
.fa-steam {
display: inline-block;
position: relative;
text-align: center;
font-size: 2.5em;
padding: .5em;
border: 3px solid white;
margin: 0 2px;
}
.fa-times:hover {
color: #e84d4d;
cursor: pointer;
}
#home {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 20%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s;
/* Firefox */
-webkit-animation: fadein 2s;
/* Safari and Chrome */
}
.about {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 37%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s;
/* Firefox */
-webkit-animation: fadein 2s;
/* Safari and Chrome */
}
#projects {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 54%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s;
/* Firefox */
-webkit-animation: fadein 2s;
/* Safari and Chrome */
}
#contact {
position: absolute;
width: 16%;
background-color: transparent;
border: 2px solid #FFF;
margin-top: 12%;
margin-left: 71%;
height: 3em;
color: #FFF;
font-size: 1em;
cursor: pointer;
text-transform: uppercase;
border-radius: 4px;
animation: fadein 2s;
-moz-animation: fadein 2s;
/* Firefox */
-webkit-animation: fadein 2s;
/* Safari and Chrome */
}
#home:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
.about:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#projects:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
#contact:hover {
color: #1dd2e2;
border: 3px solid #18bdef;
font-weight: bold;
}
@keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@-webkit-keyframes fadein {
/* Safari and Chrome */
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.bodyContent-inside {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home</title>
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro|Play" rel="stylesheet">
<link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css'>
<link rel='stylesheet prefetch' href='http://rawgit.com/fralec/elegantShareJS/master/css/elegant.css'>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<button id="home">Home</button>
<button id="fadeContent" class="about">About</button>
<button id="projects">Projects</button>
<button id="contact">Contact</button>
<div id="bodyContent">
<div class="bodyContent-inside">
<i class="fa fa-times" aria-hidden="true"></i>
<i class="fa fa-twitter" aria-hidden="true"></i>
<i class="fa fa-github" aria-hidden="true"></i>
<i class="fa fa-steam" aria-hidden="true"></i>
</div>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
<script type="text/javascript" src="js/elegant.js"></script>
<script type="text/javascript" src="js/transition.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/background.js"></script>
</body>
</html>
干杯!