Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

css 布局实用篇 #37

Open
topcss opened this issue Jul 30, 2019 · 0 comments
Open

css 布局实用篇 #37

topcss opened this issue Jul 30, 2019 · 0 comments
Labels

Comments

@topcss
Copy link
Owner

topcss commented Jul 30, 2019

/* CSS3 */
selector::before
/* CSS2 */
selector:before

CSS3引入了 ::(两个冒号)是用来区分伪类(:一个冒号)和伪元素(::两个冒号)。

伪类:操作元素本身,如 :hover、:first-child、:focus等等。
伪元素:操作元素的子元素,如 ::before、::after、::content等等。

居中对齐,上下固定,中间自适应的例子

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style type="text/css">
      html,
      body {
        height: 100%;
        margin: 0;
        padding: 0;
      }

      .flex-container {
        height: 100%;
        display: flex;
        flex-direction: column;
      }

      .flex-item {
        width: 100%;
        height: 100px;
        background-color: cadetblue;
      }
      .flex-item:nth-child(1),
      .flex-item:nth-child(3) {
        /* 即如果存在剩余空间,也不放大。 */
        flex-grow: 0;
      }

      .flex-item:nth-child(2) {
        /* 将等分剩余空间 */
        flex-grow: 1;
        background-color: bisque;

        display: flex;
      }
      .content {
        /* 在弹性子元素上设置margin: auto,可以使得该弹性子元素在两个轴方向上完全居中。 */
        margin: auto;
        height: 300px;
        width: 400px;
        background-color: burlywood;
      }

      /* 添加注释 */
      .flex-item:nth-child(1)::after,
      .flex-item:nth-child(3)::after {
        content: '高度 100 px';
      }
      .flex-item:nth-child(2)::after {
        content: '高度 自适应。又是另一个flex布局的容器';
      }
      .content::after{
        content: '居中对齐'
      }
    </style>
  </head>
  <body>
    <div class="flex-container">
      <div class="flex-item"></div>
      <div class="flex-item">
        <div class="content"></div>
      </div>
      <div class="flex-item"></div>
    </div>
  </body>
</html>
  1. CSS :befor :after 伪元素的妙用
  2. css flex弹性布局学习总结
@topcss topcss added the CSS label Jul 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant