CSS-Battle

玩一下 css-battle

Battle #1

#1. Simply Square

在这里插入图片描述`

学习基本玩法,代码区外套body

<div></div>
<style>
  body{
    margin:0;
    background: #5d3a3a;
  }
  div {
    width: 200px;
    height: 200px;
    background: #b5e0ba;
  }
</style>
</style>

#2. Carrom

在这里插入图片描述

绝对定位

<div></div>
<div></div>
<div></div>
<div></div>
<style>
  body{
    padding:0;
    margin:0;
    background: #62374e;
  }
   div{
    width:50px;
    height:50px;
    background:#fdc57b;
    position:absolute;
  }
  div:nth-child(1){
    top:50px;
    left:50px
  }
  div:nth-child(2){
    top:50px;
    right:50px
  }
  div:nth-child(3){
    bottom:50px;
    left:50px
  }
  div:nth-child(4){
    bottom:50px;
    right:50px
  }
</style>

grid布局

<div></div>
<div></div>
<div></div>
<div></div> 
<style>
  body{
    margin:0;
    background:#62374e;
    display:grid;/* 网格布局 */
    grid-template-rows: 150px 150px;/* 设置每行高度 */
    grid-template-columns: 150px 150px;/* 设置每列宽度 */
    justify-items: center;/* 基于网格居中 */
    align-items: center;/* 基于网格居中 */
    column-gap:100px;/* 设置列间距 */
  }
  div {
    width: 50px;
    height: 50px;
    background: #fdc57b;
  }
</style>

#3. Push Button

在这里插入图片描述

<div class="box">
  <div class="out">
    <div class="mid">
      <div class="inner"></div>
    </div>
  </div>
</div>
<style>
  body{
    margin:0;
    background:#6592CF;
    display:flex;
    position:relative;
  }
  .box{
    margin:auto;
    width: 300px;
    height: 150px;
    background: #243D83;
  }
  /* 超出父元素,相对定位居中 */
  .out{
    position:relative;
    display:flex;
    left:50%;
    top:50%;
    transform: translate(-50%,-50%);
    background:#6592CF;
    width:250px;
    height:250px;
    border-radius:125px;;
  }
  .mid{
    margin:auto;
    background:#243D83;
    width:150px;
    height:150px;
    border-radius:75px;
    display:flex;
  }
  .inner{
    margin:auto;
    background:#EEB850;
    width:50px;
    height:50px;
    border-radius:25px;
  }
</style>

#4. Ups n Downs

在这里插入图片描述

定位

<div></div>
<div></div>
<div></div>
<style>
  body{
    background:#62306D
  }
  div {
    width: 100px;
    height: 100px;
    background: #F7EC7D;
    border-radius:50% 50% 0 0;
    position:absolute;
  }
  div:nth-child(odd){
    bottom:50px;
    transform:rotate(180deg)
  }
  div:nth-child(1){
    left:50px
  }
   div:nth-child(2){
     left:150px;
     bottom:150px;
   }
  div:nth-child(3){
    right:50px
  }
</style>

使用伪类

<div></div>
<style>
  body{
    margin:0;
    background:#62306D;
    position:relative;
  }
  div {
    position:absolute;
    left:50%;
    top:50%;
    transform: translate(-50%,-100%);
    width: 100px;
    height: 100px;
    border-radius: 50px 50px 0 0;
    background: #F7EC7D;
  }
  /* 默认为inline,必须设置content */
  div::before,div::after{
    position:absolute;
    content:"";
    display:block;
    width: 100px;
    height: 100px;
    border-radius:0 0 50px 50px;
    background: #F7EC7D;
    top:100%;
  }
  div::before{
    right:100%;
  }
  div::after{
    left:100%;
  }
</style>

#5. Acid Rain

在这里插入图片描述

<div class="box1"></div>
<div class="box2"></div>
<style>
  body{
    background:#0B2429;
  }
  .box1,.box2{
    background:#998235;
    position:absolute;
    left:50%;
    top:50%;
    transform: translate(-50%,-50%);
  }
  .box1,.box2,.box1::before{
    width: 120px;
    height: 120px;
    border-radius: 50% 0 50% 50%;
  }
  .box1::before{
    background:#F3AC3C;
    content:'';
    display: block;
    transform: translate(-50%,50%);
  }
  .box2{
     background:#F3AC3C;
     border-radius:50%;
     transform: translate(0,-100%);
     z-index:-1
  }
</style>

#6. Missing Slice

在这里插入图片描述

菜鸟尝试

<div id="outer">  
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</div>
<style>
  body{
    background:#E3516E;
  }
  div{
    position:absolute;
    left: 50%;
    top:  50%;
    transform:translate(-50%,-50%);
  }
  #outer {
    width: 200px;
    height: 200px;
    border-radius:50%;
    overflow:hidden;
  }
  #outer div{
    width:100px;
    height:100px;
  }
  .box1{
    background:#51B5A9;
    transform:translate(-100%,-100%);
  }
  .box2{
    background:#FADE8B;
    transform:translate(0,-100%);
  }
  .box3{
    background:#F7F3D7;
    transform:translate(-100%,0);
  }
</style>

参考大佬

<div>
  <span></span>
  <span></span>
  <span></span>
</div>
<style>
  body{
    background:#E3516E;
  }
  div{
    position:absolute;
    left: 50%;
    top:  50%;
    transform:translate(-50%,-50%);
    width:200px;
    height:200px;
    display:flex;
    flex-wrap:wrap;
    border-radius:50%;
    overflow:hidden;
  }
  span{
    width:100px;
    height:100px;
  }
  span:nth-child(1){
    background:#51B5A9
  }
  span:nth-child(2){
    background:#FADE8B
  }
  span:nth-child(3){
    background:#F7F3D7
  }

</style>

#7. Leafy Trail

在这里插入图片描述

自己写的,有瑕疵,百分比不够精确

<div></div>
<div></div>
<div></div>
<style>
  body{
    background:#0B2429;
  }
  div {
    width: 150px;
    height: 150px;
    background: #998235;
    position:absolute;
    left:50%;
    top:50%;
    transform:translate(-50%,-50%);
    border-radius:66% 0 66% 0;
  }
  div:nth-child(1){
  background:#1A4341;
  transform:translate(-83%,-50%);
  }
  div:nth-child(3){
  background:#F3AC3C;
  transform:translate(-17%,-50%);
  }
</style>

参考大佬

<div></div>
<div></div>
<div></div>
<style>
  body{
    background:#0B2429;
  }
  div {
    width: 150px;
    height: 150px;
    background: #998235;
    position:absolute;
    left:50%;
    top:50%;
    transform:translate(-50%,-50%);
    border-radius:100px 0 100px 0;
  }
  div:nth-child(1){
  background:#1A4341;
  transform:translate(-125px,-50%);
  }
  div:nth-child(3){
  background:#F3AC3C;
  transform:translate(-25px,-50%);
  }
</style>

#8. Forking Crazy

在这里插入图片描述



CSS-in-CSS是一种前端开发技术,也被称为CSS嵌套或CSS模块化。它的主要思想是将CSS样式定义在组件级别,使得样式与组件的逻辑和结构紧密关联,提高代码的可维护性和可重用性。 在传统的CSS开发中,样式是全局共享的,容易造成样式冲突和难以维护。而CSS-in-CSS通过将样式封装在组件内部,实现了样式的局部作用域。这样一来,每个组件都可以拥有自己独立的样式规则,不会相互干扰。 CSS-in-CSS有多种实现方式,其中比较常见的有以下几种: 1. CSS Modules:CSS Modules是一种使用Webpack等构建工具实现的CSS-in-CSS方案。它通过给每个CSS类名添加唯一的哈希值,实现了样式的局部作用域。在使用时,可以通过import语法引入CSS文件,并在组件中使用对应的类名。 2. CSS-in-JS:CSS-in-JS是一种将CSS样式直接写在JavaScript代码中的方式。它通过使用JavaScript对象来描述样式规则,并在运行时动态生成对应的CSS。常见的CSS-in-JS库有styled-components、Emotion等。 3. CSS Nesting:CSS Nesting是一种提案,旨在原生CSS中实现嵌套样式的语法。它使用类似于Sass的嵌套语法,可以更方便地描述组件内部的样式关系。目前,CSS Nesting还处于实验阶段,尚未得到所有浏览器的广泛支持。 总的来说,CSS-in-CSS是一种将CSS样式与组件紧密结合的开发方式,可以提高代码的可维护性和可重用性。不同的实现方式适用于不同的项目需求和开发环境。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值