Vertically center input inside a div - html

I want to have the input fields vertically centered inside each div viz. color-1 color-2 color-3
Using vertical-align: middle either in each div or input is not working.
html, body {
height: 100%;
margin: 0;
padding: 0;
}
input[type="text"] {
width:150px;
height: 30px;
text-align: center;
}
#color-1 {
height: 33%;
background: {{yourName}};
text-align: center;
vertical-align: middle;
}
#color-2{
height: 33%;
background: {{color_2}};
text-align: center;
vertical-align: middle;
}
#color-3{
height: 33%;
background: {{color_3}};
text-align: center;
vertical-align: middle;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<div id="color-1">
<input type="text" ng-model="color_1" placeholder="Enter Color #1">
<h1>{{color_1}}</h1>
</div>
<div id="color-2">
<input type="text" ng-model="color_2" placeholder="Enter Color #2">
<h1>{{color_2}}</h1>
</div>
<div id="color-3">
<input type="text" ng-model="color_3" placeholder="Enter Color #3">
<h1>{{color_3}}</h1>
</div>

You can do this in two ways using display:flex or using display:table,display:table-row and display:table-cell which you need to wrap in another div.
Using display:flex is better in which you no need to wrap template in another div
Add the class color for all the color divs
Style of 'color' class
.color{
height:33%;
display: flex;
justify-content: center;
align-items:center;
}
For more info refer this https://css-tricks.com/snippets/css/a-guide-to-flexbox/

I had made demo
#color-1
{
height: 33%;
background: {{yourName}};
vertical-align: top;
background: red;
display: flex;
justify-content: center;
flex-direction: column;
}
and make divs
align="center"

Related

CSS for label under input

I need a clean way of placing the label below the text input field in the following fiddle. The label needs to be center aligned to the input
.scale-options {
margin-top: 56px;
margin-left: auto;
margin-right: auto;
display: flex;
justify-content: space-between;
width: 1100px;
}
.scale-yes,
.scale-no,
.scale-weight {
display: inline-flex;
justify-content: center;
}
.scale-yes,
.scale-no {
width: 313px;
}
.scale-weight input {
width: 233px;
display: block;
}
.scale-weight label {
text-transform: uppercase;
display: block;
position: relative;
}
<div class="scale-options">
<div class="scale-yes">
<Button>yes</Button>
</div>
<div class="scale-weight">
<input type="text" name="scaleWeight" value="24.5kg" disabled>
<label for="scaleWeight" class="scale-weight-label">12.5</label>
</div>
<div class="scale-no">
<Button>no</Button>
</div>
</div>
https://jsfiddle.net/chalcrow/wzpduq2h/3/
I've tried a number of ways of doing it, like setting display:block for both the input and label, and using clear: both on both elements. I've also tried a positioning hack by setting the relative position of the label, but it breaks parts of the other layout for some reason. I've also tried setting heights and widths for the input and label and tried the suggestion here https://stackoverflow.com/a/3463670/1549918 (there are other suggested answers on that question - I looked at them all and they're either too hacky or aren't working when I try them)
The issue is, that you declared display: inline-flex for .scale-weight and as such use flexbox with the default flex-direction: row Just remove the class selector from the list where you declare flexbox on it. After that you can align the text of the label with text-align: center;
.scale-options {
margin-top: 56px;
margin-left: auto;
margin-right: auto;
display: flex;
justify-content: space-between;
}
.scale-yes,
.scale-no /* , *//* removed */
/* .scale-weight *//* removed */ {
display: inline-flex;
justify-content: center;
}
.scale-yes,
.scale-no
{
width: 313px;
}
.scale-weight input {
width: 233px;
display: block;
}
.scale-weight label {
text-transform: uppercase;
display: block;
/* position: relative; *//* removed as it doesn't do anything and has no absolute child */
text-align: center; /* added */
}
<div class="scale-options">
<div class="scale-yes">
<Button>yes</Button>
</div>
<div class="scale-weight">
<input type="text" name="scaleWeight" value="24.5kg" disabled>
<label for="scaleWeight" class="scale-weight-label">12.5</label>
</div>
<div class="scale-no">
<Button>no</Button>
</div>
</div>

Initialise the vertical alignment of contenteditable div cursor with css

I have the following:
#container {
background-color: #ddd;
width:150px;
height: 150px;
display: grid;
vertical-align: middle; text-align: center;
align-items: center;
}
#container:focus {
align-items: center;
vertical-align: middle;
text-align: center;
}
<div id='container' contenteditable="true">
</div>
I'd like to have the cursor initialise in the center of the div container. At present, it starts at the top (Chrome) or bottom (Firefox). It centralises as soon as I start typing.
How do I initialise the vertical alignment of contenteditable div cursor with css?
Use display:table-cell;, it should work better than grid (seems to work only with Chrome)
#container {
background-color: #ddd;
width: 150px;
height: 150px;
text-align:center;
display:table-cell;
vertical-align:middle;
}
<div id='container' contenteditable="true">
</div>
A hacky idea to make it work with Firefox where you need the div to be empty:
#container {
background-color: #ddd;
width: 150px;
height: 150px;
text-align:center;
display:table-cell;
vertical-align:middle;
}
#container:empty {
line-height:150px;
padding-left:75px;
box-sizing:border-box;
text-align:left;
}
<div id='container' contenteditable="true"></div>
This solution is very short.
*:focus {
outline: none;
}
.div{
display: flex;
justify-content: center;
align-items: center;
text-align: center;
width: 300px;
height: 300px;
background: orange;
}
.div:empty{
line-height:300px;
}
<div class="div" placeholder="Type something" contenteditable></div>

How to center a image next to an input field using flexbox

I am attempting to center a image next to an input field although when attempting to do so with vertical-align: bottom; nothing happens and my image is not centered where I want it to be.
I have tried to use position:relative and then move my image using top,bottom etc but I want to do it in a way that uses flexbox.
How would I go about doing this and what divs would I have to change to get the layout I want.
Layout I am trying to achieve:
Jsfiddle
HTML
<div class="container">
<h1>Inputs</h1>
<p class="spacing">multiple inputs...</p>
<div class="searchinput">
<input type="text" name="search" id="google-input" placeholder="Google search...">
<img src="logos/google.png" alt="youtube" class="logos">
</div>
</div>
CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.logos{
width: 90px;
vertical-align: bottom;
}
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 90vh;
}
.searchinput {
margin-top: 20px;
}
input {
padding: 20px;
height: 30px;
background-color: transparent;
border: 2px solid black;
border-radius: 5px;
color: black;
font-size: 20px;
vertical-align: bottom;
}
You need to have the direct parent of the items that you want to have flex properties to have the display:flex property. So in your case it would be the .searchinput. So your .searchinput css should be the following:
.searchinput {
margin-top: 20px;
display:flex;
align-items: center;
}
So here is a snippet of the whole thing:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.logos{
width: 90px;
vertical-align: bottom;
}
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 90vh;
}
.searchinput {
margin-top: 20px;
display:flex;
align-items: center;
}
input {
padding: 20px;
height: 30px;
background-color: transparent;
border: 2px solid black;
border-radius: 5px;
color: black;
font-size: 20px;
vertical-align: bottom;
}
<div class="container">
<h1>Inputs</h1>
<p class="spacing">multiple inputs...</p>
<div class="searchinput">
<input type="text" name="search" id="google-input" placeholder="Google search...">
<img src="https://i.imgur.com/dpkbGqu.png" alt="youtube" class="logos">
</div>
</div>

Place four elements inside div next to each other, with different widths

I am attempting to put four elements next to each other inside a container div; which all are a different width.
This is my desired outcome:
Unfortunately, this is what it looks like at the moment:
.container {
width: 100%;
background: blue;
padding: 10px;
}
p {
margin: 0px;
color: #fff;
font-size: 30px;
width: 11%;
float: left;
}
button {
float: right;
}
<div class="container">
<p>Title</p>
<form>
<input type="text" class="input">
</form>
<button>Apple</button>
<button>Orange</button>
<div style="clear: both;"></div>
</div>
JsFiddle: https://jsfiddle.net/uLL708jg/
Would Flex work for this? If so, could someone show me in an answer?
Yes, flexbox could solve this. This is just a basic setup. On .container, put a display: flex; and remove all the floats. Then wrap the buttons in a div (.right here) and give it margin-left: auto; to park it on the right of the screen.
Edit
justify-content: space-between; will align the three items so that there's space between the items and so the search will be in the middle of the div .container. No need anymore of margin-left: auto;, though the div wrapper around it is needed. Read more about flexbox and how to use it at MDN.
.container {
display: flex;
width: 100%;
background: blue;
padding: 10px;
flex-flow: row wrap;
align-items: center;
justify-content: space-between;
}
p {
margin: 0px;
color: #fff;
font-size: 30px;
width: 11%;
}
<div class="container">
<p>Title</p>
<form>
<input type="text" class="input">
</form>
<div class="right">
<button>Apple</button>
<button>Orange</button>
</div>
</div>

how to center align the label for form

I am using my own classes and tags for HTML and CSS forms. I want to align the label text in middle.
I tried to use it using line height but it get messier when the text length is heavy.
and is it possible to use vertical align: middle; Because that's not even working
HTML :
<div class="type-details">
<div class="col-xs-12 no-padding text-group">
<span class="col-md-4 no-padding form-label">Placeholder:</span>
<div class="col-md-7 no-padding">
<input class="form-text" type="text">
</div>
</div>
</div>
CSS:
.type-details {
border-bottom: 1px solid #e9eef0;
padding: 20px;
width: 60%;
float: left;
}
.form-label {
float: left;
min-width: 17px;
font-size: 11px;
text-transform: uppercase;
color: #62696d;
margin-right: 15px;
}
.no-padding {
padding: 0px;
}
input.form-text {
background: #fff;
border: 1px solid #dae2e6;
font-size: 12px;
color: #62696d;
padding: 6px 8px;
box-shadow: none;
border-radius: 0;
line-height: normal;
display: inline-block;
width: 100%;
margin-bottom: 0px;
}
Check this fiddle https://jsfiddle.net/kuascjpo/2/ I want to align the span="form-label" vertically center
There are a few ways you can do this. Here is a fiddle: https://jsfiddle.net/sheriffderek/b9jg33b3/
Markup
<form action="" class="your-thing">
<label class="input-w">
<span class="label">First<br> name:</span>
<input type="text" placeholder='sheriff'>
</label>
<label class="input-w">
<span class="label">Last name:</span>
<input type="text" placeholder='derek'>
</label>
</form>
1. With display: inline-block;
The trick is that you need the element to be display inline-block. This way, you can use the vertical-align: middle; property of inline elements - and have the other properties of block elements too. Also, you don't want to use floats in this case. So,
.inline-block .input-w {
display: block; // to stack them
width: 100%;
}
.inline-block .label, .inline-block input {
/* float: none; you may need this to overide previous float rules */
display: inline-block;
vertical-align: middle;
}
2. The other way would be to use flexbox display: flex;
This involves a little more concern about the markup and the parent of these inputs.
.flex-box {
display: flex;
flex-direction: column;
}
.flex-box .input-w {
display: flex;
flex-direction: row;
}
They both have their strengths and side-affects. : )
A simple approach/hack that works is to wrap the text inside the <label></label> tags in <p></p> tags, at which point, the text-align: center; property will work to center the text.
label{
text-align: center;
}
<label><p>Your Text</p></label>
https://jsfiddle.net/cmckay/2xydfs87/
.text-group {
display:flex;
align-items:center;
}
vertical-align: baseline|length|sub|super|top|text-top|middle|bottom|text-bottom|initial|inherit;
http://www.w3schools.com/cssref/pr_pos_vertical-align.asp