CSS代码片段

CSS布局技巧精要

宽高

1. div 的高度是由它的行高 line-height 决定它内部文档流中元素总和决定的

inline-block,inline 的元素,元素与元素之间有空格

word-break:break-all;折行

2. 单行省略

/* 不换行 */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
复制代码

3. 多行省略

display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-box-orient: vertical;
overflow: hidden;
复制代码

4. 文字垂直居中

  • 不写高度,使用padding-top、padding-bottom撑开

margin上下方向合并,父级设置 border、padding

5. 一比一的正方形

width: 50px;
border: 1px solid green;
padding-top: 100%;
/* 或者 */
padding-bottom: 100%;
复制代码

6. CSS 实现横竖屏

/*竖屏时使用的样式*/
@media all and (orientation: portrait) {
  .css {
    width: 100%;
    line-height: 40px;
    text-align: center;
    background: #333;
  }
}

/*横屏时使用的样式*/
@media all and (orientation: landscape) {
  .css {
    width: 100%;
    line-height: 40px;
    text-align: center;
    background: #ddd;
  }
}
复制代码

7. select文字居右

标签属性dir="rtl"

8. 点击按钮(状态)

<style type="text/css">
a {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

.btn-blue {
    display: block;
    height: 42px;
    line-height: 42px;
    text-align: center;
    border-radius: 4px;
    font-size: 18px;
    color: #FFFFFF;
    background-color: #4185F3;
}

.btn-blue:active {
    background-color: #000;
}
</style>
<div class="btn-blue">按钮</div>
复制代码

9. 移动端设置字体

  • 各个手机系统有自己的默认字体,且都不支持微软雅黑
  • 如无特殊需求,手机端无需定义中文字体,使用系统默认
  • 英文字体和数字字体可使用 Helvetica ,三种系统都支持
/* 移动端定义字体的代码 */
body {
  font-family: Helvetica;
}
复制代码
测试:

“Would you like me to give you a formula for success?
It’s quite simple, really. Double your rate of failure.
You’re thinking of failure as the enemy of success.
But it isn’t at all… You can be discouraged by failure – or you can learn from it.
So go ahead and make mistakes.
Make all you can. Because, remember that’s where you’ll find success.
On the far side.”
复制代码

10. 隐藏滚动条:

element::-webkit-scrollbar {
    display: none
}
复制代码

目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值