Html 如何为div着色一半蓝色,一半黄色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39739270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:51:09  来源:igfitidea点击:

How to color a div half blue, half yellow?

htmlcssbackground-color

提问by TSR

Please, help me to find the easiest way to achieve this result with just one single div?

请帮我找到仅用一个 div 即可实现此结果的最简单方法?

<div></div>

enter image description here

在此处输入图片说明

回答by Ahs N

You can do this:

你可以这样做:

Here is the JSFiddledemo

这是JSFiddle演示

Snippet Example

片段示例

 div{
     width:400px;
     height:350px;
     background: linear-gradient(to right, blue 50%, yellow 50%);
    }
<div></div>

回答by Jainam

Try this code:

试试这个代码:

div {
  height: 200px;
  width: 400px;

background: blue; /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(left, blue 50% , yellow 50%); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(right, blue 50%, yellow 50%); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(right, blue 50%, yellow 50%); /* For Firefox 3.6 to 15 */
  background: linear-gradient(to right, blue 50% , yellow 50%); /* Standard syntax */
  }
<div></div>

回答by WitVault

Here you go.

干得好。

div {
  width: 400px;
  height: 200px;
  background:yellow;
  }

div::after {
  width:50%;
  height:100%;
  content:"";
  background: blue;
  display:inline-block;
}
<div> 
</div>

回答by KajalTiwari

    #leftHalf {
   background-color: blue;
   width: 50%;
   position: absolute;
   left: 0px;
   height: 100%;
}

#rightHalf {
   background-color: yellow;
   width: 50%;
   position: absolute;
   right: 0px;
   height: 100%;
}

Try with above CSS code

尝试使用上面的 CSS 代码

回答by Suryakant kumar

**Try This**
.container{
                height:120px;
                width:120px;
                border-radius:50%;
                background: linear-gradient(to right, rgb(183,88,206) 50%, rgb(170,61,200) 50%);
                transform: rotateY(0deg) rotate(45deg);
            }

 <html>
     <head>
      <title>Test Box</title>
      <style>
       .container{
        height:120px;
        width:120px;
        border-radius:50%;
        background: linear-gradient(to right, rgb(183,88,206) 50%, rgb(170,61,200) 50%);
        transform: rotateY(0deg) rotate(45deg);
       }
      </style>
     </head>
     <body>
      <div class="container">
      </div>
     </body>
    </html>

回答by Gabbax0r

html:

html:

<div class="blue_yellow"></div>

css:

css:

.blue_yellow {
background: linear-gradient(to left, blue 50%, yellow 50%);
}