Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap col-md-12 vs. width 100% behave differently when show/hide div

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?

like image 332
Safecoder Avatar asked May 01 '16 08:05

Safecoder


People also ask

Is a div 100% width by default?

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.

What does Col MD 12 mean?

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)

How do you make one line with 3 divs?

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.

What is Col auto class Bootstrap?

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.


3 Answers

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>
like image 83
Narxx Avatar answered Oct 25 '22 21:10

Narxx


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>
like image 26
marvinhagemeister Avatar answered Oct 25 '22 21:10

marvinhagemeister


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.

like image 3
Kushagra Avatar answered Oct 25 '22 19:10

Kushagra