我正在try 使用display: blockdiv内垂直居中/对齐项目.但不知何故,我仍然不知道为什么,这div个项目中的第一个项目居中不正确,就像是有点向左或有时向右缩进.

这就是我要编写的代码

<!DOCTYPE html>

<!-- FIXME: [X] Fix animesearch/refactor, migrate jikan api to v4-->
<!-- TODO (PRIORITY): [ ] Fix ani-cli, migrate from jikan v3 to v4-->
<!-- TODO: [ ] Fix ani-cli, creating a config.json when installing-->
<!-- TODO (Optional): [ ] Fix sycpll-->
<!-- TODO: [ ] Colect all the projects (OMW)-->
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
  
  <link rel="stylesheet" href="styles/main.css">
  
  <title>Arya's Portfolio</title>
</head>
<body>
  <div class="website">
    <section class="section intro">
      <ul class="navbar">
        <li style="float:left;"><a href="#">LastCleanShirt</a></li>
        <li><a href="#">About me</a></li>
        <li><a href="#">Contact me</a></li>
      </ul>

      <div class="container main-header">
        <h1>I'm</h1> <!-- This one -->
        <h1>Aryasatya</h1>
        <h1>I'm</h1>
        <h1>Aryasatya</h1>
        <h1>I'm</h1>
        <h1>Aryasatya</h1>
      </div>
    </section>
  </div>
</body>
</html>
/*
Palette

#F5F5F5
#B0D7FF
#709FDF
#3066BE
#040910


*/

* {
    margin: 0;
    padding: 0;
    font-family: "Poppins";
}


/* Section 1, Intro */

.intro {
    height: 100vh;
}

li {
    display: inline;
    float: right;
    margin: 30px;
    margin-top: 10px;
    padding: 10px;

    width: fit-content;
    height: fit-content;

    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
     box-sizing: border-box; 


}

li > a {
    font-weight: 600;
    font-size: 20px;

    text-decoration: none;
    color: #040910;
    background: white;


    background: linear-gradient(to left, white 51%, #3066BE 50%) right;
    background-size: 200%;
    transition: .5s ease-out;
    padding: 8px;

    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
     box-sizing: border-box; 
}

li > a:hover {
    background-position: left;
    color: #F5F5F5;
}

ul {
    flex: 1;
    height: 10%;
    box-sizing: border-box;
}

/* IPAD/TABLETS MEDIA */

/* MAIN-HEADER */

.main-header {
    width: 100vw;
    height: 60%;
  
    align-items : center; /* Reset display property to block */
    margin-bottom: 0;
}

.main-header > * {
    justify-contents: center; /* Set display property of child elements to block */
 /* Automatically center them horizontally */
}

.main-header > h1 {
    margin-bottom: 0; /* Add some space between h1 elements */
}

下面是一个实现的jsfidle显示它:https://jsfiddle.net/nothingman/pz8skL36/

我试着用了align-items: centerjustify-contents: center,但还是不行

推荐答案

如果你想在div <div class="container main-header"></div>中垂直居中/对齐项目,你需要在类上设置CSS规则.显示flex它,并使用flex—direction将它们放在列上,而不是在每个h1标记上设置对齐方式.有了这个你就不需要针对所有<h1>个目标了.

查看更多关于flex flex—direction的信息:https://www.w3schools.com/cssref/css3_pr_flex-direction.php&

.main-header {
  width: 100vw;
  height: 60%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

Html相关问答推荐

在FlexBox中将div拉伸到父div的完全高度

如何使用CSS在NSAttributedString中为HTML文本中的图像制作覆盖图

覆盖垫子中的边框底部 Select

SVG动画只有在打开S开发工具的浏览器时才开始播放

将精选选项的价格合并为一个价格HTML、PHP和JQuery

如何防止滑动滚动元素时touch 屏出现空白区域?

按钮悬停效果不影响按钮内的图标

页面文件夹内的 HTML 页面未加载主目录的 style.css

将组件移动到页面底部 Angular

如何正确地嵌套Flexbox

需要帮助个性化我的 CSS 导航栏:如何在鼠标悬停时突出显示
  • 元素?
  • 调整窗口大小时文本重叠导航栏

    如何在图片前面放置下拉框?

    高度大于页面高度和页面大于覆盖的 CSS 覆盖

    是否可以将文本添加到表格边框?

    辅助功能:alt 或 alt="" 用于装饰图像

    隐藏在标题后面的下拉菜单

    Bootstrap 5 网格问题,div 向下转义

    我正在创建一个 django 审查系统.我认为我的 HTML 是错误的