2016-06-12 14 views
0

Ich habe 3 DIVs, die in einem Container verpackt sind. Ich musste sie in den Behälter wickeln, um sie auf meiner Seite zu zentrieren, so wie ich sie haben wollte. Innerhalb dieser 3 DIVS sind DIV-Header, um Titelleisten für jeden DIV zu erstellen. Die DIV-Header der Titelleiste sind absolut positioniert, sodass sie sich über die obersten DIVs erstrecken, in denen sie sich befinden. Das funktioniert einwandfrei. Aber sobald ich die 3 DIVS in den Container lege, werden die Header-DIVs zu Kindern für den Container und beginnen, sich über sie auszubreiten, anstatt ihre jeweiligen DIVs. Gibt es eine Möglichkeit, sie zurückzubringen?Wie stelle ich ein DIV so ein, dass es in einem anderen spezifischen DIV absolut ist?

JSFIDDLE: https://jsfiddle.net/mh2ez7x9/2/

.main { 
 
    border: 2px solid red; 
 
    position: relative; 
 
    width: 740px; 
 
    right: 0; 
 
    left: 0; 
 
    top: 220px; 
 
    margin: 0 auto; 
 
} 
 
.update { 
 
    position: none; 
 
    display: inline-block; 
 
    background-color: #474747; 
 
    width: 700px; 
 
    height: 200px; 
 
    padding: 20px; 
 
    padding-top: 50px; 
 
    border: 1px solid black; 
 
    border-radius: 5px; 
 
    left: 280px; 
 
    top: 220px; 
 
    color: white; 
 
    font-size: 12pt; 
 
    text-shadow: 0px 0px 0 #000026, 0px 1px 0 #000026, 0px -1px 0 #000026, 1px 0px 0 #000026, 1px 1px 0 #000026, 1px -1px 0 #000026, -1px 0px 0 #000026, -1px 1px 0 #000026, -1px -1px 0 #000026, 0px 0px 0px #000026; 
 
    font-family: 'Muli', sans-serif; 
 
    box-shadow: 5px 5px 10px #000000; 
 
    z-index: 1; 
 
} 
 
.newmembers { 
 
    position: none; 
 
    display: inline-block; 
 
    margin-top: 10px; 
 
    background-color: #474747; 
 
    width: 350px; 
 
    height: 200px; 
 
    padding: 20px; 
 
    padding-top: 50px; 
 
    border: 1px solid black; 
 
    border-radius: 5px; 
 
    left: 280px; 
 
    top: 500px; 
 
    color: white; 
 
    font-size: 12pt; 
 
    text-shadow: 0px 0px 0 #000026, 0px 1px 0 #000026, 0px -1px 0 #000026, 1px 0px 0 #000026, 1px 1px 0 #000026, 1px -1px 0 #000026, -1px 0px 0 #000026, -1px 1px 0 #000026, -1px -1px 0 #000026, 0px 0px 0px #000026; 
 
    font-family: 'Muli', sans-serif; 
 
    box-shadow: 5px 5px 10px #000000; 
 
    z-index: 1; 
 
} 
 
.latest { 
 
    position: none; 
 
    display: inline-block; 
 
    margin-top: 10px; 
 
    background-color: #474747; 
 
    width: 300px; 
 
    height: 200px; 
 
    padding: 20px; 
 
    padding-top: 50px; 
 
    border: 1px solid black; 
 
    border-radius: 5px; 
 
    left: 680px; 
 
    top: 500px; 
 
    color: white; 
 
    font-size: 12pt; 
 
    text-shadow: 0px 0px 0 #000026, 0px 1px 0 #000026, 0px -1px 0 #000026, 1px 0px 0 #000026, 1px 1px 0 #000026, 1px -1px 0 #000026, -1px 0px 0 #000026, -1px 1px 0 #000026, -1px -1px 0 #000026, 0px 0px 0px #000026; 
 
    font-family: 'Muli', sans-serif; 
 
    box-shadow: 5px 5px 10px #000000; 
 
    z-index: 1; 
 
} 
 
.miniheaderA { 
 
    position: absolute; 
 
    right: 0; 
 
    left: 0; 
 
    top: 0; 
 
    border: 2px none red; 
 
    padding: 5px; 
 
    height: 20px; 
 
    background-image: url('../images/redboxtext.png'); 
 
    box-shadow: 0px 2px 5px #000000; 
 
    border-top-left-radius: 5px; 
 
    border-top-right-radius: 5px; 
 
} 
 
.miniheaderM { 
 
    position: absolute; 
 
    right: 0; 
 
    left: 0; 
 
    top: 0; 
 
    border: 2px none red; 
 
    padding: 5px; 
 
    height: 20px; 
 
    background-image: url('../images/orangeboxtext.png'); 
 
    box-shadow: 0px 2px 5px #000000; 
 
    border-top-left-radius: 5px; 
 
    border-top-right-radius: 5px; 
 
} 
 
.miniheaderU { 
 
    position: absolute; 
 
    right: 0; 
 
    left: 0; 
 
    top: 0; 
 
    border: 2px none red; 
 
    padding: 5px; 
 
    height: 20px; 
 
    background-image: url('../images/blueboxtext.png'); 
 
    box-shadow: 0px 2px 5px #000000; 
 
    border-top-left-radius: 5px; 
 
    border-top-right-radius: 5px; 
 
}
<div class="main"> 
 

 
    <div class="update"> 
 
    <div class="miniheaderU"> 
 
     <h3>News</h3> 
 
    </div> 
 
    Welcome to the all new website! 
 
    </div> 
 

 

 
    <td valign="top"> 
 
    <div class="newmembers"> 
 
     <div class="miniheaderM"> 
 
     <h3>New Members</h3> 
 
     </div> 
 
     <?php echo "new members!"; ?> 
 
    </div> 
 

 
    <div class="latest"> 
 
     <div class="miniheaderA"> 
 
     <h3>Latest Content</h3> 
 
     </div> 
 
     <?php echo "new content"; ?> 
 
    </div> 
 

 
</div>

+0

Verwenden Sie 'position: relative;' auf dem Containerelement –

+0

ok. Das hat die Header repariert, aber jetzt sind meine DIVs auf der ganzen Seite merkwürdig verschoben. Ich habe diesen Beitrag aktualisiert. – BuddyRoach

+0

Können Sie ein JSFiddle veröffentlichen, das dieses Problem reproduziert? –

Antwort

1

denke ich, die andere Ihre Ziele mißverstanden haben. Fühlen Sie sich frei, meine codepen zu sehen:

http://codepen.io/anon/pen/MeyYWV?editors=1100

Ich habe ein bisschen Reorganisation der Regeln für Sie, und ich fügte eine Klasse für meine Leichtigkeit:

.update, .newmembers, .latest{ 
    padding:0 0 1em; 
    position:relative; 
    float:left; 
    margin: 10px 20px; 
    display: inline-block; 
    background-color: #474747; 
    border: 1px solid black; 
    border-radius: 5px; 
    color: white; 
    font-size: 12pt; 
    text-shadow: 0px 0px 0 #000026, 0px 1px 0 #000026, 0px -1px 0 #000026, 1px 0px 0 #000026, 1px 1px 0 #000026, 1px -1px 0 #000026, -1px 0px 0 #000026, -1px 1px 0 #000026, -1px -1px 0 #000026, 0px 0px 0px #000026; 
    font-family: 'Muli', sans-serif; 
    box-shadow: 5px 5px 10px #000000; 
    z-index: 1; 
} 
.update { 
    width: 700px; 
    height: 200px; 
} 

.newmembers { 
    width: 350px; 
    height: 200px; 
} 
.latest { 
    width: 300px; 
    height: 200px; 
} 

Mohamed Athif war direkt an der H3, fügte ich in seiner Regel hinzu.

+0

Nein, ich war nicht falsch, sein Code ist ein Chaos, ich räumte auf und entfernen Sie alle Unordnung darauf und es ist tun, was OP will :) Ich habe gerade eine Zeile zu reinigen vermisst. – dippas

+0

Ich habe vergessen, das h3-Tag, das ich verwendet habe, hinzuzufügen. 'h3 { Rand-unten: -30px; Rand oben: 0px; } ' – BuddyRoach

+0

Hatte es ein wenig zu bearbeiten, aber es hat funktioniert. Vielen Dank! – BuddyRoach

0

Es gibt ein paar Dinge falsch mit Ihrem Code ist, wie es kein position:none ist, können Sie Ihren Code in Klassen viel duplizieren, und Sie haben ein verlorenes td in Ihrem HTML-Markup, vereinfachte ich es, keine Notwendigkeit zu verwenden position:absolute

Verwendung box-sizing: border-box y ou kann die korrekte Schreibweise Ihrer width Berechnungen tun, ohne sich Gedanken über padding/border

ich die width Werte ändern sie reaktionsfähiger zu machen.

*, 
 
*::before, 
 
*::after { 
 
    box-sizing: border-box 
 
} 
 
body { 
 
    margin: 0 
 
} 
 
.main { 
 
    max-width: 740px; 
 
    margin: 0 auto; 
 
    font-size:0 /* fix inline-block gap*/ 
 
} 
 
.miniheader { 
 
    box-shadow: 0px 2px 5px #000000; 
 
    border-top-left-radius: 5px; 
 
    border-top-right-radius: 5px; 
 
    border: 2px none red; 
 
    padding: 5px; 
 
} 
 
h3 { 
 
    margin: 0 
 
} 
 
.A { 
 
    background-color: deeppink; 
 
    background-image: url('../images/redboxtext.png'); 
 
} 
 
.M { 
 
    background-color: orange; 
 
    background-image: url('../images/orangeboxtext.png'); 
 
} 
 
.U { 
 
    background-color: skyblue; 
 
    background-image: url('../images/blueboxtext.png'); 
 
} 
 
.main > div { 
 
    display: inline-block; 
 
    background-color: #474747; 
 
    border: 1px solid black; 
 
    border-radius: 5px; 
 
    font-size: 12pt; 
 
    text-shadow: 0px 0px 0 #000026, 0px 1px 0 #000026, 0px -1px 0 #000026, 1px 0px 0 #000026, 1px 1px 0 #000026, 1px -1px 0 #000026, -1px 0px 0 #000026, -1px 1px 0 #000026, -1px -1px 0 #000026, 0px 0px 0px #000026; 
 
    font-family: 'Muli', sans-serif; 
 
    box-shadow: 5px 5px 10px #000000; 
 
    color: white; 
 
    height: 200px; 
 
    font-size:16px; 
 
} 
 
.update { 
 
    width: 100%; 
 
} 
 
.newmembers, .latest { 
 
    margin-top: 10px; 
 
    width: 50%; 
 
}
<div class="main"> 
 
    <div class="update"> 
 
    <div class="miniheader U"> 
 
     <h3>News</h3> 
 
    </div> 
 
    Welcome to the all new website! 
 
    </div> 
 
    <div class="newmembers"> 
 
    <div class="miniheader M"> 
 
     <h3>New Members</h3> 
 
    </div> 
 
    <?php echo "new members!"; ?> 
 
    </div> 
 
    <div class="latest"> 
 
    <div class="miniheader A"> 
 
     <h3>Latest Content</h3> 
 
    </div> 
 
    <?php echo "new content"; ?> 
 
    </div> 
 
</div>

0

Verwendung position: relative auf Klassen miniheaderU, miniheaderM und miniheaderA. Klasse in H3-Tag einfügen und in CSS verwenden margin-top: 0px.

, so dass Sie HTML-Code ist

<div class="main"> 

    <div class="update"> 
    <div class="miniheaderU"> 
     <h3 class="gj">News</h3> 
    </div> 
    Welcome to the all new website! 
    </div> 


    <td valign="top"> 
    <div class="newmembers"> 
     <div class="miniheaderM"> 
     <h3 class="gj">New Members</h3> 
     </div> 
     <?php 
    echo "new members!"; 
    ?> 
    </div> 

    <div class="latest"> 
     <div class="miniheaderA"> 
     <h3 class="gj">Latest Content</h3> 
     </div> 
     <?php 
    echo "new content"; 
    ?> 
    </div> 

</div> 

und steckt in CSS-Code unten

H3.gj { 
    margin-top: 0px; 
} 
Verwandte Themen