This is my current input field styling however for some reason which I am unable to see, they are not aligning properly next to each other.
Code:
<div class="labelInputField">Confirm Password</div><input type="text" class="inputField">
.labelInputField {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
padding: 8px;
width: 150px;
color: white;
background: rgba(0,0,0,1);
border: 2px solid #000;
display: inline-block;
height: 30px;
margin-top: 15px;
font-size: 16px;
}
.inputField {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
padding: 8px;
width: 180px;
color: white;
background: rgba(0,0,0,0.4);
border: 2px solid #000;
display: inline-block;
height: 30px;
font-size: 16px;
}
JSFiddle:
https://jsfiddle.net/0Lsake9j/
Any solutions?
Remove margin-top:15px and add line-height:30px; in .labelInputField style. That will make your div and input alignment proper.
.labelInputField {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
padding: 8px;
width: 150px;
color: white;
background: rgba(0,0,0,1);
border: 2px solid #000;
display: inline-block;
height: 30px;
line-height:30px;
font-size: 16px;
}
.inputField {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
padding: 8px;
width: 180px;
color: white;
background: rgba(0,0,0,0.4);
border: 2px solid #000;
display: inline-block;
height: 30px;
font-size: 16px;
}
<div class="labelInputField">Confirm Password</div><input type="text" class="inputField">
You can also test it here
Offload layout responsibility to a container and/or additional layers of markup.
Remove margin-top on the div
Use a <label for="[YOUR_INPUT_ID]"></label> and ensure your input has id="[YOUR_INPUT_ID]".
Add type="password" on your input, unless you have a specific reason not to.
Remove existing layouts styles from selectors that are styling the input and label (i.e. inline-block in this case.).
Apply your layout styling with an object that's sole purpose is to handle layout of your elements
.iHandleLayout {
display: flex;
/* rest of your layout styling etc. */
}
.labelInputField {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
padding: 8px;
min-width: 150px;
color: white;
background: rgba(0,0,0,1);
border: 2px solid #000;
font-size: 16px;
}
.inputField {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
padding: 8px;
width: 180px;
color: white;
background: rgba(0,0,0,0.4);
border: 2px solid #000;
font-size: 16px;
}
<div class="iHandleLayout">
<label class="labelInputField" for="password">Confirm Password</label>
<input type="password" class="inputField" id="password">
</div>
In the end, there are many ways to control the layout (display: inline-block, flex, table, etc), but the main principle is the separation of concerns with your styling and the markup structure as part of that separation.
Related
This question already has answers here:
spacing between form fields
(4 answers)
Closed 3 years ago.
I'm working on an autocomplete form, but I seem to have some issues with the padding between the text and submit form. I cannot seem to adjust the spacing between the text and submit items, which I need because when I increase the size of the text field, it overwrites the submit button. I believe I've tried all types of padding but they do nothing. Can someone give me a clue please?
.autocomplete {
position: relative;
display: inline-block;
}
input {
border: 1px solid;
padding: 5px;
font-size: 24px;
}
input[type=text] {
border-color: #808080;
background-color: #fff;
width: 100%;
-webkit-border-radius: 5px;
padding: 5px;
}
input[type=submit] {
margin-left: 5px;
padding: 5px;
background: #ccc;
cursor: pointer;
-webkit-border-radius: 5px;
border: 1px solid;
background-color: DodgerBlue;
border-color: #CCCCCC;
color: #fff;
}
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Country">
</div>
<input type="submit">
</form>
Add box-sizing: border-box to your input[type=text] - the default content-box setting means that when you add padding to an element it increases its dimensions.
See demo below:
.autocomplete {
position: relative;
display: inline-block;
}
input {
border: 1px solid;
padding: 5px;
font-size: 24px;
}
input[type=text] {
border-color: #808080;
background-color: #fff;
width: 100%;
-webkit-border-radius: 5px;
padding: 5px;
box-sizing: border-box; /* added */
}
input[type=submit] {
margin-left: 5px;
padding: 5px;
background: #ccc;
cursor: pointer;
-webkit-border-radius: 5px;
border: 1px solid;
background-color: DodgerBlue;
border-color: #CCCCCC;
color: #fff;
}
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Country">
</div>
<input type="submit">
</form>
You can see more examples here:
Add border to div increase div width?
border-box isn't working as expected
Have you tried using between your input text and submit button?
.autocomplete {
position: relative;
display: inline-block;
}
input {
border: 1px solid;
padding: 5px;
font-size: 24px;
}
input[type=text] {
border-color: #808080;
background-color: #fff;
width: 100%;
-webkit-border-radius: 5px;
padding: 5px;
}
input[type=submit] {
margin-left: 5px;
padding: 5px;
background: #ccc;
cursor: pointer;
-webkit-border-radius: 5px;
border: 1px solid;
background-color: DodgerBlue;
border-color: #CCCCCC;
color: #fff;
}
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Country">
</div>
<input type="submit">
</form>
You can increase the value of margin-left in input[type=submit] {} like shown below:
input[type=submit] {
margin-left: 10px; /* increased value */
padding: 5px;
background: #ccc;
cursor: pointer;
-webkit-border-radius: 5px;
border: 1px solid;
background-color: DodgerBlue;
border-color: #CCCCCC;
color: #fff;
}
or add margin right to the previous element like shown below:
input[type=text] {
border-color: #808080;
background-color: #fff;
width: 100%;
-webkit-border-radius: 5px;
padding: 5px;
margin-right: 5px; /* add margin right */
}
This question already has answers here:
I want to add a border to my button on hover event without moving the button or anything [duplicate]
(7 answers)
Closed 6 years ago.
My idea is to add a colored border to the submit button when the user hovers it.
body div:first-child {
padding: 10px 20px;
background-color: #dedede;
margin-top: 50px;
margin-left: 300px;
width: 120px;
height: 80px;
border-radius: 8px;
}
button {
border-radius: 4px;
padding: 5px 10px;
}
button:hover {
border: 6px solid crimson;
font-weight: 800;
background-color: white;
color: crimson;
}
<div class="container">
<button>SUBMIT</button>
<div>Please notice that ...</div>
</div>
But the immediately following text then becomes shifted downwards.
How can I get rid of these "Jumping" text while simultaneously having the border?
You need to define transparent border by default on button and change border-color on hover. Further avoid changing font-weight property on hover as well as it will also expand the width and height of button and it will jump on hover.
body div:first-child {
padding: 10px 20px;
background-color: #dedede;
margin-top: 50px;
margin-left: 300px;
width: 120px;
height: 80px;
border-radius: 8px;
}
button {
border: 6px solid transparent;
font-weight: 800;
border-radius: 4px;
padding: 5px 10px;
}
button:hover {
border-color: crimson;
background-color: white;
color: crimson;
}
<div class="container">
<button>SUBMIT</button>
<div>Please notice that ...</div>
</div>
Add "margin" attributes to your "button" CSS like so:
button {
border-radius: 4px;
padding: 5px 10px;
margin: 4px 0;
}
button:hover {
border: 6px solid crimson;
font-weight: 800;
background-color: white;
color: crimson;
margin: 0;
}
From Stop an element moving with padding on hover.
Just to give an alternative you could use outline to set the "border" on hover.
body div:first-child {
padding: 10px 20px;
background-color: #dedede;
margin-top: 50px;
margin-left: 300px;
width: 120px;
height: 80px;
border-radius: 8px;
}
button {
border: 0;
border-radius: 4px;
padding: 5px 10px;
margin: 5px 0;
}
button:hover {
outline: 6px solid crimson;
font-weight: 800;
background-color: white;
color: crimson;
}
<div class="container">
<button>SUBMIT</button>
<div>Please notice that ...</div>
</div>
Another option:
body div:first-child {
padding: 10px 20px;
background-color: #dedede;
margin-top: 50px;
margin-left: 300px;
width: 120px;
height: 80px;
border-radius: 8px;
}
button {
border-radius: 4px;
padding: 5px 10px;
margin: 6px 0;
border-width: 2px;
border-style: outset;
border-color: buttonface;
}
button:hover {
border-width: 6px;
border-style: solid;
border-color: crimson;
font-weight: 800;
margin: 2px 0;
background-color: white;
color: crimson;
}
<div class="container">
<button>SUBMIT</button>
<div>Please notice that ...</div>
</div>
Here are some possibilities:
Style the button dimensions (padding/margin/border) with the same px/em values like it's :hover state
set the button position to absolute/fixed
Use a fix height in a parent div
check now
body div:first-child {
padding: 10px 20px;
background-color: #dedede;
margin-top: 50px;
margin-left: 300px;
width: 120px;
height: 80px;
border-radius: 8px;
}
button {
border-radius: 4px;
padding: 5px 10px;
border: 6px solid transparent;
}
button:hover {
border: 6px solid crimson;
font-weight: 800;
background-color: white;
color: crimson;
}
<div class="container">
<button>SUBMIT</button>
<div>Please notice that ...</div>
</div>
Try adding height to button
body div:first-child {
padding: 10px 20px;
background-color: #dedede;
margin-top: 50px;
margin-left: 300px;
width: 120px;
height: 80px;
border-radius: 8px;
}
button {
border-radius: 4px;
padding: 5px 10px;
height:40px
}
button:hover {
border: 6px solid crimson;
font-weight: 800;
background-color: white;
color: crimson;
}
<div class="container">
<button>SUBMIT</button>
<div>Please notice that ...</div>
</div>
Add padding of the same size as the border to the button, and remove it on hover.
Mention the width and height to the button and remove the font-weight to avoiding a shifting of the element when hover the elements. Check below code.
body div:first-child {
padding: 10px 20px;
background-color: #dedede;
margin-top: 50px;
margin-left: 300px;
width: 120px;
height: 80px;
border-radius: 8px;
}
button {
border-radius: 4px;
padding: 5px 10px;
height:50px;
width:100px;
}
button:hover {
/*font-weight: 800;
background-color: white;*/
border: 6px solid crimson;
color: crimson;
}
<div class="container">
<button>SUBMIT</button>
<div>Please notice that ...</div>
</div>
body div:first-child {
padding: 10px 20px;
background-color: #dedede;
margin-top: 50px;
margin-left: 300px;
width: 120px;
height: 80px;
border-radius: 8px;
}
button {
border-radius: 4px;
padding: 5px 10px;
height:50px;
width:100px;
}
button:hover {
border: 6px solid crimson;
background-color: white;
color: crimson;
}
<div class="container">
<button>SUBMIT</button>
<div>Please notice that ...</div>
</div>
I want to create something like the picture, where the #body is between #leg1 and #leg2, three of them should be horizontally in line to the bottom. Any idea how to achieve this? I tweaked some property such as display:inline, or float:left, float:right, but none of them work as I expect.
.comment_leg {
s width: 60px;
/*height:18px;*/
background-color: #ffcc99;
padding: 5px;
text-align: center;
border-radius: 3px;
}
[contenteditable=true]:empty:before {
content: attr(placeholder);
display: block;
For Firefox
}
#body {
background-color: white;
/*position:relative;*/
border: 1px solid orange;
/*height:60px;*/
width: 500px;
padding: 10px;
color: black;
border-radius: 3px;
font-size: 18px;
/*border-color:yellow;*/
}
#body:focus {
outline-style: solid;
outline-color: orange;
outline-width: 0px;
}
<br>
<br>
<br>
<div class="comment_leg">leg1</div>
<div id="body" contenteditable="true" autocomplete="off" spellcheck="false" placeholder="Pika pi?"></div>
<div class="comment_leg">leg2</div>
From what I understood of your "凸" shape I guess this is what you want:
NOTE: You can adjust height and width depending on your preference.
.comment_leg {
width: 60px;
/*height:18px;*/
background-color: #ffcc99;
padding: 5px;
text-align: center;
border-radius: 3px;
vertical-align: bottom;
display: inline-block;
}
[contenteditable=true]:empty:before {
content: attr(placeholder);
display: block;
For Firefox
}
#body {
background-color: white;
border: 1px solid orange;
min-height:100px;
width: 150px;
padding: 10px;
color: black;
border-radius: 3px;
font-size: 18px;
display: inline-block;
vertical-align: bottom;
}
#body:focus {
outline-style: solid;
outline-color: orange;
outline-width: 0px;
}
<div class="comment_leg">leg1</div>
<div id="body" contenteditable="true" autocomplete="off" spellcheck="false" placeholder="Pika pi?"></div>
<div class="comment_leg">leg2</div>
I am trying to disable/remove the border or the blue glow in the text box.
.user {
width: 300px;
height: 50px;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-bottom: -8px;
color: #ffffff;
}
.user::-webkit-input-placeholder {
color: #ffffff;
}
.password {
width: 300px;
height: 50px;
border: #e9e9e9;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-top: -8px;
}
.password::-webkit-input-placeholder {
color: #ffffff;
}
The form part:
<form method="post" class="login_form">
<div style="font-size: 20px; font-family: bebasregular; text-align: right; color: #ffffff;">Login</div>
<input type="text" name="user" class="user" id="user" placeholder="Username or Email"/>
<hr/>
<input type="password" name="password" class="password" placeholder="Password"/><br/>
<input type="submit" name="login_btn" class="login_btn" value="Login"/>
</form>
Now, what I'm trying to make is that the two text boxes in the form will become transparent, even they're in focus.
i tried this code below, but it doesn't work.
.user{
width: 300px;
height: 50px;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-bottom: -8px;
color: #ffffff;
}
.user:focus{
width: 300px;
height: 50px;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-bottom: -8px;
color: #ffffff;
}
.user::-webkit-input-placeholder {
color: #ffffff;
}
.password {
width: 300px;
height: 50px;
border: #e9e9e9;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-top: -8px;
}
.password:focus {
width: 300px;
height: 50px;
border: #e9e9e9;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-top: -8px;
}
.password::-webkit-input-placeholder {
color: #ffffff;
}
You can remove this by adding: outline: none; to the .user class (or any element that may receive the outline):
JS Fiddle
Sidenote:
I think its worth noting that on your hover states, you only need to specify the properties that change on that that state. For instance, if the element text color is white initially, and you want it to still be white on hovered state, you can omit that on the hover state.
The bluish glow is coming from the default outline styles. To remove it try:
input {
outline: none;
}
This will remove it from the user, password, and when the button is pressed.
JS Fiddle
use
input {
outline: none;
}
or change
border: 2px solid rgba(222,222,222,0);
to
border: 0px;
Here's the code. How to center a search form? This form should be always at the center of the screen no matter what screen resolution is.
I tried margin: 0 auto; but it doesn't work.
Thank You.
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>
Aligning text
Just add text-align: center to form and it is centered on all screen sizes.
text-align: center
This property describes how inline-level content of a block container is aligned.
form {
text-align: center; /* Add */
}
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>
Flexbox solution(Modern):
align-items: center
Items are centered in the cross-axis
Note that in the example we are using flex-direction: column. otherwise just use justify-content: center for flex-direction: row
.search-p {
display: flex;
flex-direction: column; /* Cross axis alignment; simply said vertical stacking/positioning */
align-items: center; /* Center cross-axis */
}
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>
I like to use the new Flexbox layout to place something in the center, a good guide is here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/ Although it's rather new, but it's already supported in majority browsers and it's looking to become the new standard in the next year or so.
/* added new code */
.search-p {
display: flex;
flex-flow: column;
justify-content: center;
align-items: center;
}
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>
CSS and form validation have nothing to do with each other, one is simply styling - the other is data checking which is handled either client-side by JS (bad idea without server validation) and / or server side by (always do this) so there will be no problem. (reacted on comment).
Some other problems might surface tho since text-align: center only works when elements are display: inline-block - float: left; float: right; display: block; will all break this center.
What I would suggest doing is adding a wrapper that will center the search in the form through means of margin: 0 auto - which is a much more solid way to center elements than text-align: center which as it says is meant for text (even tho I have also abused this property many many many times over).
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
/* Added a selector here */
.search-form .search-form-field-wrap {
display: block; /* optional - divs are block by default. */
width: 80%; /* or anything else */
margin: 0 auto; /* you're gonna want to use this for centering block elements */
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
form {
text-align: center;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<!-- added a wrapper here -->
<div class="search-form-field-wrap">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</div>
</form>
</div>