1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【CSSBattle】最初の12問を解いてみた(前編)【Flexbox】

Last updated at Posted at 2023-02-27

こんにちは:laughing:
HTML/CSS初心者の rudorufu1981 と申します!

今回は、よくCSSの練習サイトとして紹介されている
CSSBattle」の最初の12問 Battle #1 - Pilot Battle
を流行りのFlexboxを使って解いてみました!
解答コードを紹介している人がググっても見つからなかったので、記事にしてみました!
Flexbox初心者の方の参考になれば嬉しいです!僕も初心者ですが:sweat_smile:

※本来のCSSBattleの楽しみ方はいかに短いコードにできるかですが、
今回はFlexboxの勉強のためにやったのでコード量は気にせずやっていきます。

今回は前編ということで、#1~#6 の6問を解いていきます。
よろしくお願いします!


記事リンク


Flexboxについての最低限の知識


【前編】 #1~#6

#1. Simply Square

Flexboxの出番がなかったですw
#1. Simply Square.png

解答コード
#1.html
<div></div>
<style>
  body {
    margin: 0;
    background: #5d3a3a;
  }
  div {
    width: 200px;
    height: 200px;
    background: #b5e0ba;
  }
</style>

#2. Carrom

遂にFlexboxの出番です:laughing:
#2. Carrom.png

解答コード(flex-direction: column を使うVer)
#2.html
<div class="container">
  <div>
    <div></div>
    <div></div>
  </div> 
  <div>   
    <div></div>
    <div></div>
  </div> 
</div> 
<style>
  body {
    display: flex;
    justify-content: center;
    align-items: center;
    background: #62374e;
  }
  .container {
    width: 300px;
    height: 200px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  .container > div {
    display: flex;
    justify-content: space-between;
  }  
  .container > div > div {
    width: 50px;
    height: 50px;
    background: #fdc57b;
  }
</style>
解説(イメージ図)

image1.png

別解(align-content: space-between を使うVer)
#2_Sub.html
<div class="container">
  <div>
    <div></div>
    <div></div>
  </div> 
  <div>   
    <div></div>
    <div></div>
  </div> 
</div> 
<style>
  body {
    display: flex;
    justify-content: center;
    align-items: center;
    background: #62374e;
  }
  .container {
    width: 300px;
    height: 200px;
    display: flex;
    flex-wrap: wrap;
    align-content: space-between;
  }
  .container > div {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }  
  .container > div > div {
    width: 50px;
    height: 50px;
    background: #fdc57b;
  }
</style>
別解の解説(イメージ図)

image2.png

#3. Push Button

Flexboxの真骨頂が発揮されました!中央揃えがしやすい!!!
#3. Push Button.png

解答コード
#3.html
<div id="s1"></div>
<div class="circle" id="c1"></div>
<div class="circle" id="c2"></div>
<div class="circle" id="c3"></div>
<style>
  body{
    display: flex;
    justify-content: center;
    align-items: center;
    background: #6592CF;
  }
  div {
    position: absolute;
  }  
  #s1 {
    width: 300px;
    height: 150px;
    background: #243D83;
  }  
  .circle {
    border-radius: 50%;
  }  
  #c1 { 
    width: 250px;
    height: 250px;
    background: #6592CF;
  }
  #c2 {
    width: 150px;
    height: 150px;
    background: #243D83;
  }
  #c3 {
    width: 50px;
    height: 50px;
    background: #EEB850;
  }   
</style>

#4. Ups n Downs

divを6個置くという発想ができるかという発想力の問題!
※別解はdiv3個Ver。translateで移動させる実装は、なぜか負けた気がする(誰と戦ってるんだ:smile:)
#4. Ups n Downs.png

解答コード(transform: translateを使わないVer)
#4.html
<div class="conteiner">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
<style>
  body {
    display: flex;
    justify-content: center;
    align-items: center;  
    background: #62306D;
  }
  .conteiner {
    display: flex;
    width: 300px;
    height: 200px;
    flex-wrap: wrap;
  }
  .conteiner > div {
    width: 100px;
    height: 100px;   
  }
  .conteiner > div:nth-child(2) {
    border-radius: 50% 50% 0 0;
    background: #F7EC7D;
  }
  .conteiner > div:nth-child(2N+4) {
    border-radius: 0 0 50% 50%;
    background: #F7EC7D;
  }
</style>
別解(transform: translateを使うVer)
#4_Sub.html
<div></div>
<div></div>
<div></div>
<style>
  body {
    display: flex;
    justify-content: center;
    align-items: center;  
    background: #62306D;
  }
  div {
    width: 100px;
    height: 100px; 
    background: #F7EC7D;
  }
  div:nth-child(odd) {
    border-radius: 0 0 50% 50%;
    transform: translate(0, 50px);
  }
  div:nth-child(even) {
    border-radius: 50% 50% 0 0;
    transform: translate(0, -50px);
  }
</style>

#5. Acid Rain

図形が重なったら流石にtranslateを使うしかない!
今度はz-indexを使うとなぜか負けた気がしたので、意地でも使わない方針で実装しました!
#5. Acid Rain.png

解答コード
#5.html
<div id="d1"></div>
<div id="d2"></div>
<div id="d3"></div>  
<style>
  body {
    display: flex;
    flex-direction: row-reverse;
    justify-content: center;
    align-items: center;
    background: #0B2429;
  }
  div {
    width: 120px;
    height: 120px;
  }
  #d1 {
    border-radius: 50%;
    transform: translate(-60px, -60px);
    background: #F3AC3C;
  }
  #d2 {
    border-radius: 50% 0 50% 50%;
    transform: translate(0, 0);
    background: #998235;
  }
  #d3 {
    border-radius: 50% 0 50% 50%;
    transform: translate(60px, 60px);
    background: #F3AC3C;
  }
</style>

#6. Missing Slice

#2のようにcontainerクラスのdivで囲うのが正統派だが、
遂にtranslateの魔力に負けた実装をしてしまった・・・!
translateは依存性が強いです:joy:
#6. Missing Slice.png

解答コード
#6.html
<div id="d1"></div>
<div id="d2"></div>
<div id="d3"></div>
<style>
  body {
    display: flex;
    justify-content: center;
    align-items:center;
    background: #E3516E;
  }
  div {
    width: 100px;
    height: 100px;
    position: absolute;
  }
  #d1 {
    border-radius: 100% 0 0 0;
    transform: translate(-50px, -50px);
    background: #51B5A9;
  }
  #d2 {
    border-radius: 0 100% 0 0;
    transform: translate(50px, -50px);
    background: #FADE8B;
  }
  #d3 {
    border-radius: 0 0 0 100%;
    transform: translate(-50px, 50px); 
    background: #F7F3D7;
  }  
</style>

以上です!
次回は後編ということで、#7~#12 の6問を解いていきます。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?