样式清除

其中附带解决高度塌陷及外边距重叠CSS

<link rel="stylesheet" href="https://www.yikzero.com/usr/uploads/Html/css/reset.css">

详细样式

html {
  -ms-text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  height: 100%;
}
body {
  margin: 0;
  font-size: 14px;
  font-family: "Helvetica Neue", Helvetica, STHeiTi, Arial, sans-serif;
  line-height: 1.5;
  color: #333;
  background-color: #fff;
  min-height: 100%;
}
article, aside, details, figcaption, figure, footer, header, hgroup, main, menu, nav, section, summary {
  display: block;
}
audio, canvas, progress, video {
  display: inline-block;
}
audio:not([controls]) {
  display: none;
  height: 0;
}
progress {
  vertical-align: baseline;
}
[hidden], template {
  display: none;
}
a {
  background: transparent;
  text-decoration: none;
  color: #08c;
}
a:active {
  outline: 0;
}
abbr[title] {
  border-bottom: 1px dotted;
}
b, strong {
  font-weight: bold;
}
dfn {
  font-style: italic;
}
mark {
  background: #ff0;
  color: #000;
}
small {
  font-size: 80%;
}
sub, sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}
sup {
  top: -0.5em;
}
sub {
  bottom: -0.25em;
}
img {
  max-width: 100%;
  border: 0;
  vertical-align: middle;
}
svg:not(:root) {
  overflow: hidden;
}
pre {
  overflow: auto;
  white-space: pre;
  white-space: pre-wrap;
  word-wrap: break-word;
}
code, kbd, pre, samp {
  font-family: monospace, monospace;
  font-size: 1em;
}
button, input, optgroup, select, textarea {
  color: inherit;
  font: inherit;
  margin: 0;
  vertical-align: middle;
}
button, input, select {
  overflow: visible;
}
button, select {
  text-transform: none;
}
button, html input[type="button"], input[type="reset"], input[type="submit"] {
  -webkit-appearance: button;
  cursor: pointer;
}
[disabled] {
  cursor: default;
}
button::-moz-focus-inner, input::-moz-focus-inner {
  border: 0;
  padding: 0;
}
input {
  line-height: normal;
}
input[type="checkbox"], input[type="radio"] {
  box-sizing: border-box;
  padding: 0;
}
input[type="number"]::-webkit-inner-spin-button, input[type="number"]::-webkit-outer-spin-button {
  height: auto;
}
input[type="search"] {
  -webkit-appearance: textfield;
  box-sizing: border-box;
}
input[type="search"]::-webkit-search-cancel-button, input[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
}
fieldset {
  border: 1px solid #c0c0c0;
  margin: 0 2px;
  padding: 0.35em 0.625em 0.75em;
}
legend {
  border: 0;
  padding: 0;
}
textarea {
  overflow: auto;
  resize: vertical;
  vertical-align: top;
}
optgroup {
  font-weight: bold;
}
input, select, textarea {
  outline: 0;
}
textarea, input {
  -webkit-user-modify: read-write-plaintext-only;
}
input::-ms-clear, input::-ms-reveal {
  display: none;
}
input::-moz-placeholder, textarea::-moz-placeholder {
  color: #999;
}
input:-ms-input-placeholder, textarea:-ms-input-placeholder {
  color: #999;
}
input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
  color: #999;
}
.placeholder {
  color: #999;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
td, th {
  padding: 0;
}
h1, h2, h3, h4, h5, h6, p, figure, form, blockquote {
  margin: 0;
}
ul, ol, li, dl, dd {
  margin: 0;
  padding: 0;
}
ul, ol {
  list-style: none outside none;
}
h1, h2, h3 {
  line-height: 2;
  font-weight: normal;
}
h1 {
  font-size: 18px;
}
h2 {
  font-size: 16px;
}
h3 {
  font-size: 14px;
}
i {
  font-style: normal;
}
* {
  box-sizing: border-box;
}
.clearfix::before, .clearfix::after {
  content: "";
  display: table;
}
.clearfix::after {
  clear: both;
}


笔记

Why

  • 某些标签的默认样式不符合我们的设计要求

比如说a标签,默认它是有一条下划线,并且字体颜色也让人觉得很难看,所以我们需要清除它默认的样式,同时根据要求给它重新添加自定义样式。

  • 各浏览器默认的样式各不同,所以会影响到我们的开发

因为在每次开发之前应该先对浏览器的默认样式进行一次清除,让每个浏览器保持一致的初始化样式。清除默认样式可以提高代码浏览器间的兼容性,同一个标签用在不同的浏览器中,默认样式可能不一样。

比如说,行高在某个浏览器中是1,在另一个浏览器中可能是1.1,这样在布局的时候会得到两种不同的效果,从而带来兼容性问题。

因此,统一清除标签的默认样式,再统一重新添加新的样式,就能避免这种问题。

解决高度塌陷及外边距重叠

.clearfix::before,
.clearfix::after{
    content: "";
    display: table;
    clear: both;
    }

高度塌陷

在文档流中,父元素的高度默认是被子元素撑开的,
也就是子元素多高,父元素就多高。
但是当为子元素设置浮动以后,子元素会完全脱离文档流
此时将会导致子元素无法撑起父元素的高度,导致父元素的高度塌陷
由于父元素的高度塌陷了,则父元素下的所有元素都会向上移动,这样将会导致页面布局混乱
所以在开发中一定要避免出现高度塌陷的问题,
我们可以将父元素的高度写死,以避免塌陷的问题出现,
但是一旦高度写死,父元素的高度将不能自动适应子元素的高度,所以这种方案是不推荐使用的。

根据W3C的标准,在页面中元素都一个隐含的属性叫做Block Formatting Context
  简称BFC,该属性可以设置打开或者关闭,默认是关闭的。
当开启元素的BFC以后,元素将会具有如下的特性:

      1.父元素的垂直外边距不会和子元素重叠    
      2.开启BFC的元素不会被浮动元素所覆盖
      3.开启BFC的元素可以包含浮动的子元素
  
  如何开启元素的BFC
      1.设置元素浮动
            - 使用这种方式开启,虽然可以撑开父元素,但是会导致父元素的宽度丢失
              而且使用这种方式也会导致下边的元素上移,不能解决问题
      2.设置元素绝对定位
      3.设置元素为inline-block
          - 可以解决问题,但是会导致宽度丢失,不推荐使用这种方式
      4.将元素的overflow设置为一个非visible的值
      
  推荐方式:将overflow设置为hidden是副作用最小的开启BFC的方式。    

overflow: hidden;

/*
 但是在IE6及以下的浏览器中并不支持BFC,所以使用这种方式不能兼容IE6。
  在IE6中虽然没有BFC,但是具有另一个隐含的属性叫做hasLayout,
  该属性的作用和BFC类似,所在IE6浏览器可以通过开hasLayout来解决该问题
  开启方式很多,我们直接使用一种副作用最小的:
      直接将元素的zoom设置为1即可
*/

/*
  zoom表示放大的意思,后边跟着一个数值,写几就将元素放大几倍
  zoom:1表示不放大元素,但是通过该样式可以开启hasLayout
  zoom这个样式,只在IE中支持,其他浏览器都不支持
*/
  zoom:1;
  overflow: hidden;
/*
 解决高度塌陷方案二:
     可以直接在高度塌陷的父元素的最后,添加一个空白的div,
     由于这个div并没有浮动,所以他是可以撑开父元素的高度的,
     然后在对其进行清除浮动,这样可以通过这个空白的div来撑开父元素的高度,
     基本没有副作用
 
 使用这种方式虽然可以解决问题,但是会在页面中添加多余的结构。
*/
/*通过after伪类,选中box1的后边*/

/*
 可以通过after伪类向元素的最后添加一个空白的块元素,然后对其清除浮动,
     这样做和添加一个div的原理一样,可以达到一个相同的效果,
     而且不会在页面中添加多余的div,这是我们最推荐使用的方式,几乎没有副作用
*/
.clearfix:after{
  /*添加一个内容*/
  content: "";
  /*转换为一个块元素*/
  display: block;
  /*清除两侧的浮动*/
  clear: both;
  }

/*
 在IE6中不支持after伪类,
     所以在IE6中还需要使用hasLayout来处理
*/
.clearfix{
  zoom:1;
  }

字体指定


1.字体存在服务端,下载看网速,使用需谨慎。
2.版权问题。

@font-face {
    /*指定字体的名字*/
    font-family: ;
    /*指定字体的路径*/
    src: url("");
    }

强制不换行,文字溢出显示省略号

div{
    /*文本强制不换行*/
    white-space: nowrap;  
    /*文本溢出显示省略号*/
    text-overflow:ellipsis; 
    /*溢出的部分隐藏*/
    overflow:hidden;
    }

简单隐藏元素缓出效果

/*隐藏的元素高度设置为0*/
element-hidden{
    height: 0;
    overflow: hidden;
    /*高度从0经过0.5秒变化出来*/
    transition: height 0.5s;
    }

element:hover element-hidden{
    height: 109px;
    display: block;
    }

移入图片框内放大


div{
    width: 300px;
    height: 150px;
    /*超出部分隐藏*/
    overflow: hidden;
    }

div img{
    /*所有变化2秒完成*/
    transition: all 2s;
}

div img:hover{
    /*
    缩放倍数,小于1为缩小,
    scale(x,y)   y可省略,与X相同
    */
    transform: scale(1.2);
}

元素页面居中及移入阴影效果

/*单行文字垂直居中*/
div{
    height: 500px;
    line-height: 500px;
    }

/*设置元素页面居中*/
.element{
    position: absolute
    left: 50%
    top: 50%
    transform: translate(-50%,-50%);
}

/*弹性盒中居中对齐*/
div{
display: flex;
justify-content: center;
align-items: center;
}

/*利用transform设置移入交互*/
.box1{
    width: 150px;
    height: 200px;
    background-color: white;
    border-radius: 15px;
    margin: 80px auto;
    box-shadow: 0 0 0 rgba(0,0,0,.1);
    transition: all .5s;
}

.box1:hover{
    transform: translate(-5px,-10px);
    box-shadow: 0 0 15px rgba(0,0,0,.1);
}
Last modification:April 28th, 2022 at 08:40 am
不积小流无以成江海,赞赏我的人多了我就有钱了。