Home Web Front-end HTML Tutorial div+css3绘制基本图形 - 白色的海

div+css3绘制基本图形 - 白色的海

May 20, 2016 pm 01:49 PM

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

这些图形的绘制用到了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>
Copy after login

css:

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

呈现:

 

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>
Copy after login

css:

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

呈现:

 

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>
Copy after login

css:

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

呈现:

 

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>
Copy after login

css:

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

呈现: 

 

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>
Copy after login

分解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>
Copy after login

呈现后发现很有意思,两条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>
Copy after login

是不是各个边都露出三角形了,要的形状就出来了,这就是有趣的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>
Copy after login

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

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

如果是直角,就把左或右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>
Copy after login

css:

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

呈现: 

 

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>
Copy after login

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>
Copy after login

呈现:

 

Statement of this Website
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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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 &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

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 &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

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.

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 &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

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 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.

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 &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles