2017-05-21 7 views
0

ich folgende HTML haben:CSS ordnen zwei divs nebeneinander

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

Und mein 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%; 

    } 

Dies macht wie folgt vor:

enter image description here

Ich möchte zwischen zirkularem div und text etwas Abstand haben. Seite-Kopf-Profil-Foto-Container-Position muss absolut sein.

Wie kann ich das beheben?

+0

Haben Sie versucht, verwenden 'display: inline-block' auf' .user_name CLASS'? –

Antwort

2

Zuerst korrigieren Sie Ihre Syntax wie className zu class und versuchen Sie den folgenden Code. Keine Notwendigkeit, position:absolute in user-name Klasse

.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

Klassenname, weil ich React! – Nitish

+0

Danke! Ich habe diese Lösung benutzt. – Nitish

0

Verwenden Sie keine absolute Positionierung im Benutzernamen. Absolute Positionierung bringt ein Element in eine bestimmte Position, egal was passiert (egal ob es überlappt)

0

Mit flex-box es wird gut für mich zu arbeiten. Ich hoffe das hilft.

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

Verwandte Themen