Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS float with min-width and margin

I was hoping you might be able to help with a problem that's been driving me nuts for a few hours now. I am trying to design a page for a search engine that can add X number of criteria and then perform a search. I'm trying to get the criteria box to re-size depending on the number of criteria, and the screen resolution. I want it to load at an initial size, and then resize itself depending on what's added to it, so that the search button and search results below move downwards. Right now, I can do this using a min-width:100%, but this produces scrollbars and the text overflows onto the right of the page because I'm using a margin-left:340px; to move this box to the right, as there is another box to the left of it, searchList.

Removing min-width causes the scroll bars to go away, but then the box is not drawn until an element is added. Adding one element causes a small box to be drawn, and only after adding a few does the whole box go to the right size. Is there some way I can move the box sideways and keeping it in line with the rest of the page, and what is the best way to achieve this?

It's the same on both chrome and firefox.

The box I'm talking about is <div id="searchCriteria"></div>. Full code is below. I would really appreciate any help :)

<html>
<head>
<style type="text/css"> 

body {
padding:0px;
margin:0px;
}

#content {
min-width:950px;
}

#header {
height:130px;
background-color:blue;
}

#searchList {
width: 300px;
height: 400px;
background-color:green;
position:absolute;
}

#searchCriteria {
background-color:red;
float:left;
min-height:400px;
min-width: 100%;
margin-left: 340px;
}


#searchResults {
background-color:purple;
height: 800px;
width: 100%;
float:left;
}

.criteria {
border: 1px solid black;
padding: 10px;
margin: 10px;
float: left;
width: 400px;

}

#buttonAndStatus {
float:left;
background-color:pink;
width:100%;
text-align:center;
}



</style>

<script type="text/javascript">

function add(){
    $("#searchCriteria").append("<p class=\"criteria\">This is a bit of search criteria.</p>");
}

</script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
</head>
<body>
    <div id="header">fasdfasdfasdf asdf asdf asdf asdf asdf asdf </div>
    <div id="content">
            <div id="searchList">
                <input type="button" name="button" Value="Click me" onClick="add()">
            </div>
            <div id="searchCriteria"></div>
            <div id="buttonAndStatus"><input type="button" name="button" Value="Search "></div>
            <div id="searchResults">asdf asdf asdf asdf </div>
    </div>
</body>
</html>
like image 633
Provster Avatar asked Nov 13 '22 16:11

Provster


1 Answers

You are getting horizontal scroll because you're telling the browser to be at least 100% in width. Just tested it, it works with max-width not min-width and add a min-width of the smallest width you'll have i.e:

#searchCriteria {
    background-colour:red;
    float:left;
    min-height:400px;
    min-width:350px; /*min width of criteria box*/
    max-width:100%; /*you don't want the width to be more 100%*/
    margin-left:340px;
}
like image 167
Digital Robot Gorilla Avatar answered Nov 15 '22 05:11

Digital Robot Gorilla