[英]call PHP image as CSS background-image: url
I have the following code in my html markup : 我的html标记中包含以下代码:
<div class="profile-img">
<img src="<?php echo $user_meta['profilepicture'][0]; ?>" />
</div>
This code generates a profile image or avatar. 此代码生成个人资料图片或头像。
I'd like to use it into CSS as a background image. 我想将其用作CSS作为背景图像。
So this is what I am trying to use : 所以这就是我要使用的:
<div class="profile-img">
<style type="text/css">
.profile-img img { background-image: url('<?php echo $user_meta['profilepicture'][0]; ?>'); }
</style>
</div>
My CSS then looks like this : 我的CSS如下所示:
.profile-img {
width: 90px;
height: 90px;
margin-left: auto;
margin-right: auto;
text-align: center;
padding-top: 15px;
}
.profile-img img {
width: 80px;
height: 80px;
padding: 5px !important;
background: #fff !important;
border: none !important;
border-radius:500px;
-moz-border-radius:500px;
-webkit-border-radius:500px;
background-position: center center;
}
What I am trying to achieve is a round image thumbnail - avatar with proportionally cropped image. 我要实现的是圆形图像缩略图-具有按比例裁剪图像的头像。
But it seems that the code I am trying to use to call the image into CSS doesn't do the trick. 但是似乎我试图用来将图像调用到CSS中的代码并不能解决问题。
Where am I going wrong here ? 我在哪里错了?
You have to add this rule removing background-image
property that will be parsed via php. 您必须添加此规则,以删除将通过php解析的
background-image
属性。
.profile-img { background-image: url('https://www.gravatar.com/avatar/4ee102e4ae1b9ab69077f7c471365f69?s=128&d=identicon&r=PG&f=1'); background-repeat: no-repeat; background-position: 50%; border-radius: 50%; width: 100px; height: 100px; }
<div class="profile-img"></div>
So your final html addition will be: 因此,您最终的html添加将是:
<div class="profile-img" style="background-image: url('<?php echo $user_meta['profilepicture'][0]; ?>');"></div>
If you want the image to be a background-image and not part of an IMG tag, and you need it to be dynamic, then you'll need something more like this: 如果您希望图像是背景图像而不是IMG标签的一部分,并且需要动态化,那么您将需要更多类似以下内容:
<div class="profile-img" style="background:url('<?php echo $user_meta['profilepicture'][0]; ?>') no-repeat center center #ffffff"></div>
This will place the image as a background within the .profile-img DIV. 这会将图像作为.profile-img DIV中的背景。 Then you'd style just your DIV:
然后,您只需设置DIV的样式即可:
.profile-img {
width: 90px;
height: 90px;
margin: 0 auto;
text-align: center;
padding: 5px !important;
border: none !important;
-moz-border-radius:50%;
-webkit-border-radius:50%;
border-radius:50%; }
To be honest, I haven't checked your CSS as I'm not sure what you are trying to accomplish beyond the circle shape. 老实说,我没有检查您的CSS,因为我不确定您要完成的工作超出了圆形。 I changed your border-radius to 50% as that's what'll create a circle.
我将边框半径更改为50%,因为这将创建一个圆。
Hope this helps 希望这可以帮助
<script>
var imageSrc = "<?php echo $user_meta['profilepicture'][0]; ?>";
$('.profile-img').css('background-image',imageSrc);
</script>
Im not sure, but a while back I used some css to have round corners for an webapp. 我不确定,但是不久前我用一些CSS为webapp设置了圆角。 Perhaps this will help you:
也许这会帮助您:
.myImg img{
-webkit-border-radius: 30px 30px 30px 30px;
-moz-border-radius: 30px 30px 30px 30px;
border-radius: 30px 30px 30px 30px;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.