I'm trying to implement some designs in CSS, but having a bit of trouble figuring out how to align this <span> correctly.
I am trying to achieve the <input> and <button> elements being centrally aligned, but the <span> element being absolutely to the right of the <input>, example:
It's important to make sure that the <span> does not affect the alignment of the other elements. The <input> and <button> should always be exactly in the middle of the parent.
Would be great if I could do this in CSS only. This is what I have so far:
div {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify {
display: block;
width: 20px;
height: 20px;
background: red;
position: absolute;
top: 0; /* Not sure how to calculate these? */
right: 0; /* Input.X + Input.Width + 15px ?? */
}
<div>
<input placeholder="Enter code" maxlength="8" size="8"/><br />
<span class="verify"></span>
<button>Register</button>
</div>
Additional Info:
It only has to work in Chrome
I can make all elements a fixed width if required
I can make DOM changes if required
I would prefer not to hardcode X/Y co-ordinates for the <span>...I might want to change the input/button dimensions later
Wrap the input and the span inside a div with position: relative and display:inline
The span .verify will get absolutely positioned, leaving the input element to it's original position (centered aligned)
By giving a top:50% and then margin-top: -10px (half of its height), it will get in the middle of it's parent height.
.wrp {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
.inpWrp {
display: inline;
position:relative;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify {
display: block;
width: 20px;
height: 20px;
background: red;
position: absolute;
margin-top: -10px;
top: 50%; /* Not sure how to calculate these? */
right: -20px; /* Input.X + Input.Width + 15px ?? */
}
<div class="wrp">
<div class="inpWrp">
<input placeholder="Enter code" maxlength="8" size="8"/>
<span class="verify"></span>
</div>
<br />
<button>Register</button>
</div>
You can wrap your input element in span and using pseudo-element :after to create the square. No need of position absolute:
div {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
vertical-align: middle;
}
.verify:after {
content: "";
width: 20px;
height: 20px;
background: red;
display: inline-block;
vertical-align: middle;
}
<div>
<span class="verify">
<input placeholder="Enter code" maxlength="8" size="8"/>
</span>
<br />
<button>Register</button>
</div>
After #kapantzak comment you can use position absolute like:
div {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify:after {
content: "";
width: 20px;
height: 20px;
background: red;
display: inline-block;
position: absolute;
top: 17px;
}
<div>
<span class="verify">
<input placeholder="Enter code" maxlength="8" size="8"/>
</span>
<br />
<button>Register</button>
</div>
Try this...
.main_container {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify {
display: inline-block;
width: 20px;
height: 20px;
background: red;
position: relative;
top: 2px;
left: 10px;
}
<div class="main_container">
<div><input placeholder="Enter code" maxlength="8" size="8"/>
<span class="verify"></span></div>
<button>Register</button>
</div>
Related
I've tried to simplify my problem into the below:
.wrapper {
position: relative;
display: inline-flex;
min-height: 1.5rem;
border: 1px solid black;
margin-right: 1rem;
padding-right: 1.5rem;
}
.input {
position: absolute;
z-index: -1;
opactiy: 0;
}
.label-wrapper {
margin-left: 2rem;
}
.label {
position: relative;
margin-bottom: 0;
}
.label:before {
position: absolute;
top: 4px;
left: -24px;
display: block;
width: 20px;
height: 20px;
pointer-events: none;
content: "";
background-color: #fff;
}
.label:after {
position: absolute;
top: 4px;
left: -24px;
display: block;
height: 40px;
content: "";
background-repeat: no-repeat;
background-position: center center;
background-size: 50% 50%;
}
<div class="wrapper">
<input class="input" type="checkbox"></input>
<label class="label">
<div class="label-wrapper">
Option 1
</div>
</label>
</div>
In addition to this, I have the following CSS (in a StyledComponent) that adds a "check" to the checkbox when the user clicks on the hidden input in the .input:
&[type="checkbox"]:checked ~ label::after {
background-image: url("${checkboxImage}");
top: -2px;
left: -29px;
width: 30px;
height: 30px;
}
This means it behaves as a checkbox should - however, I want to swap my elements around so that the checkbox sits to the right of the text instead of the left, to do this I have tried re-ordering the input so that it comes after the label but then the hidden input (which is handling the actual click behaviour is disjointed from the CSS checkbox? Can anyone point me in the direction of how I might resolve this?
Summary:
Using :before & :after to create a visual checkbox, but the logic for that checkbox is actually handled by input (which is hidden) - how can I move both the visual & hidden input to the right of the text?
Edit:
Its very simple with display: flex and flex-direction: row-reverse.
.label-wrapper {
margin-left: 24px;
margin-right: 0;
flex-grow: 1; /* ADDED THIS */
}
.label {
position: relative;
margin-bottom: 0;
display:flex;
flex-direction: row;
flex: 1; /* ADDED THIS */
}
.label.reverse{
flex-direction: row-reverse;
}
See the Snippet below:
.wrapper {
position: relative;
display: inline-flex;
min-height: 1.5rem;
border: 1px solid black;
margin-right: 1rem;
padding: 0.5rem;
width: 200px;
}
.input {
position: absolute;
z-index: -1;
visibility: hidden;
}
.label-wrapper {
margin-left: 24px;
margin-right: 0;
flex-grow: 1;
}
.label.reverse > .label-wrapper {
margin-left: 0;
margin-right: 24px;
}
.label {
position: relative;
margin-bottom: 0;
display:flex;
flex-direction: row;
flex: 1;
}
.label.reverse{
flex-direction: row-reverse;
}
.label:before {
position:absolute;
display: block;
width: 20px;
height: 20px;
content: "";
background-color: #fff;
border: 1px solid black;
border-radius:4px;
}
.label:after {
position: absolute;
display: block;
height: 20px;
content: "";
background-repeat: no-repeat;
background-position: center center;
background-size: 80%;
}
input[type="checkbox"]:checked ~ .label::after {
background-image: url("https://cdn-icons-png.flaticon.com/512/447/447147.png");
width: 20px;
height: 20px;
}
<div class="wrapper">
<input class="input input1" name="checkbox1" id="checkbox1" type="checkbox">
<label class="label" for="checkbox1">
<div class="label-wrapper">
Option 1
</div>
</label>
</div>
<div class="wrapper">
<input class="input input2" name="checkbox2" id="checkbox2" type="checkbox">
<label class="label reverse" for="checkbox2">
<div class="label-wrapper">
Option 2
</div>
</label>
</div>
Are you trying to change the left to right default position of elements?
If that's the case you can use text-align or float. Or even flexbox.
Also, put the text inside a span or something so you can control it.
If I have understood you correctly, there are many ways based on context like using absolute positioning, or put the label before input and close it. or something like this:
<label class="">
<span class="">text</span>
<input class="" type="checkbox" name="order">
</label>
I am having a container div with selected items and select input inside it.
#container {
position: relative;
text-align: left;
width: 100%;
}
._2iA8p44d0WZ {
border: 1px solid #cccccc;
border-radius: 4px;
padding: 5px;
min-height: 22px;
position: relative;
}
._7ahQImy {
padding: 4px 10px;
background: #0096fb;
margin-right: 5px;
margin-bottom: 5px;
border-radius: 10px;
display: inline-flex;
align-items: center;
font-size: 13px;
color: #fff;
white-space: nowrap;
}
<div id="container">
<div class="_2iA8p44d0WZ">
<span class="chip _7ahQImy">This is the main label</span>
<span class="chip _7ahQImy false false">secondary label</span>
<input type="text" class="searchBox" id="search_input" placeholder="Select" autocomplete="off" value="">
</div>
</div>
Requirement:
-> I am in the need to move the selected items below the #container div to display individual chips (span tags) below one after the other.
Note:
-> There are some dynamic classes inside it for which I cannot modify the css classes for those dynamic classes.
List of dynamic classes for which css properties cannot be modified, _2iA8p44d0WZ and _7ahQImy .
List of ids/classes, I can modify the css properties are #container and .chip (span).
Things I have tried:
Added css property for span element with class chip as follows,
.chip {
position: absolute;
top: 200%;
}
.chip:after {
content: '\A';
white-space: pre;
}
But this results in the overlapping of items one on another.
If I add display: block; to .chip then that also results the same..
#container {
position: relative;
text-align: left;
width: 100%;
}
._2iA8p44d0WZ {
border: 1px solid #cccccc;
border-radius: 4px;
padding: 5px;
min-height: 22px;
position: relative;
}
._7ahQImy {
padding: 4px 10px;
background: #0096fb;
margin-right: 5px;
margin-bottom: 5px;
border-radius: 10px;
display: inline-flex;
align-items: center;
font-size: 13px;
color: #fff;
white-space: nowrap;
}
.chip {
position: absolute;
top: 200%;
}
.chip:after {
content: '\A';
white-space: pre;
}
<div id="container">
<div class="_2iA8p44d0WZ">
<span class="chip _7ahQImy">This is the main label</span>
<span class="chip _7ahQImy false false">secondary label</span>
<input type="text" class="searchBox" id="search_input" placeholder="Select" autocomplete="off" value="">
</div>
</div>
Expected Result:
---------------------------
| Input |
---------------------------
| This is the main label |
| secondary label |
Kindly please help me to modify the css of span tags (.chip) without modifying the dynamic classes (I don't have control for it in real application) to have line breaks, so that it will be visible one after the other below the container.
The problem is that you are positioning the chips absolute, of course they will end up on top of each other, if you position them all with the same top value.
Doing this the other way around would IMHO make much more sense here - position the input field absolute at the top of the container, and leave those chips in normal flow instead. Add a padding-top for the inner container element, so that they don’t go beneath the input field, but under it.
#container {
position: relative;
text-align: left;
width:100%;
}
#container > div {
padding-top: 2em;
}
._2iA8p44d0WZ {
border: 1px solid #cccccc;
border-radius: 4px;
padding: 5px;
min-height: 22px;
position: relative;
}
._7ahQImy {
padding: 4px 10px;
background: #0096fb;
margin-right: 5px;
margin-bottom: 5px;
border-radius: 10px;
display: inline-flex;
align-items: center;
font-size: 13px;
color: #fff;
white-space: nowrap;
}
#search_input {
position: absolute;
top: 0;
left: 0;
}
<div id="container">
<div class="_2iA8p44d0WZ">
<span class="chip _7ahQImy">This is the main label</span>
<span class="chip _7ahQImy false false">secondary label</span>
<span class="chip _7ahQImy">Another label</span>
<span class="chip _7ahQImy false false">even more labels labels labels labels labels labels</span>
<input type="text" class="searchBox" id="search_input" placeholder="Select" autocomplete="off" value="">
</div>
</div>
You can use in html br tag, in Css you can try
.container{
width: 100px;/*insert the px you need, make som tests*/
}
.your-stuff{
display:flex;
flex-warp: warp;
}
<body>
<div class="container">
<div class="my-stuff">stuff</div>
</div>
</body>
I want to add a "view password" icon inside of an html input. To do that, I added the material icons font and used one of its components. I nested it inside of the input to allow me to position it according to the input. I set the positon property of the input as "relative" and the position property of the icon as "absolute," but somehow, it still positions absolutely relative to the whole page. How can i fix this?
.auth-input {
text-align: left;
padding: 7px 10px;
background-color: #f6f6f6;
border: none;
border-radius: 5px;
font-size: 1.05em;
width: 100%;
position: relative;
}
i.material-icons {
position: absolute;
top: 0
}
<input type="password" name="password" id="password-input" class="auth-input">
<i class="material-icons">visibility</i>
</input>
You cannot have html inside an input. It is a self closing tag. You need to put both elements into a container.
.auth-input {
text-align: left;
padding: 7px 10px;
background-color: #f6f6f6;
border: none;
border-radius: 5px;
font-size: 1.05em;
width: 100%;
position: relative;
}
.auth-input input {
width: 100%;
}
i.material-icons {
position: absolute;
top: 0
}
<div class="auth-input">
<i class="material-icons">visibility</i>
<input type="password" name="password" id="password-input" />
</div>
Wrap the two. See demo https://codepen.io/yifanai/pen/MWyvJRE
.password-input-group {
position: relative;
}
.auth-input {
text-align: left;
padding: 7px 10px;
background-color: #f6f6f6;
border: none;
border-radius: 5px;
font-size: 1.05em;
width: 100%;
position: relative;
}
i.material-icons {
position: absolute;
top: 7px;
right: 10px;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel=" stylesheet">
<div class="password-input-group">
<input type="password" name="password" id="password-input" class="auth-input" />
<i class="material-icons">visibility</i>
</div>
.auth-input {
text-align: left;
padding: 7px 10px;
background-color: #f6f6f6;
border: none;
border-radius: 5px;
font-size: 1.05em;
width: 100%;
}
.layout {
position: relative;
}
i.material-icons {
position: absolute;
top: 0;
right: 20px;
z-index: 1;
}
<span class="layout">
<input type="password" name="password" id="password-input" class="auth-input"></input>
<i class="material-icons">visibility</i>
</span>
Input TAG not have child, try it.
How can I add an image inside this search box? I'd like the image to be positioned inside and to the left of the palceholder text...
Here is the fiddle
HTML:
<div class="searchbar">
<h1>Welcome...</h1>
<div id="sb-search" class="sb-search">
<div class="search-wrapper">
<input class="search-input" placeholder="Search..." type="text" value="" name="search" id="search"/>
<img class="search-pic"src="img/search-icon.png"/>
</div>
</div>
</div>
thanks
Add this to your CSS which will position the image inside the input field.
.search-wrapper {
position: relative;
}
.search-wrapper img {
position: absolute;
top: 0px;
left: 0px;
}
Then you just need to use padding and change the top and left values to move everything about so it fits nicely and nothing is overlapped.
You might also need to set a width and height to the image so it's not too big for the input field.
You can solve it like this:
#mixin searchbar-font {}
#mixin searchbar-styles {
border: none;
outline: none;
color: $header-bg-color;
padding: 20px 65px 20px 20px;
background: transparent;
flex:1;
}
#mixin search-bar-input {
border: none;
outline: none;
}
.search-input {
border: none;
outline: none;
color: $header-bg-color;
padding: 20px 65px 20px 20px;
background: transparent;
flex:1;
}
.search-wrapper {
position: relative;
background: white;
display: flex;
align-items: center;
.search-input {
#include searchbar-font;
#include searchbar-styles;
}
.search-submit {
#include search-bar-input;
}
}
<div class="searchbar">
<h1>Welcome...</h1>
<div id="sb-search" class="sb-search">
<div class="search-wrapper">
<img class="search-pic" src="img/search-icon.png" />
<input class="search-input" placeholder="Search..." type="text" value="" name="search" id="search" />
</div>
</div>
</div>
Attached Fiddle
Technically you need to set your parent tag's position to relative, then set the image inside's position to absolute. Then you can overlay the image on your Input field. Also, one more thing to remember is you might want to set you z-index. Just in case, your image does not get behind of your input field. Make sure you are giving enough space to for your image by setting the input's padding left to somewhere around your image's width.
.search-wrapper{
.search-input{
padding-left: {image.width}px;
}
img{
position:absolute; left:0; top:0;
}
}
This should do the work.
<div class="search-container">
<input type="text" placeholder="Search...">
<img src="search-icon.png" alt="search icon">
</div>a
.search-container {
display: flex;
align-items: center;
position: relative;
}
input[type="text"] {
border: 1px solid gray;
border-radius: 20px;
padding: 10px 40px 10px 20px;
font-size: 16px;
width: 500px;
}
img {
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
height: 20px;
width: 20px;
}
<div class="search-container">
<input type="text" placeholder="Search...">
<img src="search-icon.png" alt="search icon">
</div>
.search-container {
display: flex;
align-items: center;
position: relative;
}
input[type="text"] {
border: 1px solid gray;
border-radius: 20px;
padding: 10px 40px 10px 20px;
font-size: 16px;
width: 500px;
}
img {
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
height: 20px;
width: 20px;
}
I want to display an input box in html and 2 small buttons to the right, one on top of each other. Something like this
https://jsfiddle.net/danaya/yw94f0Lt/3/
The html code is simple
<div class="list">
<div class="name">product</div>
<div class="input">
<input type="text" size="3" value="1">
<div class="inc">+</div>
<div class="dec">-</div>
</div>
<div class="price">2.99</div>
</div>
And this is the css
.list {
display: flex;
}
.name,
.input,
.price {
padding: 10px 10px;
border: 1px solid red;
}
.name {
width: 50%;
}
.input,
.price {
text-align: right;
width: 25%;
}
.input {
position: relative;
top: -5px;
}
input {
line-height: 25px;
}
.inc,
.dec {
display: block;
position: absolute;
width: 13px;
height: 13px;
background-color: #999;
text-align: center;
color: #fff;
line-height: 12px;
}
.inc {
top: 11px;
left: 40px;
}
.dec {
top: 25px;
left: 40px;
}
As it is right now, when I resize the window, the div.input is resized and so the buttons, being related to the input, lose their position by the input element.
I need to keep the flex display in the .list element, but other than that I can change anything. I also need the buttons to not increase the width of the div.input element, that's why I'm using the position:relative.
Any ideas?
Thanks
Would this work for you?
.list {
display: flex;
align-items: center;
}
.name,
.price {
padding: 0 10px;
}
.input, input {
box-sizing: border-box; /*to set the equal height to bot .input and input*/
height: 31px; /*13 + 13 (buttons) + 5 (paddings for buttons) = 31px */
}
.input {
position: relative;
/*width: 25%; Use this if you want to increate the width of your div.input*/
}
input {
padding-right: 15px; /* 15px set so that input characters do not go underneath the + and - buttons */
width: 100%; /* set 100% so that input will take 100% width of its parent size */
}
.inc,
.dec {
box-sizing: border-box;
display: block;
position: absolute;
width: 13px;
height: 13px;
background-color: #999;
text-align: center;
color: #fff;
line-height: 12px;
}
.inc {
top: 2px;
right: 2px;
}
.dec {
bottom: 2px;
right: 2px;
}
<div class="list">
<div class="name">product</div>
<div class="input">
<input type="text" size="3" value="1">
<span class="inc">+</span>
<span class="dec">-</span>
</div>
<div class="price">2.99</div>
</div>