text on the top of an image

2019-09-13 07:22发布

I want to center some text in two corners of an image, one in the top left and other in the top right. Something like this: enter image description here

.left,
.right {
  width: 300px;
  height: 200px;
  float: left;
}
.left-div,
.right-div {
  position: relative;
}
.left-text,
.right-text {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 999;
  background-color: black;
  color: white;
  padding: 5px;
}
.left {
  float: left;
}
.right {
  float: right;
}
.rotate-left {
  -moz-transform: translateX(-50%) translateY(-50%) rotate(-90deg);
  -webkit-transform: translateX(-50%) translateY(-50%) rotate(-90deg);
  transform: translateX(-50%) translateY(-50%) rotate(-90deg);
}
.rotate-right {
  -moz-transform: translateX(50%) translateY(50%) rotate(90deg);
  -webkit-transform: translateX(50%) translateY(50%) rotate(90deg);
  transform: translateX(50%) translateY(50%) rotate(90deg);
}
<div id="row">
  <div class="left-div">
    <img class="left" src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" />
    <p class="left-text rotate-left">
      text 1
    </p>
  </div>

  <div class="right-div">
    <img class="right" src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg" />
  </div>
  <p class="right-text rotate-right">
    text 2
  </p>
</div>

for some reasons the paragraphs with text are not centered where they are supposed to be, I don't know why?

Here is My JS FIDDLE

2条回答
相关推荐>>
2楼-- · 2019-09-13 07:47

When using rotate you need to adjust from where using transform-origin to control its position after the rotation.

.left,
.right {
  width: 300px;
  height: 200px;
  float: left;
}
.left-div,
.right-div {
  position: relative;
}
.left-text,
.right-text {
  position: absolute;
  top: 0;
  background-color: gray;
  color: white;
  padding: 5px;
}
.right {
  float: right;
}
.rotate-left {
  left: 0;
  transform-origin: bottom left;
  transform: translateY(-100%) rotate(90deg);
}
.rotate-right {
  right: 0;
  transform-origin: bottom right;
  transform: translateY(-100%) rotate(-90deg);
}
<div id="row">
  <div class="left-div">
    <img class="left" src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" />
    <div class="left-text rotate-left">
      text 1
    </div>
  </div>

  <div class="right-div">
    <img class="right" src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg" />
    <div class="right-text rotate-right">
      text 2
    </div>
  </div>
</div>

Sample 2, positioned outside the image

.left,
.right {
  width: 270px;
  height: 200px;
  float: left;
}
.left-div,
.right-div {
  position: relative;
  margin: 0 30px;
}
.left-text,
.right-text {
  position: absolute;
  top: 0;
  background-color: gray;
  color: white;
  padding: 5px;
}
.right {
  float: right;
}
.rotate-left {
  left: 0;
  transform-origin: top left;
  transform: rotate(90deg);
}
.rotate-right {
  right: 0;
  transform-origin: top right;
  transform: rotate(-90deg);
}
<div id="row">
  <div class="left-div">
    <img class="left" src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" />
    <div class="left-text rotate-left">
      text 1
    </div>
  </div>

  <div class="right-div">
    <img class="right" src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg" />
    <div class="right-text rotate-right">
      text 2
    </div>
  </div>
</div>

查看更多
乱世女痞
3楼-- · 2019-09-13 07:58

You may use flex and writing-mode.

example:

  #row,
  #row>div {
    display: flex;
  }
  
  .left-div {
    margin: 0 0 0 auto;
  }
  
  .right-div {
    margin: 0 auto 0 0;
  }
  
  .rotate-left,
  .rotate-right {
    order: -1;/* puts element at front in visual flow */
    writing-mode: vertical-lr;/* link for explanation  provided earlier */
    /* makeup*/
    width: 1.2em;
    background: black;
    color: white;
    padding: 0.5em;
    /* puts it a top against sibling tag */
    flex-shrink:0;
    margin: 0;
    align-self:flex-start
  }
  
  .rotate-right {/* reset values */
    writing-mode: vertical-rl;
    order: 1;
  }
  
  img {
    width: calc(100% - 2em);/* whatever is needed , if needed */
    flex: 1 0 auto;/* preserve ratio */
  }
<div id="row">
  <div class="left-div">
    <img class="left" src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" />
    <p class="left-text rotate-left">
      text 1
    </p>
  </div>

  <div class="right-div">
    <img class="right" src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg" />
  <p class="right-text rotate-right">
    text 2
  </p>
</div>

Pen to fork and play with

or did i misunderstood where you wanted these to stand ?

查看更多
登录 后发表回答