我发现了很多如何使文本居中的代码,但是没有任何效果。我的代码中一定有错误。我尝试将h1.main
居中到页面中间。顶部50%,左侧50%。但是,总是从两侧有一些像素。它不会使文本居中。
$('.menu-toggle').click(function(){$('.site-nav').toggleClass('site-nav--open',500);$(this).toggleClass('open');})
$('.menu-link').click(function(){$('.site-nav').toggleClass('site-nav--open',250);$('.menu-toggle').toggleClass('open');})
$(function($){$('a').on('click',function(e){var $anchor=$(this).attr("href");var $hrefStart=$anchor.substr(0,1);if($hrefStart=="#"){$('html,body').animate({scrollTop:$($anchor).offset().top},1500,'easeInOutExpo');e.preventDefault();}else{window.location.href=$anchor;}});})(jQuery);
@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,800');
body {
background: #1d1d1d;
font-family: 'Montserrat', sans-serif;
font-size: 16px;
margin: 0 auto;
padding: 0;
overflow-x: hidden;
}
.container {
width: 100%;
margin: 0 auto;
padding: 0;
}
header {
background-color: #1d1d1d;
color: #EBEBD3;
padding: 1em 0;
position: fixed;
width: 100%;
z-index: 999;
}
header::after {
content: '';
clear: both;
display: block;
}
.logo {
float: left;
font-size: 1rem;
margin: 0;
position: relative;
left: 5%;
text-transform: uppercase;
font-weight: 700;
}
.logo span {
font-weight: 400;
}
.site-nav {
position: absolute;
background-color: #1d1d1d;
top: 100%;
right: 0;
height: 0px;
overflow: hidden;
transition: .5s ease-in-out;
opacity: 0;
width: 100%;
z-index: 99999;
}
.site-nav--open {
height: 100vh;
opacity: 100;
}
.site-nav ul {
padding: 0;
list-style: none;
margin: 0;
}
.site-nav li {
width: 100%;
text-align: center;
/* border-bottom: 1px solid #575766;*/
}
.site-nav li:last-child {
/* border-bottom: none; */
}
.site-nav a {
font-weight: 800;
font-size: 40px;
color: #9E9E9E;
text-decoration: none;
display: block;
padding: 2em;
text-transform: uppercase;
z-index: 99999;
}
.site-nav a:hover,
.site-nav a:focus {
color: white;
}
.menu-toggle {
position: fixed;
padding: 1em;
position: absolute;
right: .75em;
top: .5em;
cursor: pointer;
}
.hamburger,
.hamburger::before,
.hamburger::after {
content: '';
display: block;
background-color: #2ecc71;
height: 3px;
width: 1.75em;
border-radius: 3px;
transition: .5s ease-in-out;
}
.hamburger::before {
transform: translateY(-6px);
}
.hamburger::after {
transform: translateY(3px);
}
.open .hamburger::before {
opacity: 0;
}
.open .hamburger::after{
transform: translateY(-3px) rotate(90deg);
}
.open .hamburger {
transform: rotate(45deg);
}
#particles-js {
height: 100vh;
width: 100%;
}
#about {
height: 100vh;
}
#portfolio {
height: 100vh;
background-color: blue;
}
#contact {
height: 100vh;
background-color: red;
}
h1.main {
color: white;
position: absolute;
padding: 0;
margin: 0 auto;
top: 50%;
display: inline-block;
left: 50%;
text-align: center;
font-size: 5rem ;
}
h2 {
color: white;
position: absolute;
top: 60%;
left: 5%;
font-size: 2em;
}
a.logo {
text-decoration: none;
color: #2ecc71;
}
span.home {
position: absolute;
top: 50%;
right: 1%;
transform: rotate(90deg);
color: #fff;
font-size: 1em;
}
@media only screen and (max-width: 500px) {
.site-nav a {
padding: 2em;
font-size: 1.5em;
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<meta name="description" content="Doplniť neskôr">
</head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="normalize.css">
<body>
<header>
<div class="container">
<a href="#particles-js" class="logo"><h1 class="logo">Lorem <span>ipsum</span></h1></a>
<nav class="site-nav">
<ul>
<li><a class="menu-link" href="#particles-js">Home</a></li>
<li><a class="menu-link" href="#about">About</a></li>
<li><a class="menu-link" href="#portfolio">Portfolio</a></li>
<li><a class="menu-link" href="#contact">Contact</a></li>
</ul>
</nav>
<div class="menu-toggle">
<div class="hamburger"></div>
</div>
</div>
</header>
<div id="particles-js">
<h1 class="main">Lorem ipsum</h1>
</div>
<span class="home">Home</span>
<div id="about">
</div>
<div id="portfolio"></div>
<div id="contact"></div>
<script src="js/particles.js"></script>
<script src="js/app.js"></script>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</body>
</html>
答案 0 :(得分:2)
让我用您的代码解释问题,因为您需要使文本居中,因此可以摆脱left: 50%
并添加left:0; width: 100%
。这两个更新可解决您的问题,请检查以下工作片段,希望对您有所帮助:)
$('.menu-toggle').click(function () { $('.site-nav').toggleClass('site-nav--open', 500); $(this).toggleClass('open'); }) $('.menu-link').click(function () { $('.site-nav').toggleClass('site-nav--open', 250); $('.menu-toggle').toggleClass('open'); })
$(function ($) { $('a').on('click', function (e) { var $anchor = $(this).attr("href"); var $hrefStart = $anchor.substr(0, 1); if ($hrefStart == "#") { $('html,body').animate({ scrollTop: $($anchor).offset().top }, 1500, 'easeInOutExpo'); e.preventDefault(); } else { window.location.href = $anchor; } }); })(jQuery);
@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,800');
body {
background: #1d1d1d;
font-family: 'Montserrat',
sans-serif;
font-size: 16px;
margin: 0 auto;
padding: 0;
overflow-x: hidden;
}
.container {
width: 100%;
margin: 0 auto;
padding: 0;
}
header {
background-color: #1d1d1d;
color: #EBEBD3;
padding: 1em 0;
position: fixed;
width: 100%;
z-index: 999;
}
header::after {
content: '';
clear: both;
display: block;
}
.logo {
float: left;
font-size: 1rem;
margin: 0;
position: relative;
left: 5%;
text-transform: uppercase;
font-weight: 700;
}
.logo span {
font-weight: 400;
}
.site-nav {
position: absolute;
background-color: #1d1d1d;
top: 100%;
right: 0;
height: 0px;
overflow: hidden;
transition: .5s ease-in-out;
opacity: 0;
width: 100%;
z-index: 99999;
}
.site-nav--open {
height: 100vh;
opacity: 100;
}
.site-nav ul {
padding: 0;
list-style: none;
margin: 0;
}
.site-nav li {
width: 100%;
text-align: center;
/* border-bottom: 1px solid #575766;*/
}
.site-nav li:last-child {
/* border-bottom: none; */
}
.site-nav a {
font-weight: 800;
font-size: 40px;
color: #9E9E9E;
text-decoration: none;
display: block;
padding: 2em;
text-transform: uppercase;
z-index: 99999;
}
.site-nav a:hover,
.site-nav a:focus {
color: white;
}
.menu-toggle {
position: fixed;
padding: 1em;
position: absolute;
right: .75em;
top: .5em;
cursor: pointer;
}
.hamburger,
.hamburger::before,
.hamburger::after {
content: '';
display: block;
background-color: #2ecc71;
height: 3px;
width: 1.75em;
border-radius: 3px;
transition: .5s ease-in-out;
}
.hamburger::before {
transform: translateY(-6px);
}
.hamburger::after {
transform: translateY(3px);
}
.open .hamburger::before {
opacity: 0;
}
.open .hamburger::after {
transform: translateY(-3px) rotate(90deg);
}
.open .hamburger {
transform: rotate(45deg);
}
#particles-js {
height: 100vh;
width: 100%;
}
#about {
height: 100vh;
}
#portfolio {
height: 100vh;
background-color: blue;
}
#contact {
height: 100vh;
background-color: red;
}
h1.main {
color: white;
position: absolute;
padding: 0;
margin: 0 auto;
top: 50%;
left: 0%;
text-align: center;
font-size: 5rem;
width: 100%;
}
h2 {
color: white;
position: absolute;
top: 60%;
left: 5%;
font-size: 2em;
}
a.logo {
text-decoration: none;
color: #2ecc71;
}
span.home {
position: absolute;
top: 50%;
right: 1%;
transform: rotate(90deg);
color: #fff;
font-size: 1em;
}
@media only screen and (max-width: 500px) {
.site-nav a {
padding: 2em;
font-size: 1.5em;
}
}
<header>
<div class="container">
<a href="#particles-js" class="logo">
<h1 class="logo">Lorem
<span>ipsum</span>
</h1>
</a>
<nav class="site-nav">
<ul>
<li>
<a class="menu-link" href="#particles-js">Home</a>
</li>
<li>
<a class="menu-link" href="#about">About</a>
</li>
<li>
<a class="menu-link" href="#portfolio">Portfolio</a>
</li>
<li>
<a class="menu-link" href="#contact">Contact</a>
</li>
</ul>
</nav>
<div class="menu-toggle">
<div class="hamburger"></div>
</div>
</div>
</header>
<div id="particles-js">
<h1 class="main">Lorem ipsum</h1>
</div>
<span class="home">Home</span>
<div id="about"></div>
<div id="portfolio"></div>
<div id="contact"></div>
<script src="js/particles.js"></script>
<script src="js/app.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
答案 1 :(得分:2)
只需将transform: translate(-50%,-50%);
添加到下面的代码
h1.main {
color: white;
position: absolute;
padding: 0;
margin: 0 auto;
top: 50%;
display: inline-block;
left: 50%;
text-align: center;
font-size: 5rem;
transform: translate(-50%,-50%);
}
transform: translate(-50%,-50%)
会将元素wrt居中放置到自己的位置,因为元素的枢轴/锚点位于左上角。
通过向左和向左推动其自身长度和高度的50%, 因此将元素居中
$('.menu-toggle').click(function() {
$('.site-nav').toggleClass('site-nav--open', 500);
$(this).toggleClass('open');
})
$('.menu-link').click(function() {
$('.site-nav').toggleClass('site-nav--open', 250);
$('.menu-toggle').toggleClass('open');
})
$(function($) {
$('a').on('click', function(e) {
var $anchor = $(this).attr("href");
var $hrefStart = $anchor.substr(0, 1);
if ($hrefStart == "#") {
$('html,body').animate({
scrollTop: $($anchor).offset().top
}, 1500, 'easeInOutExpo');
e.preventDefault();
} else {
window.location.href = $anchor;
}
});
})(jQuery);
@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,800');
body {
background: #1d1d1d;
font-family: 'Montserrat', sans-serif;
font-size: 16px;
margin: 0 auto;
padding: 0;
overflow-x: hidden;
}
.container {
width: 100%;
margin: 0 auto;
padding: 0;
}
header {
background-color: #1d1d1d;
color: #EBEBD3;
padding: 1em 0;
position: fixed;
width: 100%;
z-index: 999;
}
header::after {
content: '';
clear: both;
display: block;
}
.logo {
float: left;
font-size: 1rem;
margin: 0;
position: relative;
left: 5%;
text-transform: uppercase;
font-weight: 700;
}
.logo span {
font-weight: 400;
}
.site-nav {
position: absolute;
background-color: #1d1d1d;
top: 100%;
right: 0;
height: 0px;
overflow: hidden;
transition: .5s ease-in-out;
opacity: 0;
width: 100%;
z-index: 99999;
}
.site-nav--open {
height: 100vh;
opacity: 100;
}
.site-nav ul {
padding: 0;
list-style: none;
margin: 0;
}
.site-nav li {
width: 100%;
text-align: center;
/* border-bottom: 1px solid #575766;*/
}
.site-nav li:last-child {
/* border-bottom: none; */
}
.site-nav a {
font-weight: 800;
font-size: 40px;
color: #9E9E9E;
text-decoration: none;
display: block;
padding: 2em;
text-transform: uppercase;
z-index: 99999;
}
.site-nav a:hover,
.site-nav a:focus {
color: white;
}
.menu-toggle {
position: fixed;
padding: 1em;
position: absolute;
right: .75em;
top: .5em;
cursor: pointer;
}
.hamburger,
.hamburger::before,
.hamburger::after {
content: '';
display: block;
background-color: #2ecc71;
height: 3px;
width: 1.75em;
border-radius: 3px;
transition: .5s ease-in-out;
}
.hamburger::before {
transform: translateY(-6px);
}
.hamburger::after {
transform: translateY(3px);
}
.open .hamburger::before {
opacity: 0;
}
.open .hamburger::after {
transform: translateY(-3px) rotate(90deg);
}
.open .hamburger {
transform: rotate(45deg);
}
#particles-js {
height: 100vh;
width: 100%;
}
#about {
height: 100vh;
}
#portfolio {
height: 100vh;
background-color: blue;
}
#contact {
height: 100vh;
background-color: red;
}
h1.main {
color: white;
position: absolute;
padding: 0;
margin: 0 auto;
top: 50%;
display: inline-block;
left: 50%;
text-align: center;
font-size: 5rem;
transform: translate(-50%,-50%);
}
h2 {
color: white;
position: absolute;
top: 60%;
left: 5%;
font-size: 2em;
}
a.logo {
text-decoration: none;
color: #2ecc71;
}
span.home {
position: absolute;
top: 50%;
right: 1%;
transform: rotate(90deg);
color: #fff;
font-size: 1em;
}
@media only screen and (max-width: 500px) {
.site-nav a {
padding: 2em;
font-size: 1.5em;
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<meta name="description" content="Doplniť neskôr">
</head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="normalize.css">
<body>
<header>
<div class="container">
<a href="#particles-js" class="logo">
<h1 class="logo">Lorem <span>ipsum</span></h1>
</a>
<nav class="site-nav">
<ul>
<li><a class="menu-link" href="#particles-js">Home</a></li>
<li><a class="menu-link" href="#about">About</a></li>
<li><a class="menu-link" href="#portfolio">Portfolio</a></li>
<li><a class="menu-link" href="#contact">Contact</a></li>
</ul>
</nav>
<div class="menu-toggle">
<div class="hamburger"></div>
</div>
</div>
</header>
<div id="particles-js">
<h1 class="main">Lorem ipsum</h1>
</div>
<span class="home">Home</span>
<div id="about">
</div>
<div id="portfolio"></div>
<div id="contact"></div>
<script src="js/particles.js"></script>
<script src="js/app.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</body>
</html>
答案 2 :(得分:0)
这是一个简单的修复,您只需要更新CSS中的2个代码块即可。问题是您的容器#particle-js
没有将h1垂直居中。因此,我只设置了display: flex
并使用align-items
属性将其对齐到垂直居中。
#particles-js {
height: 100vh;
width: 100%;
display: flex;
align-items: center;
}
h1.main {
color: white;
padding: 0;
margin: 0 auto;
text-align: center;
font-size: 5rem ;
}