简体   繁体   中英

How do I stack divs on top of each other in a form box?

So I can't quite figure out why my divs are overlapping the way that I want them to position. I'm trying to make a form back that contains a header that overflows over the sides of the div, but to no avail. I've attached a picture along with this so that you can visualize what I'm trying to accomplish. It's just not coming out right.

我想做的

Created an form box with the following HTML:

<div class="container">
                <div class="row">


                    <div class="span4 featured">    
                        <h2 class="feature-title">It's Never Been Easier!
                            <p>Create Your Free Profile Now!</p></h2>
                        <form action="send-mail.php" method="post" id="contact-form">

                            <input type="text" value="Full Name" name="name" class="required">
                            <input type="text" value="Email Address" name="email" class="required email">
                            <input type="text" value="Username" name="username">
                            <input type="text" value="Password" name="password">
                            <input type="submit" name="subscribe" value="JOIN FREE">
                        </form>
                    </div>

and CSS:

.featured form input[type="submit"] {
    background: #3097be;
    color: #ffffff;
    width: 100%;
    margin-bottom: 0px;
    font-size: 20px;
    font-family: 'Open Sans', sans-serif;
    -webkit-transition: background linear .2s;
    -moz-transition: background linear .2s;
    -o-transition: background linear .2s;
    padding-left: 20intro .container {
    padding-top: 0px;
}

.featured {
    min-height: 500px;
    float: right;
    margin-top: -900px;
    margin-bottom: 30px;
}

.featured h2 {
        font-size: 24px;
        overflow: visible;
    background: url(http://i.imgur.com/oP8bXR7.png) center no-repeat;
    -webkit-border-radius: 0px; 
    -moz-border-radius: 0px; 
    -o-border-radius: 0px; 
    border-radius: 0px;
    padding: 20px 0px 5px 30px;
    color: black;
    margin-bottom: 0px;
}

.featured form {
    background: url(http://i.imgur.com/swcwFW6.png) center no-repeat;
    -webkit-border-radius: 0px; 
    -moz-border-radius: 0px; 
    -o-border-radius: 0px; 
    border-radius: 0px;
    padding: 20px 30px 30px 30px;
    margin-top: -20px;
}

.featured p {
    color: blue;
    text-align: center;

}

.featured form input{
    width: 225px;
    -webkit-border-radius: 0px; 
    -moz-border-radius: 0px; 
    -o-border-radius: 0px; 
    border-radius: 0px;
    border: none;
    padding: 3px 9px 3px 9px;
    color: #bebebe;
    box-shadow: none;
    font-family: 'Lato', sans-serif;
    margin-bottom: 20px;
    -webkit-transition: background linear .2s,color linear .2s;
    -moz-transition: background linear .2s,color linear .2s;
    -o-transition: background linear .2s,color linear .2s;
}

.featured form textarea {
    width: 200px;
    height: 70px;
    background: #474747;
    -webkit-border-radius: 0px; 
    -moz-border-radius: 0px; 
    -o-border-radius: 0px; 
    border-radius: 0px;
    border: none;
    padding: 12px 20px 12px 20px;
    color: #bebebe;
    box-shadow: none;
    font-family: 'Lato', sans-serif;
    margin-bottom: 30px;
    -webkit-transition: background linear .2s,color linear .2s;
    -moz-transition: background linear .2s,color linear .2s;
    -o-transition: background linear .2s,color linear .2s;
}

i dont think your html markup is going to effectively be able to give you the visual appearance you want. Also look up semantic HTML, you shouldnt be having a paragraph (p) tag inside a heading (h2) tag - they are for entirely different purposes as their names suggest.

you could try HTML like this:

<div class="span4 featured">
    <div class="feature-title">    
    <h2>It's Never Been Easier!</h2>
    <p>Create Your Free Profile Now!</p>
    </div>
    <form action="send-mail.php" method="post" id="contact-form"></form>
</div>

and then you could apply negative margins to the left and right:

.feature-title {margin: 0 -20px}

of course there are many ways to skin a cat and there are half a dozen ways you could achieve what you want to do

Some general formatting and a few missed tags not withstanding, the largest issue seems to be the 900px negative margin you have on .featured for some reason. It moves the whole thing up past the top of the page. I couldn't even see the form when I first popped your code into Dreamweaver. Otherwise, to simply get the input fields to stack I suggest setting them to display: block; and they should each get their own line. A simple margin: 0 auto; or in your case margin 0 auto 20px; (as long as the left and right margins are set to auto) will keep it centered.

The full content of my test document is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
    .featured form input[type="submit"] {
        background: #3097be;
        color: #ffffff;
        width: 100%;
        margin-bottom: 0px;
        font-size: 20px;
        font-family: 'Open Sans', sans-serif;
        -webkit-transition: background linear .2s;
        -moz-transition: background linear .2s;
        -o-transition: background linear .2s;
        padding-left: 20intro;
    }
    .container {
        padding-top: 0px;
    }

    .featured {
        min-height: 500px;
        float: right;
        margin-bottom: 30px;
    }

    .featured h2 {
        font-size: 24px;
        overflow: visible;
        background: url(http://i.imgur.com/oP8bXR7.png) center no-repeat;
        -webkit-border-radius: 0px; 
        -moz-border-radius: 0px; 
        -o-border-radius: 0px; 
        border-radius: 0px;
        padding: 20px 0px 5px 30px;
        color: black;
        margin-bottom: 0px;
    }

    .featured form {
        background: url(http://i.imgur.com/swcwFW6.png) center no-repeat;
        -webkit-border-radius: 0px; 
        -moz-border-radius: 0px; 
        -o-border-radius: 0px; 
        border-radius: 0px;
        padding: 20px 30px 30px 30px;
        margin-top: -20px;
    }

    .featured p {
        color: blue;
        text-align: center;

    }

    .featured form input{
        display: block;
        width: 225px;
        -webkit-border-radius: 0px; 
        -moz-border-radius: 0px; 
        -o-border-radius: 0px; 
        border-radius: 0px;
        border: none;
        padding: 3px 9px 3px 9px;
        color: #bebebe;
        box-shadow: none;
        font-family: 'Lato', sans-serif;
        margin: 0 auto 20px;
        -webkit-transition: background linear .2s,color linear .2s;
        -moz-transition: background linear .2s,color linear .2s;
        -o-transition: background linear .2s,color linear .2s;
    }

    .featured form textarea {
        width: 200px;
        height: 70px;
        background: #474747;
        -webkit-border-radius: 0px; 
        -moz-border-radius: 0px; 
        -o-border-radius: 0px; 
        border-radius: 0px;
        border: none;
        padding: 12px 20px 12px 20px;
        color: #bebebe;
        box-shadow: none;
        font-family: 'Lato', sans-serif;
        margin: 0 auto 30px;
        -webkit-transition: background linear .2s,color linear .2s;
        -moz-transition: background linear .2s,color linear .2s;
        -o-transition: background linear .2s,color linear .2s;
    }
</style>
</head>

<body>
<div class="container">
    <div class="row">
        <div class="span4 featured">    
            <h2 class="feature-title">It's Never Been Easier!</h2>
            <p>Create Your Free Profile Now!</p>
            <form action="send-mail.php" method="post" id="contact-form">

                <input type="text" value="Full Name" name="name" class="required">
                <input type="text" value="Email Address" name="email" class="required email">
                <input type="text" value="Username" name="username">
                <input type="text" value="Password" name="password">
                <input type="submit" name="subscribe" value="JOIN FREE">
            </form>
        </div>
    </div>
</div>
</body>
</html>

EDIT: I didn't even notice the p tag inside the h2. I just focused on CSS.

Try something like this: Demo

HTML:

<div class="container">
    <div class="header">
        <div>Its never...</div>
        <div>Create...</div>
    </div>
    <div class="form">
        <div class="input-item">
            <input name="input1" type="text" />
        </div>
        <div class="input-item">
            <input name="input2" type="text" />
        </div>
        <div class="input-item">
            <input name="input3" type="text" />
        </div>
        <div class="join">
            Join!
        </div>
    </div>
</div>

CSS:

.container {
    margin: 30px;
    background: gray;
    position: relative;
    width: 200px;
    padding-top: 60px;
}

.header {
    border: 1px solid black;
    background: white;
    position: absolute;
    top: 10px;
    left: -10px;
    width: 220px;
}

.form {
    background: darkgray;    
}

.input-item {
    margin: 5px;
}

.join {
    background: lightblue;
}

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