最适合网络开发者的网站

CSS 教程

CSS 主页 CSS 简介 CSS 语法 CSS 选择器 CSS 如何 CSS 注释 CSS 颜色 CSS 背景 CSS 边框 CSS 边距 CSS 填充 CSS 高度/宽度 CSS 盒子模型 CSS 大纲 CSS 文本 CSS 字体 CSS 图标 CSS 链接 CSS 列表 CSS 表格 CSS 显示 CSS 最大宽度 CSS 位置 CSS Z 索引 CSS 溢出 CSS 浮动 CSS 内联块 CSS 对齐 CSS 组合器 CSS 伪类 CSS 伪元素 CSS 不透明度 CSS 导航栏 CSS 下拉菜单 CSS 图片库 CSS 图像精灵 CSS Attr 选择器 CSS 表单 CSS 计数器 CSS 网站布局 CSS 单位 CSS 特殊性 CSS !重要 CSS 数学函数

CSS 高级

CSS 圆角 CSS 边框图像 CSS 背景 CSS 颜色 CSS 颜色关键字 CSS 渐变 CSS 阴影 CSS 文本效果 CSS Web 字体 CSS 2D 变换 CSS 3D 变换 CSS 过渡 CSS 动画 CSS 工具提示 CSS 样式图像 CSS 图像反射 CSS 对象适合 CSS 对象位置 CSS 遮罩 CSS 按钮 CSS 分页 CSS 多列 CSS 用户界面 CSS 变量 CSS 盒子尺寸 CSS 媒体查询 CSS MQ 示例 CSS 弹性框

CSS 响应式

RWD 简介 RWD 视口 RWD 网格视图 RWD 媒体查询 RWD 图像 RWD 视频 RWD 框架 RWD 模板

CSS 网格

网格简介 网格容器 网格项

CSS 南苏丹援助协会

SASS 教程

CSS 例子

CSS 模板 CSS 示例 CSS 测验 CSS 练习 CSS 证书

CSS 参考

CSS 参考 CSS 选择器 CSS 函数 CSS 参考 Aural CSS Web 安全字体 CSS 动画 CSS 单位 CSS PX-EM 转换器 CSS 颜色 CSS 颜色值 CSS 默认值 CSS 浏览器支持

初学者的 CSS 课程

尿素

CSS Styling Images


Learn how to style images using CSS.


Rounded Images

Use the border-radius property to create rounded images:


Paris

例子

Rounded Image:

img {
  border-radius: 8px;
}
亲自尝试 »
Paris

例子

Circled Image:

img {
  border-radius: 50%;
}
亲自尝试 »

Thumbnail Images

Use the border property to create thumbnail images.

Thumbnail Image:

Paris

例子

img {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 5px;
  width: 150px;
}

<img src="paris.jpg" alt="Paris">
亲自尝试 »

Thumbnail Image as Link:

Thumbnail Image as Link

例子

img {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 5px;
  width: 150px;
}

img:hover {
  box-shadow: 0 0 2px 1px rgba(0, 140, 186, 0.5);
}

<a href="paris.jpg">
  <img src="paris.jpg" alt="Paris">
</a>
亲自尝试 »

Responsive Images

Responsive images will automatically adjust to fit the size of the screen.

Resize the browser window to see the effect:

Cinque Terre

If you want an image to scale down if it has to, but never scale up to be larger than its original size, add the following:

例子

img {
  max-width: 100%;
  height: auto;
}
亲自尝试 »

Tip: Read more about Responsive Web Design in our CSS RWD Tutorial.


Center an Image

To center an image, set left and right margin to auto and make it into a block element:

Paris

例子

img {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}
亲自尝试 »

Polaroid Images / Cards

Cinque Terre

Cinque Terre

Norway

Northern Lights

例子

div.polaroid {
  width: 80%;
  background-color: white;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

img {width: 100%}

div.container {
  text-align: center;
  padding: 10px 20px;
}
亲自尝试 »

Transparent Image

The opacity property can take a value from 0.0 - 1.0. The lower value, the more transparent:

Forest

opacity 0.2

Forest

opacity 0.5

Forest

opacity 1
(default)

例子

img {
  opacity: 0.5;
}
亲自尝试 »

Image Text

How to position text in an image:

例子

Cingue Terre
Bottom Left
Top Left
Top Right
Bottom Right
Centered

Try it Yourself:

Top Left » Top Right » Bottom Left » Bottom Right » Centered »

Image Filters

The CSS filter property adds visual effects (like blur and saturation) to an element.

Note: The filter property is not supported in Internet Explorer or Edge 12.

例子

Change the color of all images to black and white (100% gray):

img {
  filter: grayscale(100%);
}
亲自尝试 »

Tip: Go to our CSS filter Reference to learn more about CSS filters.


Image Hover Overlay

Create an overlay effect on hover:

例子

Fade in text:

Avatar
Hello World
Try it Yourself »

例子

Fade in a box:

Avatar
John
Try it Yourself »

例子

Slide in (top):

Avatar
Hello World
Try it Yourself »

例子

Slide in (bottom):

Avatar
Hello World
Try it Yourself »

例子

Slide in (left):

Avatar
Hello World
Try it Yourself »

例子

Slide in (right):

Avatar
Hello World
Try it Yourself »

Flip an Image

Move your mouse over the image:

Paris

例子

img:hover {
  transform: scaleX(-1);
}
亲自尝试 »

Responsive Image Gallery

CSS can be used to create image galleries. This example use media queries to re-arrange the images on different screen sizes. Resize the browser window to see the effect:

Cinque Terre
Add a description of the image here
Forest
Add a description of the image here
Northern Lights
Add a description of the image here
Mountains
Add a description of the image here

例子

.responsive {
  padding: 0 6px;
  float: left;
  width: 24.99999%;
}

@media only screen and (max-width: 700px){
  .responsive {
    width: 49.99999%;
    margin: 6px 0;
  }
}

@media only screen and (max-width: 500px){
  .responsive {
    width: 100%;
  }
}
亲自尝试 »

Tip: Read more about Responsive Web Design in our CSS RWD Tutorial.


Image Modal (Advanced)

This is an example to demonstrate how CSS and JavaScript can work together.

First, use CSS to create a modal window (dialog box), and hide it by default.

Then, use a JavaScript to show the modal window and to display the image inside the modal, when a user clicks on the image:

Northern Lights, Norway

例子

// Get the modal
var modal = document.getElementById('myModal');

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
  modal.style.display = "block";
  modalImg.src = this.src;
  captionText.innerHTML = this.alt;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}
亲自尝试 »