2017-06-02 39 views
1

我堆栈与CSS问题。 我想在一个div我的照片中心(红色)的尺寸股利的50%,也浑圆......img中心,圆和中心在一个div

这里是我的html代码:

<ion-content class="masters"> 
    <ion-row> 
    <div class="profil-img"> 
     <img src="../../assets/img/tennis-club.jpeg" alt=""> 
    </div> 
    </ion-row> 
</ion-content> 

这里是我的CSS代码:

.profil-img img{ 
    border-radius: 50%; 
    border: 3px solid white; 
    // position:absolute; 
    max-width: 50%; 
    max-height: 50%; 
    margin-left: auto; 
    margin-right: auto; 
    vertical-align: middle; 
} 

.profil-img{ 
border: 2px solid black; 
} 

这里的结果: enter image description here

感谢您的帮助!

回答

1

对于完美的圆形图像,您必须为图像指定相同的宽度和高度。

.profil-img img{ 
    border-radius: 50%; 
    border: 3px solid white; 
    display: inline-block; 
    height:200px; 
    width: 200px; 
    margin: 0 auto; 
    vertical-align: middle; 
} 

如果你想要的形象被垂直和水平居中,你可以尝试以下方法:

.profil-img img{ 
    border-radius: 50%; 
    border: 3px solid white; 
    display: inline-block; 
    position:relative; 
    top: 50%; 
    left: 50%; 
    -webkit-transform: translate(-50%, -50%); 
    -moz-transform: translate(-50%, -50%); 
    -ms-transform: translate(-50%, -50%); 
    -o-transform: translate(-50%, -50%); 
    transform: translate(-50%, -50%); 
    height:200px; 
    width: 200px; 
} 

请让我知道,如果它为你工作。

enter image description here

+0

谢谢回答@SreenathPG!所以,如果我指定相同的宽度和高度,图像是完美的圆形,谢谢你。但实际上,图片比我的div大(我能给出我的div大小的百分比吗?并且仍然有一个完美的圆圈?),其次,使用此代码,我的图片堆叠在左侧。如果您有想法/解决方案..谢谢 – YLM

+0

是的,你可以给出该div的宽度的50%。如果你可以发布一个工作小提琴,我可以帮你。至少你能给我链接图像 – SreenathPG

+0

我编辑你的文章与屏幕 – YLM

2

我已经尝试了很多东西,现在我有一个解决方案。
这能解决你的问题吗?

.red { 
 
    position: relative; 
 
    width: 250px; 
 
    height: 250px; 
 
    background-color: #f00; 
 
    border: 2px solid #000; 
 
} 
 
.profil-img { 
 
    position: relative; 
 
    width: 50%; 
 
    height: 50%; 
 
    top: 50%; 
 
    left: 50%; 
 
    border-radius: 50%; 
 
    transform: translateY(calc(-50% - 3px)) translateX(calc(-50% - 3px)); 
 
    border: 3px solid #fff; 
 
    overflow: hidden; 
 
} 
 
.profil-img > img { 
 
    display: block; 
 
    position: absolute; 
 
    width: 100%; 
 
    min-height: 100%; 
 
    top: 50%; 
 
    left: 50%; 
 
    -ms-transform: translateY(-50%); 
 
    -webkit-transform: translateY(-50%); 
 
    transform: translateY(-50%) translateX(-50%); 
 
}
<div class="red"> 
 
    <div class="profil-img"> 
 
    <img src="https://cdn.transportbox-katzen.de/wp-content/uploads/2016/03/katze-gewohnheitstier.jpg" alt=""> 
 
    </div> 
 
</div>

+0

太棒了!这项工作,但因为我在一个离子2应用程序,我不使用'class =“红色''我有一个离子网格..所以现在唯一的问题是,我的图片是一个完美的圆圈。@UfguFufullu – YLM

+0

可能因为我的图像没有相同的高度和宽度..你有什么想法我可以把它变成圆形,无论高/宽...? @UfguFufullu – YLM

+1

'.profil-img'类将始终具有与父级相同的宽度 - 高度比率(在本例中,像'.red')。正如你想要的那样,'.profil-img'获得了父'div'的宽度和高度的一半。如果你想得到另一个比例,你必须改变父母的宽度或身高,或者两者都是@YLM – UfguFugullu