前端CSS通用、公共样式

Css 通用、公共样式

需要安装 sass 预处理器

npm install sass-loader --save-dev
npm install node-sass --save-dev

新建 common.scss 文件

// 定义颜色
$main-color: #333333;
$content-color: #606266;
$tips-color: #909399;
$border-color: #e4e7ed;

html,
body {
  font-family: Helvetica Neue, Helvetica, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  color: $main-color;
}

html, body, div, ul, li, dl, dt, dd, button, table, tbody, thead, tfoot, tr, td, th,
input, textarea, nav, header, footer, menu, section, aside, article, details, form, h1, h2, h3, h4, h5, h6, p {
  margin: 0;
  padding: 0;
  box-sizing: border-box;

  &::after {
    box-sizing: border-box;
  }

  &::before {
    box-sizing: border-box;
  }
}
a:focus,
a:active {
  outline: none;
}

a,
a:focus,
a:hover {
  cursor: pointer;
  color: inherit;
  text-decoration: none;
}

div:focus {
  outline: none;
}

.w-100 {
  width: 100%;
}

.h-100 {
  height: 100%;
}

.text-bold {
  font-weight: bold;
}

.flex {
  display: flex;
  flex-direction: row;
}

.flex-col {
  flex-direction: column;
}

.flex-wrap {
  flex-wrap: wrap;
}

.flex-nowrap {
  flex-wrap: nowrap;
}

.flex-shrink-0 {
  flex-shrink: 0;
}

.ai-center {
  align-items: center;
}

.ai-start {
  align-items: flex-start;
}

.ai-end {
  align-items: flex-end;
}

.jc-center {
  justify-content: center;
}

.jc-start {
  justify-content: flex-start;
}

.jc-end {
  justify-content: flex-end;
}

.jc-between {
  justify-content: space-between;
}

.jc-around {
  justify-content: space-around;
}

.flex-elps {
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-break: break-all;
  width: 0;
}

.fr {
  float: right;
}

.fl {
  float: left;
}

// 卡片样式
.card {
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 0 14px 0 rgba(0, 0, 0, 0.08);
  padding: 10px 0 27px;
  overflow: hidden;

  & .card-header {
    display: flex;
    padding: 0 14px 10px;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid $border-color;

    & .header-title {
      font-size: 16px;
    }

    & .header-other {
      font-size: 14px;
      color: #409eff;
    }
  }

  & .card-content {
    padding: 8px 14px 0;
  }
}

// 遮罩层 默认 fixed 定位如果需根据父元素大小加上 abso 即可
.mask-wrapper {
	position: fixed;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	background-color: rgba(0, 0, 0, 0.45);
}

// 垂直水平居中
.pos-center {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

// 边框
.border {
  border: 1px solid $border-color;

  @each $short, $long in l left, t top, r right, b bottom {
    // 缩写版,结果如: border-l
    &-#{$short} {
      border-#{$long}: 1px solid $border-color;
    }
  }
}

// 框的类型
@each $short, $long in i inline, b block, ib inline-block {
  .ds-#{$short} {
    display: $long;
  }
}

// 定位类型
@each $short, $long in rela relative, abso absolute, fixed fixed {
  .#{$short} {
    position: $long;
  }
}

// 文字对齐方式
@each $k in left, center, right {
  .text-#{$k} {
    text-align: $k;
  }
}

// 定义字体(px)单位,大于或等于12的都为px单位字体
@each $i in (12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50) {
  .fs-#{$i} {
    font-size: $i + px;
  }
}

// 定义文字行数
@for $i from 1 through 3 {
  .text-wrap-#{$i} {
    display: -webkit-box;
    overflow: hidden;
    text-overflow: ellipsis;
    line-clamp: $i;
    word-break: break-all;
    -webkit-line-clamp: $i;
    -webkit-box-orient: vertical;
  }
}

// 定义flex等分
@for $i from 0 through 12 {
  .flex-#{$i} {
    flex: $i;
  }
}

// 定义内外边距,历遍0-80
@each $i in (0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80) {
  // 得出:margin-30或者m-30
  .m-#{$i} {
    margin: $i + px;
  }

  .my-#{$i} {
    margin-top: $i + px;
    margin-bottom: $i + px;
  }

  .mx-#{$i} {
    margin-left: $i + px;
    margin-right: $i + px;
  }

  // 得出:padding-30或者p-30
  .p-#{$i} {
    padding: $i + px;
  }

  .py-#{$i} {
    padding-top: $i + px;
    padding-bottom: $i + px;
  }

  .px-#{$i} {
    padding-left: $i + px;
    padding-right: $i + px;
  }

  @each $short, $long in l left, t top, r right, b bottom {
    // 缩写版,结果如: m-l-30
    // 定义外边距
    .m#{$short}-#{$i} {
      margin-#{$long}: $i + px;
    }

    // 定义内边距
    .p#{$short}-#{$i} {
      padding-#{$long}: $i + px;
    }

    // bottom left rigth top
    .#{$short}-#{$i} {
      #{$long}: $i + px;
    }
  }
}

引入 common.scss 文件

@import  "./common.scss";

使用/示例

<div style="width: 500px;height: 100px;background-color: #F56C6C;" class="mb-10">margin-bottom</div>
<div style="width: 500px;height: 500px;background-color: #409EFF;" class="rela mb-10">
	<div class="text-wrap-1 mt-10">文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号文字显示省略号</div>
	<div class="pos-center" style="width: 100px;height: 100px;background-color: #FFA500;">居中 DIV</div>
</div>

<div style="width: 500px" class="card">
	<div class="card-header">
		<span class="header-title">标题</span>
		<span class="header-other">更多</span>
	</div>
	<div class="card-content">
		<div>内容</div>
		<div>内容</div>
		<div>内容</div>
		<div>内容</div>
		<div>内容</div>
		<div>内容</div>
	</div>
</div>

![在这里插入图片描述](https://img-blog.csdnimg.cn/20201203172259725.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1lhbmdUWF8=,size_16,color_FFFFFF,t_70

------------------------------------------------------------------------------ End

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 当我们开始为网站或应用程序编写CSS样式表时,我们通常需要创建一组通用样式,以确保整个网站或应用程序保持一致的外观和风格。 以下是一些常见的前端CSS通用样式: 1. 字体和字号:设置网站或应用程序的默认字体和字号。通常使用Sans-serif或Serif字体。 ``` body { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; } ``` 2. 颜色:设置文本和背景颜色,以及链接的默认颜色和悬停颜色。 ``` body { color: #333; background-color: #fff; } a { color: #007bff; } a:hover { color: #0056b3; } ``` 3. 边框和阴影:为网站或应用程序中的元素添加边框和阴影效果。 ``` .box { border: 1px solid #ccc; box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1); } ``` 4. 布局:设置元素的位置和大小,以及页面布局的样式。 ``` .container { max-width: 1200px; margin: 0 auto; } .header { height: 60px; background-color: #333; color: #fff; } .footer { height: 40px; background-color: #333; color: #fff; } ``` 5. 按钮:创建一组通用的按钮样式。 ``` .btn { display: inline-block; padding: 6px 12px; font-size: 14px; font-weight: 400; line-height: 1.42857143; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; border: 1px solid #ccc; border-radius: 4px; background-color: #fff; color: #333; } .btn-primary { background-color: #007bff; border-color: #007bff; color: #fff; } .btn-primary:hover { background-color: #0069d9; border-color: #0062cc; color: #fff; } ``` 以上是一些常见的前端CSS通用样式,但这只是一个开始。在编写CSS样式表时,我们应该根据实际需求创建自己的通用样式,以确保网站或应用程序的外观和风格一致。 ### 回答2: 前端CSS通用样式是Web前端开发中常用的一套样式集合,用于统一网页的外观和布局。以下是一份基本的前端CSS通用样式: 1.盒模型样式: * 设置元素的宽度和高度:width、height; * 设置元素的内边距:padding; * 设置元素的外边距:margin; * 设置元素的边框样式和宽度:border。 2.文本样式: * 设置文本颜色:color; * 设置文本大小:font-size; * 设置文本粗细:font-weight; * 设置文本字体:font-family; * 设置文本对齐方式:text-align; * 设置文本装饰:text-decoration。 3.背景样式: * 设置背景颜色:background-color; * 设置背景图片:background-image; * 设置背景重复方式:background-repeat; * 设置背景大小:background-size; * 设置背景位置:background-position。 4.布局样式: * 设置元素的浮动方式:float; * 设置元素的居中方式:text-align: center; * 设置元素的定位方式:position; * 设置元素的显示方式:display。 5.其他样式: * 设置元素的透明度:opacity; * 设置元素的阴影效果:box-shadow; * 设置元素的过渡效果:transition; * 设置元素的动画效果:animation。 以上是一些常用的前端CSS通用样式,可以根据具体需求进行扩展和修改。在前端开发中,通过合理运用这些样式,可以使网页的外观更加统一、美观,并且提高开发效率。 ### 回答3: 前端CSS通用样式是用来设置网页元素外观和布局的一组样式规则。以下是一个简单的示例: ``` /* 设置全局样式 */ body { font-family: Arial, sans-serif; /* 设置字体样式 */ background-color: #f5f5f5; /* 设置背景颜色 */ margin: 0; /* 设置页面边距 */ padding: 0; /* 设置元素内边距 */ } /* 设置链接样式 */ a { color: #337ab7; /* 设置链接颜色 */ text-decoration: none; /* 去除下划线 */ } a:hover { text-decoration: underline; /* 鼠标悬停时增加下划线 */ } /* 设置标题样式 */ h1, h2, h3 { color: #333; /* 设置标题颜色 */ font-weight: bold; /* 设置字体粗细 */ margin: 10px 0; /* 设置标题的上下边距和左右边距 */ } /* 设置按钮样式 */ .button { display: inline-block; /* 行内块级元素 */ background-color: #4caf50; /* 设置按钮背景颜色 */ color: #fff; /* 设置按钮文本颜色 */ padding: 8px 16px; /* 设置按钮内边距 */ border-radius: 4px; /* 设置按钮圆角 */ text-decoration: none; /* 去除按钮下划线 */ } .button:hover { background-color: #45a049; /* 鼠标悬停时改变背景颜色 */ } /* 设置表格样式 */ table { border-collapse: collapse; /* 合并表格边框 */ width: 100%; /* 设置表格宽度为100% */ } th, td { padding: 8px; /* 设置单元格内边距 */ text-align: left; /* 设置文本居左 */ border-bottom: 1px solid #ddd; /* 设置单元格下边框 */ } th { background-color: #f2f2f2; /* 设置表头背景颜色 */ } /* 设置输入框样式 */ input[type=text], textarea { width: 100%; /* 设置输入框宽度为100% */ padding: 12px; /* 设置输入框内边距 */ border: 1px solid #ccc; /* 设置边框 */ border-radius: 4px; /* 设置输入框圆角 */ box-sizing: border-box; /* 设置输入框尺寸计算方式为包含边框和填充 */ } /* 设置图片样式 */ img { max-width: 100%; /* 图片宽度最大为100% */ height: auto; /* 图片高度自动调整 */ } ``` 以上是一个简单的前端CSS通用样式示例,你可以在基础上根据你的项目需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值