using search bar css in react - html

I am trying to use a search bar css from code pen.
I used their exact code but the search bar seems to be really buggy on react.
Here is code sandbox for better reference. https://codesandbox.io/s/sad-moser-mdxmd?file=/src/Dashboard.js
Here's the code that I am currently using.
<div class="wrap">
<div class="search">
<input type="text" class="searchTerm" id="input_text"></input>
<button type="submit" class="searchButton">
<i class="fa fa-search"></i>
</button>
</div>
</div>
.search {
width: 100%;
position: center;
display: flex;
}
.searchTerm {
width: 100%;
border: 3px solid #00b4cc;
border-right: none;
padding: 5px;
height: 20px;
border-radius: 5px 0 0 5px;
outline: none;
color: #9dbfaf;
}
.searchTerm:focus {
color: #00b4cc;
}
.searchButton {
width: 40px;
height: 36px;
border: 1px solid #00b4cc;
background: #00b4cc;
text-align: center;
color: #fff;
border-radius: 0 5px 5px 0;
cursor: pointer;
font-size: 20px;
}
/*Resize the wrap to see the search bar change!*/
.wrap {
width: 30%;
position: flex;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
I want the search bar to look like this:
This is the link to the code that I attempted to use.
https://codepen.io/huange/pen/rbqsD

Change the css for .wrap to this -
.wrap {
/* width: 30%; */
display: flex;
/* top: 50%;
left: 50%;
transform: translate(-50%, -50%); */
}
Surely you meant display: flex; and not positon: flex;

Explanation
First, flex is for display, not for position. You can use both together, but they don't probably interact in the way you'd think.
Position strictly deals with where the element is on the page (either relative to where it would be while in the natural flow, absolutely compared to the closest parent with an explicit position, or fixed somewhere on the screen).
Display deals with how the elements are laid out (or if at all). I won't go into all of the display possibilities here, but block, inline-block, inline, and flex are the most popular and you can easily learn about them with a Google or Youtube search.
To answer your question, you need to remove position from your .wrap class as well as all of your position related items. Then add margin for centering and top and bottom space (remember that margin auto for left and right will center it). Then, to do things the react way, you need to install react-fontawesome with npm and use the icon like that for the most natural and best performance. Here's a link for how to do that, with examples of usage at the bottom: https://fontawesome.com/how-to-use/on-the-web/using-with/react
You also need a placeholder label if you want your search bar to be identical to the one on CodePen. This will give you the faded text before the user starts typing. I've added it as a prop in the input.
Finally (and this isn't appearance related), you probably want to set up two-way binding with your input so that you can actually do something with the value later. I think my code shows what's happening simply enough, but basically you just have a value in your state that's tied to your input. When the input changes, that state is updated, then when the input is rerendered, it reads its value from the state. This gives you access to the value in the other parts of your code.
CSS
.search {
width: 100%;
position: center;
display: flex;
}
.searchTerm {
width: 100%;
border: 3px solid #00b4cc;
border-right: none;
padding: 5px;
height: 20px;
border-radius: 5px 0 0 5px;
outline: none;
color: #9dbfaf;
}
.searchTerm:focus {
color: #00b4cc;
}
.searchButton {
width: 40px;
height: 36px;
border: 1px solid #00b4cc;
background: #00b4cc;
text-align: center;
color: #fff;
border-radius: 0 5px 5px 0;
cursor: pointer;
font-size: 20px;
}
/*Resize the wrap to see the search bar change!*/
.wrap {
width: 30%;
margin: 20px auto;
/* change the margin to move where it is on the page */
}
JSX
import {Component} from 'react';
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
import { faSearch } from '#fortawesome/free-solid-svg-icons';
class Foo extends Component {
state = {
input: '',
};
render() {
return (
<div class="wrap">
<div class="search">
<input
placeholder="What are you looking for?"
value={this.state.input}
onChange={(event) => this.setState({ input: event.target.value })}
type="text"
className="searchTerm"
id="input_text"
></input>
<button type="submit" className="searchButton">
<FontAwesomeIcon icon={faSearch} />
</button>
</div>
</div>
);
}
}
Not to sell myself here, but if you'd like to play around with some of these properties interactively, here's an app I built specifically for this kind of scenario: https://csspressme.web.app/

Related

Custom checkbox stretches vertically when text wraps

I have a styled checkbox that is the child of a container along with some text. The reason the checkbox and text are children of a parent is so that they can sit next to each other and be centered vertically on the UI. This has worked fine for me; however, I've noticed that the checkbox starts to change from a perfect circle into more of an oval as text starts to wrap into multiple lines (on mobile the text is two lines long and on desktop it is only one line). How could I fix this so that the checkbox does not stretch as the text wraps into multiple lines? Below is my html and styling, thank you.
.opt-in {
display: flex;
align-items: center;
color: #f4a11e;
}
input[type='checkbox'] {
position: relative;
margin: 17px 15px 0 0;
cursor: pointer;
width: 20px;
height: 20px;
-webkit-appearance: none;
background: transparent;
border: 1px solid #f4a11e;
outline: none;
border-radius: 50%;
transition: 0.5s;
}
input[type='checkbox']:before {
content: '';
position: absolute;
top: 45%;
left: 50%;
width: 4px;
height: 10px;
opacity: 0;
border-right: 1px solid #f4a11e;
border-bottom: 1px solid #f4a11e;
transform: translate(-50%, -50%) rotateZ(40deg);
transition: 0.2s;
}
input:checked[type='checkbox']:before {
opacity: 1;
}
<div class="opt-in">
<input type="checkbox" v-model="optIn" id="checkbox" />
<label for="checkbox">Opt-in to receive the latest cloud insights and industry deep dives.</label>
</div>
In the CSS code in the input[type='checkbox'] {} section, try using min-width: 20px; rather than width: 20px;.
Worked for me in a test implementation with copy/pasted code although I had to set max-width in the properties of the opt-in div to test it.
I don't have a technical explanation but I believe it has something to do with the relative position or the display: flex overriding the specified width/height in pixels.

Select input radio by clicking on it's parent wrapper

I've a tile with an input of type radio, a label and a span text below this two elements. The input and label are connected with an id. They should be next to each other. The span should be directly below the label. This works so far and my layout matches. If I click on the input or on the label, the radio gets selected. Now what I'd like to do is, to select the radio by clicking on the tile, no mather where. So every click on the tile should select my radio. I would like to solve this with pure HTML/CSS without using JS, if possible. The only idea I had, is to give position: relative; to the radio and position: absolute; to the radio__wrap and the label and make them have width/height of 100%, so I can pull over the label over the whole tile. This idea crashed my layout (hard do position the span correctly). Is there a way to solve this by using pure HTML/CSS. Below is my snippet:
.tile {
border: 1px solid transparent;
border-radius: 10px;
box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.2);
cursor: pointer;
margin-bottom: 30px;
}
.tile__wrap {
position: relative;
}
.radio {
padding: 24px 16px 16px;
display: flex;
flex-direction: column;
position: relative;
}
.radio__wrap {
margin-bottom: 10px;
}
.radio__label {
background-color: lightblue;
position: relative;
}
.radio__text {
padding-left: 24px;
display: block;
}
.tile--clickable .radio__wrap {
position: relative;
width: 100%;
height: 100%;
z-index: 5;
}
.tile--clickable .radio__label {
width: 100%;
height: 100%;
}
<div class="tile">
<div class="tile__wrap">
<div class="radio">
<div class="radio__wrap">
<input class="radio__input" id="radio01" type="radio">
<label class="radio__label" for="radio01">I'm the label</label>
</div>
<span class="radio__text">I'm the text</span>
</div>
</div>
</div>

how to allow a hidden button to be shown when focused using CSS only?

I have a container div and it has a hidden button inside it and it appears only when focusing on the container div. I want to make the button visible when focused (I want to make it focusable). Here's a fiddle
HTML:
<div class="ads" tabindex="0">
<button class="close" tabindex="0">X</button>
</div>
CSS:
.ads{
position: relative;
display: inline-block;
border-radius: 0.5625rem;
border-style: solid;
border-width: 0.03125rem;
border-color: lightgrey;
background-color: #ffffff;
width: 100%;
height: 80px;
}
.close{
display: none;
padding: 0;
background: transparent;
border: none;
margin-top: 0.5rem;
margin-right: 0.5rem;
float: right;
width: 0.5rem;
height: 0.5rem;
background-image: url('delete.png');
background-size: 100% 100%;
}
div.ads:focus{
background-color: #ebeded;
}
div.ads:focus .close{
display:inline-block;
}
button.close:focus{
display:inline-block;
}
How can I achieve that?
Thank you.
At any given moment of time only one element can be in focus or none.
But your solution assumes that there are two elements matching :focus in the document at the same time.
Here is sequence of events when you press TAB on focused div:
Your div looses focus so is does not match :focus;
Button gets hidden as it has not got focus yet;
as nothing visible/focusable inside the div focus moves to something else but not to the button.
You should find other solution.
Update: possible CSS only hack is to use opacity:0 instead of display:none.
Hack here is that opacity:0 element is considered as still displayed element so focusable.
input{
display: block;
width: 100%;
}
.ads{
position: relative;
display: inline-block;
border-radius: 0.5625rem;
border-style: solid;
border-width: 0.03125rem;
border-color: lightgrey;
background-color: #ffffff;
width: 100%;
height: 80px;
}
.close{
opacity: 0;
padding: 0;
background: transparent;
border: none;
margin-top: 0.5rem;
margin-right: 0.5rem;
float: right;
width: 0.5rem;
height: 0.5rem;
background-image: url('delete.png');
background-size: 100% 100%;
}
div.ads:focus{
background-color: #ebeded;
}
div.ads:focus .close{
opacity: 1.0;
}
button.close:focus{
opacity: 1.0;
}
<input type="text" placeholder="press on me and tab two times">
<div class="ads" tabindex="0">
<button class="close" tabindex="0">X</button>
</div>
<p>
by the second tab you should see focused button ,but you don't
</p>

How can I create a clickable event that changes position attributes in the CSS file of an outer div using only html and CSS?

I have a DIV container. inside it, I have a button. I want the button to be used to change the DIV's position attributes. I want the button click to shift the entire container to the left.
I have to do this without any scripting; only CSS and HTML.
is this possible?
perhaps with buttonclick:active{stuff}?
You can use the checkbox hack
#move-div {
display: none;
}
#move-div:checked + .movable {
left: -50px;
}
.movable {
position: relative;
background-color: #FF0000;
padding: 10px;
}
.button {
display: inline-block;
padding: 5px;
background-color: #FFF;
border-radius: 5px;
color: #000;
box-shadow: 3px 3px 5px 3px #AAA;
}
<input id="move-div" type="checkbox">
<div class="movable">
<label class="button" for="move-div">Move the div</label>
</div>

Strange gap between two buttons [duplicate]

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 8 years ago.
I'm experiencing a strange behaviour with the HTML button tag. It seems that when I place two buttons side by side, they have a 4px gap between them appearing out of nowhere.
Here is a fiddle which shows the issue.
As you can see from the image below, FireBug shows that the gap is neither a margin or a padding (since a padding would be shown in purple).
As a note: I'm using the latest version of Firefox on Windows 8.1 and I tried also with the CSS Reset from Eric Mayer, but the gap is still there.
It's not a really important problem, but it would be nice to know if it's normal or not and what causes it.
The problem is that in inline-block elements the whitespace in HTML becomes visual space on screen. Some solutions to fix it:
Use font-size: 0 to parent container(you have to define font-size to child elements):
.buttons {
width: 304px;
margin: 0 auto;
z-index: 9999;
margin-top: 40px;
font-size: 0;
}
button {
background-color: transparent;
border: 1px solid dimgray;
width: 150px;
height: 40px;
cursor: pointer;
}
<div class="buttons">
<button>Button1</button>
<button>Button2</button>
</div>
Another one is to use negative margin-left: -4px
.buttons {
width: 304px;
margin: 0 auto;
z-index: 9999;
margin-top: 40px;
}
button {
background-color: transparent;
border: 1px solid dimgray;
width: 150px;
height: 40px;
cursor: pointer;
margin-left: -4px;
}
<div class="buttons">
<button>Button1</button>
<button>Button2</button>
</div>
Last but i don't like it at all is to use html comments as spacers
between gaps:
.buttons {
width: 304px;
margin: 0 auto;
z-index: 9999;
margin-top: 40px;
}
button {
background-color: transparent;
border: 1px solid dimgray;
width: 150px;
height: 40px;
cursor: pointer;
}
<div class="buttons">
<button>Button1</button><!--
--><button>Button2</button>
</div>
All above will work. Good luck :)
It's because you have whitespace between button elements. Change your HTML to:
Fiddle
<div class="buttons">
<button>Button1</button><button>Button2</button>
</div>
If you just want to display one line between these buttons, add margin: -1px.
Fiddle
button {
background-color: transparent;
border: 1px solid dimgray;
width: 150px;
height: 40px;
margin: -1px;
cursor: pointer;
}
Additional Tweaks:
In Firefox, when you click on a button, it displays a weird dotted border like below:
Fiddle
To get rid of this, add this to your CSS:
button::-moz-focus-inner {
border: 0;
}
One more thing(Firefox): when you click on the button, the text moves. To prevent this add this to your CSS:
Fiddle
button:active {
padding: 0;
}
It can be corrected by
button {
background-color: transparent;
border: 1px solid dimgray;
width: 150px;
height: 40px;
cursor: pointer;
float:left;
}
As others have said, it is the whitespace between your elements. If you're using PHP, you could do something like this:
<div class="buttons">
<button>Button1</button><?php
?><button>Button2</button>
</div>
Otherwise, you could do this:
<div class="buttons">
<button>Button1</button><
button>Button2</button>
</div>
Or this, as suggested from the comments:
<div class="buttons">
<button>Button1</button><!--
--><button>Button2</button>
</div>
if you float: right; or float: left; you will see no space.
jsfiddle