CSS不将高度和背景颜色应用于<div>

时间:2016-04-28 15:04:16

标签: html css

为什么第3个DIV没有将背景颜色应用于整个div?有更好的方法吗?

&#13;
&#13;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <link rel="stylesheet" type="text/css" href="Page1.css">
 </head>
 <body>

<div id="wrapper"> 

Wrapper

<div id="div1">
  
  DIV1
  
</div>
  

  
  
<div id="div2">
  
  DIV2
  
</div>
  

  
  
<div id="div3">
  
 <h1>Dolorem Ipsum</h1> 
		
Dolorem Ipsum
Lot's of text: Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. Phasellus nec odio 
iaculis nisl sollicitudin imperdiet id ut lacus. 
Donec efficitur dolor non lacus fermentum egestas 
a vel metus. Vivamus lacinia pellentesque risus, quis 
volutpat urna dapibus vel. Nulla egestas vulputate neque 
sed congue. Nunc sagittis rhoncus ante a ultricies. 
Suspendisse sit amet tristique neque, vitae dignissim tortor. 
Pellentesque eu tortor elit. Aenean mollis urna urna, 
ac dapibus enim finibus rhoncus. Pellentesque imperdiet sollicitudin elementum. 
Praesent bibendum auctor neque, ac venenatis purus semper malesuada. 
Donec ut libero ornare, tincidunt sapien at, malesuada purus. 
Etiam ullamcorper, justo nec malesuada mollis, ante libero pharetra lacus, 
a pretium mauris sapien vitae leo. Nam eu sollicitudin nulla. 
Donec ut pellentesque eros, non convallis orci. Integer cursus orci nec nulla 
malesuada pellentesque. Morbi dapibus vestibulum commodo. Quisque non 
sodales ante. Sed faucibus ultrices mauris, ac finibus ex laoreet in. 
Donec libero enim, suscipit quis convallis eget, vulputate eu nunc. 
Maecenas tempor in eros non efficitur. Aliquam aliquet velit a tellus 
ultricies, vitae porta leo efficitur. Duis vitae imperdiet arcu. 
Suspendisse egestas nunc non nibh accumsan, a fermentum risus varius. Maecenas 
vitae eros eu justo laoreet malesuada feugiat aliquet turpis. Nullam in 
enim at felis imperdiet suscipit ut a sapien. Vivamus fermentum vestibulum nibh viverra pellentesque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nam pharetra risus justo, eget fermentum lorem rhoncus quis. Sed neque diam, dictum eget laoreet nec, hendrerit ac tellus. Aliquam risus enim, lacinia non congue vitae, volutpat sed risus. Nullam interdum, magna tempor interdum egestas, turpis arcu lacinia mi, non auctor massa dui nec elit. Pellentesque ac convallis mi, non dapibus justo. Suspendisse pulvinar condimentum est id lacinia. Aliquam vulputate lorem vitae velit gravida imperdiet. Quisque lacinia, velit et faucibus consequat, purus lacus maximus dui, et commodo risus ante quis enim. Mauris ut neque cursus, eleifend dui ut, tempor nisl. Fusce ut pharetra risus, ac rhoncus dui. Fusce nisl nunc, accumsan vitae imperdiet ut, bibendum ac nisl. Aliquam auctor magna at magna finibus accumsan. Etiam at libero pellentesque, venenatis odio in, bibendum odio. --------------------------------------------------------
			
</div>
 
</div>
 
 
 </body>
</html>
&#13;
Sub LoopExample()
On Error GoTo Error_Handler
Dim db              As DAO.Database
Dim rs              As DAO.Recordset
Dim iCount          As Integer
Dim strSQL          As String
Dim intManu         As Integer

strSQL = "SELECT * FROM Manufacturer"

Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL) 'open the recordset for use (table, Query, SQL Statement)

        Do While Not rs.EOF

            intManu = rs!ID
            strSQL = "SELECT Manuf FROM Manufacturer WHERE CustomerID='" & intManu & "'"

            'db.Execute (strSQL), dbFailOnError
            'DoCmd.OpenQuery strSQL, acViewNormal
            'CurrentDb.OpenRecordset (strSQL)
            Debug.Print strSQL

        rs.MoveNext
        Loop


rs.Close 'Close the recordset

Error_Handler_Exit:
On Error Resume Next
'Cleanup after ourselves
Set rs = Nothing
Set db = Nothing
Exit Sub

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf & "Error Number: " & _
Err.Number & vbCrLf & "Error Source: LoopRecExample" & vbCrLf & "Error Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

高度会被忽略,因为它已完全定位并且您已将top 设置为bottom

由于顶部和底部边缘的位置是明确设置的,因此高度无效。

答案 1 :(得分:0)

This jsFiddle可能是一种可能的解决方案。

    body {
      margin: 0 auto;
    }
    #wrapper {
      margin: 0 auto;
      height: 100%;
      width: 100%;
      right: 0px;
      left: 0px;
      top: 0px;
      bottom: 0px;
      position: relative;
      background-color: black;
    }
    #div1 {
      margin: 0 auto;
      height: 120px;
      width: 100%;
      right: 0px;
      left: 0px;
      top: 0px;
      position: fixed;
      background-color: #222222;
      text-align: center;
      color: white;
      z-index: 2;
    }
    #div2 {
      margin: 0 auto;
      height: 300px;
      width: 100%;
      top: 120px;
      right: 0px;
      left: 0px;
      position: absolute;
      background-color: #990000;
      text-align: center;
      color: white;
    }
    #div3 {
      margin: 0 auto;
      width: 100%;
      top: 380px;
      bottom: 0px;
      right: 0px;
      left: 0px;
      position: relative;
      background-color: #999955;
      height: 100%;
      min-height: 100%;
    }
<div id="wrapper">

  Wrapper

  <div id="div1">

    DIV1

  </div>




  <div id="div2">

    DIV2

  </div>




  <div id="div3">

    <h1>Dolorem Ipsum</h1> Dolorem Ipsum Lot's of text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec odio iaculis nisl sollicitudin imperdiet id ut lacus. Donec efficitur dolor non lacus fermentum egestas a vel metus. Vivamus lacinia pellentesque risus,
    quis volutpat urna dapibus vel. Nulla egestas vulputate neque sed congue. Nunc sagittis rhoncus ante a ultricies. Suspendisse sit amet tristique neque, vitae dignissim tortor. Pellentesque eu tortor elit. Aenean mollis urna urna, ac dapibus enim finibus
    rhoncus. Pellentesque imperdiet sollicitudin elementum. Praesent bibendum auctor neque, ac venenatis purus semper malesuada. Donec ut libero ornare, tincidunt sapien at, malesuada purus. Etiam ullamcorper, justo nec malesuada mollis, ante libero pharetra
    lacus, a pretium mauris sapien vitae leo. Nam eu sollicitudin nulla. Donec ut pellentesque eros, non convallis orci. Integer cursus orci nec nulla malesuada pellentesque. Morbi dapibus vestibulum commodo. Quisque non sodales ante. Sed faucibus ultrices
    mauris, ac finibus ex laoreet in. Donec libero enim, suscipit quis convallis eget, vulputate eu nunc. Maecenas tempor in eros non efficitur. Aliquam aliquet velit a tellus ultricies, vitae porta leo efficitur. Duis vitae imperdiet arcu. Suspendisse
    egestas nunc non nibh accumsan, a fermentum risus varius. Maecenas vitae eros eu justo laoreet malesuada feugiat aliquet turpis. Nullam in enim at felis imperdiet suscipit ut a sapien. Vivamus fermentum vestibulum nibh viverra pellentesque. Pellentesque
    habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nam pharetra risus justo, eget fermentum lorem rhoncus quis. Sed neque diam, dictum eget laoreet nec, hendrerit ac tellus. Aliquam risus enim, lacinia non congue vitae,
    volutpat sed risus. Nullam interdum, magna tempor interdum egestas, turpis arcu lacinia mi, non auctor massa dui nec elit. Pellentesque ac convallis mi, non dapibus justo. Suspendisse pulvinar condimentum est id lacinia. Aliquam vulputate lorem vitae
    velit gravida imperdiet. Quisque lacinia, velit et faucibus consequat, purus lacus maximus dui, et commodo risus ante quis enim. Mauris ut neque cursus, eleifend dui ut, tempor nisl. Fusce ut pharetra risus, ac rhoncus dui. Fusce nisl nunc, accumsan
    vitae imperdiet ut, bibendum ac nisl. Aliquam auctor magna at magna finibus accumsan. Etiam at libero pellentesque, venenatis odio in, bibendum odio. --------------------------------------------------------

  </div>

</div>

答案 2 :(得分:0)

只需删除height并定义某个值中的margin: 0 auto; width: 100%; top: 420px; height:780px; right: 0px; left: 0px; position: absolute; background:green; text-align:center; ,而不是百分比。

package mydraw;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class DrawImageMini {

public static void main(String[] args) throws ColorException {new DrawImageMini();}

/** Application constructor:  create an instance of our GUI class */
public DrawImageMini() throws ColorException { window = new DrawMiniGUI(this); }

protected JFrame window;
}

class DrawMiniGUI extends JFrame {
DrawImageMini app;
Container       cp;
NavigationPanel navigationPanel;
JPanel          drawPanel;

/**
 * The GUI constructor does all the work of creating the GUI and setting
 * up event listeners.  Note the use of local and anonymous classes.
 */
public DrawMiniGUI(DrawImageMini application) throws ColorException {
    super("Draw");        // Create the window
    app = application;    // Remember the application reference

    // selector for drawing modes
    JComboBox shape_chooser = new JComboBox();
    shape_chooser.addItem("Scribble");
    shape_chooser.addItem("Rectangle");
    shape_chooser.addItem("Oval");

    // selector for drawing colors
    JComboBox color_chooser = new JComboBox();
    color_chooser.addItem("Black");
    color_chooser.addItem("Blue");
    color_chooser.addItem("Red");
    color_chooser.addItem("Green");

    // Create two buttons
    JButton clear = new JButton("Clear");
    JButton quit = new JButton("Quit");

    // Set a LayoutManager, and add the choosers and buttons to the window.
    cp = this.getContentPane();
    cp.setLayout(new BorderLayout());

    // Setzt einen Panel, die Buttons in einer Leiste hat.
    navigationPanel = new NavigationPanel(new FlowLayout());
    navigationPanel.add(new JLabel("Shape:"));
    navigationPanel.add(shape_chooser);
    navigationPanel.add(new JLabel("Color:"));
    navigationPanel.add(color_chooser);
    navigationPanel.add(quit);
    navigationPanel.add(clear);
    navigationPanel.setBackground(Color.magenta);

    // Setzt den Panel, auf dem gemalt wird
    drawPanel = new JPanel();

    cp.add(navigationPanel, BorderLayout.NORTH, 0);
    cp.add(drawPanel, BorderLayout.CENTER, 1);

    // Handle the window close request similarly
    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            app.window.dispose();
            System.exit(0);
        }
    });

    // Finally, set the size of the window, and pop it up
    drawPanel.setPreferredSize(new Dimension(600, 600));
    this.pack();
    drawPanel.setBackground(Color.red);
    this.setVisible(true);
}
public int getHeight(){
    return drawPanel.getHeight();
}

public int getWidth(){
    return drawPanel.getWidth();
}
}