2017-07-16 234 views
1

我有一個外部div和兩個子div。我想將外部div固定到窗口,其中一個子div位於父div的最左側,另一個位於父div的最右側。絕對和相對的CSS位置

當我父母,它固定在窗口,但兩個子div粘在左邊,並重疊。如果我position: relative父,兩個子div分別左側和右側,但它不是固定在窗口的頂部。

我該怎麼辦?謝謝!

<div class="nav-wrapper"> 
<div class="child1"></div> 
<div class="nav-pages"></div> 
</div> 

我的CSS:

nav { 
@media only screen and (min-width: 0) { 
height: 3em; 
.nav-wrapper { 
    padding: .7em 1em 0 1em; 
} 
} 
@media only screen and (min-width: $medium-screen) { 
height: 500px; 
.nav-wrapper { 
    padding: 0em 1em 0 1em; 
    height: 64px; 
    position: relative; 
    background-color: rgba(60,63,65,0.22); 
    } 
} 
} 

nav { 
background-image: url("http://image.insider-journeys.com/overview/china.jpg"); 
background-size: cover; 
} 

.navbar-non-link { 
    padding: 0 15px; 
} 

.nav-pages { 
    padding-right: 0px; 
} 

.side-nav { 
    width: 500px; 
} 
+0

請添加您嘗試過的CSS,以便讀者更好地理解您所要求的內容。 –

+0

@AlexanderHiggins .css附 –

回答

0

試試這個:

body { 
 
    height: 1200px; 
 
} 
 
.parent { 
 
    position: fixed; 
 
    background-color: red; 
 
    height: 100px; 
 
    width:100%; 
 
} 
 

 
.child1 { 
 
    background-color: green; 
 
    width: 100px; 
 
    height: 100px; 
 
    position: absolute; 
 
    left: 0; 
 

 
} 
 

 
.child2{ 
 
    background-color: blue; 
 
    width: 100px; 
 
    height: 100px; 
 
    position: absolute; 
 
    right: 0; 
 
}
<div class="parent"> 
 
<div class="child1"></div> 
 
<div class="child2"></div> 
 
</div>

+0

非常感謝! –

+0

@ChitSiu,不客氣! – Ehsan

0

事情是這樣的:

body { 
 
    width: 100%; 
 
    min-height: 1000px; 
 
    margin:0px; 
 
    padding:0px; 
 
} 
 
div {margin:0px;padding:0px;} 
 
.wrapper { 
 
    border: 1px solid black; 
 
    width: 100%; 
 
    position: fixed; 
 
    height:50px; 
 
    top:0px; 
 
} 
 

 
.parent { 
 
    position: fixed; 
 
    width: 20%; 
 
    height: 50px; 
 
    background: red; 
 
    overflow:hidden; 
 
    top:1px; 
 
    right:40%; 
 
} 
 

 
.child1 { 
 
    position: fixed; 
 
    left: 20%; 
 
    top: 1px; 
 
    height: 50px; 
 
    width:20%; 
 
    background: green 
 
} 
 

 
.child2 { 
 
    position: fixed; 
 
    right: 20%; 
 
    top: 1px; 
 
    height: 50px; 
 
    width: 20%; 
 
    background: green 
 
}
<body> 
 
    <div class="wrapper"> 
 
    <div class="parent">parent 
 
     <div class="child1">child1</div> 
 
     <div class="child2">child2</div> 
 
    </div> 
 
    </div> 
 
</body>