Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Konrad Pilch
2,435 PointsHOw to do search like this one?
http://themes.getbootstrap.com/products/application
search bar expanding
2 Answers
Brian Hayes
20,986 PointsI came up with a pure CSS solution, as oppose to the JavaScript method used for the example you provided. Basically I used the :focus pseudo-class to change the width and added a CSS transition to animate it. You can view the codepen to see the results. Either way, here's the code. Just note, that the stuff relating to the top-bar class was just to give a container to work with. As long as the form is floated to the right the effect should work like in your example. (just watch out for collapsing containers).
<div class="top-bar">
<div class="left">
</div>
<div class="right">
<form class="search-form">
<input type="text" name="search" placeholder="Search" />
</form>
</div>
</div>
.top-bar {
background: lightblue;
padding: 0.4rem;
}
.top-bar:before {
content: "";
display: table;
}
.top-bar:after {
content: "";
display: table;
clear:both;
}
.search-form {
float: right;
}
.search-form input[name="search"] {
border-radius: 0.3rem;
border: 1px solid #aaa;
padding: 0.5rem 0.4rem;
font-size: 1.1rem;
width: 180px;
transition: width 0.3s ease-in-out;
}
.search-form input[name="search"]:focus {
width: 300px;
outline: 0;
}
Now, the example you pointed to uses JavaScript to add width values as inline styles. So while this solution pretty much accomplishes the same thing, it's not what is done in that bootstrap theme example.
William Li
Courses Plus Student 26,868 PointsCheck out this CodePen Expanding Search Bar with jQuery
Konrad Pilch
2,435 PointsThank you for this as well, when I get inot JS , I might have a look at it , unless CSS code will cause any problems id use and try to customize this one .
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsThank you, I preffer it using well, this way , hopefully it would work in older browser without JS. Shoulv though about transitions and try it xd I was thinking about it , I didn't think too deep .