为什么文字出现在div的底部?

时间:2017-06-27 10:45:07

标签: css

我已将几个段落放入div元素,但文本出现在块的底部。可能是什么问题?如果您可以建议如何使块更具响应性,那么也会很棒。

HTML:

<div class="container">
    <div class="content">
    <div class="inner">
        <h1><span>What</span> Clients Says</h1>
        <div class="main-block">
        <div class="testimonial-block-1">
            <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLdI6QE_lvzsCy_6VTsikYLz0iPFnWSv2vbGVzbwgQA8OlEbpH" alt="">
            <p>- Staciya Trollio</p>
            <p>Customer</p>
            <p>Lorem Ipsum is simply dummy text of the printing
            and industry. Lorem Ipsum has been the industry.</p>
        </div>
        <div class="testimonial-block-2">
                <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLdI6QE_lvzsCy_6VTsikYLz0iPFnWSv2vbGVzbwgQA8OlEbpH">
    <p>- David Soul</p>
                <p>Customer</p>
                <p>Lorem Ipsum is simply dummy text of the printing
                and industry. Lorem Ipsum has been the industry.</p>
        </div>      
        </div>
        </div>
    </div>
</div>

CSS:

* {
    padding: 0;
    margin: 0;
    box-sizing:border-box;
}

html, body {
    height: 100%;
}

.container {
        height: 100%;
        background: url(bg.jpg);
        min-width: 100%;
        background-size: 100%;
        font-family: sans-serif;
}

.content {
    position:absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(16,21,26,.9);
    width: 100%;
}

.inner {
    position: absolute;
  left: 50%;
  top: 40%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);

}

.inner h1 {
    color: #3170a8;
    text-align: center;
}

.inner h1 span {
    color: #ee8129;
}

.main-block {
    margin-top: 50px;
}

.testimonial-block-1 {
    float: left;
    max-width: 364px;
    background-color: #fff;
    min-height: 100px;
    margin-right: 45px;
    position:relative;
  z-index:100;
}


.testimonial-block-1::after,
.testimonial-block-2::after {
        position: absolute;
    top: 25%;
    left: -20px;
    content: '';
    width: 0;
    height: 0;
    border-right: solid 20px rgb(255,255,255);
    border-bottom: solid 20px transparent;
    border-top: solid 20px transparent;
    z-index:2;
}

.testimonial-block-1 img {
    position:relative;
    right:150px;
}

.testimonial-block-2 img,
.testimonial-block-1 img{
    position:relative;
    right:150px;
    max-width: 107px;
    height: 106px;
}

.testimonial-block-2 {
    float: right;
    max-width: 364px;
    background-color: #fff;
    min-height: 100px;
    position:relative;
    left:160px;
  z-index:100;
}

.testimonial-block-1 p:nth-child(3),
.testimonial-block-2 p:nth-child(3) {
    border-bottom:1px dashed #222;

}

Jsfiddle

3 个答案:

答案 0 :(得分:1)

试试这个:

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

.container {
  height: 100%;
  background: url(bg.jpg);
  min-width: 100%;
  background-size: 100%;
  font-family: sans-serif;
}

.content {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(16, 21, 26, .9);
  width: 100%;
}

.inner {
  position: absolute;
  left: 50%;
  top: 40%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

.inner h1 {
  color: #3170a8;
  text-align: center;
}

.inner h1 span {
  color: #ee8129;
}

.main-block {
  margin-top: 50px;
}

.testimonial-block-1 {
  float: left;
  max-width: 364px;
  background-color: #fff;
  min-height: 100px;
  margin-right: 45px;
  position: relative;
  z-index: 100;
}

.testimonial-block-1::after,
.testimonial-block-2::after {
  position: absolute;
  top: 25%;
  left: -20px;
  content: '';
  width: 0;
  height: 0;
  border-right: solid 20px rgb(255, 255, 255);
  border-bottom: solid 20px transparent;
  border-top: solid 20px transparent;
  z-index: 2;
}

.testimonial-block-1 img {
  position: relative;
  right: 150px;
}

.testimonial-block-2 img,
.testimonial-block-1 img {
  position: absolute;
  left: -150px;
  max-width: 107px;
  height: 106px;
}

.testimonial-block-2 {
  float: right;
  max-width: 364px;
  background-color: #fff;
  min-height: 100px;
  position: relative;
  left: 160px;
  z-index: 100;
}

.testimonial-block-1 p:nth-child(3),
.testimonial-block-2 p:nth-child(3) {
  border-bottom: 1px dashed #222;
}
<div class="container">
  <div class="content">
    <div class="inner">
      <h1><span>What</span> Clients Says</h1>
      <div class="main-block">
        <div class="testimonial-block-1">
          <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLdI6QE_lvzsCy_6VTsikYLz0iPFnWSv2vbGVzbwgQA8OlEbpH" alt="">
          <p>- Staciya Trollio</p>
          <p>Customer</p>
          <p>Lorem Ipsum is simply dummy text of the printing and industry. Lorem Ipsum has been the industry.</p>
        </div>
        <div class="testimonial-block-2">
          <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLdI6QE_lvzsCy_6VTsikYLz0iPFnWSv2vbGVzbwgQA8OlEbpH">
          <p>- David Soul</p>
          <p>Customer</p>
          <p>Lorem Ipsum is simply dummy text of the printing and industry. Lorem Ipsum has been the industry.</p>
        </div>
      </div>
    </div>
  </div>
</div>

我已将position: relative更改为position: absolute并添加了否定left: -150px并删除了right: 150px

答案 1 :(得分:1)

将此用于图像,否则(如果使用position: relative,无论设置如何),它们通常占用的空间将保留在容器中(即为空):

 position:absolute;
left: -150px;

并在margin-bottom s下创建testemonial-block

* {
    padding: 0;
    margin: 0;
    box-sizing:border-box;
}

html, body {
    height: 100%;
}

.container {
        height: 100%;
        background: url(bg.jpg);
        min-width: 100%;
        background-size: 100%;
        font-family: sans-serif;
}

.content {
    position:absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(16,21,26,.9);
    width: 100%;
}

.inner {
    position: absolute;
  left: 50%;
  top: 40%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);

}

.inner h1 {
    color: #3170a8;
    text-align: center;
}

.inner h1 span {
    color: #ee8129;
}

.main-block {
    margin-top: 50px;
}

.testimonial-block-1 {
    float: left;
    max-width: 364px;
    background-color: #fff;
    min-height: 100px;
    margin-right: 45px;
    position:relative;
  z-index:100;
}
.testimonial-block-1,
.testimonial-block-2 {
margin-bottom: 30px;
}

.testimonial-block-1::after,
.testimonial-block-2::after {
        position: absolute;
    top: 25%;
    left: -20px;
    content: '';
    width: 0;
    height: 0;
    border-right: solid 20px rgb(255,255,255);
    border-bottom: solid 20px transparent;
    border-top: solid 20px transparent;
    z-index:2;
}

.testimonial-block-2 img,
.testimonial-block-1 img{
    position:absolute;
    left: -150px;
    max-width: 107px;
    height: 106px;
}

.testimonial-block-2 {
    float: right;
    max-width: 364px;
    background-color: #fff;
    min-height: 100px;
    position:relative;
    left:160px;
  z-index:100;
}

.testimonial-block-1 p:nth-child(3),
.testimonial-block-2 p:nth-child(3) {
    border-bottom:1px dashed #222;

}
<div class="container">
    <div class="content">
    <div class="inner">
        <h1><span>What</span> Clients Says</h1>
        <div class="main-block">
        <div class="testimonial-block-1">
            <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLdI6QE_lvzsCy_6VTsikYLz0iPFnWSv2vbGVzbwgQA8OlEbpH" alt="">
            <p>- Staciya Trollio</p>
            <p>Customer</p>
            <p>Lorem Ipsum is simply dummy text of the printing
            and industry. Lorem Ipsum has been the industry.</p>
        </div>
        <div class="testimonial-block-2">
                <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLdI6QE_lvzsCy_6VTsikYLz0iPFnWSv2vbGVzbwgQA8OlEbpH">
    <p>- David Soul</p>
                <p>Customer</p>
                <p>Lorem Ipsum is simply dummy text of the printing
                and industry. Lorem Ipsum has been the industry.</p>
        </div>      
        </div>
        </div>
    </div>
</div>

答案 2 :(得分:1)

我认为这会解决问题,我使用display:flex将所有内容设置在正确的位置我也删除了一些position:absolute。我相信这将为您节省大量不必要的代码和定位,并使您的代码更清晰。

https://jsfiddle.net/Lsjo988z/3/

HTML:

<div class="container">
    <div class="content">
        <div class="inner">
            <h1><span>What</span> Clients Says</h1>
            <div class="main-block">

                <div class="chat-container align-start" >
                    <div class="row">
                        <div>
                            <img class="img" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLdI6QE_lvzsCy_6VTsikYLz0iPFnWSv2vbGVzbwgQA8OlEbpH" alt="">
                        </div>
                        <div class="testimonial-block">
                            <p>- Staciya Trollio</p>
                            <p>Customer</p>
                            <p>Lorem Ipsum is simply dummy text of the printing
                                and industry. Lorem Ipsum has been the industry.</p>
                        </div>
                    </div>

                </div>
                <div class="chat-container align-center" >
                    <div class="row">
                        <div>
                            <img class="img" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLdI6QE_lvzsCy_6VTsikYLz0iPFnWSv2vbGVzbwgQA8OlEbpH">
                        </div>
                        <div class="testimonial-block">
                            <p>- David Soul</p>
                            <p>Customer</p>
                            <p>Lorem Ipsum is simply dummy text of the printing
                                and industry. Lorem Ipsum has been the industry.</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

的CSS:

* {
    padding: 0;
    margin: 0;
    box-sizing:border-box;
}

html, body {
    height: 100%;
}

.container {
        height: 100%;
        background: url(bg.jpg);
        min-width: 100%;
        background-size: 100%;
        font-family: sans-serif;
}

.content {
  width: 100%;
  height: 100%;
    background-color: rgba(16,21,26,.9);

}

.inner {
  width: 100%;
}

.inner h1 {
    color: #3170a8;
    text-align: center;
}

.inner h1 span {
    color: #ee8129;
}

.main-block {
    margin-top: 50px;
}

.chat-container{
  display:flex;
  flex-wrap:wrap
}

.row{
  display:flex;
  -webkit-box-orient: horizontal;
  -webkit-flex-direction: row;
  flex-direction: row;
}
.align-start{
  justify-content: flex-start;
}
.align-center{
  justify-content: center;
}

.img{
    max-width: 107px;
    height: 106px;
}

.testimonial-block {
  width:290px;
  margin-left:43px;
    //float: left;
    max-width: 364px;
    background-color: #fff;
    min-height: 100px;
    //margin-right: 45px;
    position:relative;
  z-index:100;
}

.testimonial-block::after {
        position: absolute;
    top: 25%;
    left: -20px;
    content: '';
    width: 0;
    height: 0;
    border-right: solid 20px rgb(255,255,255);
    border-bottom: solid 20px transparent;
    border-top: solid 20px transparent;
    z-index:2;
}

.testimonial-block-1 img {
    position:relative;
    right:150px;
}


.testimonial-block p:nth-child(3) {
    border-bottom:1px dashed #222;

}

有关显示弹性功能的更多信息,您可以阅读这个非常好的解释https://css-tricks.com/snippets/css/a-guide-to-flexbox/