如何在响应图像上垂直中心文本?

时间:2022-11-24 23:19:38

How can I get the caption text on these images to move around when then the browser window is resized? My implementation is jicky and I need a way to keep the text from sliding around when the window is resized.

当浏览器窗口被调整大小时,如何让这些图像上的标题文本移动?我的实现是jicky,我需要一种方法来防止文本在窗口大小调整时滑动。

Codepen

Codepen

<div class="row">
  <div class="col-md-6">
    <img src="http://placekitten.com/600/375" class="img-responsive" />
    <h2 class="homeImageLink">
      <span>Caption Text</span>
    </h2>
  </div>
  <div class="col-md-6">
    <img src="http://placekitten.com/600/375" class="img-responsive" />
    <h2 class="homeImageLink">
      <span>Caption Text</span>
    </h2>
  </div>
</div>

.homeImageLink {
   position: absolute; 
   top: 110px; 
   left: 0;
   text-align: center; 
   width: 100%; 
}

.homeImageLink span { 
    color: red;
    font-weight: 300;
    font-style: italic;
    text-transform: uppercase;
    letter-spacing: 15px;
    pointer-events: none;
}

2 个解决方案

#1


13  

Just add one class to parent container, make it's position relative.

只需将一个类添加到父容器中,使它的位置相对。

.img-container {
  position:relative;
}

And then make homeImageLink absolute and give top at around 45%..

然后把homeImageLink设置成绝对的,并在45%左右。

It will make it vertically centered..

它会使它垂直居中。

.homeImageLink {
   position: absolute; 
   top: calc(50% - 24px); //24px is font size of H1 I assume 
   left: 0;
   text-align: center; 
   width: 100%; 
}

Demo here : http://codepen.io/anon/pen/bJadE

演示:http://codepen.io/anon/pen/bJadE

#2


0  

I came up with another solution, here's a working demo:

我想到了另一个解决方案,这里有一个工作演示:

http://codepen.io/niente0/pen/jyzdRp

http://codepen.io/niente0/pen/jyzdRp

HTML:

HTML:

<DIV class=wrapper>
     <DIV class=divimage></DIV>
     <DIV class=divtext>THIS IS A TEST</DIV>
</DIV>

CSS:

CSS:

HTML,BODY {
  max-width:1200px;
}
.wrapper {
  position:relative;
  width:100%;
  max-width:1200px;
  height:100%;
  min-height:320px
}
.divimage {  position:absolute;
  top:0;
  left:0;
  width:100%;
  height:100%;
  background:url(https://thumbs.dreamstime.com/t/empty-red-banner-corners-ropes-textile-white-background-d-illustration-70434974.jpg);
  background-repeat:no-repeat;
  background-size:100% auto;
}
.divtext {
  position:absolute;
  top:0;
  left:0;
  width:100%;
  padding-top:13.5%;  
  text-align:center;
  font-weight:bold;
  font-size:5vw;
  color:white;
  font-family:arial;
}
@media (min-width: 1200px) {
  .divtext{
    font-size:60px;
  }
}

#1


13  

Just add one class to parent container, make it's position relative.

只需将一个类添加到父容器中,使它的位置相对。

.img-container {
  position:relative;
}

And then make homeImageLink absolute and give top at around 45%..

然后把homeImageLink设置成绝对的,并在45%左右。

It will make it vertically centered..

它会使它垂直居中。

.homeImageLink {
   position: absolute; 
   top: calc(50% - 24px); //24px is font size of H1 I assume 
   left: 0;
   text-align: center; 
   width: 100%; 
}

Demo here : http://codepen.io/anon/pen/bJadE

演示:http://codepen.io/anon/pen/bJadE

#2


0  

I came up with another solution, here's a working demo:

我想到了另一个解决方案,这里有一个工作演示:

http://codepen.io/niente0/pen/jyzdRp

http://codepen.io/niente0/pen/jyzdRp

HTML:

HTML:

<DIV class=wrapper>
     <DIV class=divimage></DIV>
     <DIV class=divtext>THIS IS A TEST</DIV>
</DIV>

CSS:

CSS:

HTML,BODY {
  max-width:1200px;
}
.wrapper {
  position:relative;
  width:100%;
  max-width:1200px;
  height:100%;
  min-height:320px
}
.divimage {  position:absolute;
  top:0;
  left:0;
  width:100%;
  height:100%;
  background:url(https://thumbs.dreamstime.com/t/empty-red-banner-corners-ropes-textile-white-background-d-illustration-70434974.jpg);
  background-repeat:no-repeat;
  background-size:100% auto;
}
.divtext {
  position:absolute;
  top:0;
  left:0;
  width:100%;
  padding-top:13.5%;  
  text-align:center;
  font-weight:bold;
  font-size:5vw;
  color:white;
  font-family:arial;
}
@media (min-width: 1200px) {
  .divtext{
    font-size:60px;
  }
}