0
0

More than 1 year has passed since last update.

【技術書まとめ】HTML&CSS コーディング・プラクティスブック 7 実践シリーズ

Posted at

引き続き現在のCSSを整理するために本書を手に取った。

スマホ対応しながらPCで幅が広がりすぎないように%で余白を指定する

.container {
  width: 92%; /* 345px/375px */
  max-width: 1214px; /* これ以上広がりすぎないようにする */
  margin-left: auto;
  margin-right: auto;
}

テーブルの2カラム目まで固定して横スクロールできるようにする

/* テーブルにwrapperを作る */
.table-wrapper {
  overflow: auto; /* オーバーフローするとスクロールできるようにする */
  margin-top: 20px;
}

.table th:nth-child(1) {
  -webkit-position: sticky;
  position: sticky;
  left: 0;
}

.table th:nth-child(2) {
  -webkit-position: sticky;
  position: sticky;
  left: 42px; /* 1カラム目の横幅 */
}

FlexBoxの要素の幅を固定してテキストが多くなったときでもバランスが崩れないようにする

.teams {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 45px;
}

.team {
  /* 1:1の比率で同じ幅となる */
  flex: 1; 
}

flex-wrapで並べた最終行の下マージンを削除する

.menu-sns ul {
    display: flex;
    flex-wrap: wrap;
    /* 最終行の表示数は変化するため、セレクタではなくulで調節する */
    margin-bottom: -20px; 
}

1秒たってからイーズイン/アウトで背景を3秒かけて表示する

.hero-main {
  animation: hero-main 3s ease-in-out 1s backwards;
}

@keyframes hero-main {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

レイアウトシフト(CLS)を防ぐ方法

svgイメージにwidthとheightを指定する

<!-- svgファイルのviewBoxの値を指定する
<svg id="レイヤー_1" data-name="レイヤー 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 150.58 244.82"> -->
<img src="img/field.svg" alt="" width="151" height="245" />

ウェブフォントのFOUTを防ぐ

.hero h1 {
  white-space: nowrap; /* 不要な改行をなくす */
  line-height: 1.3; /* 行の高さを指定する */
}

画面幅320pxの場合にtransform: sacle(0.8)で対応する

@media (max-width: 359px) {
  .hero .contents {
    transform: scale(0.8);
  }
}

下からスクロールして上がってくるテキストのアニメーション

.title01,
.title02,
.title03 {
  overflow: hidden;
  height: 1.3em;
}

.title01 > span,
.title02 > span,
.title03 > span {
  animation: 1s title ease-in-out;
  display: block;
}

@keyframes title {
  0% {
    transform: translate(0, 1.3em);
  }
  100% {
    tramsform: translate(0, 0);
  }
}

ナビゲーションボタンとメニュー(メニュー・SNSアイコン)

ナビゲーションボタン
<button
  class="navbtn"
  onClick="document.querySelector('html').classList.toggle('open')"
>
/* ナビゲーションボタン */
.navbtn {
  width: 32px;
  height: 32px;
  padding: 0;
  outline: none;
  border: none;
  background: transparent;
  cursor: pointer;
  z-index: 200;
}

.navbtn-bar {
  position: relative;
  display: block;
  width: 32px;
  height: 3px;
  background-color: currentColor;
  color: #ffffff;
  opacity: 0.8;
  transition: background-color 0.5s;
}

.navbtn-bar::before,
.navbtn-bar::after {
  position: absolute;
  content: "";
  display: block;
  width: 100%;
  height: 100%;
  background-color: currentColor;
  transition: transform 0.5s;
}

.navbtn-bar::before {
  transform: translateY(-10px);
}

.navbtn-bar::after {
  transform: translateY(10px);
}

.open .navbtn-bar {
  background-color: transparent;
}
.open .navbtn-bar::before {
  transform: translateY(0) rotate(225deg);
}
.open .navbtn-bar::after {
  transform: translateY(0) rotate(-225deg);
}

/* ナビゲーションメニュー */
.nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(4, 34, 48, 0.9);
  z-index: 100;
  padding-top: 98px;
  text-align: center;
  opacity: 0;
  /* クリックされないようにする */
  pointer-events: none;
}

.open .nav {
  opacity: 1;
  pointer-events: auto;
  transition: opacity 0.5s;
}

html.open,
.open body {
  /* 不要なスクロールを防止する */
  height: 100%;
  overflow: hidden;
}

.nav-menu a {
  display: block;
  padding: 20px;
  border-top: solid 1px #eeeeee;
  font-size: 16px;
  font-weight: bold;
}
.nav-menu li:last-child a {
  border-bottom: solid 1px #eeeeee;
}

.nav-menu span {
  display: block;
  margin-top: 5px;
  font-size: 12px;
  font-weight: normal;
}

.nav-sns {
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.8;
}
.nav-sns li {
  margin: 30px 10px 0;
}
0
0
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
0
0