CSS笔记 from MDN

以下内容均来自MDN,仅作为笔记参考使用:

https://developer.mozilla.org/en-US/docs/Learn/CSS/Styling_text/Fundamentals

Nested statements are a specific subset of at-rule, the syntax of which is a nested block of CSS rules that will only be applied to the document if a specific condition is matched:

  • The @media at-rule content is applied only if the device which runs the browser matches the expressed condition;
  • the @supports at-rule content is applied only if the browser actually supports the tested feature;
  • the @document at-rule content is applied only if the current page matches some conditions.

dot used to be followed by a class name

hash(#) used to be followed by an id

The universal selector (*) is the ultimate joker. It allows selecting all elements in a page.

Presence and value attribute selectors

These attribute selectors try to match an exact attribute value:

  • [attr] : This selector will select all elements with the attribute attr, whatever its value.
  • [attr=val] : This selector will select all elements with the attribute attr, but only if its value is val.
  • [attr~=val]: This selector will select all elements with the attribute attr, but only if the value val is one of a space-separated list of values contained in attr‘s value, for example a single class in a space-separated list of classes.

Substring value attribute selectors

  • [attr|=val] : This selector will select all elements with the attribute attr for which the value is exactly val or starts with val- (careful, the dash here isn‘t a mistake, this is to handle language codes.)
  • [attr^=val] : This selector will select all elements with the attribute attr for which the value starts with val.
  • [attr$=val] : This selector will select all elements with the attribute attr for which the value ends with val.
  • [attr*=val] : This selector will select all elements with the attribute attr for which the value contains the string val (unlike [attr~=val], this selector doesn‘t treat spaces as value separators but as part of the attribute value.)

需要理解

Combinators Select
AB Any element matching both A and B at the same time.
A B Any element matching B that is a descendant of an element matching A (that is: a child, or a child of a child, etc.)
A > B Any element matching B that is a direct child of an element matching A.
A + B Any element matching B that is the next sibling of an element matching A (that is: the next child of the same parent.)
A ~ B Any element matching B that is among the next sibling of an element matching A (that is: one of the next children of the same parent.)

element selectors have low specificity. Class selectors have a higher specificity, so will win against element selectors. ID selectors have an even higher specificity, so will win against class selectors. The only way to win against an ID selector is to use !important.

In CSS, there is a special piece of syntax you can use to make sure that a certain rule will win over all others: !important. Adding this to the end of a property value will give it superpowers.

But really, don‘t use it if you can avoid it. Because !important changes the way the cascade normally works, it can make debugging CSS problems really hard to work out, especially in a large stylesheet.

px: pixel size

em: ?em = ?*parent element size, i.e. font-size of <p> is 10px, then the <span> with the font-size of 1.4em will be 14px.

rem: ?rem = ?*root element size, i.e. font-size of <html> is 10px, then the <li> or <span> inside the <html> tags whose font-size is 1.4em will be 14px, no matter the size of its parent element.

  • font-style: Used to turn italic text on and off. Possible values are as follows (you‘ll rarely use this, unless you want to turn some italic styling off for some reason):
    • normal: Sets text to normal font (turns existing italics off.)
    • italic: Sets text to use the italic version of the font if available; if not available, it will simulate italics with oblique instead.
    • oblique: Sets text to use a simulated version of an italic font, created by slanting the normal version.
  • font-weight: Sets how bold the text is. This has many available values available in case you have many font variants available (such as -light-normal-bold-extrabold-black, etc.), but realistically you‘ll rarely use any of them except for normal and bold:
    • normalbold: Normal and bold font weight
    • lighterbolder: Sets the current element‘s boldness to be one step lighter or heavier than its parent element‘s boldness.
    • 100900: Numeric boldness values that provide finer grained control than the above keywords, if needed.
  • text-transform: Allows you to set your font to be transformed. Values include:
    • none: Prevents any transformation.
    • uppercase: Transforms ALL TEXT TO CAPITALS.
    • lowercase: Transforms all text to lower case.
    • capitalize: Transforms all words to Have The First Letter Capitalized.
    • full-width: Transforms all glyphs to be written inside a fixed-width square, similar to a monospace font, allowing aligning of e.g. latin characters along with asian language glyphs (like Chinese, Japanese, Korean.)
  • text-decoration: Sets/unsets text decorations on fonts (you‘ll mainly use this to unset the default underline on links when styling them.) Available values are:
    • none: Unsets any text decorations already present.
    • underline: Underlines the text.
    • overline: Gives the text an overline.
    • line-through: Puts a strikethrough over the text.
  •  
    text-shadow: 4px 4px 5px red;
    1. The horizontal offset of the shadow from the original text — this can take most available CSS length and size units, but you‘ll most comonly use px. This value has to be included.
    2. The vertical offset of the shadow from the original text; behaves basically just like the horizontal offset, except that it moves the shadow up/down, not left/right. This value has to be included.
    3. The blur radius — a higher value means the shadow is dispersed more widely. If this value is not included, it defaults to 0, which means no blur. This can take most available CSS length and size units.
    4. The base color of the shadow, which can take any CSS color unit. If not included, it defaults to black.

    The text-align property is used to control how text is aligned within its containing content box. The available values are as follows, and work in pretty much the same way as they do in a regular word processor application:

    • left: Left justifies the text.
    • right: Right justifies the text.
    • center: Centers the text.

    The line-height property sets the height of each line of text — this can take most length and size units, but can also take a unitless value, which acts as a multiplier and is generally considered the best option — the font-size is multiplied to get the line-height.

    The letter-spacing and word-spacing properties allow you to set the spacing between letters and words in your text. You won‘t use these very often, but might find use for them to get a certain look, or to improve the legibility of a particularly dense font. They can take most length and size units.

    Many font properties can also be set through the shorthand property font. These are written in the following order:  font-stylefont-variantfont-weightfont-stretchfont-sizeline-height, and font-family.

    list-style-type: Sets the type of bullets to use for the list, for example square or circle bullets for an unordered list, or numbers, letters or roman numerals for an ordered list.
    list-style-position: Sets whether the bullets appear inside the list items, or outside them before the start of each item.

    ol {
      list-style-type: upper-roman;
      list-style-position: inside;//outside
    }

    list-style-image: Allows you to use a custom image for the bullet, rather than a simple square or circle.

    ul {
      list-style-image: url(star.svg);
    }

    The start attribute allows you to start the list counting from a number other than 1.

    The reversed attribute will start the list counting down instead of up.

    <ol start=“4" reversed>
      <li>Toast pitta, leave to cool, then slice down the edge.</li>
      <li>Fry the halloumi in a shallow, non-stick pan, until browned on both sides.</li>
      <li>Wash and chop the salad.</li>
      <li>Fill pitta with salad, humous, and fried halloumi.</li>
    </ol>

    The value attribute allows you to set your list items to specific numerical values.

    <ol>
      <li value="2">Toast pitta, leave to cool, then slice down the edge.</li>
      <li value="4">Fry the halloumi in a shallow, non-stick pan, until browned on both sides.</li>
      <li value="6">Wash and chop the salad.</li>
      <li value="8">Fill pitta with salad, humous, and fried halloumi.</li>
    </ol>
    • Link (unvisited): The default state that a link resides in, when it isn‘t in any other state. This can be specifically styled using the :link pseudo class.
    • Visited: A link when it has already been visited (exists in the browser‘s history), styled using the :visited pseudo class.
    • Hover: A link when it is being hovered over by a user‘s mouse pointer, styled using the :hover pseudo class.
    • Focus: A link when it has been focused (for example moved to by a keyboard user using the Tab key or similar, or programmatically focused using HTMLElement.focus()) — this is styled using the :focus pseudo class.
    • Active: A link when it is being activated (e.g. clicked on), styled using the :activepseudo class.
    a {
    
    }
    
    a:link {
    
    }
    
    a:visited {
    
    }
    
    a:focus {
    
    }
    
    a:hover {
    
    }
    
    a:active {
    
    }

    顺序很重要: lvfha

    <div class="one"></div>
    <div class="two"></div>
    html {
      font-family: sans-serif;
      background: #ccc;
    }
    
    .one, .two {
      background: red;
      width: 300px;
      height: 150px;
      padding: 20px;
      border: 10px solid black;
      margin: 20px auto;
    }
    
    .two {
      box-sizing: border-box;
    }
    var one = document.querySelector(‘.one‘);
    var two = document.querySelector(‘.two‘);
    
    function outputWH(box) {
      var width = box.offsetWidth;
      var height = box.offsetHeight;
      box.textContent = ‘Width: ‘ + width + ‘px, Height: ‘ + height + ‘px.‘
    }
    
    outputWH(one);
    outputWH(two);

    当你想生成一个border-box的时候(换言之,想生成一个以border作为box外边界、而非content的box的时候——所给他设置的padding和border被包含在box内部)https://developer.mozilla.org/en-US/docs/Learn/CSS/Styling_boxes/Box_model_recap

时间: 2024-11-05 18:46:54

CSS笔记 from MDN的相关文章

HTML+CSS笔记 CSS中级 颜色&长度值

颜色值 在网页中的颜色设置是非常重要,有字体颜色(color).背景颜色(background-color).边框颜色(border)等,设置颜色的方法也有很多种: 1.英文命令颜色 语法: p{color:red;} 2.RGB颜色 这个与 photoshop 中的 RGB 颜色是一致的,由 R(red).G(green).B(blue) 三种颜色的比例来配色 p{color:rgb(133,45,200);} 每一项的值可以是 0~255 之间的整数,也可以是 0%~100% 的百分数. 每

css笔记(二)——几种常用的模式

文本垂直居中 对于行内元素,height会自动收缩到包裹住文本的高度,所以不存在这个问题.但是对于block和inline-block等盒子元素,如果设置了height属性,则文本默认会在上方显示.如果希望文本在垂直方向上居中,可以设置line-height属性等于height属性,或者大于height属性 <div> hello world </div> div { height: 200px; line-height: 200px; } 文本水平居中,图标分列左右两侧 效果是左

css笔记-display属性

css笔记-display属性 display属性可取值 display:none | inline | block | list-item | inline-block | table | inline-table | table-caption | table-cell | table-row | table-row-group | table-column | table-column-group | table-footer-group | table-header-group | co

css笔记-选择器详解

css笔记-选择器详解 CSS通过选择器来定位要应用样式的元素. 下面对所有的选择器做了一个解释(CSS为版本号). CSS选择器详解 选择器 例子 例子描述 CSS .class .intro 选择 class="intro" 的所有元素. 1 #id #firstname id="firstname" 的所有元素. 1 * * 选择所有元素. 2 element p 选择所有 <p> 元素. 1 element,element div,p 选择所有

HTML/CSS笔记归纳整理

前言: 前端无非就是围绕标签.属性.属性值这三个词展开的. *HTML基本语法: 1. 常规标签 <标签 属性1="属性值1" 属性2="属性值2"></标签> # 一个标签可以没有属性也可以有多个属性, 属性和属性之间不分先后顺序. 2. 空标签 <标签 属性1="属性值1" 属性2="属性值2" /> # 空标签没有结束标签, 用"/"代替. *HTML标题: <

HTML+CSS笔记 CSS进阶再续

CSS的布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了.布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上,又不同于我们常说的 CSS 布局样式或 CSS 布局模板.如果说布局模型是本,那么 CSS 布局模板就是末了,是外在的表现形式. CSS包含3种基本的布局模型,用英文概括为:Flow.Layer 和 Float. 在网页中,元素有三种布局模型:1.流动模型(Flow)2.浮动模型 (Float)3.

HTML+CSS笔记--1

(初接触html+css时一些笔记) 1.父盒子的高可以不用给,让里面的内容撑起来就好. 2.对于外边距合并问题,解决方法overflow : hidden;,或者是另外加一个边框可以下来. 3.对于网站的logo,我们要给它设置关键字,写在a元素里, 1 .logo a{ 2 display: block; 3 height: 40px; 4 text-indent: -2000em;/*首行缩进 em是一个字的大小,rem是以一个根的大小 这个字是给搜索引擎看的*/ 5 } 4.我们可以将具

HTML+CSS笔记 CSS中级 缩写入门

盒子模型代码简写 回忆盒模型时外边距(margin).内边距(padding)和边框(border)设置上下左右四个方向的边距是按照顺时针方向设置的:上右下左. 语法: margin:10px 15px 12px 14px;/*上设置为10px.右设置为15px.下设置为12px.左设置为14px*/ 通常有三种缩写的方法: 1.如果top.right.bottom.left的值相同, margin:10px 10px 10px 10px; 可缩写为: margin:10px; 2.如果top和

HTML+CSS笔记 CSS进阶续集

元素分类 在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 常用的块状元素有: <div>.<p>.<h1>...<h6>.<ol>.<ul>.<dl>.<table>.<address>.<blockquote> .<form> 常用的内联元素有: <a>.<span>.<br>