I am trying to implement a drop down similar to autocomplete. I noticed that it behaves differently when I used col-md-12 vs. width 100%. I have two samples below.
http://codepen.io/safecoder/pen/reQxZz
<div class="result_optins col-md-12">
It uses col-md-12. When I start typing in the input, the text below got pushed down.
http://codepen.io/safecoder/pen/YqRqWm
<div class="result_optins">
.search_cont .result_optins{
width: 100%;
display:none;
}
It uses width: 100%. When I start typing in the input, the options box overlaps the text below.
I could not figure out why it behaves differently. Can anyone shed some light on this?
Also, how can I achieve what it looks like in the second case (overlapping) if I still want to use col-md-12?
auto automatically computes the width such that the total width of the div fits the parent, but setting 100% will force the content alone to 100%, meaning the padding etc. will stick out of the div, making it larger than the parent. so setting the 'width' to 'auto' would be better? Yes, but that's the default anyway.
In short, they are used to define at which screen size that class should apply: xs = extra small screens (mobile phones) sm = small screens (tablets) md = medium screens (some desktops) lg = large screens (remaining desktops)
Three or more different div can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side.
If you use col class the columns will be of equal width. If you use col-auto the width of the column will be the width of the contents in the column. You can even use a combination of both, like below. In this case the first column width will be equal to the contents of that column.
Bootstrap's column has float: left
, and that's why it looks different.
If you want to keep overlapping, you'll need to override Bootstrap's float: left
by adding a helper class of your own, with float: none
.
In your custom CSS file you could do something like:
.h-fn {
float: none !important;
}
and add that class to the proper HTML element:
<div class="result_optins col-md-12 h-fn"> ... </div>
A much cleaner way to position dropdown options is with position: absolute
. No need for complex floats or anything else.
$('input').on('keyup', function() {
$('.result').addClass('result-visible');
});
$(document).on('click', function() {
$('.result').removeClass('result-visible');
});
.input-wrapper {
position: relative;
display: inline-block;
}
.result {
position: absolute;
top: 100%;
left: 0;
display: none;
margin: 0;
padding: 0;
background: #eee;
min-width: 100%;
}
.result li {
padding: .2rem;
border-bottom: 1px solid #aaa;
}
.result-visible {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input-wrapper">
<input type="text" placeholder="type here">
<ul class="result">
<li>Result 1</li>
<li>Result 2</li>
</ul>
</div>
<div>Content below input</div>
The bootstrap class col-md-12
also has float: left
set which helps in overflowing the child element from the parent. Since your search bar has a fixed height, you'll need to add float-left
to .result_optins
class and it will behave in the same way.
Here's a pen for better understanding of what's happening.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With