Text

带边框的 text-align 例子

  1. <style>
  2. h4 {
  3. text-align: center;
  4. }
  5. p {
  6. text-align: justify;
  7. }
  8. .links {
  9. margin-right: 0px;
  10. border: 1px solid #000;
  11. }
  12. .fullCard {
  13. border: 1px solid #ccc;
  14. border-radius: 5px;
  15. margin: 10px 5px;
  16. padding: 4px;
  17. }
  18. .cardContent {
  19. padding: 10px;
  20. }
  21. </style>
  22. <div class="fullCard">
  23. <div class="cardContent">
  24. <div class="cardText">
  25. <h4>Google</h4>
  26. <p>Google was founded by Larry Page and Sergey Brin while they were Ph.D. students at Stanford University.</p>
  27. </div>
  28. <div class="cardLinks">
  29. <a href="https://en.wikipedia.org/wiki/Larry_Page" target="_blank" class="links">Larry Page</a>
  30. <a href="https://en.wikipedia.org/wiki/Sergey_Brin" target="_blank" class="links">Sergey Brin</a>
  31. </div>
  32. </div>
  33. </div>

width height

调整 width

通过 absolute, relative 或 percentage 的方式,限定 element 的宽度

  1. img {
  2. width: 220px;
  3. }
  4. div.p {
  5. width: 50%;
  6. }

调整 height

通过控制 viewpoint比例

vw : 占 xx% viewpoint’s width

vh : 占 xx% viewpoint’s height

vmin : vw和vh中的 xx% 的较小值

vmax : vw和vh中的 xx% 的较大值

通过控制 viewpoint 的比例来控制element的宽高

全屏

  1. #elem {
  2. height: 100%;
  3. }
  4. -->
  5. #elem {
  6. height: 100vh;
  7. }

配置子元素不受父元素container限制,而受浏览器 viewpoint 控制

  1. #parent {
  2. width: 400px;
  3. }
  4. #child{
  5. /* This is equal to 100% of the parent width, not the whole page. */
  6. width: 100%;
  7. }
  8. -->
  9. #child{
  10. /* 100% viewpoint's width, not the parent's width */
  11. width: 100vw;
  12. }

设置响应垂直居中

  1. #elem{
  2. width: 60vw;
  3. height: 60vh;
  4. margin: 20vh auto;
  5. }

等宽列

  1. .column-2{ float: left; width: 50vw; }
  2. .column-4{ float: left; width: 25vw; }
  3. .column-8{ float: left; width: 12.5vw; }

position

position 布局方式有三种:relative , absolute 以及 fixed

relative

Changing an element’s position to relative does not remove it from the normal flow - other elements around it still behave as if that item were in its default position.

top, bottom,left,right 将控制 normal flow 位置时的item偏移

top:向下偏移

bottom:向上偏移

left:向右偏移

right:向左偏移

absolute

lock the element in place relative to its parent container.

Unlike the relativeposition, this removes the element from the normal flow of the document, so surrounding items ignore it.

absolute,是相对位置下的绝对位置

fixed

Similar to absolute, it also removes the element from the normal flow of the document.

Other items no longer “realize” where it is positioned, which may require some layout adjustments elsewhere.

One key difference between the fixedand absolutepositions is that an element with a fixed position won’t move when the user scrolls.

float

Floating elements are removed from the normal flow of a document and pushed to either the leftor rightof their containing parent element.

margin 调整水平居中

block类型的element配置margin属性值为 auto 可以将 element 的位置渲染为水平居中

img 调整为水平居中

img 默认为inline element,若将其display属性改为block,将margin配置为auto,则也可以使img渲染为水平居中

background

渐变色

background: linear-gradient(gradient_direction, color 1, color 2, color 3, …);

gradient_direction: 默认垂直渐变,单位 xxx deg,正度数为顺时针旋转,负度数为逆时针旋转

渐变条纹

  1. background: repeating-linear-gradient(
  2. 45deg,
  3. yellow 0px,
  4. red 30px,
  5. black 60px,
  6. blue 80px
  7. );

使用图片

  1. body {
  2. background: url(https://i.imgur.com/MJAkxbh.png);
  3. }

动画

animation property

The animation properties control how the animation should behave.

animation-name

sets the name of the animation, which is later used by @keyframesto tell CSS which rules go with which animations.

animation-duration

sets the length of time for the animation.

animation-fill-mode

specifies a style for the element when the animation is not playing (before it starts, after it ends, or both).

forwards : The element will retain the style values that is set by the last keyframe (depends on animation-direction and animation-iteration-count)

backwards : The element will get the style values that is set by the first keyframe (depends on animation-direction), and retain this during the animation-delay period

both : The animation will follow the rules for both forwards and backwards, extending the animation properties in both directions

animation-iteration-count

allows you to control how many times you would like to loop through the animation.

infinite 为一直循环下去

animation-timing-function

controls how quickly an animated element changes over the duration of the animation.

Value Description
linear The animation has the same speed from start to end
ease Default value. The animation has a slow start, then fast, before it ends slowly
ease-in The animation has a slow start
ease-out The animation has a slow end
ease-in-out The animation has both a slow start and a slow end
cubic-bezier The animation goes along the P0->P3 as a cubic bezier curve

A Cubic Bezier curve is defined by four points P0, P1, P2, and P3. P0 and P3 are the start and the end of the curve and, in CSS these points are fixed as the coordinates are ratios. P0 is (0, 0) and represents the initial time and the initial state, P3 is (1, 1) and represents the final time and the final state.

cubic-bezier曲线的路径:P0(0, 0) —> P1(x1, y1) —> P2(x2, y2) —> P3(1, 1)

  1. animation-timing-function: cubic-bezier(x1, y1, x2, y2);

特别值得注意的地方为,x∈[0, 1],y>=0(不一定 <=1)

ease-out,与cubic-bezier对标,相当于

  1. animation-timing-function: cubic-bezier(0, 0, 0.58, 1);

ease-in,与cubic-bezier对标,几乎相当于

  1. animation-timing-function: cubic-bezier(0.5, 0.1, 0.65, 0.6);

@keyframes

the @keyframesrule controls what happens during that animation.

This is done by giving CSS properties for specific “frames” during the animation, with percentages ranging from 0% to 100%.

  • create a class or id, set its property animation-name and other animation properties
  • set @keyframes with class or id
  • set x% animation action

未在 x% 中改变的项,与动作前保持一致

  1. <style>
  2. div {
  3. margin: auto;
  4. height: 40px;
  5. width: 70%;
  6. background: black;
  7. border-radius: 5px;
  8. }
  9. #anim {
  10. animation-name: colorful;
  11. animation-duration: 3s;
  12. }
  13. @keyframes colorful {
  14. 0% {
  15. background-color: blue;
  16. }
  17. 50% {
  18. background-color: red;
  19. }
  20. 100% {
  21. background-color: yellow;
  22. }
  23. }
  24. </style>
  25. <div id="anim"></div>

通过 top,bottom,left,right 可以控制在动画中的位置变化

通过 opacity 可以通知在动画中的透明度

响应式设计

创建media

  1. @media (max-width: 100px) { /* CSS Rules */ }
  2. @media (min-height: 350px) { /* CSS Rules */ }
  1. p {
  2. font-size: 20px;
  3. }
  4. /* Add media query below */
  5. @media (max-height: 800px) {
  6. p {
  7. font-size: 10px;
  8. }
  9. }

图像响应式

  1. img {
  2. max-width: 100%;
  3. display: block;
  4. height: auto;
  5. }

max-width 设置为 100% 使图像填充置container宽度

display 设置为 block 使图像由 inline 类型element 变为 block 类型element

height 设置为 auto,可以保证展示的图像按照原始图像的长宽比