基於CSS3實現元素寬度向兩側延伸

效果圖

在這裏插入圖片描述

方法一:使用轉換中的角度轉換

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>標題</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style type="text/css">
        nav{
            width: 200px; height: 40px; 
            background: #363636; margin: 50px auto; 
            display:flex; justify-content: space-between;
        }
        div{
            width: 100px; height: 40px;
            position: relative;;
        }
        a{
            display: block; width: 100px; height: 40px; color: #fff; 
            line-height: 40px; text-align: center;
            position: absolute; top: 0; left: 0;
        }
        div:hover a{
            background: red; color:#fff
        }
        span{
            width: 100px; height: 5px; background: red;
            position: absolute; top: -5px; left: 0;
            transform: rotateY(90deg);
        }
        div:hover span{
            transform: rotateY(0deg);
            transition: transform 0.5s;
        }
    </style>
</head>
<body>
    <nav>
        <div>
            <a href="#">首頁</a>
            <span></span>
        </div>
        <div>
            <a href="#">論壇</a>
            <span></span>
        </div>
    </nav>
</body>
</html>

方法二:使用轉換中的縮放轉換

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>標題</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style type="text/css">
        nav{
            width: 200px; height: 40px; background: #363636;
            margin: 50px auto; display: flex; justify-content: space-between;
        }
        div{
            width: 100px; height: 40px; position: relative;
        }
        a{
            display: block; width: 100px; height: 40px;
            line-height: 40px; text-align: center; color: #fff;
        }
        span{
            width: 10px; height: 5px;
            position: absolute; top: -5px; left: 50%;
            margin-left: -5px;
        }
        div:hover a{
            background: red; color: #fff;
        }
        div:hover span{
            transform: scaleX(10); background: red;
            transition: transform 0.5s;
        }
    </style>
</head>
<body>
    <nav>
        <div>
            <a href="#">首頁</a>
            <span></span>
        </div>
        <div>
            <a href="#">論壇</a>
            <span></span>
        </div>
    </nav>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章