0

How to center the text inside that box?

Here's how I want elements sorted out:

enter image description here

In a smaller screen, elements stack on top of each other, and the .text-box div, that contains the text I want to center, has a fixed height. For larger widths, the .text-box div should have a height igual to larger image's height minus shorter image's height

See the Fiddle here

HTML

  <div id="wrapper">
    <div class="box">
      <img class="img-large" src="http://placekitten.com/900/800" alt=""/>
    </div>
    <div class="box">
        <img src="http://placekitten.com/900/400" alt=""/>    
    </div>
    <div class="box-text">
        <div class="vcenter-outer">
            <div class="vcenter-inner">
                <p>center this text vertically</p>  
            </div>
        </div>
    </div>
  </div>

CSS

#wrapper {
    background: #ffeaea;
    height: 100vh;
}
img {
    width: 100%;
    display: block;
}
.img-large {
    height: 100vh;
    width: auto;
}
.box {
    position: relative;
    width: 100%;
    overflow: hidden;
}
.box-text {
    text-align: center;
    background: yellow;
    height: 100%;
}

@media only screen and (min-width: 768px) {
    .box {
        float: left;
        width: 50%;
    }
}

.vcenter-outer {
    background: yellow;
    display: table;
}

.vcenter-inner {
    background: lightblue;
    display: table-cell;
    vertical-align: middle;
}

Why does the .text-box div span through the whole wrapper?

zok
  • 6,065
  • 10
  • 43
  • 65
  • 1
    [How to center an element horizontally and vertically?](http://stackoverflow.com/questions/19461521/how-to-center-an-element-horizontally-and-vertically) – Josh Crozier Feb 10 '15 at 20:17
  • Have you tried making it not display as a table and instead as a block? – shaun Feb 10 '15 at 20:17

1 Answers1

1

Basicly You could use display:flex and float together

.trio {
  height:100vh;
  width:100vh;
}
.trio>div {
  float:left;
  display:flex;
  align-items:center;
  justify-content:center;
  width:50vh;
  height:50vh;
  box-shadow:inset 0 0 0 2px;
}
.trio .first {
  height:100vh;
}
<div class="trio">
  <div class="first">
    <p>Center</p>
  </div>
  <div class="next">
    <p>Center</p>
  </div>
  <div class="next">
    <p>Center</p>
  </div>
</div>

display:table works too with an extra level of div inbricated as you did , same idea: float + vh

codepen to play with

G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
  • Nice! I'm close to what I want to do here: http://jsfiddle.net/isaacalves/6qcu2pxy/ I just can't make the images bleed horizontally and keep centered inside their div – zok Feb 10 '15 at 20:49