css3 自定义变量和动态计算示例
示例1:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> body { /*定义一个变量*/ --index: 3; } div { /*为了看清,添加一个边框*/ border: 1px solid blue; width: 100px; height: 100px; background-color: rgb(calc(255/var(--index)),calc(255/var(--index)),calc(255/var(--index))); } </style> </head> <body> <div></div> <br/> <div></div> </body> </html>
示例2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" style="height: 100%;"> <head> <title></title> <style type="text/css"> body { position: relative; padding: 0px; margin: 0px; border-width: 0px; width: 100%; height: 100%; --leftWidth: 300px; } .left { position: absolute; left: 0px; top: 0px; bottom: 0px; background-color: Red; width: var(--leftWidth); box-sizing: border-box; border:10px solid green; } .right { position: absolute; top: 0px; right: 0px; bottom: 0px; background-color: Yellow; width: calc(100% - var(--leftWidth)); box-sizing: border-box; border:10px solid blue; } </style> </head> <body> <div class="left"> </div> <div class="right"> </div> </body> </html>
桂棹兮兰桨,击空明兮溯流光。