我目前有四张个人资料卡,而Iam试图找到一种方法来将它们快速地放在页面中央。我将它们浮动到左侧,但是一旦将它们居中,它们就会垂直堆叠在一起。当涉及移动和平板电脑视图时,这尤其是一个问题。有没有一种方法可以解决此问题,并将所有四个显示在一行中?

在此先感谢您的帮助。



a {
  color: inherit;
  text-decoration: none;
}

a:hover {
  color: rgba(0, 0, 0, .65);
}

a:hover {
  color: rgba(0, 0, 0, .45);
}

.window,
.box,
ul,
li {
  margin: 0;
  overflow: hidden;
  padding: 0;
  position: relative;
}

ul {
  font-family: 'Fjalla One', sans-serif;
  list-style-type: none;
  text-transform: uppercase;
}

li {
  display: inline-block;
}

img {
  display: block;
  width: 100%;
}

.btn {
  background: rgba(255,255,255,0.8);
  border: 1px solid rgba(255,255,255,0.5);
  border-radius: 40px;
  color: rgba(0,0,0,0.75);
  display: block;
  font-size: 1em;
  font-weight: 400;
  height: 44px;
  letter-spacing: 5px;
  line-height: 42px;
  margin: 10px auto;
  padding: 0;
  position: relative;
  text-align: center;
  text-transform: uppercase;
  -webkit-transition: all .5s ease;
  transition: all .5s ease;
  vertical-align: middle;
  width: 60%;
}

.btn:hover {
  background: rgba(0,0,0,0.1);
  border: 1px solid rgba(0,0,0,0.15);
}

.overlay {

  background-size: cover;
  height: calc(100% + 40px);
  margin: -20px;
  position: relative;
  width: calc(100% + 40px);
}

.window {

  height: 560px;
  margin: 2em auto 0;
  width: 300px;
  float: left;
  padding: 5px;
}

.header {
  background: blue;
  color: #FFF;
  height: 380px;
  left: 0;
  position: absolute;
  text-align: center;
  top: 0;
  width: inherit;
}

.header:before {
  border: 2px solid rgba(161, 220, 255, 0.34);
  border-radius: 100%;
  content: "";
  height: 140px;
  left: 0;
  margin: 67px auto;
  padding: 0;
  position: absolute;
  right: 0;
  top: 0;
  width: 140px;
  z-index: 2;
  -webkit-transform: scale(1.24, 1.24);
  -moz-transform: scale(1.24, 1.24);
  transform: scale(1.24, 1.24);
}

.header img {
  border: 5px solid #A1DCFF;
  border-radius: 100%;
  height: 140px;
  margin: 4em auto 2.5em;
  object-fit: cover;
  width: 140px;
}

.header h2 {
  display: inline-block;
  font-family: 'Quicksand', sans-serif;
  font-size: 28px;
  font-weight: 400;
  letter-spacing: -2px;
  margin: 0 auto;
  padding: 0;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}

.header h4 {
  color: rgba(255, 255, 255, 0.75);
  display: block;
  font-size: 15px;
  margin: 0 auto;
  padding: 5px 0 0;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  text-transform: uppercase;
}

.footer {
  background: rgba(0, 97, 145, 0.75);
  bottom: 0;
  color: #FFF;
  left: 0;
  position: absolute;
  text-align: center;
  top: 380px;
  width: inherit;
}

.footer ul {
  display: flex;
  font-size: 16px;
  font-weight: 400;
  height: 40px;
  line-height: 40px;
  padding: 20px 20px;
}

.footer li:first-child {
  border: 0;
}

.footer li {
  border-left: 2px solid rgba(255, 255, 255, .15);
  font-family: 'Quicksand', sans-serif;
  padding: 0 4px 0 6px;
  width: 100%;
}

.footer a span {
  color: #9CDFF5;
  display: inline-block;
  font-size: 26px;
  padding: 0 4px 0 0;
  vertical-align: middle;
}

<div class="container">
<div class="window">
  <div class="overlay"></div>
  <div class="box header">
    <img src="http://via.placeholder.com/350x150" alt="" />
    <h2>Nera Parian Mohn</h2>
    <h4>Norway</h4>
  </div>
  <div class="box footer">
    <ul>
      <li><a href=""><span class="ion-ios-camera-outline"></span> 401</a></li>
      <li><a href=""><span class="ion-ios-heart-outline"></span> 333K</a></li>
      <li><a href=""><span class="ion-ios-person-outline"></span> 225M</a></li>
    </ul>
    <a href="" class="btn">Follow</a>
  </div>
</div>

<div class="window">
  <div class="overlay"></div>
  <div class="box header">
    <img src="http://via.placeholder.com/350x150" alt="" />
    <h2>Nera Parian Mohn</h2>
    <h4>Norway</h4>
  </div>
  <div class="box footer">
    <ul>
      <li><a href=""><span class="ion-ios-camera-outline"></span> 401</a></li>
      <li><a href=""><span class="ion-ios-heart-outline"></span> 333K</a></li>
      <li><a href=""><span class="ion-ios-person-outline"></span> 225M</a></li>
    </ul>
    <a href="" class="btn">Follow</a>
  </div>
</div>

<div class="window">
  <div class="overlay"></div>
  <div class="box header">
    <img src="http://via.placeholder.com/350x150" alt="" />
    <h2>Nera Parian Mohn</h2>
    <h4>Norway</h4>
  </div>
  <div class="box footer">
    <ul>
      <li><a href=""><span class="ion-ios-camera-outline"></span> 401</a></li>
      <li><a href=""><span class="ion-ios-heart-outline"></span> 333K</a></li>
      <li><a href=""><span class="ion-ios-person-outline"></span> 225M</a></li>
    </ul>
    <a href="" class="btn">Follow</a>
  </div>
</div>
<div class="window">
  <div class="overlay"></div>
  <div class="box header">
    <img src="http://via.placeholder.com/350x150" alt="" />
    <h2>Nera Parian Mohn</h2>
    <h4>Norway</h4>
  </div>
  <div class="box footer">
    <ul>
      <li><a href=""><span class="ion-ios-camera-outline"></span> 401</a></li>
      <li><a href=""><span class="ion-ios-heart-outline"></span> 333K</a></li>
      <li><a href=""><span class="ion-ios-person-outline"></span> 225M</a></li>
    </ul>
    <a href="" class="btn">Follow</a>
  </div>
</div>
</div>

最佳答案

您可以使用百分比进行响应。

我也建议使用@media查询,看看
https://www.w3schools.com/cssref/css3_pr_mediaquery.asp



.container {
    text-align: center;
 }
a {
  color: inherit;
  text-decoration: none;
}

a:hover {
  color: rgba(0, 0, 0, .65);
}
a:hover {
  color: rgba(0, 0, 0, .45);
}
.window,
.box,
ul,
li {
  margin: 0;
  overflow: hidden;
  padding: 0;
  position: relative;
}
ul {
  font-family: 'Fjalla One', sans-serif;
  list-style-type: none;
  text-transform: uppercase;
}
li {
  display: inline-block;
}
img {
  display: block;
  width: 100%;
}
.btn {
  background: rgba(255,255,255,0.8);
  border: 1px solid rgba(255,255,255,0.5);
  border-radius: 40px;
  color: rgba(0,0,0,0.75);
  display: block;
  font-size: 1em;
  font-weight: 400;
  height: 44px;
  letter-spacing: 5px;
  line-height: 42px;
  margin: 10px auto;
  padding: 0;
  position: relative;
  text-align: center;
  text-transform: uppercase;
  -webkit-transition: all .5s ease;
  transition: all .5s ease;
  vertical-align: middle;
  width: 60%;
}
.btn:hover {
  background: rgba(0,0,0,0.1);
  border: 1px solid rgba(0,0,0,0.15);
}
.overlay {
  background-size: cover;
  height: calc(100% + 40px);
  margin: -20px;
  position: relative;
  width: calc(100% + 40px);
}
.window {
  height: 560px;
  margin: 1em 5%;
  width: 90%;
  padding: 5px;
}
.header {
  background: blue;
  color: #FFF;
  height: 380px;
  left: 0;
  position: absolute;
  text-align: center;
  top: 0;
  width: 100%;
}
.header:before {
  border: 2px solid rgba(161, 220, 255, 0.34);
  border-radius: 100%;
  content: "";
  height: 140px;
  left: 0;
  margin: 67px auto;
  padding: 0;
  position: absolute;
  right: 0;
  top: 0;
  width: 140px;
  z-index: 2;
  -webkit-transform: scale(1.24, 1.24);
  -moz-transform: scale(1.24, 1.24);
  transform: scale(1.24, 1.24);
}
.header img {
  border: 5px solid #A1DCFF;
  border-radius: 100%;
  height: 140px;
  margin: 4em auto 2.5em;
  object-fit: cover;
  width: 140px;
}
.header h2 {
  display: inline-block;
  font-family: 'Quicksand', sans-serif;
  font-size: 28px;
  font-weight: 400;
  letter-spacing: -2px;
  margin: 0 auto;
  padding: 0;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}
.header h4 {
  color: rgba(255, 255, 255, 0.75);
  display: block;
  font-size: 15px;
  margin: 0 auto;
  padding: 5px 0 0;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  text-transform: uppercase;
}
.footer {
  background: rgba(0, 97, 145, 0.75);
  bottom: 0;
  color: #FFF;
  left: 0;
  position: absolute;
  text-align: center;
  top: 380px;
  width: 100%;
}
.footer ul {
  display: flex;
  font-size: 16px;
  font-weight: 400;
  height: 40px;
  line-height: 40px;
  padding: 20px 20px;
}

.footer li:first-child {
  border: 0;
}
.footer li {
  border-left: 2px solid rgba(255, 255, 255, .15);
  font-family: 'Quicksand', sans-serif;
  padding: 0 4px 0 6px;
  width: 100%;
}
.footer a span {
  color: #9CDFF5;
  display: inline-block;
  font-size: 26px;
  padding: 0 4px 0 0;
  vertical-align: middle;
}
@media screen and (min-width: 480px) {
    .window {
      margin: 1%;
      width: 44%;
      display: inline-block;
    }
}
@media screen and (min-width: 700px) {
    .window {
      margin: 1%;
      width: 21%;
      max-width: 300px;
    }
}

<div class="container">
<div class="window">
  <div class="overlay"></div>
  <div class="box header">
    <img src="http://via.placeholder.com/350x150" alt="" />
    <h2>Nera Parian Mohn</h2>
    <h4>Norway</h4>
  </div>
  <div class="box footer">
    <ul>
      <li><a href=""><span class="ion-ios-camera-outline"></span> 401</a></li>
      <li><a href=""><span class="ion-ios-heart-outline"></span> 333K</a></li>
      <li><a href=""><span class="ion-ios-person-outline"></span> 225M</a></li>
    </ul>
    <a href="" class="btn">Follow</a>
  </div>
</div>

<div class="window">
  <div class="overlay"></div>
  <div class="box header">
    <img src="http://via.placeholder.com/350x150" alt="" />
    <h2>Nera Parian Mohn</h2>
    <h4>Norway</h4>
  </div>
  <div class="box footer">
    <ul>
      <li><a href=""><span class="ion-ios-camera-outline"></span> 401</a></li>
      <li><a href=""><span class="ion-ios-heart-outline"></span> 333K</a></li>
      <li><a href=""><span class="ion-ios-person-outline"></span> 225M</a></li>
    </ul>
    <a href="" class="btn">Follow</a>
  </div>
</div>

<div class="window">
  <div class="overlay"></div>
  <div class="box header">
    <img src="http://via.placeholder.com/350x150" alt="" />
    <h2>Nera Parian Mohn</h2>
    <h4>Norway</h4>
  </div>
  <div class="box footer">
    <ul>
      <li><a href=""><span class="ion-ios-camera-outline"></span> 401</a></li>
      <li><a href=""><span class="ion-ios-heart-outline"></span> 333K</a></li>
      <li><a href=""><span class="ion-ios-person-outline"></span> 225M</a></li>
    </ul>
    <a href="" class="btn">Follow</a>
  </div>
</div>
<div class="window">
  <div class="overlay"></div>
  <div class="box header">
    <img src="http://via.placeholder.com/350x150" alt="" />
    <h2>Nera Parian Mohn</h2>
    <h4>Norway</h4>
  </div>
  <div class="box footer">
    <ul>
      <li><a href=""><span class="ion-ios-camera-outline"></span> 401</a></li>
      <li><a href=""><span class="ion-ios-heart-outline"></span> 333K</a></li>
      <li><a href=""><span class="ion-ios-person-outline"></span> 225M</a></li>
    </ul>
    <a href="" class="btn">Follow</a>
  </div>
</div>
</div>

关于html - 在页面中居中配置文件卡,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47542941/

10-12 18:43