CSS, HTML 碎片式整理(二)

2020-02-27  本文已影响0人  肉桂猿

1. 盒模型
CSS最基础的布局。
keyword - content, padding, border, outline, margin, box-sizing, block box(块级元素), inline box(行内元素)

image

媒体查询 - “响应式设计(Responsive Design)” 是一种让网站针对不同的浏览器和设备“呈现”不同显示效果的策略,这样可以让网站在任何情况下显示的很棒!媒体查询是做此事所需的最强大的工具。


image

content-box (default)
width/height includes only the content, no padding, no border
W3C标准盒模型 宽度=内容宽度(content)+padding + border+ margin

border-box
width/height includes content, padding and border


2. 定位
display: relative, fixed, absolute


3. 浮动
float: left, right - 网页上实现文本环绕图片的效果
clear float

image.png image.png

4. 文字居中

text-align: center

5. 块居中

P.blocktext {
    margin-left: auto;
    margin-right: auto;
    width: 8em;
}
<P class="blocktext">This rather...

6. 垂直居中

For a document that looks like this:

<div class=container3>
  <p>This paragraph…
</div>

the style sheet looks like this:

div.container3 {
    height: 10em;
    position: relative; /* 1 */
} 

div.container3 p {
    margin: 0;
    position: absolute; /* 2 */
    top: 50%; /* 3 */
    transform: translate(0, -50%); /* 4 */
} 

The essential rules are:

another way is using flex:

div.container5 {
  height: 10em;
  display: flex;
  align-items: center;
}

div.container5 p {
  margin: 0;
}

6. 水平居中& 垂直居中

<div class=container4>
  <p>Centered!
</div>
div.container4 {
  height: 10em;
  position: relative;
}

div.container4 p {
  margin: 0;
  background: yellow;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%);
}

The margin-right: -50% is needed to compensate the left: 50%. The left rule reduces the available width for the element by 50%. The renderer will thus try to make lines that are no longer than half the width of the container. By saying that the right margin of the element is further to the right by that same amount, the maximum line length is again the same as the container's width.

When you remove the margin-right: -50% and resize the window again, you'll see that the sentences will be broken already when the window is still twice as wide as the text lines.

Another way is using flex or table:

// flex
div.container6 {
  height: 10em;
  display: flex;
  align-items: center;
  justify-content: center;
}
// table
.parent {
  width: 100%;
  height: 100%;
  display: table;
  text-align: center;
}

.parent > .child {
  display: table-cell;
  vertical-align: middle;
}

refer: A Guide to Flexbox

上一篇 下一篇

猜你喜欢

热点阅读