使用position:relative将文本和图像定位为水平居中

时间:2019-04-09 10:05:48

标签: html css css-position

我想知道如何将文本水平居中放置。与图像水平居中。我不想使用position: absolute属性。这是因为左右文本的长度可以更改,并且要具有绝对属性,您需要固定的宽度/大小。

body {
    padding: 0;
    margin: 0;
}

.right-text {
    position: relative;
    display: inline-block;
    font-family: arial;
    font-size: 13px;
}

.icon {
    position: relative;
    display: inline-block;
    width: 32px;
    height: 32px;
    background: url("https://i.imgur.com/VXlnn8a.png");
}

.left-text {
    position: relative;
    display: inline-block;
    font-family: arial;
    font-size: 13px;
    font-weight: 700;
}
<div class="right-text">Right</div><div class="icon"></div><div class="left-text">Left</div>

4 个答案:

答案 0 :(得分:1)

通过将以下代码添加到父级div

,您可以使用flexbox将项目水平居中对齐
.parent{
   display:flex;
   align-items: center;
}

这是您的代码中正在执行此更改的代码段

   

    

    body{
           padding: 0;
           margin: 0;
           font-family: arial;
        }  
.content{
       display: flex;
       align-items: center;
    }

.right-text {
        font-size: 13px;
    }

    .icon {
        width: 32px;
        height: 32px;
        background: url("https://i.imgur.com/VXlnn8a.png");
    }

.left-text {
        font-size: 13px;
        font-weight: 700;
    }
        <div class="content">
           <div class="right-text">Right</div>
           <div class="icon"></div>
           <div class="left-text">Left</div>
        </div>

答案 1 :(得分:0)

您只需添加一个flexbox容器并将其属性align-items设置为居中。

<div class="container">
  <div class="right-text">Right</div>
  <div class="icon"></div>
  <div class="left-text">Left</div>
</div>

这是类容器:

.container{
  display: flex;
  align-items: center;
  justify-content: center;
}

答案 2 :(得分:0)

嗨,如果您想垂直对齐,这里是更新的代码段

body {
    padding: 0;
    margin: 0;
}

.right-text {
    position: relative;
    display: inline-block;
    font-family: arial;
    font-size: 13px;
}

.icon {
    position: relative;
    display: inline-block;
    width: 32px;
    height: 32px;
    background: url("https://i.imgur.com/VXlnn8a.png");
    top: 10px;
    margin: 0 5px;
}

.left-text {
    position: relative;
    display: inline-block;
    font-family: arial;
    font-size: 13px;
    font-weight: 700;
}
<div class="right-text">Right</div><div class="icon"></div><div class="left-text">Left</div>

答案 3 :(得分:0)

您实际上需要将right-texticonleft-text元素包装在childContainer中,然后将childContainer元素包装在parentContainer

<div class="parentContainer">
  <div class="childContainer">
    <div class="right-text">Right</div><div class="icon"></div><div class="left-text">Left</div>
  </div>
</div>

下一步,您必须应用具有{strong> center FlexBoxjustify-content值的align-items,并且还需要添加height: 100vh。所提及的更改将应用​​于以下代码段。试试这个,希望对您有所帮助。谢谢

body {
    padding: 0;
    margin: 0;
}

.right-text {
    position: relative;
    display: inline-block;
    font-family: arial;
    font-size: 13px;
}

.icon {
    position: relative;
    display: inline-block;
    width: 32px;
    height: 32px;
    background: url("https://i.imgur.com/VXlnn8a.png");
}

.left-text {
    position: relative;
    display: inline-block;
    font-family: arial;
    font-size: 13px;
    font-weight: 700;
}

.parentContainer {
  display: flex;
  height: 100vh;
  justify-content: center;
  align-items: center;
}
<div class="parentContainer">
  <div class="childContainer">
    <div class="right-text">Right</div><div class="icon"></div><div class="left-text">Left</div>
  </div>
</div>