百科狗-知识改变命运!
--

font - 文本字体的复合属性 - css 字体属性

是丫丫呀1年前 (2023-11-21)阅读数 14#技术干货
文章标签字体

font

设置或检索对象中的文本字体,是复合属性。

浏览器支持

浏览器都支持font

语法:

font:[[font-style||font-variant||font-weight]?font-size[/line-height]?font-family]
默认值看独立属性自身
适用于所有元素
继承性
动画性看独立属性自身
计算值看独立属性自身

取值:

  • font-style:指定文本字体样式
  • font-variant:指定文本是否为小型的大写字母
  • font-weight:指定文本字体的粗细
  • font-size:指定文本字体尺寸
  • line-height:指定文本字体的行高
  • font-family:指定文本使用某个字体或字体序列

说明:

设置或检索对象中的文本特性。该属性是复合属性。

  • 使用第一种声明方式参数必须按照如上的排列顺序,且font-sizefont-family是不可忽略的。每个参数仅允许有一个值。忽略的将使用其参数对应的独立属性的默认值。
  • 对于如何使用客户端系统没有字体和自定义字体,可以参阅@font-face规则。
  • 对应的脚本特性为font

如果font字体相关的属性的简写:

  • 必须包含以下值:font-stylefont-family
  • 可以选择性包含以下值:font-stylefont-variantfont-weightline-height
  • font-style,font-variantfont-weight必须在font-size之前
  • font-variant只可以是normalsmall-capsline-height必须跟在font-size后面,由"/"分隔,例如"16px/3"
  • font-family必须最后指定

系统关键字

font:caption | icon | menu | message-box | small-caption | status-bar

可以将font属性指定为单个关键字,它将选择系统字体,或者作为字体相关的属性的简写。

如果将font指定为系统关键字,则它必须是以下之一:caption,icon,menu,message-box,small-caption,status-bar

  • caption:使用有标题的系统控件的文本字体(如按钮,菜单等)(CSS2)
  • icon:使用图标标签的字体(CSS2)
  • menu:使用菜单的字体(CSS2)
  • message-box:使用信息对话框的文本字体(CSS2)
  • small-caption:使用小控件的字体(CSS2)
  • status-bar:使用窗口状态栏的字体(CSS2)

系统关键字前缀

浏览器经常会多实现几个带前缀的关键字;Gecko 实现了-moz-window,-moz-document,-moz-desktop,-moz-info,-moz-dialog,-moz-button,-moz-pull-down-menu,-moz-list,和-moz-field.

例子

/* set the font size to 12px and the line height to 14px.set the font family to sans-serif */
p { font: 12px/14px sans-serif }

/* set the font size to 80% of the parent elementor default value (if no parent element present).set the font family to sans-serif */
p { font: 80% sans-serif }

/* set the font weight to bold,the font-style to italic,the font size to large,and the font family to serif. */
p { font: bold italic large serif }

/* use the same font as the status bar of the window */
p { font: status-bar }

HTML Content

change the radio buttons below to see the generated shorthand and it's effect.

font-style
none
normal
italic
oblique
font-variant
none
normal
small-caps
font-weight
none
normal
bold
font-size
12px
16px
24px
line-height
none
1.2
3
font-family
courier
serif
sans-serif
font :

font-style
optional

font-variant
optional

font-weight
optional

font-size
mandatory

line-height
optional

font-family
mandatory
this is some sample text.

CSS Content

body, input {
  font: 14px arial;
}

.propinputcont {
 float: left;
 text-align: center;
 margin-right: 5px;
 width: 80px;
}

.setpropcont {
 float: left;
 margin-right: 5px;
 width: 120px;
}

.propinputs, .setpropcont {
 margin-bottom: 1em;
}

.curcss {
 border: none;
 border-bottom: 1px solid black;
 text-align: center;
 width: 80px;
}

.mandatory {
 border-bottom-color: red;
}

.cf:before,
.cf:after {
 content: " ";
 display: table;
}

.cf:after {
 clear: both;
}

.tar {
  width: 40px;
  text-align: right;
}

font - 文本字体的复合属性 - css 字体属性

JavaScript Content

var textareas = document.getelementsbyclassname("curcss"),
 shorttext = "",
 getcheckedvalue,
 setcss,
 getproperties,
 injectcss;

getproperties = function () {
 shorttext =
   getcheckedvalue("font_style") + " " +
 getcheckedvalue("font_variant") + " " +
 getcheckedvalue("font_weight") + " " +
 getcheckedvalue("font_size") +
 getcheckedvalue("line_height") + " " +
 getcheckedvalue("font_family");

 return shorttext;
}

getcheckedvalue = function(radio_name) {
 oradio = document.forms[0].elements[radio_name];
 for (var i = 0; i  1) {
 old[1].parentelement.removechild(old[1]);
 }
 css = document.createelement("style");
 css.innerhtml = ".fontshorthand{font: " + cssfragment + "}
";
 document.body.appendchild(css);
}

setcss();

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)