search
HomeWeb Front-endHTML Tutorialdiv+css3绘制基本图形 - 白色的海

基本图形包括:矩形、圆角矩形、圆形、椭圆形、三角形、值线、弧

这些图形的绘制用到了CSS圆角属性,不考虑IE8。

下面的实现在chrome浏览器运行通过。

 

1.矩形

比较简单,通过CSS设置宽度、高度、背景色即可。

html:

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="rectangle"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span>

css:

<span style="color: #000000;">        .rectangle {
            width: 150px;
            height: 100px;
            background-color: orangered;
        }</span>

呈现:

 

2.圆角矩形

在矩形设置的基础上,增加圆角属性设定,这里用的单位是百分比,好处是可适应整体大小的变化而变化。

html:

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">='rounded-rectangle'</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span>

css:

<span style="color: #000000;">        .rounded-rectangle {
            width: 150px;
            height: 100px;
            background-color: orangered;
            <span style="background-color: #ffff00;">border-radius: 10%;</span>
        }</span>

呈现:

 

3.圆

设置为正方形,将圆角设成50%即可,其实就是圆角的半径为正方形的的半径。

html:

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">='circle'</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span>

css:

<span style="color: #000000;">        .circle {
            width: 100px;
            height: 100px;
            background-color: orangered;
            <span style="background-color: #ffff00;">border-radius: 50%;</span>
        }</span>

呈现:

 

4.椭圆

在圆形的基础上,将正方形设置成矩形即可。

html:

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">='ellipse'</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span>

css:

<span style="color: #000000;">        .ellipse {
            width: 150px;
            height: 100px;
            background-color: orangered;
            border-radius: 50%;
        }</span>

呈现: 

 

5.三角形

乍一看三角形这个样子,还真是无从下手,没有什么现成的方法一步到位的完成,绘制它需要用到border的特性,这个很有意思。

html:

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">='triangle'</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span>

分解1

现在我们来看一下有趣的border,做一个正方形,宽高都设成100px,设定四个边的border的宽度为10px,每条边设置不同的颜色。

<span style="color: #000000;">        .triangle{
            width: 100px;
            height: 100px;
            border-style: solid;
            background-color: orangered;
            border-top-color: red;
            border-right-color: green;
            border-bottom-color: blue;
            border-left-color:blueviolet;
            <span style="background-color: #ffff00;">border-top-width: 10px;</span>
            <span style="background-color: #ffff00;">border-bottom-width: 10px;</span>
            <span style="background-color: #ffff00;">border-left-width: 10px;</span>
            <span style="background-color: #ffff00;">border-right-width: 10px;</span>
        }</span>

呈现后发现很有意思,两条border边的交界处是斜角边,

分解2

继续,将各条边的宽度放大,将正方形宽高都设成0px,将每条边的border的宽度都设成50px(原正方形宽度或高度的一半)

<span style="color: #000000;">        .triangle{
            <span style="background-color: #ffff00;">width: 0px;</span>
            <span style="background-color: #ffff00;">height: 0px;</span>
            border-style: solid;
            background-color: orangered;
            border-top-color: red;
            border-right-color: green;
            border-bottom-color: blue;
            border-left-color:blueviolet;
            border-top-width: <span style="background-color: #ffff00;">50px</span>;
            border-bottom-width: <span style="background-color: #ffff00;">50px</span>;
            border-left-width: <span style="background-color: #ffff00;">50px</span>;
            border-right-width: <span style="background-color: #ffff00;">50px</span>;
        }</span>

是不是各个边都露出三角形了,要的形状就出来了,这就是有趣的border。

  

分解3

最后一步就简单了,把不需要的边都透明掉,只留下底边,并且透明掉背景。

<span style="color: #000000;">        .triangle{
            width: 0px;
            height: 0px;
            border-style: solid;
            background-color: <span style="background-color: #ffff00;">transparent</span>;
            border-top-color: <span style="background-color: #ffff00;">transparent</span>;
            border-right-color: <span style="background-color: #ffff00;">transparent</span>;
            <span style="background-color: #ffff00;">border-bottom-color: blue;</span>
            border-left-color:<span style="background-color: #ffff00;">transparent</span>;
            border-top-width: 50px;
            border-bottom-width: 50px;
            border-left-width: 50px;
            border-right-width: 50px;
        }</span>

透明掉各边和背景后,需要的三角形就出来了,很有趣。

如果要使他变成钝角,就把底边的宽度变小,如果是锐角,就增加宽度。

如果是直角,就把左或右border的宽度设成0px。

各种三角形可通过各边的宽度值的调整来实现。 

 

6.直线

直线就比较简单,压缩高度或宽度就变成了直线。

html:

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">='line'</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span>

css:

<span style="color: #000000;">        .line{
            width: 100px;
            <span style="background-color: #ffff00;">height: 3px;</span>
            background-color: orangered;
        }</span>

呈现: 

 

7.弧

本质上是利用圆角来实现,现在需要把矩形的左上角的圆角绘制成弧形,那么把右边和底边border的宽度设成0px,让他们不可见,设置左上角圆角的半径,让其变大,看得明显些,其余的圆角半径全都设成0px,这样一个弧形就完成了。

html:

<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">='arc'</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span>

css:

<span style="color: #000000;">        .arc {
            width: 100px;
            height: 100px;
            border-style: solid;
            border-top-width: 10px;
            <span style="background-color: #ffff00;">border-bottom-width: 0px;</span>
            border-left-width: 10px;
            <span style="background-color: #ffff00;">border-right-width: 0px;</span>
            border-top-color: blue;
            border-bottom-color: red;
            border-left-color: red;
            border-right-color: red;
            background-color: <span style="background-color: #ffff00;">transparent</span>;
            border-top-right-radius: 0px;
            <span style="background-color: #ffff00;">border-top-left-radius: 100px;</span>
            border-bottom-right-radius: 0px;
            border-bottom-left-radius: 0px;
        }</span>

呈现:

 

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
What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

What is the purpose of the <iframe> tag? What are the security considerations when using it?What is the purpose of the <iframe> tag? What are the security considerations when using it?Mar 20, 2025 pm 06:05 PM

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

What is the viewport meta tag? Why is it important for responsive design?What is the viewport meta tag? Why is it important for responsive design?Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools