CSS:将用户个人资料图片和标题中的用户名垂直对齐

时间:2019-02-26 22:27:27

标签: php html css

在尝试包括用户的个人资料图片之前,标题已垂直对齐,但现在似乎无法获取。经过反复试验,到目前为止,这是我的经验:

HTML / PHP:

<ul id="right-side">
  <li>
    <a href="">
    <img src="<?php echo $user['profile_pic'];?>" id="profilepic">
    <?php echo $user['user_name'];?>
    </a>
  </li>
</ul>

CSS:

nav {
position: fixed;
right: 0px;
top: 0px;
z-index: 1000;
background-color: rgba(0, 0, 0, 0.5);
width: 100%;
}

#right-side {
display: inline;
float: right;
text-align: right;
}

#right-side a {
vertical-align: middle;
}

#profilepic {
height:50px; 
width 50px; 
border-radius:50px;
}

2 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码。

nav {
position: fixed;
right: 0px;
top: 0px;
z-index: 1000;
background-color: rgba(0, 0, 0, 0.5);
width: 100%;
padding: 0 10px;
}

#right-side {
  display: inline;
  float: right;
  text-align: right;
}

#right-side li {
  list-style-type: none;
}

#right-side a {
  display: flex;
  align-items: center;
}

#right-side a img {
  margin-right: 10px;
}

#profilepic {
  height:50px; 
  width 50px; 
  border-radius:50px;
}
<nav>
  <ul id="right-side">
    <li>
      <a href="">
        <img src="https://cdn.onlinewebfonts.com/svg/img_235843.png" id="profilepic">
        John
      </a>
    </li>
  </ul>
</nav>

答案 1 :(得分:0)

我假设您的意思是希望用户名和图片都垂直并排对齐?如果是这样,check out this fiddle

CSS

#right-side {
display: inline;
float: right;
text-align: right;
}

#profile-pic {
  left: 0;
  float: left;
  width: 50px;
}

#profile-name {
  margin-left: 55px;
  top: 0;
}


#profilepic {
height: 50px;
width: 50px;
border-radius: 50px;
}

HTML

  <ul id="right-side">
            <div id="profile-pic">
            <img src="https://i.stack.imgur.com/34AD2.jpg" id="profilepic">
            </div>
            <div id="profile-name">
            User Name
            </div>   
  </ul>