2017-05-21 133 views
0

两个div我有以下HTML:CSS安排彼此相邻

<div className={`page-header-profile-photo-container`}> 
    <div className="user-picture"> 
    </div> 
    <div className="user-name"> 
     <p>Sample GmbhH</p> 
    </div> 
</div> 

而且我的CSS:

.page-header-profile-photo-container{ 
    position: absolute; 
    top: 10%; 
    right: 130px; 
    width: 200px; 

    } 
    .user-picture { 
    position: relative; 
    top: 10%; 
    width: 40px; 
    height: 40px; 
    border-radius: 50%; 
    background-color: #787567; 
    } 
    .user-name{ 
    position: absolute; 
    top: 5%; 

    } 

这使得像以下:

enter image description here

我想在圆形div和文本之间留有一些空间。 page-header-profile-photo-container的位置必须是绝对的。

我该如何解决这个问题?

+0

您是否尝试过使用'显示:内联block'您'.user_name class'? –

回答

2

首先更正您的语法,如classNameclass并尝试以下代码。无需position:absoluteuser-name

.page-header-profile-photo-container{ 
 
    width: 200px; 
 
    position: absolute; 
 
    top: 10%; 
 
    right: 130px; 
 

 
    } 
 
    .user-picture { 
 
    position: relative; 
 
    width: 40px; 
 
    height: 40px; 
 
    border-radius: 50%; 
 
    background-color: #787567; 
 
    display: inline-block; 
 
    vertical-align: middle; 
 
    } 
 
    .user-name{ 
 
    display: inline-block; 
 
    vertical-align: middle; 
 
    }
<div class=page-header-profile-photo-container> 
 
    <div class="user-picture"> 
 
    </div> 
 
    <div class="user-name"> 
 
     <p>Sample GmbhH</p> 
 
    </div> 
 
</div>

+0

className因为实际上我正在使用React! – Nitish

+0

谢谢!我用这个解决方案。 – Nitish

0

不要在用户名中使用绝对定位。绝对定位将物品置于特定位置(不管它是否重叠)

0

使用flex-box它会工作为我好。希望这个帮助。

.page-header-profile-photo-container{ 
 
    background-color: #f5f5f5; 
 
    display: flex; 
 
    flex-direction: row; 
 
    align-items: center; 
 
    justify-content: space-between; 
 
    position: absolute; 
 
    height: 50px; 
 
    top: 10%; 
 
    right: 130px; 
 
    padding: 5px 10px; 
 
    width: 200px; 
 

 
    } 
 
    .user-picture { 
 
    position: relative; 
 
    width: 40px; 
 
    height: 40px; 
 
    border-radius: 50%; 
 
    /*background-color: #787567;*/ 
 
    } 
 
    .user-picture img{ 
 
    border-radius: 50%; 
 
    display: block; 
 
    height: auto; 
 
    max-width: 100%; 
 
    } 
 
    .user-name{ 
 
    font-size: 15px; 
 
    }
<div class=page-header-profile-photo-container> 
 
    <div class="user-picture"> 
 
     <img src="http://placehold.it/40x40" alt="Profile Picture" /> 
 
    </div> 
 
    <div class="user-name"> 
 
     <p>Sample GmbhH</p> 
 
    </div> 
 
</div>