简体   繁体   English

如何在图像的中心点上放置文本?

[英]how to position text on an image's center point?

I want to position text on a image's center point. 我想将文本放置在图像的中心点上。 I've tried this: 我已经试过了:

<span ng-repeat="image in post.postImages" ng-init="image.showDeleteIcon= false" ng-style="{ 'display' : ($index > 3) ? 'none' :'inlne'}" ng-mouseover="image.showDeleteIcon=true" ng-mouseleave="image.showDeleteIcon=false">
  <a id="{{$parent.$index}}{{$index}}" onclick="SetDataGallery(this.id)" ng-href="../upload/post-photos/{{image.attachmentURL}}">
    <img class="img-responsive feed-photo" ng-src="../upload/post-photos/{{image.attachmentURL}}" alt="Photo" style="display:inline;" ng-style="{ 'opacity' : ($index == 3 && post.postImages.length > 4) ? '0.5' : '1' }">
    <a href="#" class="imgDelete" ng-if="post.timelineStrore.hasControl" ng-show="image.showDeleteIcon" title="Delete Photo" ng-click="DeletePostAttachment(post.timelineStrore.id, image.postAttachmentId,'image')">
      <i class="fa fa-close"></i>
    </a>
    <i class ="imgcounter" style="color: #23527c;cursor: pointer !important;font-family:Times, Times New Roman;font-style:normal;font-size:50px;" ng-if="post.postImages.length - 4 > 0 && $index == 3" id="{{$parent.$parent.$index}}{{$index}}" onclick="$('#' + this.id).click();"> + {{post.postImages.length - 4}}</i>
  </a>
</span>

and my css: 和我的CSS:

.imgcounter {
  z-index: 500;
  text-align: center;
  margin-top: 45 px;
  position: absolute;
  color: tomato;
  cursor: pointer !important;
  margin-left: -120 px;
}

It's working, but not on different size of images like below: 它可以工作,但不能在不同尺寸的图像上显示,如下所示:

第一张图片 第二张照片

The text is showing, but based on image size the text is not properly positioned. 显示文本,但是基于图像大小,文本放置不正确。

How can I fix this? 我怎样才能解决这个问题?

Obviously, hardcoding specific values like you're doing isn't going to work for arbitrary sizes. 显然,像您正在做的那样对特定的值进行硬编码对任意大小都无效。

There are lots and lots of techniques for centering elements vertically (horizontally is trivial). 很多技术可以使元素垂直居中(水平是微不足道的)。 Here's a simple one: 这是一个简单的例子:

 span { display:inline-block; position:relative; } .imgcounter { font-size: 40px; color:#FFF; z-index:2; position: absolute; transform: translate(-50%,-50%); /* <-- here 50% refers to the size of .imgcounter */ top:50%; left:50%; /* <-- here the 50% measures the containing span */ } 
 <span> <img src="http://placekitten.com/350/150"> <i class="imgcounter">X</i> </span> <span> <img src="http://placekitten.com/150/350"> <i class="imgcounter">TEXT</i> </span> <span> <img src="http://placekitten.com/250/350"> <i class="imgcounter">23%</i> </span> 

Incidentally there are a number of other problems with your code, most prominently you can't nest <a> tags inside each other like you're doing, and you should make up your mind whether to put your css inline or in a .css file. 顺便说一下,您的代码还有许多其他问题,最显着的是您不能像做的那样在彼此之间嵌套<a>标记,因此您应该下定决心将CSS内联还是放在.css中文件。 (Put it in a .css file.) (将其放入.css文件中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM