search
HomeWeb Front-endHTML Tutorial<译>用CSS剪切圆形图片_html/css_WEB-ITnose

用CSS剪切圆形图片

在这个教程,我们会介绍一下使用CSS技巧来渲染出圆形的<译>用CSS剪切圆形图片_html/css_WEB-ITnose元素,主要来实现这个效果的CSS属性是border-radius.

尽管对于正方形的图片来实现圆形这个效果是相当简单的,但是对于长方形来说可能需要多一点点工作。

VIEW DEMO

DOWNLOAD SOURCE

正方形图片

一个百分百正方形的img标签要变成圆形的只需要一行CSS代码。这个技巧在正方形图片上使用的最方便。

HTML

 <img  class="circle--square lazy" src="/static/imghwm/default1.png" data-src="woman.png" alt="<译>用CSS剪切圆形图片_html/css_WEB-ITnose" >

CSS

.circular--squareP{    border-radius:50%;}

通过设置img标签的所有的border-radius属性为正方形宽/高的50%,我们就可以把这个img标签变成圆的。

长方形图片

长方形图片会稍微有一点技巧一点。

去渲染一个圆形,必须以圆形图片为基础

要解决这个问题,我们可以通过在img标签外面套一层div,然后我们通过将超过这个外层div的img标签的内容给裁掉来实现。具体的话可以通过将外层div的overflow属性设置为hidden。

为了能够让照片的主题不要被裁掉,我们必须要区别对待水平和垂直方向的图片。

水平方向的图片

HTML

<div>    <img  src="/static/imghwm/default1.png"  data-src="images/brack-obama.png"  class="lazy" / alt="<译>用CSS剪切圆形图片_html/css_WEB-ITnose" ></div>

CSS

.circular--landscape{    display:inline-block;    position:relative;    width:200px;    height:200px;    overflow:hidden;    border-radius:50%;}.circular--landscape img{    width:auto;    height:100%;    margin-left:-50%;}

高度和宽度属性必须要保持一样来确保这个div(.circular--landscape)能够作为正方形渲染起来

除此之外,高度和宽度属性必须要等于或者小于img的高度。这能够确保img元素能够占满外层div而不会多出一部分空白

一般来说,水平方向图片的主题(但不一定)会位于图片的中心位置。为了能够让我们尽量不会把图片的主题裁剪啦,我们可以通过把图片往左移来弥补图片剪切的内容有点偏右的问题。

我们移动img标签的大小是外层div的25%,在这个例子中就是向左50px,我们可以通过设置margin-left的属性来完成设置

margin-left:-50px;

图片的主题会接近图片的水平方向中心的假设并不一定是对的,最好在你选择使用这个技巧的使用把这个假设记住。

垂直方向的图片

HTML

<div>    <img  src="/static/imghwm/default1.png"  data-src="images/woman-portrait.png"  class="lazy" / alt="<译>用CSS剪切圆形图片_html/css_WEB-ITnose" ></div>

CSS

.circular--portrait{    position:relative;    width:200px;    height:200px;    overflow:hidden;    border-radius:50%;}.circular--portrait img{    width:100%;    height:auto;}

对于垂直方向上的图片的主题在垂直方向的中心的假设当然也不适用于每一个垂直方向上的图片。

和水平方向的图片类似,外层div的宽度和高度最好等于垂直方向图片你的宽度,这样的话可以产生最好的效果。

对于垂直方向的图片,我们把宽度设置为100%,高度设置为auto(和水平方向的图片相反)

我们不需要移动这个img元素,因为这张照片的主题就在上方中心位置。

回顾

这个技巧最好适用于正方形的img标签,主题正好位于图片的中心。但是,我们的世界并不是那么完美的,所有如果需求是这样,我们就可以使用div来把长方形img标签变圆。

CSS中用来负责把图片变圆的属性是border-radius,把边的圆角变成高度/宽度的50%就可以产生一个圆。

原文链接:circular-images-css

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

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

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 Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

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.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft