Larry
Larry

Reputation: 1248

Relative padding and vh units - bug or spec misunderstanding?

DEMO

Sometimes I'll create a square (or any rectangle, really) that will respect its ratio at any size using a method similar to this method.

What I want:

Proposed solution

CSS

    .square {
      position: relative;
      height: 0;
      padding-bottom: 100%;
      overflow: hidden;
    }

    .square-inner {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
    }

    .mw { max-width: 90vh;} /* solution, but makes things break */

HTML

    <div class="square mw">
        <div class="square-inner"></div>
    </div>

What should happen

What actually happens:

The spec says that the relative value is calculated from the 'containing block', which to me seems as though it should be the current width of the container.

Browser behaviour:

Am I interpreting the spec incorrectly, or is this a possible bug in implementation by browser vendors?

Upvotes: 7

Views: 14025

Answers (2)

apaul
apaul

Reputation: 16170

The problem is in using both lengths in % and vh.

Try this:

Working Example

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  font-family: sans-serif;
  font-weight: 100;
}

.neat {
  width: 50%;
  max-width: 600px;
  min-width: 320px;
  margin: 0 auto;
}

.col {
  float: left;
  padding: 2rem;
  width: 90vh;    /*** Important bit changed width:50%; to width:90vh; ***/
  max-width: 50%; /*** Important bit added max-width:50%; ***/
}

.square {
  position: relative;
  height: 0;
  padding-bottom: 100%;
  overflow: hidden;
}

.square-inner {
  position: absolute;
  background-color: #333333;
  color: white;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 1.5rem;
}

.mw {
  max-width: 90vh;
}

Upvotes: 1

cage rattler
cage rattler

Reputation: 1597

I don't think Opera supports vh,and there are known issues. I'm wondering if this bug is affecting what you're seeing: http://code.google.com/p/chromium/issues/detail?id=124331.

Upvotes: 0

Related Questions