CSS -按钮未正确对齐

nszi6y05  于 2023-01-22  发布在  其他
关注(0)|答案(4)|浏览(126)

无论我做什么(现在有几个小时了),我都无法将<div class="kafelek">并排对齐;当前代码如下所示:

body {
  background-color: #003C3D;
}

.header {
  text-align: center;
  align-content: center;
  padding: 20px;
  font-family: 'Open Sans';
  font-size: 24px;
  color: #C3CECB;
}

#headerlogo {
  width: 400px;
  height: auto;
  object-fit: cover;
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  animation: slide-in 1s ease-in-out forwards;
}

@keyframes slide-in {
  from {
    top: -100px;
  }
  to {
    top: 20px;
  }
}

.pre1 {
  width: 50%;
  text-align: center;
  align-content: center;
  font-family: 'Open Sans';
  font-size: 24px;
  color: #C3CECB;
  float: left;
}

.pre2 {
  width: 50%;
  text-align: center;
  align-content: center;
  font-family: 'Open Sans';
  font-size: 24px;
  color: #C3CECB;
  float: right;
}

.alignkafel1,
.alignkafel2 {
  display: flex;
  flex-wrap: wrap;
}

.row {
  display: flex;
}

.kolumna {
  display: inline-block;
  width: 48%;
  font-family: 'Open Sans';
  color: #C3CECB;
  text-align: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.kolumna:first-child {
  margin-right: 0px;
}

.kolumna:last-child {
  margin-left: 0px;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

@media screen and (max-width: 800px) {
  .kolumna {
    width: 100%;
    align-content: center;
  }
}

kolumna2 {
  display: inline-block;
  width: 48%;
  font-family: 'Open Sans';
  color: #C3CECB;
  text-align: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.kolumna2:first-child {
  margin-right: 0px;
}

.kolumna2:last-child {
  margin-left: 0px;
}

@media screen and (max-width: 800px) {
  .kolumna2 {
    width: 100%;
    align-content: center;
  }
}

.kafelek {
  background-color: #006D57;
  border-radius: 10px;
  text-align: center;
  height: 100px;
  font-family: 'Open Sans';
  font-size: 20px;
  color: #C3CECB;
  margin: 5px;
  margin-top: 50px;
  margin-right: 50px;
  margin-bottom: 1px;
  margin-left: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

@media screen and (max-width: 800px) {
  .kafelek {
    display: flex;
    justify-content: center;
  }
}

.kafelek:hover {
  border: 2px solid #7EDF00;
  transition: 0.3s;
  cursor: pointer;
}
<div class="header">
  <img src="" img id="headerlogo" />
  <br /> <br /> Centrum zgłoszeniowe AOK
</div>

<div class="pre1"> Delivery Support </div>
<div class="pre2"> Middle Mile</div>


<div class="alignkafel1">
  <div class="kafelek">Koszta Dodatkowe</div>
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
</div>

<div class="alignkafel2">
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
</div>

我一直在尝试多种选择,但最后一切都搞砸了。
它看起来像这样:Messed up website
我希望8个.kafelek(即按钮)中的每一个都在一行中彼此完全对齐,并且正确居中以与.pre1和.pre2对齐
请帮帮忙;-;

oxosxuxt

oxosxuxt1#

按钮容器的宽度为0,这会使弹性框无法正常工作。添加宽度可以解决此问题。

.alignkafel1,
.alignkafel2 {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  width: 60%;
  margin: 0 auto;
}

我添加了一个60%的宽度来使按钮大致与上面的pre对齐。目前pre已经设置了float属性。理想情况下,您需要一个父容器来强制页面内容的边界,并避免使用float css属性。

d4so4syb

d4so4syb2#

在alignkafel1&2中,设置align-items和justify content属性,以控制作为flex项的按钮的对齐方式。

pftdvrlh

pftdvrlh3#

我删除了.alignkafel1.alignkafel2类,并将它们替换为一个带有CSS Flex.alignkafel类。flex-direction: row属性用于.alignkafel类,以提供所需的布局。我还从代码中删除了未使用的CSS类。

    • 您可以看到以下示例:**
body {
  background-color: #003C3D;
}

.header {
  text-align: center;
  align-content: center;
  padding: 20px;
  font-family: 'Open Sans';
  font-size: 24px;
  color: #C3CECB;
}

#headerlogo {
  width: 400px;
  height: auto;
  object-fit: cover;
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  animation: slide-in 1s ease-in-out forwards;
}

@keyframes slide-in {
  from {
    top: -100px;
  }
  to {
    top: 20px;
  }
}

.pre1 {
  width: 50%;
  text-align: center;
  align-content: center;
  font-family: 'Open Sans';
  font-size: 24px;
  color: #C3CECB;
  float: left;
}

.pre2 {
  width: 50%;
  text-align: center;
  align-content: center;
  font-family: 'Open Sans';
  font-size: 24px;
  color: #C3CECB;
  float: right;
}

.row {
  display: flex;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

.kafelek {
  background-color: #006D57;
  border-radius: 10px;
  text-align: center;
  height: 100px;
  font-family: 'Open Sans';
  font-size: 20px;
  color: #C3CECB;
  margin-top: 50px;
  margin-bottom: 1px;
  margin-right: 10px;
  margin-left: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.kafelek:nth-child(4) {
  margin-right: 100px !important;
}

.kafelek:nth-child(5) {
  margin-left: 100px !important;
}
.kafelek:hover {
  border: 2px solid #7EDF00;
  transition: 0.3s;
  cursor: pointer;
}

.alignkafel {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}
<div class="header">
  <img src="" img id="headerlogo" />
  <br /> <br /> Centrum zgłoszeniowe AOK
</div>

<div class="pre1"> Delivery Support </div>
<div class="pre2"> Middle Mile</div>


<div class="alignkafel">
  <div class="kafelek">Koszta Dodatkowe</div>
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
  <div class="kafelek">COMING SOON</div>
</div>
q9rjltbz

q9rjltbz4#

我修复了你的代码,只要粘贴到你的html和css文件,一切都会正常工作。:D

<div class="header">
    <img src="" img id="headerlogo" />
    <br /> <br /> Centrum zgłoszeniowe AOK
  </div>
  
  <div class="pre1"> 
    <p>Delivery Support </p>

    <div class="alignkafel">
        <div class="kafelek">Koszta Dodatkowe</div>
        <div class="kafelek">COMING SOON</div>
        <div class="kafelek">COMING SOON</div>
        <div class="kafelek">COMING SOON</div>
      </div>

  </div>
  <div class="pre2"> 
    </p>Middle Mile</p>

    <div class="alignkafel">
        <div class="kafelek">COMING SOON</div>
        <div class="kafelek">COMING SOON</div>
        <div class="kafelek">COMING SOON</div>
        <div class="kafelek">COMING SOON</div>
      </div>
</div>



    body {
  background-color: #003C3D;
}

.header {
  text-align: center;
  align-content: center;
  padding: 20px;
  font-family: 'Open Sans';
  font-size: 24px;
  color: #C3CECB;
}

#headerlogo {
  width: 400px;
  height: auto;
  object-fit: cover;
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  animation: slide-in 1s ease-in-out forwards;
}

@keyframes slide-in {
  from {
    top: -100px;
  }
  to {
    top: 20px;
  }
}

.pre1 {
  width: 50%;
  text-align: center;
  align-content: center;
  font-family: 'Open Sans';
  font-size: 24px;
  color: #C3CECB;
  float: left;
}

.pre2 {
  width: 50%;
  text-align: center;
  align-content: center;
  font-family: 'Open Sans';
  font-size: 24px;
  color: #C3CECB;
  float: right;
}

.alignkafel {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.row {
  display: flex;
}

.kolumna {
  display: inline-block;
  width: 48%;
  font-family: 'Open Sans';
  color: #C3CECB;
  text-align: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.kolumna:first-child {
  margin-right: 0px;
}

.kolumna:last-child {
  margin-left: 0px;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

@media screen and (max-width: 800px) {
  .kolumna {
    width: 100%;
    align-content: center;
  }
}

.kolumna2 {
  display: inline-block;
  width: 48%;
  font-family: 'Open Sans';
  color: #C3CECB;
  text-align: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.kolumna2:first-child {
  margin-right: 0px;
}

.kolumna2:last-child {
  margin-left: 0px;
}

@media screen and (max-width: 800px) {
  .kolumna2 {
    width: 100%;
    align-content: center;
  }
}

.kafelek {
  background-color: #006D57;
  border-radius: 10px;
  text-align: center;
  height: 100px;
  font-family: 'Open Sans';
  font-size: 20px;
  color: #C3CECB;
  margin: 5px;
  margin-top: 50px;
  margin-right: 50px;
  margin-bottom: 1px;
  margin-left: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

@media screen and (max-width: 800px) {
  .kafelek {
    display: flex;
    justify-content: center;
  }
}

.kafelek:hover {
  border: 2px solid #7EDF00;
  transition: 0.3s;
  cursor: pointer;
}

相关问题