CSS 개념--당신이 필요로 하는 유일한 지침

"나는 단지 여기에서 나의 CSS 학습 프로그램을 정리할 뿐이다. 당신의 관심은 내가 업데이트하는 동력이 될 것이다. 또한 그것이 당신이 CSS에 대한 이해를 향상시키는 데 도움이 되기를 바란다. 언제든지 나의 다른 내용을 보십시오(정기적으로 업데이트)."

카탈로그


1. CSS selector Priority
2. Inheritable and non-inheritable properties in CSS
3. Difference between display's block, inline and inline-block
4. How to hide the elements
5. What's the difference between link and @import
6. Transition and Animation
7. Pseudo class and pseudo element
8. How to understand requestAnimationframe
9. Content box vs Border box
10. Why Moving Elements With Translate() Is Better Than position:absolute Top/left
11.How do you understand CSS Sprites
12.What are the measures to optimize CSS performance
13.Why are we using css preprocessor and postprocessor
14.How to determine whether an element has appeared in our viewport
15.Understand the media query
16.When does z-index not work
17.CSS layout unit
18.How to achieve a two column layout
19.How to achieve a three column layout
20.Horizontal and vertical centering
21.Understanding Flex box model
22.Responsive Design
23.Position and Floating

기타 내용


1. CSS 선택기 우선 순위

The CSS selector priority is classified in to few different categories, each carries a different weightage. We can calculate the actual priority by summing up all the selectors' weightage.

Selector Syntax Weightage
Inline Style style="color 1000
Id selector #id 100
class selector .classname 10
attributes selector d[ref=“abc”] 10
pseudo class selector li:first-child 10
HTML tag selector div 1
pseudo element selector li:after 1
  • In fact, if you use the !important rule, it will override ALL previous styling rules for that specific property on that element!
p {
  background-color: red !important;
}
  • If two styles applied have the same priority, the latter one will be selected.

2. CSS의 상속 및 상속 불가 속성

1. non-inheritable properties

  • Display

  • Text attributes : vertical-align、text-decoration、 text-shadow、 white-space、 unicode-bidi

  • Box model attributes: width、height、margin、border、padding

  • Background attributes: background、background-color、background-image、background-repeat、background-position、background-attachment

  • Position attributes: float、clear、position、top、right、bottom、left、min-width、min-height、max-width、max-height、overflow、clip、z-index

  • Generated content attributes: content、counter-reset、counter-increment

  • **Outline style attributes: **outline-style、outline-width、outline-color、outline

  • Page style attributes:size、page-break-before、page-break-after

  • Audio style attributes:pause-before、pause-after、pause、cue-before、cue-after、cue、play-during

2. inheritable properties

  • font attributes: font-family、font-weight、font-size、font-style

  • text attributes: text-indent、text-align、line-height、
    word-spacing、letter-spacing、text-transform、color.

  • Visibility

  • List Layout attributes: list-style

  • cursor

3. 블록, 인라인 및 인라인 블록 간의 차이를 표시합니다.

  • block: block starts on a NEW line and takes up the full width available. So that means block elements will occupy the entire width of its parent element.

  • inline: displays the element inline or on the same line. In other words, inline elements do NOT start on a new line and only takes up as much width as its content.

  • inline-block: It’s essentially the same thing as inline, except that you can set height and width values.

4. 요소 숨기기

  • display: none: such element will not be rendered, thus it will not take up any space in the page, and the event binded to such element will not be triggered.

  • visibility: hidden: the element will still hold in the page and it will respond to the events.

  • opacity: 0: set the transparency of the element to 0, behaves the same as visibility: hidden

  • position: absolute: use absolute position to move the element outside of the viewport.

  • z-index:negative value: use other elements to fully cover it.

  • transform: scale(0,0): scale the size of the element to 0,0 such element will still exist in the page, however it will not listen to the events binded.

5.link와 @import의 차이점

These are both used to reference external CSS files.

  • link is an html tag which can load more than just css files. @import on the other hand can only load css.

  • link can load the css at the same time as the webpage is loading, @import can only load css after the webpage has been completed loaded.

  • Javascript can mutate the link attributes by accessing the DOM, @import does not support such way.

6. 변환 및 애니메이션

  • transitions: For a transition to take place, an element must have a change in state, and different styles must be identified for each state. The easiest way for determining styles for different states is by using the :hover, :focus, :active, and :target pseudo-classes .

  • animations: when more control is required, transitions need to have multiple states. In return, this is why we need animation. It does not need to be triggered by any events and the animation can be looped. We can set multiple key frame points by using

7. 위류와 위원소

Pseudo-classes act as simple selectors in a sequence of selectors and thereby classify elements on non-presentational characteristics, pseudo-elements create new virtual elements.

8.requestAnimationframe 이해 방법

There used to be just one way to do a timed loop in JavaScript: setInterval(). If you needed to repeat something pretty fast (but not as-fast-as-absolutely-possible like a for loop), you’d use that. For the purposes of animation, the goal is sixty “frames” per second to appear smooth, so you’d run a loop like this:

setInterval(function() {
  // animiate something
}, 1000/60);

Now there is a better way by using requestAnimationframe

The window.requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. The method takes a callback as an argument to be invoked before the repaint.

Your callback routine must itself call requestAnimationFrame() again if you want to animate another frame at the next repaint. requestAnimationFrame() is 1 shot. The number of callbacks is usually 60 times per second.


* 이점 *
  • CPU 자원 절약: SetTinterval로 이루어진 애니메이션을 사용하고 페이지가 숨겨지거나 최소화될 때 SetTinterval은 백엔드에서 애니메이션 작업을 수행합니다.이 때 페이지가 보이지 않거나 사용할 수 없는 상태이기 때문에 애니메이션을 새로 고치는 것은 무의미하고 CPU 자원을 완전히 낭비한다.
  • RequestAnimationFrame은 완전히 다릅니다.페이지 처리가 비활성화되면 페이지에 대한 화면 새로 고침 작업도 일시 중지됩니다.따라서 시스템 뒤의 RequestAnimationFrame도 렌더링을 중지합니다.페이지가 활성화되면 애니메이션은 마지막 상태를 유지합니다.CPU 오버헤드는 어디에서나 효율적으로 절감됩니다.
  • 제한: 고주파 이벤트(크기 조정, 스크롤 등)에서 하나의 리셋 간격에서 여러 함수가 실행되는 것을 방지하기 위해 RequestAnimationFrame는 함수가 리셋 간격마다 한 번만 실행되는 것을 확보하여 유창성을 확보하고 함수 실행 원가를 더욱 잘 절약할 수 있다.대부분의 모니터가 16.7ms마다 한 번씩 새로 고쳐지고 여러 개의 드로잉이 화면에 반영되지 않기 때문에 새로 고침 간격 내에 이 기능을 여러 번 실행하는 것은 무의미하다.
  • How to throttle requestAnimationFrame to a specific frame rate
  • DOM 작업 감소:requestAnimationFrame는 각 프레임의 모든 DOM 작업을 수집하여 다시 그리기 완료
  • 왜 우리는 setTimeout를 사용하여 애니메이션을 제어하지 말아야 하는가:
  • setTimeout은 비동기적인 작업이기 때문에 모든 동기화 작업이 완료될 때만 실행되기 때문에 시간 지연이 항상 존재합니다.
  • 고정 시간대와 화면 주사율이 완전히 일치하지 않아 프레임이 분실되었다.
  • 9. 컨텐트 상자 및 프레임

    • content-box: The width & height of the element only include the content. In other words, the border, padding and margin aren’t part of the width or height. This is the default value.

    • border-box: The padding and border are included in the width and height.


    10. Translate()를 사용하여 위치보다 요소를 이동하는 이유: 절대 위/왼쪽

    Translate is a method in transform property. Changing the transform or opacity will not trigger the browser to reflow and repaint, it will only trigger compositions.

    However, changing the absolute positioning will trigger a re-layout, which will trigger re-paint and composititon. The transform ask the browser to create a GPU layer for the element, but changing the absolute positioning will use the CPU. Therefore translate() is more efficient and can shorten the drawing time of smooth animation. When translate changes its position, the element still occupies its original space, and this will not happen with absolute positioning.

    11. CSS 요정을 어떻게 이해하는가

    CSS Sprites are a means of combining multiple images into a single image file for use on a website, to help with performance.

    We can use background-image,background-repeat,background-position
    to located the image.

    Pros:

    • It can minimize the http request that client side has to make for retrieving image resources from the server.

    • Combining multiple image into one will also reduce the image size

    Cons:

    • Require precise measurements on each image's size.

    • When some part of the image has changed, we have to edit the combined image.

    12. CSS 성능을 최적화하기 위한 조치

    • Loading performance:
    1. compress CSS file to reduce file size

    2. use single css property instead of shorthand property

    3. do not use @import, use link instead

    • Selectors:
    1. Use key selectors instead of Descendant combinator as the latter will iterate through all its descendants on the tree.

    2. When using ID selector, don't add unnecessary selectors

    3. do not use * selector

    4. use class selector instead of HTML tag selector

    5. avoid repeatly assigning styles to elements, make use of the inheritable properties.

    • Rendering performance:
    1. use float and position carefully as they consume a lot of resources.

    2. avoid frequent rerendering

    3. use CSS spirte efficiently

    13. css 프로세서와 후프로세서를 사용하는 이유

    • css preprocessor: less, sass, stylus

    • postprocessor: postCss

    Reason for use:

    • To make a clear CSS structure, easy to expand.

    • It can easily prevent different browers' syntax difference.

    • Multiple inheritance can be easily achieved.

    • Perfectly compatible with CSS code and can be applied to old projects.

    14. 요소가 뷰포트에 나타날지 여부를 결정하는 방법

    • window.innerHeight is the viewport height

    • document.body.scrollTop || document.documentElement.scrollTop is the distance that brower has scrolled.

    • imgs.offsetTop is the distance of the element's top to the top of the document

    If img.offsetTop < window.innerHeight + document.body.scrollTop` || document.documentElement.scrollTop that means the img has already shown up in the viewport.

    15. 언론의 질의를 이해한다

    Media queries in CSS3 extended the CSS2 media types idea: Instead of looking for a type of device, they look at the capability of the device.

    Media queries can be used to check many things, such as:

    width and height of the viewport
    width and height of the device
    orientation (is the tablet/phone in landscape or portrait mode?)
    resolution
    Using media queries are a popular technique for delivering a tailored style sheet to desktops, laptops, tablets, and mobile phones (such as iPhone and Android phones).

    16.z지수는 언제 작용하지 않는가

    The z-index property only works on elements with a position value other than static (e.g. position: absolute;, position: relative;, or position: fixed).

    It will not be functioning properly if :

    • Parent container's position is relative

    • the element with z-index has also been set with float

    17. CSS 레이아웃 셀

    CSS units can be separated in the following categories:
  • 절대 단위
  • 글꼴 상대 단위
  • 뷰포트 상대 단위
  • 절대 단위:
    일부 단위는 화면 크기나 글꼴에 영향을 받지 않는 절대값에 따라 달라집니다.이러한 단위의 디스플레이는 화면의 DPI(인치당 포인트)에 따라 화면 해상도에 따라 다를 수 있습니다.
    이러한 단위는 다음과 같습니다.
  • 픽셀
  • 인치
  • cm
  • cm
  • 개인컴퓨터(picas)
  • 글꼴 상대 단위:
    일부 단위는 문서 또는 상위 요소의 글꼴 크기 또는 글꼴 패밀리에 따라 달라집니다.여기에는 다음 셀이 포함됩니다.
  • em
  • ex
  • ch
  • 뷰포트 상대 단위:
    뷰포트의 높이와 너비 크기에 따라 여러 단위가 달라집니다. 예를 들어,
  • vh(윈도우 높이)
  • vw(창 너비)
  • vmin(뷰포트 최소)
  • vmax(뷰포트 최대)
  • 백분율(%) 단위는 위의 범주에 속하지 않습니다.

    18. 두 열의 배치를 어떻게 실현하는가

    Two column layout usually refers to a layout which has a fix width left column and auto fill right column

    1.Float left element to the left, set the width to 200px, set the margin-left of the right element to the width of left element.

    .outer {
    height: 100px;
    }
    .left {
    float: left;
    width: 200px;
    background: tomato;
    }
    .right {
    margin-left: 200px;
    width: auto;
    background: gold;
    }

    2.make use of the absolute position, before that we have to set the position property of the parent container to anything other than static. Then set the left element's position to absolute with 200px width. Follow by right element's margin-left to 200px.

    .outer {
    position: relative;
    height: 100px;
    }
    .left {
    position: absolute;
    width: 200px;
    height: 100px;
    background: tomato;
    }
    .right {
    margin-left: 200px;
    background: gold;
    }

    3.Use the flex layout, set left element width to 200px, set right element flex property to 1

    .outer {
    display: flex;
    height: 100px;
    }
    .left {
    width: 200px;
    background: tomato;
    }
    .right {
    flex: 1;
    background: gold;
    }

    19. 삼란 배치를 어떻게 실현하는가

    Three column layout refers to having the left and right element with fixed width, middle element will auto fill the gap.

    1.Use absolute position, set left element to a certain width. Set right element's top and right attribute to 0 so it will stick to the right side of the container. Lastly set the margin-left and margin-right of the centered element to the respective width of left and right element.

    `.outer {
    position: relative;
    height: 100px;
    }

    .left {
    position: absolute;
    width: 100px;
    height: 100px;
    background: tomato;
    }

    .right {
    position: absolute;
    top: 0;
    right: 0;
    width: 200px;
    height: 100px;
    background: gold;
    }

    .center {
    margin-left: 100px;
    margin-right: 200px;
    height: 100px;
    background: lightgreen;
    }`

    2.Use flex layout, set left and right element with a fixed width, let centered element's flex: 1

    `.outer {
    display: flex;
    height: 100px;
    }

    .left {
    width: 100px;
    background: tomato;
    }

    .right {
    width: 100px;
    background: gold;
    }

    .center {
    flex: 1;
    background: lightgreen;
    }`

    20. 수평 및 수직 고정

    • use the absolute position, set left and top to 50% so that the left corner of the element will appear in the center of the element. Use translate to adjust the center point of the child element to match the parent's.

    .parent{position: relative;}
    .child {position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    }

    • Use flex layout, set align-items:center and justify-content:center

    .parent {
    display: flex;
    justify-content:center;
    align-items:center;
    }

    21. Flex box 모델 이해

    Flex is a new value added to the CSS display property. Along with inline-flex it causes the element that it applies to in order to become a flex container, and the element's children to each become a flex item. The items then participate in flex layout, and all of the properties defined in the CSS Flexible Box Layout Module may be applied.
    Get your hands on experience in MDN




    22. 응답성 디자인

    Responsive Web design is a website that can be compatible with multiple terminals, instead of making a specific version for each terminal.

    The basic principle is to use media query (@media) query to detect different device screen sizes for processing.

    About compatibility: The page header must have the viewport declared by meta.

    <meta name="’viewport’" content="”width=device-width," initial-scale="1." maximum-scale="1,user-scalable=no”"/>

    23. 포지셔닝 및 부동

    Read the BFC documentation to continue

    A block formatting context is a part of a visual CSS rendering of a web page. It's the region in which the layout of block boxes occurs and in which floats interact with other elements.
    Formatting contexts affect layout, but typically, we create a new block formatting context for the positioning and clearing floats rather than changing the layout, because an element that establishes a new block formatting context will:

    contain internal floats.
    exclude external floats.
    suppress margin collapsing.


    진행 중...
    진행 중...

    좋은 웹페이지 즐겨찾기