calc()函数实现动态布局

calc()函数可以使用calc()给元素的border、margin、pading、font-size和width等属性设置动态值。

calc()能让你给元素的做计算,你可以给一个div元素,使用百分比、em、px和rem单位值计算出其宽度或者高度,比如说“width:calc(50% + 2em)”,这样一来你就不用考虑元素DIV的宽度值到底是多少,而把这个烦人的任务交由浏览器去计算。

 

 

 本文来自于菜鸟教程

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以,我们可以使用flex布局实现同样的效果,不需要使用calc()函数。下面是一个不使用calc()函数的示例代码: ```html <!DOCTYPE html> <html> <head> <style> .container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; } .bird { position: relative; width: 50px; height: 50px; background-color: #ffcc66; border-radius: 50%; animation: bird-fly 2s ease-in-out infinite alternate; } .circle { position: absolute; width: 40px; height: 40px; border: 2px solid #ccc; border-radius: 50%; animation: circle-rotate 4s linear infinite; } .circle1 { top: -150px; right: 50%; transform: translateX(110px); } .circle2 { top: calc(50% - 20px); left: -150px; transform: translateY(-110px); } .circle3 { bottom: -150px; right: 50%; transform: translateX(110px); } @keyframes circle-rotate { from { transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg); } to { transform: rotateX(360deg) rotateY(360deg) rotateZ(360deg); } } @keyframes bird-fly { from { transform: translateY(0) rotateZ(0deg); } to { transform: translateY(-100px) rotateZ(360deg); } } </style> </head> <body> <div class="container"> <div class="bird"></div> <div class="circle circle1"></div> <div class="circle circle2"></div> <div class="circle circle3"></div> </div> </body> </html> ``` 在上面的代码中,我们使用了flex布局实现元素的居中和垂直居中,不需要使用calc()函数计算位置。同时,我们使用了transform属性来调整圆圈的位置和旋转效果,通过设置top、bottom、left、right等属性来调整元素的位置。其他的样式和动画效果与之前的代码相同。 注意:在使用flex布局时,需要确保浏览器支持flex布局,否则可能会出现布局问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值