Skip to content Skip to sidebar Skip to footer

Text On The Top Of An Image

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: for some reasons the paragraphs with text are not ce

Solution 1:

You may use flex and writing-mode.

example:

#row,
  #row>div {
    display: flex;
  }
  
  .left-div {
    margin: 000 auto;
  }
  
  .right-div {
    margin: 0 auto 00;
  }
  
  .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: 10 auto;/* preserve ratio */
  }
<divid="row"><divclass="left-div"><imgclass="left"src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" /><pclass="left-text rotate-left">
      text 1
    </p></div><divclass="right-div"><imgclass="right"src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg" /><pclass="right-text rotate-right">
    text 2
  </p></div>

Pen to fork and play with

or did i misunderstood where you wanted these to stand ?

Solution 2:

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);
}
<divid="row"><divclass="left-div"><imgclass="left"src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" /><divclass="left-text rotate-left">
      text 1
    </div></div><divclass="right-div"><imgclass="right"src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg" /><divclass="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: 030px;
}
.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);
}
<divid="row"><divclass="left-div"><imgclass="left"src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" /><divclass="left-text rotate-left">
      text 1
    </div></div><divclass="right-div"><imgclass="right"src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg" /><divclass="right-text rotate-right">
      text 2
    </div></div></div>

Post a Comment for "Text On The Top Of An Image"