居中内容中间页

时间:2017-06-13 15:46:15

标签: html css

我使用HTML和CSS创建了4个信息框,但我似乎无法将4个方框放在页面中间(水平居中),我无法看到我做错了什么。有没有人有任何想法?什么都有帮助,欢呼!

.infobox-list {
    display: inline-block;
    text-align: center;
}

ul, ol, li {
    margin: 0;
    padding: 0;
    list-style-position: outside;
    list-style-type: none;
}

h1, h2, h3, h4, h5, h6, ul, li, ol, form, fieldset {
    margin: 0;
    padding: 0;
}

*, *:before, *:after {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

ul, menu, dir {
    display: block;
    list-style-type: disc;
    -webkit-margin-before: 1em;
    -webkit-margin-after: 1em;
    -webkit-margin-start: 0px;
    -webkit-margin-end: 0px;
    -webkit-padding-start: 40px;
}

.hr {
  border-color:rgba(255,255,255,0.3);
  margin-left:9px;
  width: 150px;
}

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    padding: 0;
    margin: 0;
    background-color: #fcfcfc;
    color: #555;
    min-width: 20em;
}

.info-boxes li {
  width: 245px;
    height: 345px;
    background: #e2ecf6;
    border: 0px solid #efefef;
    border-radius: 4px;
    margin: 0 0px 0px;
    position: relative;
    overflow: hidden;
    transform: scale(0.8); 
}

.ptext
{
  text-align:center;
}

.profile
{
  margin-top:40%;
  margin-left:-10px;
  width:75px;
  height:75px;
}

.performance
{
  margin-top:40%;
  margin-left:-1.5px;
  width:75px;
  height:75px;
}

.learning
{
  margin-top:40%;
  margin-left:-11px;
  width:75px;
  height:75px;
}

.team
{
  margin-top:40%;
  margin-left:-9px;
  width:75px;
  height:75px;
}

.smallicons
{
  width:50px;
  height:50px;
}

.everything {
  text-align:center;
}

.infobox-list li {
  display: inline-block;
}

a {
  color: white;
  text-decoration: none;
  font-weight:lighter;
}

.info-boxes li .infobox {
  display: table-cell;
  vertical-align: middle;
  height: 245px;
  width: 345px;
}

.info-boxes li .infobox:before { 
  content: '';
  position: absolute;
  left: 20%;
  width: 160%; 
  height: 200%; 
  background-color: rgb(255, 255, 255); /* fallback */
  background-color: rgba(255, 255, 255, 0.2);
  top: 0;
  -webkit-transform: rotate(36deg);
  -moz-transform: rotate(36deg);
  transform: rotate(36deg);
} 

.info-boxes li:hover .shade {
  animation-name: windowshade;
}

.info-boxes li .shade,
.info-boxes li:hover .shade {
  animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
  animation-duration: 1s;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
}

section p {
  line-height: 1.3em;
  color: #6d6e71;
  width: 95%;
  padding: 0 10px;
  margin-top: 5px;
  margin-left: 0px;
  text-align:left;
}

p {
  display: block;
}

.info-boxes li .shade {
  position: absolute;
  width: 245px;
  height: 355px;
  left: 0;
  top: 0;
  background-color: #00688B;
  color: #fff;
  display: table;
  vertical-align: middle;
  padding: 20px 10px 0;
  transform: translateY(-340px);
  animation-name: windowshade-out;
}

.info-boxes li .shade h3 {
  color: #fff;
  padding: 10px;
  font-weight: bold
}

.info-boxes li .shade p {
  color: #fff;
  line-height: 1.4em;
  font-weight: normal
}

.ie9 .info-boxes li:hover .shade {
  top: 245px
}

.info-boxes li:hover a {
  text-decoration: none
}

@keyframes windowshade {
  0% {
    transform: translateY(-340px)
  }
  100% {
    transform: translateY(0)
  }
}

@keyframes windowshade-out {
  0% {
    transform: translateY(0)
  }
  100% {
    transform: translateY(-340px)
  }
}
<br style="line-height:100px;>"
<div class="everything">
<section class="info-boxes">
  <ul class="infobox-list">
    <li>
      <a href="#">
        <div class="infobox">
          <table>
            <tr>
          <img class="profile" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497365894/Profile-Icon_twnm83.png"></tr>
            <tr>
              <p class="ptext">Profile</P></tr></table>
                    </div>
        <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497301295/About-Me_dudglr.png">
              </td>
              <td><p>About Me</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Resume_tb7t02.png"
              </td>
              <td><p>Resume</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497280574/Accountabilities_qfdcns.png"</td>
              <td><p>Accountabilities<p/><hr class="hr"></td>
            </tr>
            <tr>
              <td></td>
              <td><p></p></td>
            </tr>
          </table>
        </div>
      </a>
    </li>
    
    <li>
      <a href="#">
        <div class="infobox">
          <table>
            <tr>
          <img class="performance" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497365894/Performance-Icon_bwevmx.png"></tr>
            <tr><p class="ptext">Performance</P></tr></table>
        </div>
        <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Goals_aw4nso.png">
              </td>
              <td><p>Goals</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284934/EA_n8lvj1.png"
              </td>
                <td><p>Effectiveness Assessment</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/development_yfv6o1.png"
              </td>
              <td><p>Development Plan</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497285433/resources_b3r88g.png"</td>
              <td><p>Resources<p/><hr class="hr"></td>
            </tr>
            <tr>
              <td></td>
              <td><p></p></td>
            </tr>
          </table>
          </div>
       </a>
    </li>
    <li>
           <div class="infobox">
             <table>
               <tr>
             <img class="learning" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497365894/Learning-Icon_p8gqbk.png"></tr>
               <tr><p class="ptext">Learning</P></tr></table>
                    </div>
                    <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-Plan_v43ne7.png">
              </td>
              <td><p>Training Plan</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-History_czttv1.png"
              </td>
                <td><p>Training History</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497298863/coursefeedback_qdh1wm.png"
              </td>
              <td><p>Course Feedback</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497299106/Favourites_y9gkce.png"</td>
              <td><p>Training Favourites<p/><hr class="hr"></td>
            </tr>
            <tr>
              <td></td>
              <td><p></p></td>
            </tr>
          </table>
                    </div>
                </a>
            </li>
    <li>
   <div class="infobox">
     <table>
       <tr>
     <img class="team" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497365894/TeamIcon_as9ynw.png"></tr>
       <tr><p class="ptext">Team</p></tr></table>
                    </div>
                    <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497300652/Team_iovnl5.png">
              </td>
              <td><p>Team</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Goals_aw4nso.png"
              </td>
                <td><p>Team Goals</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/development_yfv6o1.png"
              </td>
              <td><p>Team Development</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-Plan_v43ne7.png"
              </td>
              <td><p>Team Training</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497301295/Approval_f9y1da.png"</td>
              <td><p>Approvals<p/><hr class="hr"></td>
            </tr>
            <tr>
              <td></td>
              <td><p></p></td>
            </tr>
          </table>
                    </div>
                </a>
            </li>      
</ul> 
</section>
</div>

5 个答案:

答案 0 :(得分:1)

由于您的列表为display: inline-block;,因此您只需向text-align: center;部分添加.info-boxes即可。我还建议您从<ul>中移除填充,以便项目真正居中:

.info-boxes {
    text-align: center;
}    

.infobox-list {
    display: inline-block;
    text-align: center;
    padding: 0;
}

完整代码段

 .info-boxes {
    text-align: center;
 }    

.infobox-list {
    display: inline-block;
    text-align: center;
    padding: 0;
}

ul, ol, li {
    margin: 0;
    padding: 0;
    list-style-position: outside;
    list-style-type: none;
}

h1, h2, h3, h4, h5, h6, ul, li, ol, form, fieldset {
    margin: 0;
    padding: 0;
}

*, *:before, *:after {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

ul, menu, dir {
    display: block;
    list-style-type: disc;
    -webkit-margin-before: 1em;
    -webkit-margin-after: 1em;
    -webkit-margin-start: 0px;
    -webkit-margin-end: 0px;
    -webkit-padding-start: 40px;
}

.hr {
  border-color:rgba(255,255,255,0.3);
  margin-left:9px;
  width: 150px;
}

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    padding: 0;
    margin: 0;
    background-color: #fcfcfc;
    color: #555;
    min-width: 20em;
}

.info-boxes li {
  width: 245px;
    height: 345px;
    background: #e2ecf6;
    border: 0px solid #efefef;
    border-radius: 4px;
    margin: 0 0px 0px;
    position: relative;
    overflow: hidden;
    transform: scale(0.8); 
}

.ptext
{
  text-align:center;
}

.profile
{
  margin-top:40%;
  margin-left:-10px;
  width:75px;
  height:75px;
}

.performance
{
  margin-top:40%;
  margin-left:-1.5px;
  width:75px;
  height:75px;
}

.learning
{
  margin-top:40%;
  margin-left:-11px;
  width:75px;
  height:75px;
}

.team
{
  margin-top:40%;
  margin-left:-9px;
  width:75px;
  height:75px;
}

.smallicons
{
  width:50px;
  height:50px;
}

.everything {
  text-align:center;
}

.infobox-list li {
  display: inline-block;
}

a {
  color: white;
  text-decoration: none;
  font-weight:lighter;
}

.info-boxes li .infobox {
  display: table-cell;
  vertical-align: middle;
  height: 245px;
  width: 345px;
}

.info-boxes li .infobox:before { 
  content: '';
  position: absolute;
  left: 20%;
  width: 160%; 
  height: 200%; 
  background-color: rgb(255, 255, 255); /* fallback */
  background-color: rgba(255, 255, 255, 0.2);
  top: 0;
  -webkit-transform: rotate(36deg);
  -moz-transform: rotate(36deg);
  transform: rotate(36deg);
} 

.info-boxes li:hover .shade {
  animation-name: windowshade;
}

.info-boxes li .shade,
.info-boxes li:hover .shade {
  animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
  animation-duration: 1s;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
}

section p {
  line-height: 1.3em;
  color: #6d6e71;
  width: 95%;
  padding: 0 10px;
  margin-top: 5px;
  margin-left: 0px;
  text-align:left;
}

p {
  display: block;
}

.info-boxes li .shade {
  position: absolute;
  width: 245px;
  height: 355px;
  left: 0;
  top: 0;
  background-color: #00688B;
  color: #fff;
  display: table;
  vertical-align: middle;
  padding: 20px 10px 0;
  transform: translateY(-340px);
  animation-name: windowshade-out;
}

.info-boxes li .shade h3 {
  color: #fff;
  padding: 10px;
  font-weight: bold
}

.info-boxes li .shade p {
  color: #fff;
  line-height: 1.4em;
  font-weight: normal
}

.ie9 .info-boxes li:hover .shade {
  top: 245px
}

.info-boxes li:hover a {
  text-decoration: none
}

@keyframes windowshade {
  0% {
    transform: translateY(-340px)
  }
  100% {
    transform: translateY(0)
  }
}

@keyframes windowshade-out {
  0% {
    transform: translateY(0)
  }
  100% {
    transform: translateY(-340px)
  }
}
<br style="line-height:100px;>"
<div class="everything">
<section class="info-boxes">
  <ul class="infobox-list">
    <li>
      <a href="#">
        <div class="infobox">
          <table>
            <tr>
          <img class="profile" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497365894/Profile-Icon_twnm83.png"></tr>
            <tr>
              <p class="ptext">Profile</P></tr></table>
                    </div>
        <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497301295/About-Me_dudglr.png">
              </td>
              <td><p>About Me</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Resume_tb7t02.png"
              </td>
              <td><p>Resume</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497280574/Accountabilities_qfdcns.png"</td>
              <td><p>Accountabilities<p/><hr class="hr"></td>
            </tr>
            <tr>
              <td></td>
              <td><p></p></td>
            </tr>
          </table>
        </div>
      </a>
    </li>
    
    <li>
      <a href="#">
        <div class="infobox">
          <table>
            <tr>
          <img class="performance" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497365894/Performance-Icon_bwevmx.png"></tr>
            <tr><p class="ptext">Performance</P></tr></table>
        </div>
        <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Goals_aw4nso.png">
              </td>
              <td><p>Goals</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284934/EA_n8lvj1.png"
              </td>
                <td><p>Effectiveness Assessment</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/development_yfv6o1.png"
              </td>
              <td><p>Development Plan</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497285433/resources_b3r88g.png"</td>
              <td><p>Resources<p/><hr class="hr"></td>
            </tr>
            <tr>
              <td></td>
              <td><p></p></td>
            </tr>
          </table>
          </div>
       </a>
    </li>
    <li>
           <div class="infobox">
             <table>
               <tr>
             <img class="learning" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497365894/Learning-Icon_p8gqbk.png"></tr>
               <tr><p class="ptext">Learning</P></tr></table>
                    </div>
                    <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-Plan_v43ne7.png">
              </td>
              <td><p>Training Plan</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-History_czttv1.png"
              </td>
                <td><p>Training History</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497298863/coursefeedback_qdh1wm.png"
              </td>
              <td><p>Course Feedback</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497299106/Favourites_y9gkce.png"</td>
              <td><p>Training Favourites<p/><hr class="hr"></td>
            </tr>
            <tr>
              <td></td>
              <td><p></p></td>
            </tr>
          </table>
                    </div>
                </a>
            </li>
    <li>
   <div class="infobox">
     <table>
       <tr>
     <img class="team" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497365894/TeamIcon_as9ynw.png"></tr>
       <tr><p class="ptext">Team</p></tr></table>
                    </div>
                    <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497300652/Team_iovnl5.png">
              </td>
              <td><p>Team</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Goals_aw4nso.png"
              </td>
                <td><p>Team Goals</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/development_yfv6o1.png"
              </td>
              <td><p>Team Development</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-Plan_v43ne7.png"
              </td>
              <td><p>Team Training</p><hr class="hr"></td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497301295/Approval_f9y1da.png"</td>
              <td><p>Approvals<p/><hr class="hr"></td>
            </tr>
            <tr>
              <td></td>
              <td><p></p></td>
            </tr>
          </table>
                    </div>
                </a>
            </li>      
</ul> 
</section>
</div>

答案 1 :(得分:0)

我会做其他人说的话并在你的部分中使用text-align:center。然后你必须做一些调整,以确保你没有额外的填充或边缘,从你的外观可能。

答案 2 :(得分:0)

我注意到问题出现在body标签的第一行<br style="line-height:100px;>",只是将双引号放在标签内,它就会被解决了。
我在浏览器中对它进行了测试,但它确实有效。

答案 3 :(得分:0)

添加以下代码:

.infobox-list {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
        -ms-flex-align: center;
            align-items: center;
    -webkit-box-pack: center;
        -ms-flex-pack: center;
            justify-content: center;
    -ms-flex-wrap: wrap;
        flex-wrap: wrap;
    padding: 0; 
}

而不是

.infobox-list {
    display: inline-block;
    text-align: center;
}

小提琴链接:https://jsfiddle.net/ash06229/4exm16xo/5/

答案 4 :(得分:-1)

您需要添加css样式

.info-boxes{text-align:center}