This question already has answers here:
Centering in CSS Grid
                                
                                    (7个答案)
                                
                        
                                2年前关闭。
            
                    
我已经尝试解决这个问题已有一段时间了。如果我实际以视觉方式表示它,我将能够最好地解释它:

我正在尝试垂直对齐导航栏。

html - 无法在CSS网格框中将导航栏垂直居中-LMLPHP

我尝试使用align-self,但是似乎没有任何变化。

/* Navigation */

.navigation {
  overflow: hidden;
  margin: 0;
  align-self: center;
}

.navigation a {
  display: block;
  float: right;
  color: #f2f2f2;
  text-align: center;
  text-decoration: none;
  font-family: 'Quicksand', sans-serif;
  font-size: 1rem;
}

.navigation a:hover {
  background-color: #ddd;
  color: black;
}

/* Hide the link that should open and close the navigation on small screens */
.navigation .icon {
    display: none;
}


这是我对CSS感到困惑的情况,还是div定位不正确的情况?

<!-- Navigation bar -->

    <div class="navigation" id="myNavigation">
      <a href="index.html">Home</a>
      <a href="#services">About Me</a>
      <a href="#about-me">Services</a>
      <a href="#portfolio">Portfolio</a>
      <a href="#contact">Contact</a>
      <a href="javascript:void(0);" class="icon" onclick="openNavigation()">&#9776;</a>
    </div>
  </div>

最佳答案

好的,所以我找到了解决方案。我需要将CSS网格设置为display: flexalign-items: center

10-06 12:41
查看更多