Margin right negative makes moves input to the right - html

I have the following HTML to place a button inside an input (JSFiffle Example):
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
form {
border: 1px solid green;
max-width: 400px;
}
input {
border: 1px solid red;
background-color: white;
display: inline-block;
font-size: 18px;
line-height: 1;
margin: 0;
margin-right: -80px;
padding: 12px;
vertical-align: middle;
width: 100%;
}
button {
background-color: white;
border: none;
cursor: pointer;
display: inline-block;
font-size: 28px;
line-height: 1;
margin: 0;
outline: 0;
vertical-align: middle;
}
i {
display: block;
line-height: 1;
margin: 0;
padding: 0;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<form>
<input type="text">
<button><i class="fa fa-search"></i></button>
</form>
Problem
The JSFiddle Example I posted works great ...
But in my application when the negative margin right value is bigger then the icon width the input moves right and gets outside of the container with a space on the left:
I am not able to replicate this on JSFiddle but I wonder if someone has any idea what to look for because I have been trying everything and not able to solve it.

I'd suggest something more along these lines:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
form {
border: 1px solid green;
max-width: 400px;
}
.search-input {
display: flex;
flex-flow: row nowrap;
height: 40px;
border: 1px solid red;
background-color: white;
font-size: 18px;
line-height: 1;
}
.search-field {
flex: auto;
border: none;
padding: 10px;
}
.search-button {
width: 40px;
height: 40px;
font-size: 24px;
cursor: pointer;
border: none;
background: transparent;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<form>
<div class="search-input">
<input class="search-field" type="text">
<button class="search-button" type="submit"><i class="fa fa-search"></i></button>
</div>
</form>

Since you said you didn't want a flex solution, how about removing the negative margin-right and replacing width: 100% with width: 88%.
That being said and since I'm a big fan of flexbox, the following is a different flex solution from James' offering:
form {
border: 1px solid green;
max-width: 400px;
display: flex; /* Added */
justify-content: flex-end; /* Added */
}
input {
border: 1px solid red;
background-color: white;
display: inline-block;
font-size: 18px;
line-height: 1;
margin: 0;
/* margin-right: -80px; */ /* Removed */
padding: 12px;
vertical-align: middle;
/* width: 100% */ /* Removed */
flex: 0 1 100%; /* Added */
}

You can make the input's container position: relative and the button position: absolute. Then you can move the button wherever you want relative to the container. I would suggest using something other than the form element, so that you can have more than one element in the form. I've added a simple wrapping div.
Then you set right to a little more than 0 so that the border(s) can be seen, and the top to half way down, less half the height of the button so it's centered vertically.
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
form {
border: 1px solid green;
max-width: 400px;
}
.search {
/* Making the container relative... */
position: relative;
}
input {
border: 1px solid red;
background-color: white;
display: inline-block;
font-size: 18px;
line-height: 1;
margin: 0;
padding: 12px;
vertical-align: middle;
width: 100%;
}
button {
background-color: white;
border: none;
cursor: pointer;
display: inline-block;
font-size: 28px;
line-height: 1;
margin: 0;
outline: 0;
vertical-align: middle;
/* means you can absolutely position the button relative to that */
position: absolute;
/* make it one pixel off the right for the border */
right: 1px;
/* make it 50% - half its height to center it vertically */
top: calc(50% - 14px);
}
i {
display: block;
line-height: 1;
margin: 0;
padding: 0;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<form>
<div class="search">
<input type="text">
<button><i class="fa fa-search"></i></button>
</div>
</form>

Related

Button moves when resizing page

My submit button moves when I resize the page. This behavior is bad (obviously). What is the correct way to make it so that my button is left aligned to my textarea?
#chatboxTranscript {
width: 31.25em;
/* padding: 0em; */
background-color: #36393f;
color: #fff;
display: flex;
border: 0.125em solid #000;
/* Increase border width */
border-radius: 0.3125em 0.3125em 0 0;
/* Round the border */
box-sizing: border-box;
margin: 0 auto;
height: 12.5em;
/* Maintain the height */
}
#chatboxInput {
top: -0.125em;
height: 3.35em;
left: -2.2em;
width: 37.3em;
/* Keep the width the same */
background-color: #36393f;
/* Keep the color the same */
border: 0.15em solid #000;
border-radius: 0 0 0.3125em 0.3125em;
/* Round the border */
padding-bottom: 1.5em;
box-sizing: border-box;
margin: 0 auto;
display: flex;
/* border-top: none; */
position: relative;
}
.input-group {
display: flex;
justify-content: space-between;
}
button[type="submit"] {
position: relative;
display: inline-block;
width: 4em;
top: -0.1em;
height: 3em;
margin: 0 -2em;
border: 0.125em solid #000;
background-color: #36393f;
border-radius: 0.3125em;
}
<div id="chatbox" class="chatbox" data-role="chatbox">
<div id="chatboxTranscript" class="chatbox-transcript"></div>
<div class="input-group">
<!-- <form action="#" onsubmit="handleChatboxFormSubmit(event);"> -->
<textarea type="text" name="chatboxInput" id="chatboxInput" /> </textarea>
<button type="submit">Submit</button>
<!-- </form> -->
</div>
</div>
I tried multiple ways of fixing using absolute positioning (still same behavior), display (did nothing), margin (moves my textarea to the left) and asking chatgpt (output nonsensical CSS that didn't work).
Is this what you're trying to achieve?
I've removed all position layout styles since they don't help much in your particular layout.
The main issue was the .input-group since it didn't have a width and you're using flex for the layout. The content would take the entire width from its parent (#chatbox) therefore the button went to the right-hand side of the screen.
To fix this, I simply added a width for the .input-group as well as a margin of auto so it now aligns nicely with #chatboxTranscript div.
#chatboxTranscript {
width: 31.25em;
background-color: #36393f;
color: #fff;
display: flex;
border: 0.125em solid #000; /* Increase border width */
border-radius: 0.3125em 0.3125em 0 0; /* Round the border */
box-sizing: border-box;
margin: 0 auto;
height: 12.5em; /* Maintain the height */
}
#chatboxInput {
height: 3.35em;
width: 37.3em; /* Keep the width the same */
background-color: #36393f; /* Keep the color the same */
border: 0.15em solid #000;
border-radius: 0 0 0.3125em 0.3125em; /* Round the border */
padding-bottom: 1.5em;
box-sizing: border-box;
margin: 0 auto;
display: flex;
}
.input-group {
display: flex;
justify-content: space-between;
width: 31.25em;
margin: 0 auto;
}
button[type='submit'] {
display: inline-block;
width: 4em;
height: 3em;
border: 0.125em solid #000;
background-color: #36393f;
border-radius: 0.3125em;
}
#chatboxTranscript {
width: 31.25em;
/* padding: 0em; */
background-color: #36393f;
color: #fff;
display: flex;
border: 0.125em solid #000; /* Increase border width */
border-radius: 0.3125em 0.3125em 0 0; /* Round the border */
box-sizing: border-box;
margin: 0 auto;
height: 12.5em; /* Maintain the height */
}
#chatboxInput {
top: -0.125em;
height: 3.35em;
left: -2.2em;
width: 37.3em; /* Keep the width the same */
background-color: #36393f; /* Keep the color the same */
border: 0.15em solid #000;
border-radius: 0 0 0.3125em 0.3125em; /* Round the border */
padding-bottom: 1.5em;
box-sizing: border-box;
margin: 0 auto;
display: flex;
/* border-top: none; */
position: relative;
}
.input-group {
display: flex;
justify-content: space-between;
}
button[type="submit"] {
position: fixed;
display: inline-block;
width: 4em;
top: -0.1em;
height: 3em;
margin: 0 -2em;
border: 0.125em solid #000;
background-color: #36393f;
border-radius: 0.3125em;
}
I edited position of button from "reletive" to "fixed". It will work in page.

Why is my input element wider than the other elements even though I already applied "box-sizing: border-box"?

I have an input element which has a padding of 1em on both the left and the right side. But I have applied "box-sizing: border-box" to it. However, It's width is still more than the other elements. I think it might be because I need to remove some piece of code but I'm not sure. The input element is definitely the one issue as the other element is properly center aligned.
Below is the code:
:root {
--main-color: #00308f;
--secondary-color: #7cb9e8;
--dark-color: #444;
--light-color: #fafafa
}
body {
font-family: Montserrat, sans-serif;
background-color: var(--light-color);
color: var(--dark-color);
text-align: justify;
margin-top: 70px;
margin-left: 0;
margin-right: 0;
margin-bottom: 0;
padding: 1em
}
.my-contacts-div {
align-items: center
}
.contacts-list {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center
}
.contact-card {
width: 288px;
margin: .5em;
border-radius: .5em;
box-shadow: var(--secondary-color) 1px 1px 10px;
padding: 0 .75em;
word-wrap: break-word
}
.contact-form {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
display: grid;
align-self: center;
width: 350px;
z-index: 1;
background-color: var(--secondary-color);
border-radius: 1em;
padding: 1em;
box-shadow: 1px 1px 1.5em var(--main-color);
visibility: hidden
}
.contact-form:target {
visibility: visible
}
.input-field {
margin: .5em 0;
border: solid 1px var(--secondary-color);
border-radius: .5em;
padding: 0 1em;
height: 40px;
box-sizing: border-box
}
.searchbar {
margin: .5em;
width: 100%
}
#media screen and (max-width:687px) {
.my-contacts-div {
padding: 0
}
.contact-card {
width: 100%
}
}
#media screen and (max-width:614px) {
body {
margin-top: 130px
}
}
<div class="my-contacts-div">
<h2>Heading</h2>
<form><input class="input-field searchbar" type="text" placeholder="Search here..."></form>
<div class="contacts-list">
<div class="contact-card">
<h3>Other component</h3>
</div>
</div>
</div>
What is wrong with it?
The issue was most likely that when you were using '.input-field' in the CSS, it was maybe not correctly using it so I just put 'form input.input-field' and also added some CSS to the form element. Now it is looking completely aligned.
:root {
--main-color: #00308f;
--secondary-color: #7cb9e8;
--dark-color: #444;
--light-color: #fafafa
}
body {
font-family: Montserrat, sans-serif;
background-color: var(--light-color);
color: var(--dark-color);
text-align: justify;
margin-top: 70px;
margin-left: 0;
margin-right: 0;
margin-bottom: 0;
padding: 1em
}
.my-contacts-div {
align-items: center
}
.contacts-list {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center
}
.contact-card {
width: 288px;
margin: .5em;
border-radius: .5em;
box-shadow: var(--secondary-color) 1px 1px 10px;
padding: 0 .75em;
word-wrap: break-word
}
.contact-form {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
display: grid;
align-self: center;
width: 350px;
z-index: 1;
background-color: var(--secondary-color);
border-radius: 1em;
padding: 1em;
box-shadow: 1px 1px 1.5em var(--main-color);
visibility: hidden
}
.contact-form:target {
visibility: visible
}
form input.input-field {
margin: .5em 0;
border: solid 1px var(--secondary-color);
border-radius: .5em;
padding: 0 1em;
height: 40px;
box-sizing: border-box;
}
form {
box-sizing: border-box;
padding: 0 0.5em;
}
.searchbar {
margin: .5em;
width: 100%
}
#media screen and (max-width:687px) {
.my-contacts-div {
padding: 0
}
.contact-card {
width: 100%
}
}
#media screen and (max-width:614px) {
body {
margin-top: 130px
}
}
<div class="my-contacts-div">
<h2>Heading</h2>
<form><input class="input-field searchbar" type="text" placeholder="Search here..."></form>
<div class="contacts-list">
<div class="contact-card">
<h3>Other component</h3>
</div>
</div>
</div>
Add display: flex in the tag. It will solve the overflowing issue.
display: flex fills the entire container taking margin and padding into consideration. That's why it didn't overflow.
display: block is at 100% width by default and then adds margin and padding after causing the overflow.

file upload button not supported in all the browsers

I have created a file upload with search button at the right side. This one is showing correctly in chrome only, but I need this to support all modern browsers. I have tried in Mozilla where this is not being supported. Can anybody please help me to solve this. Thank You.
#import url(https://fonts.googleapis.com/css?family=Roboto);
*,
*:after,
*:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html {
font-size: 16px
}
body {
background-color: #fff;
margin: 0;
padding: 0;
font-family: 'Roboto', sans-serif;
font-size: 1rem;
}
.container {
margin: 20px auto;
max-width: 1140px;
padding: 25px 25px;
border: 1px solid #000;
}
.row {
clear: both;
}
#myInput {
width: 85%;
border: 1px solid #000;
display: inline-block;
}
input[type=file]::-webkit-file-upload-button {
width: 0;
padding: 0;
margin: 0;
-webkit-appearance: none;
border: none;
border: 0px;
}
x::-webkit-file-upload-button,
input[type=file]:after {
-webkit-appearance: button;
border-collapse: separate;
border-radius: 7px;
-webkit-border-radius: 7px;
-moz-border-radius: 7px;
content: 'Search';
color: #080708;
background: #e3e3e3;
text-decoration: none;
display: inline-block;
left: 100%;
margin-left: 50px;
position: relative;
padding: 10px 46px 10px 40px;
}
#media only screen and (max-width: 767px) {
.container {
width: 100%;
padding: 15px;
}
#myInput {
width: 64%;
}
.btn-View {
width: 10%;
}
table td {
padding: 12px;
}
}
<div class="container">
<div class="row">
<input type="file" name="myInput" id="myInput">
</div>
</div>
You can make it work in many ways.
The simple way if you do not need the file name to be seen, using
the input file along with the label is the quickest way.
https://codepen.io/anupkumarmaharjan/pen/mWXbVj
Using the javascript can be handy. Go through these links for better understandings.
https://codepen.io/anupkumarmaharjan/pen/NpyKPm
https://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way

Add icon via :before to <input> element

I've got an <input>-element. I'd like to precede it with a search-icon using the :before selector including a SVG-font I created.
.icon-search:before {
content: "\f109";
text-indent: -2em;
}
My problem is that the pseudo-selector :beforedoesn't seem to work with <input> Can you tell me how to make this work in the most elegant way?
This is how I want it to look once I'm done.
The before/after pseudo element is contained inside the element but input can't have that, so you have to use another selector like span or div. This is an implementation similar to what bootstrap does...
FIDDLE
HTML:
<div class="wrapper">
<div class="inputgroup"> <span class="innericon searchicon"></span>
<input type="text" class="search" placeholder="search..." />
</div>
</div>
CSS:
body {
color: gray;
}
.wrapper {
margin: 20px auto;
width: 40%;
}
* {
box-sizing: border-box;
}
.inputgroup {
position:relative;
display: inline-block;
height: 30px;
line-height: 30px;
vertical-align: middle;
}
.search {
border-radius: 8px;
height: 30px;
line-height: 30px;
padding: 3px 6px 3px 32px;
border: none;
font-size: 13px;
display: inline-block;
vertical-align: middle;
border: 1px solid #CCC;
box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.075) inset;
}
.inputgroup .innericon {
position: absolute;
top: 4px;
left: 8px;
z-index: 2;
display: block;
text-align: center;
pointer-events: none;
}
.searchicon:before {
content: "\f109";
}
Here is one way to achieve this: http://jsfiddle.net/n9gjb3kr/
You need to put your input inside a div:
<div class="icon-search"><input type="text"></input></div>
.icon-search:before {
content: "\f109";
position: absolute;
left: 0;
top: 0;
height: 24px;
width: 24px;
margin-left: .5em;
width: 0;
overflow: visible;
display: inline-block;
line-height: 24px;
z-index: 1;
}
.icon-search {
position: relative;
line-height: 24px;
}
.icon-search > input {
border-radius: 12px;
height: 24px;
border: 1px solid black;
margin: 0;
padding: 0;
padding-left: 32px;
}
You can wrap the input into span, setting the span with position:relative and
then add to input and icon position:absolute and play with margins and z-index to create the same effect. This is pretty solid for responsive as well, if you style the span and the input the right way taking the responsive design in account.
OR
Using jquery as already mentioned in the comments

Flexbox only working in chrome

i'm trying to achieve a two column div where the one is fixed and the other one is stretched to fill rest. For this i've at the moment used a flexbox, however i've found out that it does not work in safari and IE 10. So how can i achieve the same result without flexbox?
http://jsfiddle.net/LLExL/4086/
.stat-result {
border: 1px solid #ddd;
width: 100%;
display: flex;
margin-bottom: 20px !important;
}
.stat-result .stat-meta {
float: left;
width: 100%;
min-height: 40px;
border-right: 1px solid #ddd;
}
.stat-result .result-meta {
float: left;
width: 100px;
height: 100%;
text-align: center;
}
.stat-result .result-meta i {
font-size: 50px;
line-height: 80px;
}
.stat-result .stat-meta .stat-row {
width: 100%;
height: 50%;
border-bottom: 1px solid #ddd;
padding-left: 10px;
padding-right: 10px;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box;
}
.stat-result .stat-meta .stat-row span {
line-height: 40px;
font-weight: 700;
font-family: Oswald;
text-transform: uppercase;
}
.stat-result .stat-meta .stat-row1 {
width: 100%;
height: 50%;
padding-left: 10px;
padding-right: 10px;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box;
}
.stat-result .stat-meta .stat-row1 span {
line-height: 40px;
}
<div class="stat-result">
<div class="stat-meta">
<div class="stat-row">
<span>Titan Vs. Mousesports</span>
</div>
<div class="stat-row1">
<span>1.35</span>
<span style="float: right;">Test</span>
</div>
</div>
<div class="result-meta">
<i class="fa fa-check-circle"></i>
</div>
</div>
You are lacking all the flexbox prefixes. According to caniuse you can actually use flexbox for safari and IE10.
Updated Fiddle http://jsfiddle.net/LLExL/4088/
And css
.stat-result {
border: 1px solid #ddd;
width: 100%;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
margin-bottom: 20px !important;
}
Edit: I'd like to recommend using gulp/grunt and an autoprefixer to automatically prefix your css.
If you don't want to use flexbox, you can use table-cell property to achieve the same.
.stat-result {
border: 1px solid #ddd;
width: 100%;
display: table;
margin-bottom: 20px !important;
}
.stat-result .stat-meta {
display:table-cell;
width:calc(100% - 100px);
min-height: 40px;
border-right: 1px solid #ddd;
}
.stat-result .result-meta {
display: table-cell;
width: 100px;
height: 100%;
text-align: center;
}
FIDDLE DEMO