CSS媒体查询和flex实现自适应多栏布局

效果

CSS媒体查询和flex实现自适应多栏布局_第1张图片

代码


<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>flexlayouttitle>
  <style type="text/css">
    * {
      margin: 0;
      padding: 0;
    }
    body {
      display: flex;
      flex-direction: column;
      min-height: 100vh;
    }
    .container {
      flex: 1;
      display: flex;
    }
    footer {
      height: 40px;
      background: #dddddd;
    }

    nav, aside {
      width: 200px;
      min-width: 200px;
      max-width: 200px;
      height: auto;
      background: yellowgreen;
    }
    main {
      flex: 1;
      background: red;
    }
    @media (max-width: 1000px) {/*宽度不大于1000px时, 隐藏右边侧栏*/
      aside {
        display: none;
      }
    }
    @media (max-width: 768px) {/*宽度不大于768px时, 排列方式改为纵向*/
      .container {
        flex-direction: column;
      }
      nav, aside {
        width: 100%;
        min-width: 100%;
        max-width: 100%;
      }
      main {
        height: 90vh;
      }
    }
  style>
head>

<body>
  <div class="container">
    <nav>
      <div>
        <a href="#">link1a>
      div>
    nav>
    <main>
      <div class="block1">
        this is block1
      div>
      <div class="block2">
        this is block2
      div>
    main>
    <aside>
      <div class="side1">side1div>
      <div class="side2">side2div>
    aside>
  div>
  <footer>
    copyrigtht C <a href="#">xuana>
  footer>
body>

html>

你可能感兴趣的:(CSS,flex,css,自适应布局)