かつてはJavaScriptを利用していたものの、今ではCSSのみで実装できる10の小技


201120jQuery便CSSJavaScriptCSS
 
10



(一)Div

(二)

(三)

(四)

(五)

(六)

(七)

(八)

(九)div

(十)


HTMLCSS :)

1. Div


HTML5a div  pWeb div 

HTML
<a href="#">
  <div>
    <h1>The Latest News</h1>
    <p>In dignissim a lacus ac sodales...</p>
    <p class="more">Learn More &raquo;</p>
  </div>
</a>

See the Pen Clickable div by Mana (@manabox) on CodePen.


2. 


jQuery使CSS :nth-child() 使n


:nth-child(even)  

:nth-child(2n+1)  

:nth-child(odd)  

:nth-child(3n)  3


n :nth-last-child() 使

HTML
<table>
  <tr>
    <td>こんな感じで</td>
    <td>奇数の行に</td>
  </tr>
  <tr>
    <td>色が</td>
    <td>つきます。</td>
  </tr>
</table>

CSS
tr:nth-child(odd) {
  background: #ddd;
}

See the Pen Table change colour odd rows by Mana (@manabox) on CodePen.


3. 


HTML5placeholder color FirefoxIEFirefox ::-moz-placeholderIE input:-ms-input-placeholder 

HTML
<input type="text" placeholder="キーワードを入力">

CSS
input[type="text"] {
  color: #f39;
  font-size: 16px;
  padding: 5px;
  width: 200px;
}

/* Firefox */
::-moz-placeholder {
  color: #666;
}

/* IE */
input:-ms-input-placeholder {
  color: #666;
}

See the Pen HTML5 Placeholder text field by Mana (@manabox) on CodePen.


4. 


:before  :after 使


HTML
<form action="#">
  <p>
    <input type="checkbox" id="test1" checked>
    <label for="test1">Apple</label>
  </p>
</form>

CSS
[type="checkbox"]:checked,
[type="checkbox"]:not(:checked) {
    position: absolute;
    left: -9999px;
}
[type="checkbox"]:checked + label,
[type="checkbox"]:not(:checked) + label
{
    position: relative;
    padding-left: 28px;
    cursor: pointer;
    line-height: 20px;
    display: inline-block;
    color: #666;
}
[type="checkbox"]:checked + label:before,
[type="checkbox"]:not(:checked) + label:before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 18px;
    height: 18px;
    border: 1px solid #ddd;
    background: #fff;
}
[type="checkbox"]:checked + label:after,
[type="checkbox"]:not(:checked) + label:after {
    content: '';
    width: 8px;
    height: 8px;
    background: #00a69c;
    position: absolute;
    top: 6px;
    left: 6px;
    -webkit-transition: all 0.2s ease;
    transition: all 0.2s ease;
}
[type="checkbox"]:not(:checked) + label:after {
    opacity: 0;
    -webkit-transform: scale(0);
    transform: scale(0);
}
[type="checkbox"]:checked + label:after {
    opacity: 1;
    -webkit-transform: scale(1);
    transform: scale(1);
}

See the Pen Custom checkbox CSS only by Mana (@manabox) on CodePen.


HTML
<form action="#">
  <p>
    <input type="radio" id="test1" name="radio-group" checked>
    <label for="test1">Apple</label>
  </p>
</form>

CSS
[type="radio"]:checked,
[type="radio"]:not(:checked) {
    position: absolute;
    left: -9999px;
}
[type="radio"]:checked + label,
[type="radio"]:not(:checked) + label
{
    position: relative;
    padding-left: 28px;
    cursor: pointer;
    line-height: 20px;
    display: inline-block;
    color: #666;
}
[type="radio"]:checked + label:before,
[type="radio"]:not(:checked) + label:before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 18px;
    height: 18px;
    border: 1px solid #ddd;
    border-radius: 100%;
    background: #fff;
}
[type="radio"]:checked + label:after,
[type="radio"]:not(:checked) + label:after {
    content: '';
    width: 12px;
    height: 12px;
    background: #F87DA9;
    position: absolute;
    top: 4px;
    left: 4px;
    border-radius: 100%;
    -webkit-transition: all 0.2s ease;
    transition: all 0.2s ease;
}
[type="radio"]:not(:checked) + label:after {
    opacity: 0;
    -webkit-transform: scale(0);
    transform: scale(0);
}
[type="radio"]:checked + label:after {
    opacity: 1;
    -webkit-transform: scale(1);
    transform: scale(1);
}

See the Pen Custom radio button CSS only by Mana (@manabox) on CodePen.


5. 


便CSS使jQuery

HTML
<label for="check">Click me</label>
<input id="check" type="checkbox">

<div class="panel">
  <p>I am some hidden text. ここにテキストが入ります。</p>
</div>

CSS
input {
  display: none;
}
label {
  cursor: pointer;
  display: inline-block;
  padding: 10px 20px;
  border-radius: 4px;
  background: #0bd;
  color: #FFF;
  -webkit-transition: 0.1s;
  transition: 0.1s;
}
label:hover {
  background: #0090aa;
}
.panel {
  -webkit-transition: .3s ease;
  transition: .3s ease;
  height: 0;
  overflow: hidden;
  background: #F5F0CF;
  margin-top: 10px;
  padding: 0;
  border-radius: 5px;
}
input:checked + .panel {
  height: auto;
  padding: 15px;
}

See the Pen Pure css slide toggle by Mana (@manabox) on CodePen.


6. 


使

HTML
<section class="ac-container">
  <div>
    <input id="ac-1" name="accordion-1" type="radio" checked>
    <label for="ac-1">About us</label>
    <article>
      <p>Well, the way they make shows is...</p>
    </article>
  </div>
  <div>
    <input id="ac-2" name="accordion-1" type="radio">
    <label for="ac-2">How we work</label>
    <article>
      <p>Like you, I used to think the world was...</p>
    </article>
  </div>
</section>

CSS
.ac-container {
  max-width: 400px;
  border: 1px solid #ccc;
  border-top: none;
}
.ac-container label {
  height: 30px;
  line-height: 1.8;
  font-size: 20px;
  padding: 5px 20px;
  display: block;
  cursor: pointer;
  color: #666;
  background: #eee;
  border-top: 1px solid #ccc;
}
.ac-container input {
  display: none;
}
.ac-container article {
  overflow: hidden;
  height: 0;
  transition: 0.6s;
}
.ac-container input:checked ~ article {
  height: 150px;
  border-top: 1px solid #ccc;
}

See the Pen Pure Css Accordion Menu by Mana (@manabox) on CodePen.


7. 


 a title  content: attr(title); 

HTML
<a href="#" title="Hello from speech bubble!" class="tooltip">CSS Tooltip! Hover me!</a>

CSS
.tooltip {
  display: inline;
  position: relative;
}
.tooltip:hover:after{
  display: flex;
  justify-content: center;
  background: #444;
  border-radius: 8px;
  color: #fff;
  content: attr(title);
  margin: -83px auto 0;
  font-size: 16px;
  padding: 13px;
  width: 220px;
}
.tooltip:hover:before{
  border: solid;
  border-color: #444 transparent;
  border-width: 12px 6px 0 6px;
  content: "";
  left: 45%;
  bottom: 30px;
  position: absolute;
}

See the Pen CSS tooltip by Mana (@manabox) on CodePen.


8. 


<html lang="">jaen-US調Edit on CODEPEN en-US 

CSS
:lang(ja){ color: blue; }
:lang(en-US){ color: orange; }

See the Pen Change colour by language by Mana (@manabox) on CodePen.


9. div


JavaScript使CSSFlexboxFlexbox使CSS div  display: flex; Safari

HTML
<div class="main">
  <section class="col-1">
    <h1>Column 1</h1>
    <p>Lorem ipsum dolor sit amet...</p>
  </section>
  <section class="col-2">
    <h1>Column 2</h1>
    <p>Morbi non semper sapien...</p>
    <p>Integer vel pellentesque elit...</p>
  </section>
  <section class="col-3">
    <h1>Column 3</h1>
    <p>Integer commodo...</p>
  </section>
</div>

CSS
.main {
  display: -webkit-flex;
  display: flex;
}

See the Pen Flexbox same height box by Mana (@manabox) on CodePen.


10. 


Flexbox使justify-content: center;  align-items: center; OK position: absolute; 使CSS

HTML
<div class="main">
  <section>
    <h1>Centre</h1>
    <p>Lorem ipsum dolor sit amet...</p>
  </section>
</div>

CSS
.main {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 400px;
}
.main section {
  width: 250px;
}

See the Pen Center div both horizontally and vertically by Mana (@manabox) on CodePen.



JavaScriptCSSCSS

シェアする

コメント

“かつてはJavaScriptを利用していたものの、今ではCSSのみで実装できる10の小技” への1件のコメント

  1. keitahful . より:

    HTMLのコード表示がおかしくなってますよー。

ニュースレター

Web制作の最新情報やWebクリエイターボックスからのお知らせ、中の人の近況等を定期的にお送りいたします。 ぜひご登録ください!もちろん無料です! :)