css3 - 学习css构建图形时,遇到一个很有意思的现象,具体代码如下
怪我咯
怪我咯 2017-04-17 11:48:31
0
3
417
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>CSS构建图形</title>
    <style type="text/css">
        #circle{
            width: 100px;
            height: 100px;
            background-color: #4285F4;
            border-radius: 50%;/*我的理解是各边长度的百分比*/
            text-align: center;
            line-height: 100px;
            float: left;
        }
        #oval{
            background-color: #4285F4;
            text-align: center;
            line-height: 100px;
            width: 200px;
            height: 100px;
            border-radius: 50%;
            float:left;
        }
        #triangle-up{
            font-size: 12px;
            width: 0;
            height: 0;
            border-left: 50px solid transparent;
            border-right: 50px solid transparent;
            border-bottom: 100px solid red;
            background-color: #4285F4;
            text-align: center;
    
            float: left;
            margin-left: 10px;
        
        }

        #triangle-down{
            text-align: center;
            width: 0;
            height: 0;
            border-left: 50px solid pink;
            border-right: 50px solid pink;
            border-top: 100px solid red;
            float:left;
            margin-left: 10px;


        }
    </style>
</head>
<body>
    <p id="circle">圆形</p>
    <p id="oval">椭圆</p>
    <p id="triangle-up">上三角形</p>
    <p id="triangle-down">下三角形</p>
</body>
</html>

我想在图形中添加文字 在三角形中文字老是下移,原因是啥?多谢

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
阿神

Because you set width:0; the width is 0, the space where the text is not displayed will naturally wrap downwards. You can use pseudo code to achieve this

#triangle-up,
#triangle-down{
    position:relative;
}
#triangle-up::after,
#triangle-down::after{
    content:"上三角形";
    position:absolute;
    left:0;
    right:0;
    margin:auto;
    top:50%;
    transform:translate(0,-50%,0);
}
Peter_Zhu

Because the triangle is just the border of p, and the text is inside the content of p

刘奇

The text within the

tag is aligned with 基线 by default. The baseline here can be regarded as the inner boundary of border.
So 上三角的基线位置在上方,容器区域在上方; 下三角的基线位置在下方,容器区域在下方.

The white-space attribute of text ignores whitespace by default. Display text vertically with width 0

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template