如何使用:: after放置CSS三角形? [英] How to position a CSS triangle using ::after?

查看:49
本文介绍了如何使用:: after放置CSS三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用:: after创建了一个带有底部箭头的div.这是HTML代码:

I've created a div with a bottom arrow using ::after. Here is the HTML code:

<div class="sidebar-resources-categories">Topics</div>
<div class="text-content">ok ok</div>

这是CSS:

.sidebar-resources-categories{
    height: 50px;
    margin-bottom: 20px;
    background-color: #e8e8e8;
    font-weight: 600;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
}
.sidebar-resources-categories::after{
    content: '';
    position: absolute;
    left: 42%;
    top: 100%;
    width: 0;
    height: 0;
    border-left: 20px solid transparent;
    border-right: 20px solid transparent;
    border-top: 20px solid #e8e8e8;
    clear: both;
}

这是结果:

我希望箭头位于灰色div的最底部.我不想在div和底部箭头之间包含内容.你知道我该怎么做吗?

I would like the arrow to be at the very bottom of the grey div. I don't want to have the content between the div and the bottom arrow. Do you know how I can do that?

推荐答案

只需将 position:relative 添加到父元素 .sidebar-resources-categories

http://jsfiddle.net/matthewabrman/5msuY/

说明::: after元素位置基于其父元素,在您的示例中,您可能具有.sidebar-res ...的父元素,该元素的高度已设置,因此在其下方呈现.添加相对于.sidebar-res ...的位置,可使after元素移动到其父元素的100%,该元素现在变为.sidebar-res ...,因为它的位置设置为相对.我不确定如何解释它,但这是预期的行为.

explanation: the ::after elements position is based off of it's parent, in your example you probably had a parent element of the .sidebar-res... which had a set height, therefore it rendered just below it. Adding position relative to the .sidebar-res... makes the after elements move to 100% of it's parent which now becomes the .sidebar-res... because it's position is set to relative. I'm not sure how to explain it but it's expected behaviour.

详细了解此主题: http://css-tricks.com/absolute-positioning-inside-relative-positioning/

这篇关于如何使用:: after放置CSS三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆