【问题标题】:Trouble centering text and image无法将文本和图像居中
【发布时间】:2018-07-24 09:49:47
【问题描述】:

我正在尝试设置 div 的样式,我需要能够将两行文本居中,而图像 (.hand) 位于右侧。我不能得到这个。我一定不明白如何做到这一点,因为我已经查找了解决方案,但它们对我不起作用。这是我的代码笔:https://codepen.io/iamgonge/pen/MQvEWY?editors=1100

这是它应该是什么样子的图像:what section should look like.

这是我的代码: 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%;
}

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

【问题讨论】:

    标签: html css


    【解决方案1】:

    你应该把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;
    }
    

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

    【讨论】:

      【解决方案2】:

      你可以这样做。它主要与内联块和两个包装器 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>

      codepen 中的相同:https://codepen.io/anon/pen/QQMqOw

      【讨论】:

        【解决方案3】:

        您可以尝试使用 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>

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-04-11
            • 2014-01-18
            • 1970-01-01
            • 1970-01-01
            • 2019-11-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多