内容垂直对齐div内部

时间:2016-07-18 10:47:08

标签: html css twitter-bootstrap vertical-alignment

我正在练习我的css和bootstrap技能,并坚持使用中间内容的垂直和水平对齐。我相信我在这里遗漏了一些东西,但我无法找到它。

<div class="container">
    <div class="jumbotron">
        <h1>Heading here.</h1>
        <p>Some random text goes here.</p>
    </div>
</div>

<div class="parent container text-center">
    <div class="row child text-center">
        <ul class="list-inline lis">
            <li class="box par">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg"/>
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
        </ul>
    </div>
</div>

这是我的HTML结构。

 .box {
        background:#ff6a00;
        margin:10px;
        height:200px;
        width:200px;

    }

     li {
        list-style:none;
        display:inline;
         text-align:center;
    }
     .lis {
         height:100vh;
         width:100%;
     }
     .par {
         display:table;

     }
     .ch {
         display:table-cell;
         vertical-align:middle;

     }

为了在中间对齐li包含,我给出了父表显示和内容表单元格显示。但它似乎没有用。 有什么建议??

2 个答案:

答案 0 :(得分:0)

    .box {
        background: #ff6a00;
        margin: 10px;
        height: 200px;
        width: 200px;
    }

    li {
        list-style: none;
        display: inline;
        text-align: center;
    }

    .lis {
        height: 100vh;
        width: 100%;
    } 
    .box{
        height: 200px;
    }

    .box a {
        display: block;
        position: relative;
        top: 50%;
        transform: translateY(-50%);
    }

答案 1 :(得分:0)

&#13;
&#13;
.box {
  background:#ff6a00;
  white-space: nowrap;
  vertical-align: top;
  margin:10px;
  height:200px;
  width:200px;
}
.list-inline .box:before {
  vertical-align: middle;
  display: inline-block;
  margin: 0 0 0 -5px;
  overflow: hidden;
  height: 100%;
  content: '';
  width: 1px;
}

li {
  list-style:none;
  display:inline;
  text-align:center;
}
.lis {
  height:100vh;
  width:100%;
}
.ch {
  vertical-align: middle;
  display: inline-block;
  white-space: normal;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
    <div class="jumbotron">
        <h1>Heading here.</h1>
        <p>Some random text goes here.</p>
    </div>
</div>

<div class="parent container text-center">
    <div class="row child text-center">
        <ul class="list-inline lis">
            <li class="box par">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg"/>
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
            <li class="box par container">
                <a href="#" class="ch">
                    <img class="img-responsive" src="https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2008/03/hdr-76.jpg" />
                    <p>Something random again.</p>
                </a>
            </li>
        </ul>
    </div>
</div>
&#13;
&#13;
&#13;