【问题标题】:How to create Irish flag using just HTML and CSS? [duplicate]如何仅使用 HTML 和 CSS 创建爱尔兰国旗? [复制]
【发布时间】:2020-05-25 22:22:36
【问题描述】:

我使用 HTML 和 CSS 创建了印度国旗(Ashok Chakra 除外)。现在我想创建爱尔兰国旗,它只是垂直三色,而不是像印度国旗那样的水平三色。那么我该如何实现呢?我创建了以下代码。

body {
  background-color: yellow;
  margin-left: 40%;
  margin-right: 40%;
}

.top {
  background-color: orange;
  width: 320px;
  height: 80px;
}

.mid {
  background-color: white;
  width: 320px;
  height: 80px;
}

.botm {
  background-color: green;
  width: 320px;
  height: 80px;
}
<html>

<head>
  <link rel="stylesheet" type="text/css" href="india.css">
  <title>Flag</title>
</head>

<body>
  <div class="top"></div>
  <div class="mid"></div>
  <div class="botm"></div>
</body>

</html>

【问题讨论】:

  • 很多方法来创建 3 列
  • 您真正的问题是如何将 div 放在同一行? 尝试搜索,您会找到数百万篇文章。

标签: html css


【解决方案1】:

如下例所示,带有linear-gradient 或双插入box-shadow 的单个div 就足够了


线性渐变

.irishflag { 
        background: linear-gradient(to right, #169B62 33.33%, #fff 0, #fff 66.66%, #FF883E 0);
        width: 270px;
        height: 135px;
    }
&lt;div class="irishflag"&gt;&lt;/div&gt;

盒子阴影

.irishflag { 
    box-shadow: 90px 0 #169B62 inset, -90px 0 #FF883E inset;
    width: 270px;
    height: 135px;
}
&lt;div class="irishflag"&gt;&lt;/div&gt;

颜色和比例取自wikipedia

【讨论】:

    【解决方案2】:

    只需添加一个带有display:flex 的标志容器;和倒序。像这样:

    body{
      background-color:yellow;
      margin-left:40%;    
      margin-right:40%;
    }
    
    .top{
      background-color:orange;
      width:320px;
      height:80px;
    }
    
    .mid{
      background-color:white;
      width:320px;
      height:80px;
    }
    
    .botm{
      background-color:green;
      width:320px;
      height:80px;
    }
    
    .flag{
      display: flex;
        flex-direction: row-reverse;
    }
      <div class="flag">
        <div class="top"></div>
        <div class="mid"></div>
        <div class="botm"></div>
      </div>

    【讨论】:

    【解决方案3】:

    您可以使用 flexbox 将您的“颜色”排列成一行,如下所示:

    body{
        background-color:yellow;
        margin-left:40%;    
        margin-right:40%;
        display: flex     /* <-- Make children aligned in a row */
    }
    
    .right{
        background-color:orange;
        width:320px;
        height:80px;
    }
    
    .mid{
        background-color:white;
        width:320px;
        height:80px;
    }
    
    .left{
        background-color:green;
        width:320px;
        height:80px;
    }
    <body>
      <div class="left"></div>
      <div class="mid"></div>
      <div class="right"></div>
    </body>

    【讨论】:

      【解决方案4】:

      你可以通过给父div一个显示值flex来获得大部分的方法:

      display: flex;
      

      然后它的所有直接子代一个flex的值,表示每个的宽度应该接近父代宽度的三分之一:

      flex: 1 1 33%;
      

      工作示例:

      .flag {
        display: flex;
        width: 291px;
        height: 180px;
        box-shadow: 6px 6px 6px rgba(0, 0, 0, 0.2);
      }
      
      .third {
        flex: 1 1 33%;
      }
      
      .ireland .left {
        background-color: green;
      }
      
      .ireland .center {
        background-color: white;
      }
      
      .ireland .right {
        background-color: orange;
      }
      <div class="flag ireland">
        <div class="left third"></div>
        <div class="center third"></div>
        <div class="right third"></div>
      </div>

      【讨论】:

      【解决方案5】:

      浮动方式:

       .top {
           background-color:
               orange;
           width: 120px;
           height: 320px;
           float: left;
       }
      
       .mid {
           background-color:
               white;
           width: 120px;
           height: 320px;
           float: left;
       }
      
       .botm {
           background-color:
               green;
           width: 120px;
           height: 320px;
           float: left;
       }
      
       .flag {
           overflow: hidden;
       }
      

      HTML:

        <div class="flag">
          <div class="top"></div>
          <div class="mid"></div>
          <div class="botm"></div>
        </div>
      

      【讨论】:

        猜你喜欢
        • 2018-10-26
        • 1970-01-01
        • 2012-06-28
        • 1970-01-01
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        • 2017-05-11
        • 1970-01-01
        相关资源
        最近更新 更多