简体   繁体   English

无法将文本和图像居中

[英]Trouble centering text and image

Im trying to style a div I need to be able to center both lines of text on top of each other with the image ( .hand ) next them on the right.我正在尝试为 div 设置样式,我需要能够将两行文本彼此.hand ,图像( .hand )位于右侧。 I can not get this.我不能得到这个。 I must not be understanding how to do this because I've looked up solutions but they are not working for me.我一定不明白如何做到这一点,因为我已经查找了解决方案,但它们对我不起作用。 Here is my codepen: https://codepen.io/iamgonge/pen/MQvEWY?editors=1100这是我的代码笔: https ://codepen.io/iamgonge/pen/MQvEWY ? editors = 1100

here is an image of what it should look like: what section should look like.这是它应该是什么样子的图像:什么部分应该是什么样子。

here is my code: HTML:这是我的代码: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: 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%;
}

Any help here would be greatly appreciated!这里的任何帮助将不胜感激!

You can try using flexbox.您可以尝试使用 flexbox。

Enclose the h1 and p in a div( .text ) and thenh1p在 div( .text ) 中,然后

add display:flex;添加display:flex; in the .events container.events容器中

also you will need to set the margin of h1 and p since they have a default margin.您还需要设置h1p的边距,因为它们具有默认边距。

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

Please see the sample code below.请参阅下面的示例代码。

 .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>

You should put the events and moveit into a container 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>

And then the minimal CSS:然后是最小的 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;
}

This gets you close to your picture.这让你接近你的照片。 Of course, adjust the fonts, etc. for a closer match.当然,调整字体等以获得更接近的匹配。

You can do it like this.你可以这样做。 It works mainly with inline-blocks and two wrapper DIVs, the inner one wrapping the two text elements, the outer one wrapping the inner wrapper and the image.它主要与内联块和两个包装 DIV 一起使用,内部一个包装两个文本元素,外部一个包装内部包装和图像。 that outer wrapper is centered with text-align: center , which works since it's an inline-block.该外部包装器以text-align: center ,因为它是一个内联块,所以它有效。 The vertical centering is done the usualy way: position: relative , top: 50% and transform: translateY(-50%) :垂直居中以通常的方式完成: 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>

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

This should give you something close.这应该给你一些接近的东西。 I used flexbox.我使用了弹性盒。 You would just need to style the font style, boldness, and etc.您只需要设置字体样式、粗体等的样式。

--HTML-- --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-- --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