如何防止div溢出到另一个div上

Jac*_*ack 3 css twitter-bootstrap twitter-bootstrap-3

我对以下标题的唯一问题是,通过更改屏幕尺寸,这两个按钮与搜索按钮重叠。我需要它们留在原处,并且在小屏幕中没有足够的空间将搜索框和按钮放在水平线上。两个按钮会跳起来。就在三个小图像的下方。

我删除了一些与问题无关的CSS样式以简化代码。

演示版

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">


<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet"
    href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/4.2.1/bootstrap-social.css">

<style>

html, body {
    height: 100%;
}

body {
    margin: 0;
    padding: 0;
}

.show-xs {
    display: none;
}

@media ( max-width : 480px) {
    .top-center {
        width: 100%;
    }
    .top-right {
        padding-bottom: 4px;
    }

    #logo {
        width: 120%;
    }
    .btn-search {
        float: right;
    }
    .login {
        margin-left: 10px;
        margin-right: -23px;
    }
    .show-xs {
        display: block;
    }
    .hide-xs {
        display: none;
    }
}

body {
    margin: 0;
    padding: 0;
}

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

.top-part {
    width: 100%;
    background-color: green;
}

.top { 
    max-width: 1509px;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    text-align: center;
    position: relative;
}

.top-center {
    display: inline-block; 
    width: 100%;
    height: 100%;
}

.top-right {
    display: inline-block;
    float: right;
    height: 100%;
    margin-right: 3%; //
    width: 200px; //
    padding-left: 20px;
}

.left-part {
    float: left;
    height: 100%;
    margin-left: 6%;
    margin-top:-0.3%;
    width: 7%;
}

.middle-part {
    width: 82%;
    float: left;
    height: 100%;
    text-align: left;
}

.logo {
    text-align: left;
    padding-bottom:3px;
    padding-top:7px;
}

.login {
    text-align: left;
}

@media only screen and (max-width: 600px) {
    .top-center {
        width: 100%;
    }
}

.left-part img {
    width: 100%;
    display: block;
    padding: 2px;
    margin-top: 9px;
}

.logo img { //
    height: 40px;
    width: 70%;
    max-width: 90%;
    display: inline-block;
}

.login input, .login button {
    display: inline-block;
}

.login input {
    width: 44%;
}

@media only screen and (max-width: 600px) {
    .login input {
        width: 100%;
    }
}

.social ul {
    list-style: none;
    margin: 0;
}

.social li {
    height: 30px;
    width: 30px;
    float: right;
    background-color: blue;
    margin-left: 4px;
    margin-top: 4px;
    background-repeat: no-repeat;
    background-size: cover;
}

.social li a {
    display: inline-block;
    width: 100%;
    height: 100%;
}

.social1 {
    background-image:
        url();
}

.social2 {
    background-image:
        url();
}

.social3 {
    background-image:
        url();
}

.mybuttons {
    margin-top: -63px;
    float: right;
    padding: 0;
}

.mybuttons a {
    display: inline-block;
}


.btn-search {
    background-color: green;
    border-style: none;
}

.myBtns {
    border-style: outset;
    color: black;
    background-color: green;
    border-color: gray;
    border-width: 1px;
    padding: 2px 7px 3px 13px;
}

.myBtns:hover {
    text-decoration: none;
    color: white;
    cursor: pointer;
}

</style>
</head>
<body>

<header>
    <div class="top-part">
        <div class="left-part show-xs">
            <img
                src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR4Aa9iHfxi_6jmKGL8stVsrJ98FBVmy5DK9c6BsGHweaQ5PBoc"
                id="logo">
        </div>
        <div class="logo show-xs">
            <img
                src="https://pixabay.com/static/uploads/photo/2012/05/07/18/44/banner-48962_640.png">
        </div>
        <div class="top-right">
            <div class="social">
                <ul>
                    <li class="social3"><a href="www.googleplus.com"></a></li>
                    <li class="social2"><a href="www.twitter.com"></a></li>
                    <li class="social1"><a href="http://www.facebook.com"></a></li>
                </ul>

            </div>
        </div>
        <div class="top">
            <div class="top-center">
                <div class="left-part hide-xs">
                    <img id="logo" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR4Aa9iHfxi_6jmKGL8stVsrJ98FBVmy5DK9c6BsGHweaQ5PBoc" />
                </div>
                <div class="middle-part">
                    <div class="logo hide-xs">
                        <img src="https://pixabay.com/static/uploads/photo/2012/05/07/18/44/banner-48962_640.png" />
                    </div>
                    <div class="login">
                        <form>
                            <input id="searchType" type="hidden" name="searchType"
                                value="">
                            <input id="searchTerm" name="searchTerm"
                                class="form-control myInput" type="text" value="" />
                            <input id="searchBox2" name="searchBox2" class="form-control myInput"
                                type="text" value="" />
                            <button class="btn-search" type="submit" title="Search">
                                <div style="background-color: green;">
                                    <span class="glyphicon glyphicon-search"
                                        style="font-size: 20px;"></span>
                                </div>
                            </button>
                        </form>
                        <div id="msg" class="msg">
                            <p>Message goes here</p></div>
                    </div>

                </div>

                    <div class="mybuttons">
                        <a id="Btn1" class="myBtns" >BTN1BTN1</a> 
                        <a id="Btn2" class="myBtns">BTN2BTN2</a>
                    </div>
            </div>

        </div>

    </div>

</header>

</body>


</html>
Run Code Online (Sandbox Code Playgroud)

mac*_*mac 5

为了实现您想要的布局,您需要在同一个父容器中显示.login表单和.mybuttonsas 。inline-block如果将容器 div 添加到.login.mybuttons,则可以非常轻松地完成此操作。

这是您更新的小提琴

解释

在您的标记中,我将这两个区域都放在.middle-partdiv 中,并添加容器以设置为display: inline-block.

<div class="middle-part">
    ...

    <div class="login-outer>
        <div class="login">
          (put your login <form> and <inputs> here)                    
        </div>
    </div>
    <div class="buttons-outer">
        <div class="mybuttons">
            <a id="Btn1" class="myBtns" >BTN1BTN1</a> 
            <a id="Btn2" class="myBtns">BTN2BTN2</a>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

然后在你的CSS中:

// set both containers to inline-block
.login-outer, .buttons-outer      
{
    position: relative;
    display: inline-block;
    vertical-align: top;
}
// Specify breakpoint widths. When there isn't enough space
// to display these widths in the same line, they will stack instead.
.login-outer
{ 
    min-width: 500px;  // or whatever
}
.buttons-outer 
{ 
    min-width: 190px;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!