Div推着兄弟Div的内容

时间:2013-09-15 13:44:08

标签: javascript css html

我有2个div嵌套在主体(a)和(b)中。 Div(b)的上边距为负,因此它位于div(a)的顶部。

一切都很好,除了div(b)的内容被困在底部,在div(a)下面,因为看起来他们父母的css属性没有影响他们。

这是html:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Website</title>
<script src="js/jquery-1.9.1.min.js"></script>
<link href="css/carousel.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>

<body>
<div class="top-menu">
  <div class="container">
    <div class="logo"><img src="img/logo.png" alt="logo"></div>
    <ul class="nav">
            <li>who we are</li>
            <li>what we do</li>     
        </ul>
    <input type="text" placeholder="login/register" class="header-login">
    <button type="submit" class="header-btn"></button>
      <span class="za"><img src="img/south-africa.png"></span>
      <span class="sng"><img src="img/singapore.png"></span>
    </div>
</div>


<div class="carousel-main">

              <div id="myCarousel" class="carousel slide">   
              <div class="container">
               <ol class="carousel-indicators">
                    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                    <li data-target="#myCarousel" data-slide-to="1"></li>
                    <li data-target="#myCarousel" data-slide-to="2"></li>
               </ol>
               </div>
                        <div class="carousel-inner home-carousel">
                          <div class="item active">
                            <img src="img/1.jpg">
                            <div class="container">
                            <div class="carousel-caption">
                              <h3>Risk Management and<br>
Business Finance made simple. </h3>
                            </div>
                            </div>
                          </div>

                          <div class="item">
                            <img src="img/2.jpg">
                            <div class="container">
                            <div class="carousel-caption">
                              <h3>Manage my company.</h3>
                            </div>
                            </div>
                          </div>

                          <div class="item">
                            <img src="img/3.jpg">
                            <div class="container">
                            <div class="carousel-caption">
                              <h3>Investing my money.</h3>
                            </div>
                            </div>
                          </div>


                         </div> 
                </div>
                </div>

                <div class="sub-menu">
                <ul>
                    <li>finance</li>
                    <li>Manage</li>
                    <li>invest</li>
                </ul>
                </div>




<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    $('#myCarousel').carousel({
        interval: 9000
    });

    $('#myCarousel').carousel('pause');
});
</script>
</body>
</html>

这是style.css

@charset "utf-8";
/* CSS Document */
/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/
@import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,900);

html, body
{
    width:100%;
    height:100%;
    font-family: 'Source Sans Pro', sans-serif !importantsa;
}

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

.top-menu
{
    height:70px;
    width:100%;
}

.logo
{
    height:100%;
    width:277px;
    float:left;
}

.logo img
{
    height:100%;
}

.container
{
    width:960px;
    margin:0 auto;
    position:relative;
}

.top-menu .nav

{
float:left;
}

.top-menu ul li
{
display:inline-block;
padding:30px 10px;
text-transform:uppercase;
font-family: 'Source Sans Pro', sans-serif;
color:#808184;

}

.header-login

{
font-family: 'Source Sans Pro', sans-serif;
margin:25px;
padding-left:10px;
font-size:17px;
text-transform:uppercase;
background-color:#ededed;
border:none;
max-width:140px;
float:left;
}
.header-btn

{
height:30px;
width:30px;
background-image:url(../img/login.png);
background-color:transparent;
border:none;
margin-top:22px;
margin-left:-20px;
margin-right:30px;
float:left;
}

.top-menu span

{
margin-top:27px;
float:left;
margin-left:10px;
}

.sub-menu
{
    width:100%;
    height:170px;
    margin-top:-280px;
    background-color:rgba(247,148,30,0.6);
    z-index:2;
}

.sub-menu ul

{
margin:0 auto;
width:600px;
height:100%;

}

.sub-menu ul li

{
display:inline-block;
height:200px;
width:190px;
text-transform:uppercase;
color:#fff;
}

这是carousel.css:

@charset "UTF-8";
/* CSS Document */
.carousel {
  margin-bottom: 20px;
  line-height: 1;
  float:left;
  z-index:-1;
}

.carousel-inner {
  position: relative;
  width: 100%;
  height:700px;
  overflow: hidden;
   z-index:-1;
}

.carousel-inner > .item {
  position: relative;
  display: none;
  overflow: hidden;
  -webkit-transition: 0.6s ease-in-out left;
     -moz-transition: 0.6s ease-in-out left;
       -o-transition: 0.6s ease-in-out left;
          transition: 0.6s ease-in-out left;
          height:100%;
}

.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
  display: block;
  line-height: 1;
}

.carousel-inner > .active,
.carousel-inner > .next,
.carousel-inner > .prev {
  display: block;
}

.carousel-inner > .active {
  left: 0;
}

.carousel-inner > .next,
.carousel-inner > .prev {
  position: absolute;
  top: 0;
  width: 100%;
}

.carousel-inner > .next {
  left: 100%;
}

.carousel-inner > .prev {
  left: -100%;
}

.carousel-inner > .next.left,
.carousel-inner > .prev.right {
  left: 0;
}

.carousel-inner > .active.left {
  left: -100%;
}

.carousel-inner > .active.right {
  left: 100%;
}


.carousel-indicators {
  position: absolute;
  top:600px;
  right:0;
  z-index: 5;
  margin: 0 auto;
  list-style: none;
}

.carousel-indicators li {
  display: block;
  float: left;
  width: 20px;
  height: 20px;
  margin-left: 5px;
  text-indent: -999px;
  background-image:url(../img/indicator.png);
  background-repeat:no-repeat;
  opacity:0.6;
 }

.carousel-indicators .active {
  opacity:1;
}


.slider

{
    height:700px;
    width:800px;
    margin: 0 auto;
    margin-bottom:20px;
}

.slider img

{
    width:100%;
}

.carousel-main

{
    height:700px;
    background-color: transparent;
    margin: 0 auto;
    width:100%;

}


.home-carousel > .item > img, .home-carousel > .item > a > img {
    display: block;
    line-height: 1;
    min-height:100%;
    width:100%;
    max-height:100%;
}

.inner-carousel > .item > img, .inner-carousel > .item > a > img {
    display: block;
    line-height: 1;
    height:350px;
    width:800px;

}

.carousel-caption {
  position: absolute;
  top: -400px;
  font-family: 'Source Sans Pro', sans-serif;
 }

.carousel-caption h3 {
    font-weight:900;
    font-size:45px;
    line-height:44px;   
    letter-spacing:-1px;
    color:#fff;
    text-shadow: rgba(0,0,0, 0.5) 1px 1px 2px;
}

.carousel-caption p {
    font-weight:100;
    font-size:18px;
    line-height:38px;
    margin-top:-30px;
}
.carousel-main #myCarousel

{
    width:100%;
    height:700px;
    margin: 0 auto;
    margin-bottom:-200px;
}


.carousel-control {
  position: absolute;
  top: 400px;
  left: 0px;
  width: 60px;
  height: 60px;
  margin-top: -20px;
  font-size: 60px;
  font-weight: 100;
  line-height: 30px;
  color: #ffffff;
  text-align: center;
  background-image:url(../img/leftarrow.png);
  background-size:cover;
  background-color:transparent;
  border: 0px solid rgb(255, 255, 255);
border-radius: 100px;
  opacity: 1;
  filter: alpha(opacity=50);
   transition: all 2s ease-in-out;
    -webkit-transition: all .6s ease-in-out; /** Chrome & Safari **/
    -moz-transition: all .6s ease-in-out; /** Firefox **/
    -o-transition: all .6s ease-in-out;
}

.carousel-control.right {
  right:0;
  left: auto;
  background-image:url(../img/rightarrow.png);

}

.carousel-control:hover {
    color: rgb(255, 255, 255);
    opacity:.6;
    text-decoration: none;
}

.carousel-control:focus {
    background-color:none;
    text-decoration: none;
    opacity: 0.9;
}



.home-carousel .carousel-caption h4 {
    font-weight:500;
    font-size:29px;
    color:#F7F4ED;
    line-height:35px;
      margin: 20px 0 20px;
  text-align:center;
}


.home-carousel .carousel-caption .introduction {
    font-weight:300;
    font-size:13px;
    line-height:19px;
    text-align:center;
    margin:0 auto;
    color:#F7F4ED;
    height:200px;
    width:250px;
}

@media (max-width: 1030px) {

    .carousel-control {
  position: absolute;
  bottom:0;
  left: 0px;
  margin-top:225px;
  width: 60px;
  height: 60px;
  font-size: 60px;
  font-weight: 100;
  line-height: 30px;
  color: #ffffff;
  text-align: center;
  background-image:url(../img/leftarrow.png);
  background-size:cover;
    background-color:#474C71;
  border: 0px solid rgb(255, 255, 255);
border-radius: 0px;
  opacity: 1;
  filter: alpha(opacity=50);
   transition: all 2s ease-in-out;
    -webkit-transition: all .6s ease-in-out; /** Chrome & Safari **/
    -moz-transition: all .6s ease-in-out; /** Firefox **/
    -o-transition: all .6s ease-in-out;
}

.carousel-control.right {
  left: 61px;
  background-image:url(../img/rightarrow.png);

}


}

以下是相同的工作版本:

http://deeptest.co.za/apexpeak/

请帮助并谢谢

3 个答案:

答案 0 :(得分:1)

您的内容位于底部,因为类名为“carousel”的元素(此元素的id ='myCarousel')已设置为:

float: left;

它位于第3行的carousel.css中。

答案 1 :(得分:1)

对于浮动和内联元素,边距(因此,负边距)的处理方式与非浮动块级元素的处理方式不同。

如果您想使用此方法定位子菜单,则需要添加

float: left;

到子菜单div及其兄弟姐妹

这样:

.carousel-main, top-menu, #myCarousel{
    float: left;
}

或类似于现有CSS中的正确位置。

答案 2 :(得分:-1)

.sub-menu {
    width: 100%;
    height: 200px;
    /*margin-top: -280px; - REMOVE THIS */
    background-color: rgba(247, 148, 30, 0.4);
    z-index: 2;
    /* ADD THE FOLLOWING */
    position: relative;
    top: -280px;
}