About common css tips. Continuously update here.
1. CSS关闭按钮
1 2 3 4 5 6 7 8 9 10 11
| .c-modal-close_button::before { content: "\00D7"; }
.c-modal-close_button { position: absolute; top: 8px; right: 15px; font-size: 25px; color: var(--color-navy-e9); }
|
1 2 3
| input[name="input"]:focus{ background:var(--color-gray-f8); }
|
1 2 3 4 5 6
| input[type="text"], input[type="password"], textarea, select { outline: none; }
|
4. CSS伪元素实现三角形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| .triangle{ width: 200px; height: 100px; border-radius: 5px; border: 2px solid #000; position: relative; } .triangle:after{ content: ""; position: absolute; left: 200px; top:12px; width: 0; height: 0; border-top: 10px solid transparent; border-left: 10px solid #fff; border-right: 10px solid transparent; border-bottom: 10px solid transparent; } .triangle:before{ content: ""; position: absolute; left: 200px; top:10px; width: 0; height: 0; border-top: 12px solid transparent; border-left: 12px solid #000; border-right: 12px solid transparent; border-bottom: 12px solid transparent; }
|
5. CSS上下跳动的动画效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| .c-style-item:hover { background:var(--color-green-00); color:red; animation: shake 2s infinite; opacity: 1; }
@keyframes shake { 0% { transform: translate(0px, 0px); } 50% { transform: translate(0px, -10px); } 100% { transform: translate(0px, 0px); } }
|
6.CSS不固定宽度模块屏幕居中
1 2 3 4 5 6 7 8
| .c-dialog { position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); border: solid 1px #ccc; background: #ccc; }
|
7. HTML使用pre的情况下,让长文字自动换行
1 2 3 4 5 6 7 8 9 10 11
| pre{ white-space:pre-wrap; word-wrap:break-word; }
.c-modal { ... word-wrap: break-word; white-space : normal }
|
8. 纯CSS3实现点击行展开
1 2 3 4 5 6 7 8 9 10 11
| <label class="drop" for="_1">Collapse 1 </label> <input id="_1" type="checkbox"> <div>This is content text 1</div>
<label class="drop" for="_2">Collapse 1 </label> <input id="_2" type="checkbox"> <div>This is content text 2</div>
<label class="drop" for="_3">Collapse 1 </label> <input id="_3" type="checkbox"> <div>This is content text 3</div>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| .drop { cursor: pointer; display: block; background: #090; }
.drop+input { display: none; }
.drop+input+div { display: none; }
.drop+input:checked+div { display: block; }
|
8. div内显示两行,超出部分用省略号显示
1 2 3 4 5 6
| .break-word { word-wrap:break-word; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
|
9. 常用的box-shadow
1 2 3
| .box-shadow { box-shadow: rgba(0,0,0,.2) 0 1px 5px 0px; }
|
1 2 3 4 5 6 7 8 9
| <body> <div id="wrapper"> <main> </main>
<footer> </footer> </div> </body>
|
1 2 3 4 5 6 7 8 9 10
| body, #wrapper { display: flex; flex-direction: column; min-height: 100vh; }
footer { margin-top: auto; }
|
11. CSS渐变色
图1:
1
| background: linear-gradient(#58a, #fb3);
|
图2:
1
| background: linear-gradient(45deg, #58a, #fb3);
|
图3:
1
| background: linear-gradient(to right, #58a 20%, #fb3 60%);
|
12. CSS3设置Border内边框 / 外边框
内边框:
外边框:
1
| box-sizing: content-box;
|
12. select box自定义
1 2 3 4 5 6
| <div class="select-wrap"> <i class="iconfont icon-pulldown" /> <select :value="value" v-on="listeners"> <slot /> </select> </div>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| .select-wrap { margin: 0; position: relative; &:hover { opacity: 0.8; } .iconfont { border-top: 4px solid #878787; border-right: 4px solid transparent; border-bottom: 4px solid transparent; border-left: 4px solid transparent; } .icon-pulldown { position: absolute; right: 11px; top: 57%; z-index: 2; color: #999; transform: translate(0, -50%); } select { width: 100%; border: solid 1px #d2d2d2; height: 28px; min-width: 208px; padding: 3px 5px; font-size: 13px; background: #fff; outline: none; cursor: pointer; &:disabled { background-color: #f5f5f5; } &:focus { border: 1px solid #606060; } &::-ms-expand { display: none; } } } @media only screen and (max-width: 480px) { .select-wrap { width: 100%; min-width: 170px; select { height: 40px; min-width: 170px; font-size: 16px; } } }
|
13. radio自定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <input type="radio" :checked="itemPrice.bring.flg === 'bring_priceless'" :id="'bring_free' + index" value="bring_priceless" :name="'bring_flg' + index" @change="createItemPrices" class="radio-input" :class="{ 'radio-error': bringBuyActive && priceErrorObj.priceErrors[index].bring.free_error, }" /> <label :for="'bring_free' + index">無料で買取</label>/>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| .radio-input { display: none; } .radio-input + label { padding-left: 20px; position: relative; margin-left: 20px; cursor: pointer; width: auto; } .radio-input + label::before { content: ""; display: block; position: absolute; top: 2px; left: 0; width: 12px; height: 12px; border: 1px solid #999; transform: scale(1.5); border-radius: 50%; }
.radio-input.radio-error + label::before { border: 1px solid red; transform: scale(1.5); }
.radio-input:checked + label::before { border: 1px solid #0275ff; transform: scale(1.5); }
.radio-input:checked + label::after { content: ""; display: block; position: absolute; top: 4px; left: 2px; background: #0275ff; border-radius: 50%; width: 8px; height: 8px; transform: scale(1.5); }
.radio-input.radio-error + label::before { border: 1px solid red; transform: scale(1.5); }
.input_error { border: solid 2px red; }
.input_text_error { margin: 3px 0; color: red; margin-left: 125px; }
.disabled { background: #d3d3d3 !important; }
|