search
HomeWeb Front-endHTML Tutorial用CSS3实现带小三角形的div框(不用图片)_html/css_WEB-ITnose

现在看到了很多带小三角形的方框,如微信、Mac版的QQ、QQ空间的时间轴等等,在聊天或者是发表的状态的内容外面都有一个带小三角形的矩形框包围着,感觉看着很不错,于是决定亲自动手写一个,我上次用的是偏移背景图片法,那么今天就不用图片,用CSS3实现一下,下面我们来看一下实现代码。

首先我们来看一下CSS3实现三角形原理:其实就是对于transparent的应用

假如html代码是这样的

<div class="arrow-up">     <!--向上的三角--></div><div class="arrow-down">    <!--向下的三角--></div><div class="arrow-left">    <!--向左的三角--></div><div class="arrow-right">    <!--向右的三角--></div>

下面用CSS3分别实现向上、下、左、右的三角形

/*箭头向上*/.arrow-up {    width:0;     height:0;     border-left:30px solid transparent;    border-right:30px solid transparent;    border-bottom:30px solid #fff;}     /*箭头向下*/.arrow-down {    width:0;     height:0;     border-left:20px solid transparent;    border-right:20px solid transparent;    border-top:20px solid #0066cc;}     /*箭头向左*/.arrow-left {    width:0;     height:0;     border-top:30px solid transparent;    border-bottom:30px solid transparent;     border-right:30px solid yellow; }   /*箭头向右*/.arrow-right {    width:0;     height:0;     border-top:50px solid transparent;    border-bottom: 50px solid transparent;    border-left: 50px solid green;}

ok代码整理在一起后,效果大概是这样的:

 

好了原理我们了解了,那么我们就实战一下吧,来实现带小三角形的div框。

 

首先,写出html代码:

<div class="entry">    <div class="entry-trangle"><!--本Div只来实现三角形,无其他作用--></div>    hello,我出生了<br/>    hello,我出生了<br/>    hello,我出生了<br/>    hello,我出生了<br/></div>

挂载有类"entry-trangle"的div只用来实现小三角形。对这个div用css3的transparent实现三角形,然后绝对定位、设置z-index:-1;、margin-left:-19px;,看下面css代码:

<style type="text/css"> *{margin:0;padding:0;} body{ background:#666; font:14px/20px "Microsoft YaHei"; } .entry{ margin:0 auto; margin-top:20px; width:280px;  background:#fff; padding:10px;  /*设置圆角*/ -webkit-border-radius:5px; -moz-border-radius:5px; border-radius:5px; } .entry-trangle{ position:absolute; margin-left:-19px; width:0; height:0; border-top:10px solid transparent; border-bottom:10px solid transparent; border-right:10px solid #fff; z-index:-1; }</style>

border-radius:5px;用来实现圆角;绝对定位、z-index:-1;的目的都是使控制小三角形的div在最底层,不影响父级div里面的内容布局。

好了,我们已经完工了。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Give an example of an HTML tag with an attribute.Give an example of an HTML tag with an attribute.May 16, 2025 am 12:02 AM

The usage methods of HTML tags and attributes include: 1. Basic usage: Use tags such as and, and add necessary information through attributes such as src and href. 2. Advanced usage: Use data-* custom attributes to achieve complex interactions. 3. Avoid common mistakes: Make sure the property values ​​are surrounded by quotes. 4. Performance optimization: Keep it simple, use standard attributes and CSS class names to ensure that the image has alt attributes. Mastering these will improve web development skills.

What is the difference between an HTML tag and an HTML attribute?What is the difference between an HTML tag and an HTML attribute?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.