如何仅使用纯 CSS 来制作编辑图标样式?

How can I make the edit icon styling using only pure CSS?

我想用纯CSS来设计上图。

到目前为止,我只能在没有编辑图标的情况下进行设计。

以下是我目前想出的HTML和CSS:

<div>
    <img src="static\assets\images\avatar.jpg" class="main-profile-img" />
</div>

<style>
    .main-profile-img {
        width: 140px;
        height: 140px;
        border-radius: 50%;
        border-style: solid;
        border-color: #FFFFFF;
        box-shadow: 0 0 8px 3px #B8B8B8;
    }
</style>

我只需要右上角编辑图标的 HTML 和 CSS 代码。

您可以尝试在编辑图标中添加position: absolute;。测试代码如下。希望它能解决我们的问题

HTML

<div>
  <img src="static\assets\images\avatar.jpg" class="main-profile-img" />
</div>

<div class="edit-icon">
 <img src="static\assets\images\avatar.jpg"/>
</div>

CSS

 .main-profile-img {
   width: 140px;
   height: 140px;
   border-radius: 50%;
   border-style: solid;
   border-color: #FFFFFF;
   box-shadow: 0 0 8px 3px #B8B8B8;
 }

.edit-icon {
    position: absolute;
    top: 15px;
    left: 120px;
    z-index: 1;
    border-radius: 50%;
}

JS FIddle Link : https://jsfiddle.net/SJ_KIllshot/wh0usc2y/

HTML 代码在这里

<div>
<figure>
    <img src="https://image.shutterstock.com/image-vector/male-avatar-profile-picture-vector-260nw-221431012.jpg" class="main-profile-img" />
    <img class="editIcon" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAkFBMVEX///86Ozs7PDvz8/M7Ozv09PT+/v79/f319fX4+Pg1NjZeXl5RUlJISEgqKys4OTgyMzMuLy8mKCjq6ura2trj4+NMTU1cXFzg4OAhIyO4uLhmZ2cnKSk/QECZmZmlpaXJycmVlZV9fX2urq51dXXCwsLGxsZxcXGLjIxlZmaNjY2qqqqfoKAcHh7R0dGDhISZT5zBAAAUh0lEQVR4nN1d6WKqOhAGF5CoBFCrWO1ij92X93+7mxUhZBeqvfw4zWmHJB8zk2+SDCEI6BVFrUKgKvQva13d6uN2vp/9HGOdLLnimP43qhX4X9oF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60UsBYj+t8HHw/1VgFW+3n6lBIi/19DOl/4jPFidVrxfRU2AdJjhE/nNXr6y+HcAziqADBJJ+2KzKPy/zay58PZHNFgtr72JPhhTPkyqSVkQH0ooBTi6YoA8Kbh8FI+9oV8li5oLv1uYqSu/5kEm3x9jESBdTGDffaqm1YtDKUuEu3KAoHhXH//WBBhP4u9ZKUujuqSJGkRA+baMJ3qA/NtTVO5hjabRrKpqc/FUaF1tkb5kJSIwT2+XQQWQj5vV57TYKJowgFzT3z/zIgPKeq/mAnlBjn1LqhOM+LjJITFW5MeVcjn0/8nz+7rc5RmA5nYucUGQ5cVu+vidNDpeAeSQ4tO/tULE/jx8eHp8mc33l0bTvvbz9cvd9nvY7K908e8/kp8zQV90OwoAAAAASUVORK5CYII=" />
    </figure>
</div>

CSS 代码在这里

figure{
  width: 140px;
  height: 140px;
  position: relative;
}
    .main-profile-img {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 50%;
        border-style: solid;
        border-color: #FFFFFF;
        box-shadow: 0 0 8px 3px #B8B8B8;
    }
    .editIcon{
      position: absolute;
      right: -10px;
      top: 0px;
      width: 20px;
      height: 20px;
      background: #fff;
      border-radius: 100%;
      box-shadow: 0 0 8px 3px #B8B8B8
    }

Fiddle Link

https://jsfiddle.net/Lrgexwc4/1/

与你的 html:

使用伪元素作为:after(我只用font awesome来举例)

.main-profile-img {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  border-style: solid;
  border-color: #FFFFFF;
  box-shadow: 0 0 8px 3px #B8B8B8;
   position: relative;
}

.wrap {
  position: relative;
}

.wrap:after {
font-family: 'Font Awesome 5 Free';
font-weight: 900;
content: "\f044";
position: absolute;
width: 30px;
height: 30px;
border-radius: 50%;
border: 1px solid grey;
top: 0;
left: 113px;
background: white;
color: blue;
align-items: center;
display: flex;
justify-content: center;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">

<div class="wrap">
  <img src="https://i.stack.imgur.com/mSXoO.png" class="main-profile-img" />
</div>

WITH 改变你的 html:

.main-profile-img {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  border-style: solid;
  border-color: #FFFFFF;
  box-shadow: 0 0 8px 3px #B8B8B8;
  position: relative;
}

.wrap {
  position: relative;
}

button {
  position: absolute;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid grey;
  top: 0;
  left: 113px;
  background: white;
  color: blue;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">

<div class="wrap">
  <img src="https://i.stack.imgur.com/mSXoO.png" class="main-profile-img" />
  <button><i class="fas fa-edit"></i></button>
</div>

div {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  border-style: solid;
  border-color: #FFFFFF;
  box-shadow: 0 0 8px 3px #B8B8B8;
  position: relative;
}

div img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
}

div i {
  position: absolute;
  top: 20px;
  right: -7px;
  /* border: 1px solid; */
  border-radius: 50%;
  /* padding: 11px; */
  height: 30px;
  width: 30px;
  display: flex !important;
  align-items: center;
  justify-content: center;
  background-color: white;
  color: cornflowerblue;
  box-shadow: 0 0 8px 3px #B8B8B8;
}
<link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet" />
<div>
  <img src="https://cdn.pixabay.com/photo/2016/08/08/09/17/avatar-1577909_960_720.png" class="main-profile-img" />
  <i class="fa fa-edit"></i>
</div>

inner span用作position:absolute

.user {
  width: 140px;
  height: 140px;
  border-radius: 100%;
  border:2px solid #FFFFFF;
  box-shadow: 0 0 8px 3px #B8B8B8;
  position:relative;
}

.user img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
}

span.icon {
  position: absolute;
  top: 10px;
  right: 0;
  background:#e2e2e2;
  border-radius:100%;
  width:30px;
  height:30px;
  line-height:30px;
  vertical-align:middle;
  text-align:center;
  color:#0000ff;
  font-size:14px;
  cursor:pointer;
}
<link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet" />
<div class="user">
  <img src="https://cdn.pixabay.com/photo/2014/04/03/10/32/businessman-310819_960_720.png" class="main-profile-img" />
  <span class="icon"><i class="fa fa-edit"></i></span>
</div>

-- 简化版--

我们需要相同的铅笔图标,没有边框,并使用一个 HTML 标签。最终结果:

JSfiddle 演示:https://jsfiddle.net/allenski/tx0zyLr8/(适用于 IE11)。

HTML代码:

<div class="edit-pencil"></div>

CSS代码:

.edit-pencil {
    position: relative;
    display: inline-block;
    width: 25px;
    height: 36px;
    vertical-align: middle;
    cursor: pointer;
}
.edit-pencil:before {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-2px, -3px) rotate(-45deg);
    width: 3px;
    height: 5px;
    background-color: #7081a3;
    box-shadow: 1px 0px 0px #7081a3, 2px 0px 0px #7081a3, 3px 0px 0px #7081a3, -1px 0px 0px #7081a3, -2px 0px 0px #7081a3, -3px 0px 0px #7081a3, -3.3px 0px 0px #7081a3, 7px 0px 0px #7081a3;
    transition: all 300ms;
    content: '';
}
.edit-pencil:after {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-9px, 3px) rotate(-45deg);
    font-size: 1px;
    border: solid 3em transparent;
    border-left-width: 0;
    border-right-width: 5em;
    border-right-color: #7081a3;
    transition: all 300ms;
    content: '';
}
.edit-pencil:hover:before {
    background-color: #333333;
    box-shadow: 1px 0px 0px #333333, 2px 0px 0px #333333, 3px 0px 0px #333333, -1px 0px 0px #333333, -2px 0px 0px #333333, -3px 0px 0px #333333, -3.3px 0px 0px #333333, 7px 0px 0px #333333;
}
.edit-pencil:hover:after {
    border-right-color: #333333;
}