简体   繁体   中英

Nesting DIV - Nested DIV doesn't take height?

So I'm trying to set the height of the "content" class but it doesn't seem to be working. I'm quite a noob at nested DIVs and I've tried the fixes that I've found googling but nothing seems to work. Help?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="description" content="Website Horizontal Scrolling with jQuery" />
    <meta name="keywords" content="jquery, horizontal, scrolling, scroll, smooth"/>
       <link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/>
</head>
<style>   
body {
background-mage: url(bg.jpg), url(bgrepeat.jpg);
background-repeat: no-repeat, repeat-x;
background-attachment:fixed;
background-position:left bottom;
background-color: #D3C8B6;
}
#wrapper {
text-align: center;
width: 100%;
position:absolute;
bottom:0;
}
 #header,
  #main,
  #sidebar,
  #footer {
    display:inline;
    position:relative;
    float:left;

  }
  #header,
  #footer {
    width:100%;
    background-color:#eee;
            opacity:1.0;
filter:alpha(opacity=100);
  }
  #header {
    margin-bottom:1%;
    height:100px;
  }
  #footer {
    margin-top:1%;
    height:40px;
  }
  #main {
    width:20%;
    height:475px;
    margin-right:1%;
    text-align: center;
    background-color:#eee;
            opacity:1.0;
filter:alpha(opacity=100);
  }
  #sidebar {
    width:79%;
    height:475px;
    overflow: hidden;
  }
   .viewport{
       width:100%;
       height:475px;
       overflow-x:scroll;

   }

   .inside{
       width:9000px;
       height:200px;
   }

   .inside div{
       height:450px;
       width:700px;
       float:left;
       margin-right:4px;

   }
   .content {
       height:100px;
       width:300px;
       overflow-y: scroll;
       position:relative;
       top: 10px;
       right:10px;
       }

   .one{
       background-image: url(images/frame.png)
   }

   .two{
       background-image: url(images/frame2.png);
       text-align:center;
       z-index: 1;
   }

   .three{
       background-image: url(images/frame1.png);
   }
   .four{
        background-image: url(images/frame3.png);
   }
   .five {
       background-image: url(images/frame4.png);
   }

</style>
<body>
<div id="wrapper">
<div id="header">
</div>
<div id="main">
<div class="one-l">Home</div>
 <div class="two-l">Portfolio</div>
 <div class="three-l">Resume</div>
 <div class="four-l">Blog</div>
 <div class="five-l">Contact</div>
 </div>

<div id="sidebar">

<div class="viewport" >
     <div class="inside" >
         <div class="one">home</div>
         <div class="two">
         <div class="content" width="200">
         Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas bibendum, leo vitae ornare dignissim, lorem urna tempor felis, in fringilla urna justo non velit. Cras imperdiet viverra ligula, vitae auctor neque elementum eget. In nec quam est, quis molestie magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec gravida, elit a iaculis consequat, ligula nisl cursus turpis, in giat eu placerat vel, viverra vel nibh. Ut vitae felis ac nisi euismod porta. Aliquam et gravida mauris. Maecenas id massa ligula, et blandit orci. In hac habitasse platea dictumst. Donec eu tortor libero. Donec eget leo mi. Mauris quis neque vitae massa facilisis placerat ut et felis. Nullam eleifend faucibus diam, sit amet pellentesque leo euismod id. Morbi interdum placerat nibh, in mattis sem eleifend quis. Nunc non nunc sed lorem condimentum molestie mattis blandit dui. Nulla urna ligula, auctor id venenatis eu, placerat ut dui. In fringilla purus gravida sapien cursus imperdiet porta ligula lobortis. Sed pellentesque, nisi quis tristique pulvinar, justo odio sollicitudin risus, non euismod dui ante nec tortor.
         </div></div>
         <div class="three">resume</div>
         <div class="four">blog</div>
         <div class="five">contact</div>
     </div>
 </div>

This is happening because the height property defined in .inside div takes precedence. In this case, to make the height property work inside class .content you must do something like this:

.content
{
    height: 200px !important;
}

You can see a Demo at JS Bin: http://jsbin.com/exucek/1/edit

Try changing the height property value to see it being applied in real time. You'll see that when the value is too small, scroll-bars appear.


If you're curious about that !important CSS rule, take a look here:

What does !important mean in CSS?

Your CSS will use whatever the most specific rule is. You have specified a height on

.inside div

which is more specific than

.content

where you are also setting a height. In order to have your CSS use the height you have set for .content, you need to make the selector more specific, like so:

.inside div{
       height:450px;
       width:700px;
}
.inside .content {
       height:100px;
       width:300px;
}

that should do the trick.

you have a mistake in your code!
replace <div class="content" style=" width:200px;" > <div class="content" style=" width:200px;" > with

<div class="content" width=200 >  

or change your css code:

.content {
       height:200px;
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM