HTML中可以使用文本标记、段落标记、列表标记和排版标记对文本进行各种各样的格式化工作。
下面让我们来进行上述内容的学习:
HTML为单独的词或者句子定义了两种样式:物理样式(Physical Style)和逻辑样式(Logical Styles)。
物理样式标记也称为字体样式元素或实体字符控制标记,因为它们为浏览器提供了特定的字体指令。现在各类网页开发工具中仍然有这类标记。
物理样式标记(Physical Style):
标记 | 说明 |
b软件工程专业!/b | 黑体 |
i软件工程专业!/i | 斜体 |
u软件工程专业!/u | 下划线 |
del软件工程专业!/del | 删除线 |
big软件工程专业!/big | 变大字号 |
small软件工程专业!/samll | 变小字号 |
Xsup2/sup | 上标--X2 |
Ysub1/sub | 下标--Y1 |
逻辑样式标记(Logical style):
逻辑样式标记也称为基于内容的标记。
标记 | 主要用途 |
abbretc./abbr | 表示缩写 |
address江苏省南京市/address | 表示地址(address) |
cite软件工程!/cite | 书名、影视名等的引用,斜体 |
code软件工程!/code | 计算机代码,显示固定宽度字体 |
dfn软件工程!/dfn | 定义一个词,通常为斜体 |
samp软件工程!/samp | 显示固定宽度字体 |
kbd软件工程!/kbd | 键盘输入,显示无格式的固定宽度字体 |
em软件工程!/em | 强调,通常为斜体 |
strong软件工程!/strong | 强调,显示黑体字符(重要性) |
var变量/var | 变量,显示斜体字符 |
文本格式化其它标记使用
标题字(heading):标题是通过 h1~h6 等标记进行定义的。h1 定义最大的标题,h6 定义最小的标题。浏览器会自动地在标题的前后添加空行。
添加空格 (nbsp; -non break space):
基本语法: nbsp;
添加特殊符号:编写网页代码时有时需要一些特殊符号,如?、?等,可以直接添加相应的符号代码即可。
段落格式化
p标记属性语法:浏览器会自动地在段落的前后添加空行
预格式化文本标记
实现预先排版好的文本原封不动地显示出来
居中标志
center 实现网页中段落居中显示
段落缩进
可以实现文字缩排,一对缩排标记可以缩进5个西方字符位置
列表标记
列表将网页中相关的资料以条目的形式而形成的一种特殊的表,列表主要分为以下几类;
无序列表:ul
无序列表是一个项目的列表,此列项目使用粗体圆点(典型的小黑圆圈)进行标记。
无序列表始于 ul 标签。每个列表项始于 li。
2. 有序列表:ol
同样,有序列表也是一列项目,列表项目使用数字进行标记。
有序列表始于 ol 标签。每个列表项始于 li 标签。
3. 定义列表:dl
自定义列表不仅仅是一列项目,而是项目及其注释的组合。
自定义列表以 dl 标签开始。每个自定义列表项以 dt 开始。每个自定义列表项的定义以 dd 开始。
Query弹出层插件lightbox
相信用这个插件的朋友会遇到一些问题:
1.顶部滚动
顶部菜单fixed固定,但是点击用了lightbox的图片 点击预览时菜单会向右滚动;仔细查看发现预览层弹出时滚动条隐藏了;解决方法:
在 lightbox.css 中去掉隐藏属性:
滚动条隐藏.png
2.图片抖动
预览层弹出和收起时图片的动画抖动显得有点卡顿;解决方法:
在 lightbox.js 中找到:
"transition-duration",e+"ms"进入时间 和 "transition-duration",i+"ms"收起时间;
进去.png
收起.png
然后改成你想要的时间,比如:
进入时间:"transition-duration","200ms"
收起时间:"transition-duration","150ms"
3.弹出窗底部有个横线
线1.png
在 lightbox.css 中找到:
fancybox-caption{padding:30px 0;border-top:1px solid hsla(0,0%,100%,.4);
线.png
把border-top出掉即可
演示网站cuanyair.com(头部幻灯片部分)
为 Web 开发人员,我们希望用户无论使用哪种浏览器浏览网页,HTML 元素的外观都是一样的。遗憾的是,由于浏览器的运行方式,情况并非如此。每个浏览器都有自己的默认样式表,其中规定了一些适用于元素的最基本规则。在大多数情况下,这些默认样式是有用的。但是,由于这些默认值的不同,有时会在特定浏览器中出现表现不一致的现象,影响用户的浏览体验。
为了避免这些不一致,CSS Reset 由此而来。本文先用部分章节介绍 CSS Reset的历史,后面重点介绍推荐的 Modern CSS Reset。
本节按时间线来整体回顾 CSS Reset 的发展历程。
最基本的全局重置方法是使用通配符选择器将所有元素的 padding 和 margins 重置为零。根据浏览大多数网站 CSS 样式得出的结论,下面应该是最常用的 CSS 重置方法,简单粗暴。
* {
padding: 0;
margin: 0;
}
2007 年,根据杰夫-斯塔尔(Jeff Starr)收集的大量不同的 CSS 重置方法。最早的应该是 Tantek ?elik 的 undohtml.css(http://tantek.com/log/2004/undohtml.css)。Tantek 的 CSS Reset 可移除一些比较突出和不需要的默认浏览器样式。特别是,它能去除链接和点击链接上通常不受欢迎的下划线和边框,消除大多数块元素的边距和填充,甚至将通用字体大小设为 1em。
Tips:从样式中可以看到 CSS Reset 没有包含对 HTML5 新增的语义化标签。
/* undohtml.css */
/* (CC) 2004 Tantek Celik. Some Rights Reserved. */
/* http://creativecommons.org/licenses/by/2.0 */
/* This style sheet is licensed under a Creative Commons License. */
/* Purpose: undo some of the default styling of common (X)HTML browsers */
/* link underlines tend to make hypertext less readable,
because underlines obscure the shapes of the lower halves of words */
:link,:visited { text-decoration:none }
/* no list-markers by default, since lists are used more often for semantics */
ul,ol { list-style:none }
/* avoid browser default inconsistent heading font-sizes */
/* and pre/code too */
h1,h2,h3,h4,h5,h6,pre,code { font-size:1em; }
/* remove the inconsistent (among browsers) default ul,ol padding or margin */
/* the default spacing on headings does not match nor align with
normal interline spacing at all, so let's get rid of it. */
/* zero out the spacing around pre, form, body, html, p, blockquote as well */
/* form elements are oddly inconsistent, and not quite CSS emulatable. */
/* nonetheless strip their margin and padding as well */
ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,body,html,p,blockquote,fieldset,input
{ margin:0; padding:0 }
/* whoever thought blue linked image borders were a good idea? */
a img,:link img,:visited img { border:none }
/* de-italicize address */
address { font-style:normal }
/* more varnish stripping as necessary... */
Eric Meyer 的 CSS Reset 应该是当前最流行的。它的内容与 Tantek 的不同(包含了一些新增的 HTML5 元素),但实现目的都一样:移除默认样式。您可能会认出这个著名的代码块,它在开发者的 DevTools 风格面板中随处可见。该 CSS Reset的优点是简单易用,但是它也有一些缺点,例如会影响一些网页元素的默认样式,例如 form 元素。
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
多年后,随着 HTML5 变得越来越真实,Richard Clark的 HTML5 Reset等重置技术也开始流行起来。它仍然是Meyer Reset的修改版,保留了Meyer Reset 重置的精神。
Richard Clark 的 HTML5 Reset 在 Meyer Reset的基础上新增了 mark、hr、ins等元素的样式重置,同时指定了 input、select 等表单元素垂直居中。
/*
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com
Twitter: @rich_clark
*/
html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
margin:0;
padding:0;
border:0;
outline:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
}
body {
line-height:1;
}
article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section {
display:block;
}
nav ul {
list-style:none;
}
blockquote, q {
quotes:none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content:'';
content:none;
}
a {
margin:0;
padding:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
}
/* change colours to suit your needs */
ins {
background-color:#ff9;
color:#000;
text-decoration:none;
}
/* change colours to suit your needs */
mark {
background-color:#ff9;
color:#000;
font-style:italic;
font-weight:bold;
}
del {
text-decoration: line-through;
}
abbr[title], dfn[title] {
border-bottom:1px dotted;
cursor:help;
}
table {
border-collapse:collapse;
border-spacing:0;
}
/* change border colour to suit your needs */
hr {
display:block;
height:1px;
border:0;
border-top:1px solid #cccccc;
margin:1em 0;
padding:0;
}
input, select {
vertical-align:middle;
}
normalize.css 代表了 CSS Reset 第一个有意义的转变,来感受下它的与众不同:
例如下面这部分代码,用完备的注释来说明包含此段 CSS Reset 的原因。
/**
* 1. Remove the bottom border in Chrome 57-
* 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
*/
abbr[title] {
border-bottom: none; /* 1 */
text-decoration: underline; /* 2 */
text-decoration: underline dotted; /* 2 */
}
当前,normalize.css 很受开发者的喜爱,它已经发布了 8.0.1 版本,超过51K个 Github stars。但是最近圈子里又出现了 Modern CSS Reset 很受开发者的喜爱。
2023年9月18日,安迪-贝尔(Andy Bell)--英国设计师、前端开发员和设计公司创始人,发布了一篇博文介绍了Modern CSS Reset。 整个 Reset 的源码比较简单:
/* Box sizing rules */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Prevent font size inflation */
html {
-moz-text-size-adjust: none;
-webkit-text-size-adjust: none;
text-size-adjust: none;
}
/* Remove default margin in favour of better control in authored CSS */
body, h1, h2, h3, h4, p,
figure, blockquote, dl, dd {
margin: 0;
}
/* Remove list styles on ul, ol elements with a list role, which suggests default styling will be removed */
ul[role='list'],
ol[role='list'] {
list-style: none;
}
/* Set core body defaults */
body {
min-height: 100vh;
line-height: 1.5;
}
/* Set shorter line heights on headings and interactive elements */
h1, h2, h3, h4,
button, input, label {
line-height: 1.1;
}
/* Balance text wrapping on headings */
h1, h2,
h3, h4 {
text-wrap: balance;
}
/* A elements that don't have a class get default styles */
a:not([class]) {
text-decoration-skip-ink: auto;
color: currentColor;
}
/* Make images easier to work with */
img,
picture {
max-width: 100%;
display: block;
}
/* Inherit fonts for inputs and buttons */
input, button,
textarea, select {
font: inherit;
}
/* Make sure textareas without a rows attribute are not tiny */
textarea:not([rows]) {
min-height: 10em;
}
/* Anything that has been anchored to should have extra scroll margin */
:target {
scroll-margin-block: 5ex;
}
通读了 Modern CSS Reset 之后,发现一些比较有意思的点,分析如下:
1)盒模型重置:
/* Box sizing rules */
*,
*::before,
*::after {
box-sizing: border-box;
}
这条规则将所有元素和伪元素设置为 border-box,而不是默认的 content-box。现在我们更注重让浏览器通过灵活的布局、流动的字体和空间来完成更多工作,因此这条规则已经不像以前那么有用了。·
border-box 和 content-box 是 box-sizing 属性的两个值。与 content-box 不同,border-box 值表示元素的尺寸还包括边框和填充。
2)列表样式重置:
/* Remove list styles on ul, ol elements with a list role, which suggests default styling will be removed */
ul[role='list'],
ol[role='list'] {
list-style: none;
}
增加按条件的列表样式重构,不至于覆盖全量列表,按需重置。
3)body样式重置:
/* Set core body defaults */
body {
min-height: 100vh;
line-height: 1.5;
}
对 body 元素,引入了现代单位 vh,并设置了最小高度为当前视口高度。
4)标题样式重置:
/* Balance text wrapping on headings */
h1, h2,
h3, h4 {
text-wrap: balance;
}
这条规比较特殊,但新的 text-wrap 属性会让标题看起来很可爱。可能部分UI同学会觉得这不合适,所以你可能想删除这一条。
4)标题样式重置:
/* Anything that has been anchored to should have extra scroll margin */
:target {
scroll-margin-block: 5ex;
}
最后,如果一个元素是锚定的,那么用滚动边距在其上方增加一点空间是有意义的,只有当该元素是目标元素时才会考虑到滚动边距。这只是一个小小的调整,却能带来很多用户体验!如果你有一个固定的页眉,可能需要调整一下。
没有哪种 CSS Reset 最好,选择哪种取决于我们的项目需求和个人偏好。
如果你在项目中尽量多的保留默认样式,小懒在这里强烈推荐你选择 normalize.css,如果你更倾向于完全自定义样式,Meyer 的 CSS Reset 可能更适合你,如果你想改变默认的盒模型计算方式,box-sizing: border-box 是一个不错的选择。
在选择任何 CSS Reset 方案之前,请确保你深刻理解其工作原理,并充分测试其在你的项目中的效果。
*请认真填写需求信息,我们会在24小时内与您取得联系。