CSS: Styling the appearance of the input div

Here is the code I am working with:

<div class="lighter">
  <form method="get" action="http://www.google.pl" id="search">
    <span>
      <input id="button_search" type="image" src="images/search.png" id="button_search"/>
    </span>
    <span>
      <input type="text" class="search rounded" placeholder="Search...">
    </span>
    <div class= "list_search">
      <ul>
        <li class="search_link"><a href="">Search all of Movies</a></li>
        <li class="search_link"><a href="">Advanced Search</a></li>
      </ul>
    </div>
  </form>
</div>

I am wondering if it is possible to use only CSS to make the list_search appear when the input type="text" is in focus. If so, how can this be achieved?

Thank you for your assistance.

Answer №1

If you adjust your HTML structure slightly, it is possible to achieve this effect using only CSS.

div {
    background: #f0a;
    display: none;
    height: 200px;
    width: 200px; }

input:focus + div { display: block; }

The key here is to initially hide the div, and then when the input field gains focus, the immediately following sibling div will change its display property to block.

In order for this technique to work, the targeted element must be the immediate next sibling, as traversing up the DOM tree is not possible. This means that you should separate the input from the span.

You can view a demonstration of this on my fiddle: http://jsfiddle.net/TheNix/U28sd/

UPDATE:
The "adjacent selector" (+) should be compatible with IE7 and newer versions (though there may be issues with asynchronously loaded content). Keep in mind that the targeted element must directly follow the trigger element, so it's not just "the next matching Y element", but specifically "the element right after X, IF it matches Y".

In some cases, if you are familiar with the markup and willing to experiment, you can "count" your way through elements. For instance, input + div + div would select the second div (assuming the markup remains unchanged). However, this approach is not recommended because any alterations to the markup could break the styling and complicate maintenance.

For more information on CSS selectors, refer to: http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors

Answer №2

For those who are new to JavaScript, I recommend using jQuery for its simplicity and ease of use.

$(':input').focus(function(event){
    $('.search_results').toggle();
}).blur(function(event){
    $('.search_results').toggle();
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ever thought about harnessing the power of SassDoc to create detailed documentation for your sass

After experimenting with sassdoc for documenting our sass mixins and functions, I realized that there doesn't seem to be a straightforward way to generate documentation for our sass variables. Specifically, I am looking for a solution that can output ...

Choosing changeable object label in CSS

Looking for a way to style multiple customized HTML tags (Angular.js directives) that share the same suffix? Imagine you have three modal tags that you want to style similarly: 1. <a-modal> 2. <b-modal> 3. <c-modal> Is there a selector ...

Attempting to trigger the timer to begin counting down upon accessing the webpage

Take a look at this example I put together in a fiddle: https://jsfiddle.net/r5yj99bs/1/ I'm aiming to kickstart as soon as the page loads, while still providing the option to pause/resume. Is there a way to show the remaining time as '5 minute ...

How to incorporate multiple fonts into spark RichText using CSS - Flex edition!

I'm currently facing a challenge with embedding multiple fonts in a spark RichText component. Specifically, I am unsure about how to proceed with displaying bold font using <b> tags in HTML. The font I am working with has separate regular and bo ...

Utilizing Font-face and background images in asset-pipeline plugin: A comprehensive guide

I am currently using the asset-pipeline from CodeSleeve to handle my style sheets in my Laravel project. I have successfully downloaded the application.css file, but I have a question: how can I include images and use font-face in this setup? Here is a sn ...

Efficiently centering content in a grid layout using automatic fit repetition for optimized responsiveness

I've implemented a responsive grid where each item has its own hidden details section that is revealed upon clicking the item. The structure of the HTML/CSS setup is as follows: <div class="grid"> <div class="item"> ...

What steps can be taken to avoid an input with a width of 100% from overflowing?

I'm facing an issue that needs to be resolved. Currently, I have a group of div elements arranged in a table-like structure with a row and three columns. When these divs reach a maximum width of 768px, the first column is hidden, leaving only the se ...

Adding a stylish underline to text using HTML and CSS

Looking for some assistance with my Html and CSS website project. I am currently facing a challenge trying to place a border below the text as shown in the provided image. Below is the snippet of my Html code: <div class="Sectionheading"> ...

How to make a letter stretch all the way down the paper?

As I'm creating a table of contents for my website, I was inspired by an interesting design idea that I came across: I am particularly drawn to how the T extends down and each section is numbered with the title wrapped around it. How can I achieve th ...

The background color and image failed to cover the entire CSS box model

Hi there, I've been struggling with this issue and can't seem to figure out why. No matter what adjustments I make, the background color and image just won't fill the entire CSS box model. If you want to take a look at what I'm talking ...

unable to decrease the gap between the rows of the table

I'm currently developing an application that involves a table with rows and columns, along with a checkbox container arranged as shown in the image below: https://i.stack.imgur.com/QoVrg.png One issue I'm facing is getting the letters A, B, C, ...

The Jumbotron and background images vanish as the window size transitions from full screen to smaller dimensions

Encountering an issue with my homepage: the jumbotron and background images disappear when the window size changes from full screen to small screen. However, upon resizing the window, the images re-appear. How can I make these images responsive? Below is ...

Animation of two divs stacked on top of each other

I am trying to replicate the animation seen on this website . I have two divs stacked on top of each other and I've written the following jQuery code: $('div.unternehmen-ahover').hover( function () { $('div.unternehmen-ahover' ...

Trouble with CSS Styling in Implementation of Website on Heroku

While the deployment of the application on the server works perfectly, there seems to be an issue on the website itself. The content is displayed without the CSS styles. Original Website link: This problem is not present when accessing the application lo ...

The slider feature on Google Reviews is currently not functioning properly with the owl carousel

I am facing an issue with integrating Google Reviews slider using Google API and owl carousel. The problem lies in the fact that the Google reviews data is appended last after the page has loaded completely. This causes the owl carousel function to be call ...

Continuous horizontal columns

Is there a way to create horizontal columns with inline-blocks, like the method described here, without having vertical gaps between items on the second line due to different heights? I want to eliminate the vertical gaps between the tiles using only CSS. ...

Boundary around an object within a unified picture

I'm facing a bit of difficulty with a task. I need to add a border around an element within an image (I can't insert an image due to low reputation), and this border should match the width and height of the element exactly. It also needs to be re ...

Initial row of divs impacted by stepdown process

I'm currently troubleshooting a website experiencing significant "div stepdown" issues. The problem is specifically in the 'Press' Section of The Vanity Group. The first row appears staggered, despite my attempts to adjust the display prope ...

Prevent Copying and Pasting within the Text Editor

@if (Model.CanMaintainNcrLineManagement) { <tr> <td>@Html.TextAreaFor(model => model.Description, new { id = "txArNcrLineDescriptionValue", @style = "height:520px" })</td> </tr> } else { <tr class="read-only-editor"> &l ...

Fashioning PHP using CSS

Excuse my lack of experience with PHP, but I am new to working with it. I am currently using a premium Wordpress theme called Kingsize. While the theme is visually appealing, it does not display the author on posts by default. I have managed to add it to ...