繁体   English   中英

无法将文本和图像居中

[英]Trouble centering text and image

我正在尝试为 div 设置样式,我需要能够将两行文本彼此.hand ,图像( .hand )位于右侧。 我不能得到这个。 我一定不明白如何做到这一点,因为我已经查找了解决方案,但它们对我不起作用。 这是我的代码笔: 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 ) 中,然后

添加display:flex; .events容器中

您还需要设置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;
}

这让你接近你的照片。 当然,调整字体等以获得更接近的匹配。

你可以这样做。 它主要与内联块和两个包装 DIV 一起使用,内部一个包装两个文本元素,外部一个包装内部包装和图像。 该外部包装器以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>

在代码笔中也是如此: https ://codepen.io/anon/pen/QQMqOw

这应该给你一些接近的东西。 我使用了弹性盒。 您只需要设置字体样式、粗体等的样式。

--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;
}

暂无
暂无

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

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