CSS - Align Text, Input and Image in DIV - html

I'm trying to align some text, and input field and an image which is approx 25x25 px
What I get is the text and Image at the top of the div and the input slightly lower down.
How do I align them so they are all vertically level with each other.
This is what I have so far:
div.block {overflow:hidden; border:1px solid #000 }
div.block label {width:150px; display:block; float:left; text-align:left; vertical-align:middle; }
div.block.input {margin-left:4px; float:left; vertical-align:middle; }
https://jsfiddle.net/a3cmfpzL/
Thanks

This is for Horizontal:
.block{
display:flex;
}
label{
border:1px solid red;
}
img{
height:25px;
width:25px;
}
<div class='block'>
<label>Test Label</label>
<input type='text' value='1234567890'/>
<img src='http://placekitten.com/301/301'/>
</div>
If you're looking for Vertical direction then:
*{
border:0;
padding:0;
}
.block{
display:flex;
flex-direction:column;
}
label{
border:1px solid red;
width:100px;
}
img{
height:25px;
width:25px;
}
<div class='block'>
<label>Test Label</label>
<input type='text' value='1234567890'/>
<img src='http://placekitten.com/301/301'/>
</div>

I would strongly advise you to use flexbox.
Can be a bit confusing at the beginning, but once you get the hang of it, you'll love it.
Here you can find a perfect guide for using it.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Something like this (just written out of my mind, so untested)
Try removing your classes first.
Your CSS
.flex {
display: flex;
flex-direction: column;
justify-content: space-between; // only use this if you don't justify-self the elements
}
.border {
border:1px solid #000;
}
img {
width: 25px;
height: 25px;
justify-self: top; // if you don't use flex-direction on .flex
}
input {
justify-self: top; // if you don't use flex-direction on .flex
}
Your HTML
<div style="flex border">
<img src='image.png'/>
<label>Test Label</label>
<input type='text' value='1234567890'/>
</div>

Few Issues
This div.block.input isn't selecting anything so all those styles aren't being used
You made the label a block level element so it won't respond to the vertical-align property.
The <input> and the <img> are inline elements so they will share the same line box in which they live, Their vertical alignment will be set by the vertical-align property.
The default value for vertical-align property is baseline:
Align the baseline of the box with the baseline of the parent box. If the box does not have a baseline, align the bottom margin edge with the parent's baseline.
In this case we have an <img> which doesn't have a baseline, So it will be aligned based on it's bottom margin, in our case the img have no margin so it's basically the bottom edge.
The <input> However have a baseline, So it's baseline will be aligned with the bottom edge of the img next to it.
DEMO
div.block {
overflow: hidden;
border: 1px solid #000
}
div.block label {
width: 150px;
display: block;
float: left;
text-align: left;
vertical-align: middle;
}
div.block.input {
margin-left: 4px;
float: left;
vertical-align: middle;
}
div {
position: relative;
}
div:after {
content: '';
position: absolute;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(red, red) 0 24.5px/ 100% 1px no-repeat
}
<p>red line is the baseline</p>
<div class='block'>
<label>Test Label</label>
<input type='text' value='1234567890' />
<img src='https://i.picsum.photos/id/527/25/25.jpg' />
</div>
Solutions
I cleaned the code a bit and remove unnecessary styles.
Floats aren't necessary
If you to apply a width to the label use display:inline-block; instead
1) vertical-align: middle;
* {
padding: 0;
margin: 0;
}
div.block {
overflow: hidden;
border: 1px solid #000
}
label {
width: 150px;
display: inline-block;
vertical-align: middle;
background: brown;
}
input {
vertical-align: middle;
border: 0;
background: red;
}
img {
vertical-align: middle;
}
<div class='block'>
<label>Test Label</label>
<input type='text' value='1234567890' />
<img src='https://i.picsum.photos/id/527/25/25.jpg' />
</div>
Items will be aligned relative to middle of the tallest item which is the img.
2) If you want elements to share the same height
Knowing the <img> is the tallest and it's height, we can simple apply that same height to the other elements, We still need to apply vertical-align:top to align the elements based on their top edge instead of the baseline
* {
padding: 0;
margin: 0;
/* because we defined a height on the element,
we need to ensure that any extra space should be
calculated within that height */
box-sizing: border-box;
}
div.block {
overflow: hidden;
border: 1px solid #000;
font-size:0;
}
label {
display: inline-block;
width: 150px;
height: 25px;
font-size:16px;
vertical-align: top;
line-height:25px;
background:brown;
}
input {
height: 25px;
vertical-align: top;
border:0;
background:red;
}
<div class='block'>
<label>Test Label</label>
<input type='text' value='1234567890' />
<img src='https://i.picsum.photos/id/527/25/25.jpg' />
</div>
3) Flexbox
Which is the simplest and i won't be covering because it have been covered in other answers.

Try adding position:absolute and display:flex inside your div.block.
That should do the trick if I understand your question correctly.

Related

i want the input box and the search button combine? why the div is applicable of the margin i want that search from the top 40px below

i want to
.k1 {
width: 760px;
height: 30px;
margin-top: 40px;
}
.k2 {
width: 670px;
height: 30px;
}
.k3 {
width: 75px;
height: 30px;
}
<div class="k1">
<input type="text" class="k2"></input>
<button value="search" class="k3" ></button>
</div>
i want the input box and button combine and 40px from the top
why div is varied with the margin
the elements in the div is some where and div block is some where
the elemets inside the div vary with the div margin if div margin-top is 40 px the tags inside the is div is with margin-top with some margin from the top of the document
Something like this?:
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #ffffcc;
}
.k1 {
margin-top: 40px;
height: 30px;
overflow: hidden;
}
.k2 {
display: block;
float: left;
width: calc(100% - 75px);
height: 100%;
}
.k3 {
display: block;
float: left;
width: 75px;
height: 100%;
}
<div class="k1">
<input type="text" class="k2">
<button value="search" class="k3">Search</button>
</div>
Please explain more on the issue. If you want to align both input and button from the top and want to add margin to a particular element(input or button). In that case, you have to add vertical-align:top to .k2 or .k3
.k1{
width:760px;
height:30px;
margin-top:40px;
}
.k2{
width:670px;
height:30px;
vertical-align:top;
}
.k3{
width:75px;
height:30px;
vertical-align:top;
}
The first input does not need a closing tag. It should be:
<input type="text" class="k2" />
The button and inputs have display: inline-block (which takes margin to left) by default. Code will be:
.k2{
width:670px;
height:30px;
float: left
}
.k3{
width:75px;
height:30px;
float: left
}
So either use float: left; to both element and the clearfix class to the parent (if you are using Bootstrap), otherwise you will have to use height or clear property for the parent.
Just put the button as input and include its type in it.
.k1{
width:760px;
height:30px;
margin-top:40px;
}
.k2{
width:670px;
height:30px;
}
.k3{
width:75px;
height:35px;
}
<div class="k1">
<input type="text" class="k2"></input>
<input type="button" value="search" class="k3"></input>
</div>

Vertically aligning floating divs in modern browsers (2014)

I'd like to ask this question again as its previous incarnation was half a decade ago. We need not consider anything pre-IE9 for the purposes of this discussion:
I am trying to float two divs with different font-sizes. I can't find a way to align the text on the same baseline. Here is what I have been trying:
<div id="header">
<div id="left" style="float:left; font-size:40px;">BIG</div>
<div id="right" style="float:left;">SMALL</div>
</div>
I am struggling with this currently and the best solution I've found is magic offsets from inspection, and that's hardly robust. Inline-block has its own issues I'd prefer to avoid.
Edit:
http://jsfiddle.net/crw4r/10/
As you can see, floats align at the top, not at the baseline.
You could use display: table-cell instead of floats?
#header {
display: table;
width: 100%;
}
#header div {
display: table-cell;
}
#left {
font-size: 40px;
}
#right {
text-align: right;
}
Demo
Set the line-height to be the same on both.
http://jsfiddle.net/crw4r/6/
eg.
line-height: 42px;
or if this is not what you want...
you could use absolute positioning.
http://jsfiddle.net/crw4r/7/
or, you could set the line height on both and add margin to the top of the smaller one, so the sum of the line-height and top margin are the same on both text.
http://jsfiddle.net/crw4r/13/
With display: inline-block, the divs are automatically aligned on the baseline. To compensate for the float, you can use text-align
#left {
display: inline-block;
width: 50%;
font-size: 40px;
text-align: left;
}
#right {
display: inline-block;
width: 50%;
text-align: right;
}
See JSFiddle
If you need to account for white space, use width: 49% for one of the divs
JSFiddle
<div id="container">
<div class="left"><span>Big</span></div>
<div class="right"><span>Small</span></div>
</div>
#container{
width:100%;
margin:0px auto;
}
#container div{
position:relative;
height: 42px;
width: 100px;
}
#container div span{
position:absolute;
bottom:0;
right:0;
}
.left{
float:left !important; font-size:40px;
}
.right{
float:right !important;
}
Try below css and html
CSS
.header {
overflow: hidden;
width: 200px;
display:table;
}
.header > div{
display:table-row;
}
.header > div > div{
display:table-cell;
vertical-align:baseline;
width:50%;
}
.big {
text-decoration: underline;
font-size: 40px;
}
.small {
text-decoration: underline;
font-size: 12px;
}
HTML
<div class="header">
<div>
<div class="big">BIG</div>
<div class="small">SMALL</div>
</div>
</div>

Conflict between making a div a clickable link and vertical centering

To make the rounded boxes (see http://jsfiddle.net/L36nq/) clickable/hoverable, I need to use
a {
display: block;
}
Yet to keep the images (the two squares) centered, I need to use
#left {
display:table;
...
}
#right {
display:table;
...
}
How do I make the full rounded boxes clickable/hoverable while keeping the images centered?
HTML
<div id="left">
<div id="cell">
<a href="http://google.com/">
<img src="box.svg" width="75%" height="75%" />
</a>
</div>
</div>
<div id="right">
<div id="cell">
<a href="http://bing.com/">
<img src="box.svg" width="75%" height="75%" />
</a>
</div>
</div>
CSS
#left {
display:table; position:absolute;
top:25%; left:25%; width:24%; height:50%;
vertical-align:middle; text-align:center; margin: auto; border-radius:30px;
background-color: #D88;
}
#right {
display:table; position:absolute;
top:25%; left:51%; width:24%; height:50%;
vertical-align:middle; text-align:center; margin: auto; border-radius:30px;
background-color: #88D;
}
#cell {display:table-cell; vertical-align:middle;}
a {
display: block;
}
body { background-color: #9D9; }
a {
display: block;
min-width: 100%;
min-height: 100%;
position:absolute;
top:0;
}
Your anchor tag is not inheriting and height or width so it's essentially 0x0 px. The above css worked for me in fiddle: http://jsfiddle.net/MkfAe/
Something like this might work for you. http://jsfiddle.net/ZNXvT/1/
I'm taking your #cell elements and keeping them as block-level elements, and setting a height: 100%; on them. I also set height: 100%; on your anchor element.
#cell {
height: 100%;
}
a {
display: block;
height: 100%;
}
Now onto the magic. You can add an element inside the a (I called mine .valign-height) and set that as height: 100%;, and also add a vertical-align: middle;.
a .valign-height {
display: inline-block;
height: 100%;
vertical-align: middle;
width: 0;
}
Then with the image (or svg) itself, I set that to vertical-align: middle;.
a img {
vertical-align: middle;
}
An element is not able to be vertically-aligned by itself, as vertical-align aligns to its siblings. If we have a hidden element that will spoof the vertical-align for you, it will center your content always. This way, you can keep the anchor filling the space, and keep your img element vertically centered.

Span, Input, Span 100% width CSS issue

This is what i'm doing right now. I'm using LESS CSS for my design. I need to put 2 spans between a specified input. all the elements should be 100% width. spans should always 20px width input width can be change according to the screen width. Can anyone help me?
span width: 20px;
<div class="wrapper">
<span class="span-one">span</span>
<input type="text" class="input">
<span class="span-two">span</span>
</div>
You can achieve this with absolute positioning. It will take the spans out of the flow and put them on top of the input. You should also put the input in a div to do this as it doesn't naturally get 100% width when display:block set on it.
HTML
<div class="wrapper">
<span class="span-one">span</span>
<div class="input"><input type="text" class="input"></div>
<span class="span-two">span</span>
</div>
CSS
.wrapper { position: relative; }
div.input { margin: 0 20px; }
input {
width: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box; }
span.span-one {
position: absolute;
width:20px; height:20px;
left:0; top:0;
background-color: red; }
span.span-two {
position: absolute;
width:20px; height:20px;
right:0; top:0;
background-color: red; }
Here's the fiddle: http://jsfiddle.net/ywUeu/1/
Of course the word 'span' in the spans is longer than 20px so it comes out of the span.
Might be best to add 'box-sizing' to input as I've done too.
Positioning is not ideal but you already approved the answer...this is an FYI....
Instead of positioning use: display: table-cell;
http://jsfiddle.net/Riskbreaker/CBC5A/1/
With your HTML:
.wrapper, .text {
width: 100%;
}
.wrapper > span {
display: inline-block;
width: 20px;
}
.wrapper.input {
width: calc(100% - 40px);
}
Better way:
<div class="wrapper">
<div class="input"><input type="text" class="text-input"></div>
</div>
CSS:
/*
.wrapper, .input{ width is 100% by default }
*/
.text-input{
margin: 0 20px;
width: calc(100% - 40px);
}

Center a button in a tiny div

I have a div with unknown height, though in this example I'm using 3px. I want to center the button but it seems to always offset by some arbitrary amount. I could do an absolute positioning trick dynamically once I know the height but I would prefer a css solution if possible.
<div style="width: 100%; height: 3px;">
<div class="special">
<input type="button" />
</div>
</div>
div{
height: 100%;
overflow: visible;
}
.special{
position:relative;
text-align: center;
padding: 1px;
border: 1px solid red;
}
input{
height: 100%;
width: 100px;
min-height: 8px;
}
The idea is that with the min-height the button will overflow evenly over the top and bottom of the div.
jsfiddle
You can get this to work by doing the following:
CSS:
div {
height: 100%;
overflow: visible;
}
.special {
text-align: center;
border: 1px solid red;
position:relative;
}
input {
position:absolute;
margin-top:-4px;
margin-bottom:-4px;
top:0px;
bottom:0px;
width:100px;
min-height:7px;
}
Note the input declaration in particular. Instead of height:100% I used position:absolute with top:0px and bottom:0px. This will set the height to 100%. It needs to be position of absolute so that you can do margin-top:-4px and margin-bottom:-4px to get the overlap. You can see that it works for any height of the outer <div/>.
The JSFiddle below has some added controls so you can change the height of the .special <div/> without needing to refresh.
http://jsfiddle.net/bmyAW/8/