故障居中文字和图像



im试图为Div设置样式,我需要能够将两行文本彼此隔离,而图像(.hand(在右侧。我不能得到这个。我一定不能理解如何做到这一点,因为我查找了解决方案,但它们对我不起作用。这是我的Codepen:https://codepen.io/iamgonge/pen/mqvewy?editors=1100

这是它应该是什么样的图像:哪个部分应该是。

这是我的代码:html:

 <div class="events">
   <h1>You're Cool, We're Cool,</h1>
   <p class="moveit">come see us at a event near you.</p>
   <img class="hand"src="http://res.cloudinary.com/adamscloud/image/upload/v1518559592/hand_lco9ed.png">
 </div>

CSS:

.events {
    background: #fbdd37;
  height: 150px;
}
.events h1{
    margin-top: 0;
    position: relative;
    float: left;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
.moveit{
    margin-top: 0;
    position: relative;
    float: left;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
.hand {
    width: 8%;
}

这里的任何帮助将不胜感激!

您可以尝试使用Flexbox。

h1p封闭在DIV(.text(中,然后

.events容器中添加display:flex;

您还需要设置h1p的余量,因为它们具有默认的边距。

p,h1{  margin:10px 20px;  }

请参阅下面的示例代码。

.events {
  background: #fbdd37;
  height: 150px;
  padding:0;
  margin:0;
  display:flex;
  justify-content:center;
  align-items:center;
}
.text{
  text-align:center;
}
.hand {
    width: 15%;
}
p,h1{
  margin:10px 20px;
}
 <div class="events">
   <div class="text">
     <h1>You're Cool, We're Cool,</h1>
     <p class="moveit">
       come see us at a event near you.
     </p>
   </div>
   <img class="hand"src="http://res.cloudinary.com/adamscloud/image/upload/v1518559592/hand_lco9ed.png">
 </div>

您应该将eventsmoveit放入容器Div:

<div class="events">
   <div id="container"> <!-- This div added -->
     <h1>You're Cool, We're Cool,</h1>
     <p class="moveit">come see us at a event near you.</p>
   </div>
   <img class="hand"src="http://res.cloudinary.com/adamscloud/image/upload/v1518559592/hand_lco9ed.png">
 </div>

,然后最小CSS:

.events {
    background: #fbdd37;
  height: 150px;
  text-align:center;
  width:100%; 
}
.moveit{
    margin-top: 0;
}
#container{
text-align:center;  
  float:left; 
  margin-left:500px;
}
.hand {
  width: 8%;
  float:left;
  margin-left:50px;
}

这使您靠近图片。当然,调整字体等,以进行更紧密的匹配。

这应该给您一些接近的东西。我使用了flexbox。您只需要设计字体样式,大胆等。

- html-

 <div class="events">
    <div class="texts">
      <div class="first-line">
        <h1>You're Cool, We're Cool,</h1>
      </div>
    <div class="second-line">
       <h2 class="moveit">come see us at a event near you.</h2>
    </div>
  </div>
  <img class="hand" 
   src="http://res.cloudinary.com/adamscloud/image/upload/
   v1518559592/hand_lco9ed.png"/>
  </div>

- CSS-

.events {
  background: #fbdd37;
  height: 150px;
  display: flex;
  text-align: center;
  justify-content: center;
}
.hand {
  width: 10%;
  height: 40%;
  margin: 35px 20px;
}

您可以这样做。它主要与内联块和两个包装师一起工作,内部包裹两个文本元素,外部包装包装内包装器和图像。该外套以text-align: center为中心,因为它是一个内联块。垂直中心是通常的方式:position: relativetop: 50%transform: translateY(-50%)

.events {
  background: #fbdd37;
  height: 150px;
  position: relative;
  text-align: center;
}
.outer_wrap {
  display: inline-block;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}
.inner_wrap {
  display: inline-block;
}
.events h1 {
  margin-bottom: 0;
}
.moveit {
  margin-top: 0;
}
.hand {
  display: inline-block;
  width: 120px;
  padding-left: 10px;
  height: auto;
}
<div class="events">
  <div class="outer_wrap">
    <div class="inner_wrap">
      <h1>You're Cool, We're Cool,</h1>
      <p class="moveit">come see us at a event near you.</p>
    </div>
    <img class="hand" src="http://res.cloudinary.com/adamscloud/image/upload/v1518559592/hand_lco9ed.png">
  </div>
</div>

在Codepen中相同:https://codepen.io/anon/pen/qqmqow

最新更新