Help us understand the problem. What is going on with this article?

CSSだけで正方形を作る

More than 5 years have passed since last update.

やりたいこと

  • 固定値を使わない
  • JavaScriptを使わない
  • ウィンドウサイズやレイアウトが変わっても正方形を自動で維持する

ブレイクスルー

  • padding-bottomはy方向に影響するプロパティだが、そこで%指定するとx方向を基準にした描画が行われる

Example

この赤い正方形をCSSだけで作る。

Screen Shot 2014-11-27 at 19.33.33.png


index.jade

doctype html
html
  head
    link(href="styles/main.css" rel="stylesheet")
  body
    .square-wrapper
      .spacer
      .content

main.stylus

.square-wrapper
  position relative
  width 10%

.spacer
  width 100%
  padding-bottom 100%

.content
  position absolute
  top 0
  right 0
  bottom 0
  left 0
  background red

この場合、.spacer.square-wrapperの幅と同じ高さが確保される。
そうすると.square-wrapperの高さも拡張されるため、結果として.contentも正方形になる。

参考URL

How can I create a perfectly square DIV where height is equal to viewport?

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away