Sanjay
Sanjay

Reputation: 550

How to show div on input focus in css?

I have a input and a div whose height and width is equal to border bottom of input. I just want to show div which is firstly hidden.

.cool{
  font-size: 30px;
  margin: auto;
  outline: none;
  padding-bottom: 0.2em;
  border-width: 0px 0px 2px 0px;
  border-bottom-color: black;
}
div.bar{
  width: 365.3px;
  height: 2px;
  background-color: #42c0fb;
  position: relative;
  top: -2px;
  display: none;
}
input:focus .bar{
  display: block
}
<div style = "margin: auto;">
  <input class = "cool" type = "text" />
  <div class = "bar"></div>
</div>

Please help. The above code doesn't work.

Upvotes: 4

Views: 8683

Answers (5)

core114
core114

Reputation: 5325

Try like this

/* form starting stylings ------------------------------- */
.group 			  { 
  position:relative; 
  margin-bottom:45px; 
}
input 				{
  font-size:18px;
  padding:10px 10px 10px 5px;
  display:block;
  width:300px;
  border:none;
  border-bottom:1px solid #757575;
}
input:focus 		{ outline:none; }



/* active state */
input:focus ~ label, input:valid ~ label 		{
  top:-20px;
  font-size:14px;
  color:#5264AE;
}

/* BOTTOM BARS ================================= */
.bar 	{ position:relative; display:block; width:300px; }
.bar:before, .bar:after 	{
  content:'';
  height:2px; 
  width:0;
  bottom:1px; 
  position:absolute;
  background:#42c0fb; 
  transition:0.2s ease all; 
  -moz-transition:0.2s ease all; 
  -webkit-transition:0.2s ease all;
}
.bar:before {
  left:50%;
}
.bar:after {
  right:50%; 
}

/* active state */
input:focus ~ .bar:before, input:focus ~ .bar:after {
  width:50%;
}

/* HIGHLIGHTER ================================== */
.highlight {
  position:absolute;
  height:60%; 
  width:100px; 
  top:25%; 
  left:0;
  pointer-events:none;
  opacity:0.5;
}

/* active state */
input:focus ~ .highlight {
  -webkit-animation:inputHighlighter 0.3s ease;
  -moz-animation:inputHighlighter 0.3s ease;
  animation:inputHighlighter 0.3s ease;
}

/* ANIMATIONS ================ */
@-webkit-keyframes inputHighlighter {
	from { background:#5264AE; }
  to 	{ width:0; background:transparent; }
}
@-moz-keyframes inputHighlighter {
	from { background:#5264AE; }
  to 	{ width:0; background:transparent; }
}
@keyframes inputHighlighter {
	from { background:#5264AE; }
  to 	{ width:0; background:transparent; }
}
<div class="group">      
      <input type="text" required>
      <span class="highlight"></span>
      <span class="bar"></span>
     
    </div>

Upvotes: 2

sol
sol

Reputation: 22919

The sibling selector will work for you in this case.

But you could also use :focus-within

.cool {
  font-size: 30px;
  margin: auto;
  outline: none;
  padding-bottom: 0.2em;
  border-width: 0px 0px 2px 0px;
  border-bottom-color: black;
}

div.bar {
  width: 365.3px;
  height: 2px;
  background-color: #42c0fb;
  position: relative;
  top: -2px;
  display: none;
}

div:focus-within .bar {
  display: block
}
<div style="margin: auto;">
  <input class="cool" type="text" />
  <div class="bar"></div>
</div>

Upvotes: 1

Nisarg Shah
Nisarg Shah

Reputation: 14531

When you use input:focus .bar selector, the browser searches for .bar under the descendents of input. Whereas, .bar is a sibling of the input.

You could use one of the sibling selectors here. Like: input:focus + .bar or input:focus ~ .bar

.cool {
  font-size: 30px;
  margin: auto;
  outline: none;
  padding-bottom: 0.2em;
  border-width: 0px 0px 2px 0px;
  border-bottom-color: black;
}

div.bar {
  width: 365.3px;
  height: 2px;
  background-color: #42c0fb;
  position: relative;
  top: -2px;
  display: none;
}

input:focus + .bar {
  display: block
}
<div style="margin: auto;">
  <input class="cool" type="text" />
  <div class="bar"> Hello</div>
</div>

Upvotes: 7

Sasha B.
Sasha B.

Reputation: 9

  .cool{
			font-size: 30px;
			margin: auto;
			outline: none;
			padding-bottom: 0.2em;
			border-width: 0px 0px 2px 0px;
			border-bottom-color: black;
		  }
  div.bar{
			  width: 365.3px;
			  height: 2px;
			  background-color: #42c0fb;
			  position: relative;
			  top: -2px;
			  display: none;
        }
  input:focus + .bar{
			display: block
		    }
<div style = "margin: auto;">
		<input class = "cool" type = "text" />
		<div class = "bar"></div>
</div>

Upvotes: 0

Super User
Super User

Reputation: 9642

Use sibling selector + for this. check updated snippet below..

.cool{
  font-size: 30px;
  margin: auto;
  outline: none;
  padding-bottom: 0.2em;
  border-width: 0px 0px 2px 0px;
  border-bottom-color: black;
}
div.bar{
  width: 365.3px;
  height: 2px;
  background-color: #42c0fb;
  position: relative;
  top: -2px;
  display: none;
}
input:focus + .bar{
  display: block
}
<div style = "margin: auto;">    
    <input class = "cool" type = "text" />
    <div class = "bar">bar text</div>
</div>

Upvotes: 1

Related Questions