4

I am using bootstrap and I am attempting to make a Cell that is 100% height and width of its host container.

The title is 30% of that space and the value is 70%. There are upper and lower limits of the value that take up 20% each leaving 60% of the space for the actual value.

HTML:

<div class="container-fluid parameterContainer">
<div class="row paramHead">
    <span class="virtAlignFix"></span>
    <div class="centerText">
      SPO2 (%)
     </div>
</div>
<div class="row paramValWrapper">
  <div class="row paramLimit">
    <span class="virtAlignFix"></span>
    <div class="centerText">
    200
    </div>
  </div> 
  <div class="row paramValue">
    <span class="virtAlignFix"></span>
    <div class="centerText">
    80
    </div>
  </div>
  <div class="row paramLimit">
    <span class="virtAlignFix"></span>
     <div class="centerText">
     40
     </div>
  </div>
</div>

CSS:

html {
    height: 100%;
    width: 100%;
}
body {
    height: 100%;
    width: 100%;
}
.virtAlignFix {
    line-height: 100%;
}

.parameterContainer {
    height: 100%;
    width: 100%;
}

.paramValWrapper {
    height: 70%;
    width: 100%;
}

.paramLimit {
    height: 20%;
    width: 100%;
    text-align:center
}

.paramHead {
    height: 30%;
    width: 100%;
    text-align:center
}

.paramValue {
    height: 80%;
    width: 100%;
    text-align:center
}

.centerText {
 vertical-align:middle; 
    display:inline-block;  
}

Fiddle of my attempt: http://jsfiddle.net/WCBjN/1173/

EDIT: added height % to body and page.

6
  • 1
    CSS 101: heights in percentages only work if the parent element has an explicit height set as well.
    – CBroe
    Oct 20, 2016 at 14:12
  • yes, that is the case
    – JBurlison
    Oct 20, 2016 at 14:13
  • 1
    No, it is not. The parent of .parameterContainer does not have any height set.
    – CBroe
    Oct 20, 2016 at 14:14
  • oic in my fiddle. Ok I updated it. As you see the values are at to top of the Div.
    – JBurlison
    Oct 20, 2016 at 14:21
  • line-height: 100% also does not do what you appear to think it does. You really need to do a little more research. Centering stuff has been discussed to death a million times over already.
    – CBroe
    Oct 20, 2016 at 14:24

1 Answer 1

7

Making text central is fairly trivial, the easiest approach is as follows. You can check out the jsFiddle too: https://jsfiddle.net/jL9bs0j7/

.text-container {
  height:200px;
  width:400px;
  border: 1px solid #000;
  text-align: center;
}

.text-container span {
  display: block;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}
<div class="text-container">
  <span>Hello World</span>
</div>

2
  • Sorry kinda new to css.
    – JBurlison
    Oct 20, 2016 at 14:22
  • No worries, this little trick is nice and simple and saves lots of complicated work-arounds.
    – Matt
    Oct 20, 2016 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.