Kam
Kam

Reputation: 6008

How can I remove spaces between two side by side elements on all browsers

I have this code:

<div class="headmenus">
    <ul>
    <li><a href="index.php">HOME</a></li>
    <li><a href="about.php">ABOUT US</a></li>
    <li><a href="search.php">COURSES</a></li>
    <li><a href="vacancies.php">VACANCIES</a></li>
    <li><a href="contact.php">CONTACT US</a></li>  
    <li></li>           
    </ul>
 </div>
 <div class="searchform2">
    <form action="search.php#goto1" method="get" id="headersearch">
       <input type="hidden" name="p" value="0" />
       <input type="text" name="keyword" id="headerkeyword" placeholder="Search any keyword" value="" />
       <input type="submit" value="SEARCH" class="but1" />
    </form>
 </div>

I'd like the headmenus and searchform2 that are positioned side by side to be touching on all browsers i.e. I want to remove spaces between headmenus and searchform2 on all browsers.

Here's the CSS:

.searchform2 {
    float: right;
    display: inline;
    width:auto !important;
    border:2px solid #616566;
}
.searchform2 input {
    border:2px solid #616566;
    float: left;
/*  margin-right: 15px;
    padding: 4px;
    width: auto;
    height:26px;
*/}
.searchform2 .but1 {
    background: url("images/search.png") no-repeat scroll 8px 9px #89AB20;
    border: 0 none !important;
    color: #FFFFFF !important;
    float: right !important;

    display: inline;
    font-weight: bold;
    padding: 6px 12px 6px 23px !important;
    width: auto !important;
    font-family: Arial;
    font-size: 12px;
    height: 30px;
    margin-right: 0 !important;
    cursor:pointer;
}

and here's the headmenus css:

.headmenus {
    background: none repeat scroll 0 0 #0086B2;
    display: inline;
    float: left;
    margin: 0;
    width: 74.3%;
}
.headmenus ul {
    border-bottom: 1px solid #FFFFFF;
    border-top: 1px solid #FFFFFF;
    display: inline;
    float: left;
    font-family: 'texgyreadventorregular';
    font-size: 14px;
    margin: 0;
    padding: 0;
    width: 100%;
}
.headmenus ul li {
    border-right: 1px solid #FFFFFF;
    color: #FFFFFF;
    display: inline;
    float: left;
    list-style-type: none;
    padding: 0;
    text-align: center;
    width: 13.35%;
}

.headmenus ul li a {
    color: #FFFFFF;
    display: inline;
    float: left;
    font-family: 'texgyreadventorregular';
    font-size: 14px;
    list-style-type: none;
    text-align: center;
    width: 100%;
    padding:5px 0px;
}

.headmenus ul li:hover {
    background: #89ab20;
    cursor: pointer;
}
.headmenus ul li.active {
    background: #89ab20;
}
.headmenus ul li:last-child {
    border-right: 0 none;
    float: right;
    width: auto; !important
}

What's happening now is that the headmenus, when I play with its width, I can make sure it touched the input text box but when I switch browsers I need to re-edit that width property

Upvotes: 1

Views: 202

Answers (2)

RobertO
RobertO

Reputation: 2663

You don't need any box-sizing. Just set .searchform2 width: 25.7%, move border to .searchform2 form and add overflow: hidden due to floating elements inside.

See fiddle: http://jsfiddle.net/nNQF9/9/

I'd like to explain something:

total width or height of element = width/height + padding + margin + border-width

For instance if is set width: 50% and additionaly border: 2px soid whatever then total width of element is 50% + 4px.

If we want widths to perfectly fit some ratio and we need borders, padding or margins then we need more cointainers:

<div class="cointainer_left_75_percents">
   <div class="container_with_borders_paddings_margins">
      ...
   </div>
</div>
<div class="cointainer_right_25_percents">
   <div class="container_with_borders_paddings_margins">
      ...
   </div>
</div>

Or we can use experimental property box-sizing which changes impact of borders, paddings and margins on total widths and heights.

Upvotes: 0

Mandy Schoep
Mandy Schoep

Reputation: 981

You should remove your border-right: 1px solid #FFFFFF; in .headmenus ul li. Then the button will be next to each other without a gap. See this fiddle for an example (elements are gray for visibility).

*{box-sizing: border-box; -moz-box-sizing: border-box;} takes care of displaying it next to each other in all browsers. Check out this article from Paul Irish if you are interested in the box-sizing property.

You need to make sure that your .searchform2 has a width:25.6% !important; which makes sure that it fills up all the empty space.

Upvotes: 1

Related Questions