I am unable to set my text as vertical-center. My text is placed in position:absolute div.
<div class="mydiv">Frameworks and Extensions</div>
and the CSS:
.mydiv{
width:100px;
height:70px;
border:1px solid red;
position:absolute;
text-align:center;
background-color:#ccc;
}
See fiddle: http://jsfiddle.net/3Zv5s
Sometimes Text will come two or one line. The text should be in vertical center like in table td.
Thanks for your valuable time and suggestion.
Try with this CSS:
.mydiv{
width:100px;
height:70px;
border:1px solid red;
display: table-cell;
vertical-align: middle;
text-align:center;
background-color:#ccc;
}
Try this
UPDATED
You could set container to position: absolute; and make .mydiv to display:table-cell and vertical-align:middle.
HTML -
<div id="container"><div class="mydiv">Frameworks and Extensions</div></div>
CSS -
#container{
position: absolute;
}
.mydiv{
width:100px;
height:70px;
border:1px solid red;
display: table-cell;
vertical-align: middle;
text-align:center;
background-color:#ccc;
}
Try for this
You could put your text into a table and use vertical-align
http://jsfiddle.net/3Zv5s/6/
Try this:
HTML:
<div class="mydiv">
<span class="span">Frameworks and Extensions</span>
</div>
CSS:
.mydiv{display:table-row;}
.span{display:table-cell;vertical-align:middle;height:inherit;}
Fiddle here.
You could use a combination of display: table and display: table-cell.
Change slightly your markup:
<div class="mydiv">
<div>Frameworks and Extensions</div>
</div>
And your CSS:
.mydiv{
width:100px;
height:70px;
border:1px solid red;
display: table;
background-color:#ccc;
overflow: hidden;
}
.mydiv div {
vertical-align: middle;
display: table-cell;
}
http://jsfiddle.net/3Zv5s/2/
Option #1 : table-cell
Simplest solution, but only for IE7+
<div class="mydiv">
<span>Frameworks and Extensions</span>
</div>
.mydiv {
width:100px;
height:70px;
border:1px solid red;
position:absolute;
text-align:center;
background-color:#ccc;
display: table;
}
.mydiv>span {
display: table-cell;
vertical-align: middle;
}
Fiddle
Option #2 : double span
A bit more tricky, but works under IE7
<div class="mydiv">
<span><span>Frameworks and Extensions</span></span>
</div>
.mydiv {
width:100px;
height:70px;
border:1px solid red;
position:absolute;
text-align:center;
background-color:#ccc;
display: block;
line-height: 65px; /* 70px applied on 1st span */
}
.mydiv>span {
display: inline-block;
vertical-align: middle; /* 2nd span centered */
line-height: 0;
}
.mydiv>span>span {
line-height: 20px; /* here's the "true" line-height */
}
Fiddle
Related
.wrap{
position:fixed;
left:0; top:45px;
width:100%;
text-align:center;
background:gold;
}
.datewrap{
display:inline-block;
margin:0 5px;
border:2px solid red;
overflow:hidden;
}
.btnow{
display:inline-block;
background:green;
color:white;
margin:0 5px;
border:2px solid red;
}
<div class='wrap'>
<div class='datewrap'>323232</div>
<div class='btnow'>NOW</div>
</div>
Why is btnow moved down? It should be inline with datewrap.
If I remove overflow:hidden from datewrap - it's ok.
But I need overflow:hidden on datewrap.
When you use of overflow:hidden[overflow property evaluating to something other than visible] , the baseline is the bottom edge of the margin-box[insert margin-bottom and see result],so this element for align its baseline with baseline of other element move up a bit.
for fix use of vertical-align: top; like this:
.btnow {
vertical-align: top;
//Other css
}
.wrap{
position:fixed;
left:0; top:45px;
width:100%;
text-align:center;
background:gold;
}
.datewrap{
display:inline-block;
margin:0 5px;
border:2px solid red;
overflow:hidden;
}
.btnow{
display:inline-block;
background:green;
color:white;
margin:0 5px;
border:2px solid red;
vertical-align: top;
}
<div class='wrap'>
<div class='datewrap'>323232</div>
<div class='btnow'>NOW</div>
</div>
text-align really shouldn't be used to position elements. There are far better ways to achieve this.
I don't know why overflow is causing it to "teeter-totter", but below is some code to fix this.
.wrap{
display: flex;
justify-content: center;
/* and if you want to make sure the elements are always aligned vertically */
align-items: center;
/* remember: justify-content will always control the same direction as the flex
** box; so, if the flex box is a row, justify-content will control the horizontal
** spacing and align-items will control the vertical spacing, but if the flex box
** is a column justify-content will control the vertical and align-items will
** control the horizontal. */
width: 100%;
position: fixed;
top: 45px;
left: 0;
background: gold;
}
.datewrap, .btnow {
margin: 0 5px;
display: inline-block;
border: 2px solid red;
}
.datewrap{
overflow: hidden;
}
.btnow{
color: white;
background: green;
}
<div class='wrap'>
<div class='datewrap'>323232</div>
<div class='btnow'>NOW</div>
</div>
.wrap{
position:fixed;
left:0; top:45px;
width:100%;
text-align:center;
background:gold;
}
.datewrap{
display:inline-block;
margin:0 5px;
border:2px solid red;
overflow:hidden;
}
.btnow{
display:inline-block;
background:green;
color:white;
margin:0 5px;
border:2px solid red;
position:inherit;
}
<div class='wrap'>
<div class='datewrap'>323232</div>
<div class='btnow'>NOW</div>
</div>
The vertical-align CSS property specifies the vertical alignment of an inline or table-cell box.
Reference Link:
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align
.wrap {
position: fixed;
left: 0;
top: 45px;
width: 100%;
text-align: center;
background: gold;
}
.datewrap, .btnow {
display: inline-block;
margin: 0 5px;
border: 2px solid red;
}
.datewrap {
overflow: hidden;
vertical-align: bottom
}
.btnow {
color: white;
}
<div class='wrap'>
<div class='datewrap'>323232</div>
<div class='btnow'>NOW</div>
</div>
Note:
If you are writing code in real-time, you need to minimize your CSS.
when I display img inline-block.but there is a gap between the first line and the second !! below is the sample, picture1 and picture3 have a gap?I dont't want the gap..so help me..
img {
display:inline-block;
width:200px;
background-color:#ccc;
border:5px solid red;
padding:10px;
text-align:center;
margin:0px;
}
<img alt="picture1"/><img alt="picture2"/><img alt="picture3"/><img alt="picture4"/>
Images make a gap by default, you can fix that using vertical-align
img {
display: inline-block;
width: 200px;
background-color: #ccc;
border: 5px solid red;
padding: 10px;
text-align: center;
margin: 0px;
vertical-align: middle;
}
http://jsfiddle.net/opz672zn/
vertical-align may help you to fix the issue.
img {
display:inline-block;
width:200px;
background-color:#ccc;
border:5px solid red;
padding:10px;
text-align:center;
margin:0px;
vertical-align: top; <!--Added-->
}
<img alt="picture1"/><img alt="picture2"/><img alt="picture3"/><img alt="picture4"/>
Working Fiddle
I'm trying to make the div have same height so i used display table , problem that i having is the width will grow.
As an example try adding content in the tableright , when it is exceeded the width , it will not break to the next line but it will expand horizontally to the tableleft.
Please Advice.
Below is the sample code
<div id="maintable">
<div id="table-row">
<div id="tableleft></div>
<div id="tableright></div>
</div>
</div>
<style>
#maintable
{display:table;
width:100%;
}
#table-row
{
display:table-row
width:100%;
}
#tableleft
{
width:60%;
display:table-cell;
}
#tableright
{
width:40%;
display:table-cell;
}
</style>
You made few syntax error there.
Further to make sure if one big un spaced word is placed in table-cell i have used "table-layout: fixed;"
check this fiddle: http://jsfiddle.net/5q9K8/17/
The HTML:
<div id="maintable">
<div id="table-row">
<div id="tableleft">lorem</div>
<div id="tableright">ipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsum</div>
</div>
</div>
The Css:
#maintable
{
display:table;
width:100%;
table-layout: fixed;
word-wrap:break-word;
}
#table-row
{
display:table-row;
width:100%;
}
#tableleft
{
width:60%;
display:table-cell;
background: #eef;
}
#tableright
{
width:40%;
display:table-cell;
background: #efe;
}
Add width as accordingly here just an example to show div height equal to table height.
css
.maintable {
width:100%;
display: table;
border: 1px solid blue;
}
.maintable .table-row {
display: table-row;
border: 2px solid black;
}
.maintable .table-row .tableleft, .maintable .table-row .tableright {
display: table-cell;
border: 4px solid red;
}
.tableright {
width: 60%;
}
.tableleft {
width: 40%;
}
DEMO
Final DEMO
I am having some trouble with div positioning. I'm working on a comment system in wich comments can get upvotes and downvotes. For every comment the up/down vote-buttons needs to be left of my comment text, and vertically aligned in the middle of my comment-container div. (regardless of how big the comment is)
At the moment it wont work properly, because the buttons wont get to the middle of the div. (see: http://jsfiddle.net/mcSfe/1838/)
In the testcase i want the leftside to be stretched all the way down, and the red box vertically centered in the middle of the leftside. vertical-align, and display:table-cell, did not brought the right result..
Here is my test html code:
<div class="commentContainer">
<div class="leftside">
<div class="innerleft">
test
</div>
</div>
<div class ="CommentBox">
<p>hello</p>
<p>this is my comment</p>
<p>another line of comment</p>
</div>
and here is my test css code:
div.commentContainer{
float:left;
border:1px solid blue;
}
div.leftside {
float:left;
width: 50px;
background: gray;
text-align: center;
}
div.innerleft {
float:left;
width: 25px;
height: 25px;
margin-left:13px;
background: red;
}
div.CommentBox {
float:right;
width:200px;
background-color:green;
}
Remove float from .commentbox and .leftside and add display:table-cell with vertical-align:middle
div.commentContainer{
float:left;
border:1px solid blue;
}
div.leftside {
width: 50px;
background: gray;
text-align: center;
display: table-cell;
vertical-align: middle
}
div.innerleft {
float:left;
width: 25px;
height: 25px;
margin-left:13px;
background: red;
}
div.CommentBox {
width:200px;
background-color:green;
display: table-cell
}
DEMO
LIke this
demo
css
div.commentContainer{
float:left;
border:1px solid blue;
display:table;
}
div.leftside {
display:table-cell;
width: 50px;
background: gray;
text-align: center;
}
div.innerleft {
width: 25px;
height: 25px;
margin-left:13px;
background: red;
vertical-align:middle;
}
div.CommentBox {
display:table-cell;
width:200px;
background-color:green;
}
Inside of using floats, use inline-block.
JSFiddle
CSS
div.commentContainer{
float:left;
border:1px solid blue;
}
div.leftside {
display:inline-block;
vertical-align:middle;
width: 50px;
background: gray;
text-align: center;
}
div.innerleft {
float:left;
width: 25px;
height: 25px;
margin-left:13px;
background: red;
}
div.CommentBox {
display:inline-block;
vertical-align:middle;
width:200px;
background-color:green;
}
Issues regarding inline-block whitespace can be addressed separately.
I have a div (#wrapper) containing 2 divs standing side by side.
I would like the right-div to be vertically aligned. I tried vertical-align:middle on my main wrapper but it is not working. It is driving me crazy!
Hope someone can help.
http://cssdesk.com/LWFhW
HTML:
<div id="wrapper">
<div id="left-div">
<ul>
<li>One</li>
<li>Two</li>
</ul>
</div>
<div id="right-div">
Here some text...
</div>
</div>
CSS:
#wrapper{
width:400px;
float:left;
height:auto;
border:1px solid purple;}
#left-div{
width:40px;
border:1px solid blue;
float:left;}
#right-div{
width:350px;
border:1px solid red;
float:left;}
ul{
list-style-type: none;
padding:0;
margin:0;}
You'll have no luck with floated elements. They don't obey vertical-align.
You need display:inline-block instead.
http://cssdesk.com/2VMg8
Beware!
Be careful with display: inline-block; as it interprets the white-space between the elements as real white-space. It does not ignores it like display: block does.
I recommend this:
Set the font-size of the containing element to 0 (zero) and reset the font-size to your needed value in the elements like so
ul {
margin: 0;
padding: 0;
list-style: none;
font-size: 0;
}
ul > li {
font-size: 12px;
}
See a demonstration here: http://codepen.io/HerrSerker/pen/mslay
CSS
#wrapper{
width:400px;
height:auto;
border:1px solid green;
vertical-align: middle;
font-size: 0;
}
#left-div{
width:40px;
border:1px solid blue;
display: inline-block;
font-size: initial;
/* IE 7 hack */
*zoom:1;
*display: inline;
vertical-align: middle;
}
#right-div{
width:336px;
border:1px solid red;
display: inline-block;
font-size: initial;
/* IE 7 hack */
*zoom:1;
*display: inline;
vertical-align: middle;
}
You can do this quite easily with display table and display table-cell.
#wrapper {
width: 400px;
float: left;
height: auto;
display: table;
border: 1px solid green;
}
#right-div {
width: 356px;
border: 1px solid red;
display: table-cell;
vertical-align: middle;
}
EDIT: Actually quickly messed around on CSS Desk for you - http://cssdesk.com/RXghg
ANOTHER EDIT: Use Flexbox. This will work but it's pretty outdated - http://www.cssdesk.com/davf5
#wrapper {
display: flex;
align-items: center;
border:1px solid green;
}
#left-div {
border:1px solid blue;
}
#right-div {
border:1px solid red;
}
I realize this is an ancient question however I thought it would be useful to post a solution to the float vertical alignment issue.
By creating a wrapper around the content you want floated, you can then use the ::after or ::before pseudo selectors to vertically align your content within the wrapper. You can adjust the size of that content all you want without it affecting the alignment. The only catch is that the wrapper must fill 100% height of its container.
http://jsfiddle.net/jmdrury/J53SJ/
HTML
<div class="container">
<span class="floater">
<span class="centered">floated</span>
</span>
<h1>some text</h1>
</div>
CSS
div {
border:1px solid red;
height:100px;
width:100%;
vertical-align:middle;
display:inline-block;
box-sizing: border-box;
}
.floater {
float:right;
display:inline-block;
height:100%;
box-sizing: border-box;
}
.centered {
border:1px solid blue;
height: 30px;
vertical-align:middle;
display:inline-block;
box-sizing: border-box;
}
h1 {
margin:0;
vertical-align:middle;
display:inline-block;
box-sizing: border-box;
}
.container:after, .floater:after, .centered:after, h1:after {
height:100%;
content:'';
font-size:0;
vertical-align:middle;
display:inline-block;
box-sizing: border-box;
}
I do my best to avoid using floats... but - when needed, I vertically align to the middle using the following lines:
position: relative;
top: 50%;
transform: translateY(-50%);
A possible solution is to make wrapper div flex with items aligned on center as specified by https://spin.atomicobject.com/2016/06/18/vertically-center-floated-elements-flexbox/.
The only downfall of my modifications is you have a set div height...I don't know if that's a problem for you or not.
http://cssdesk.com/kyPhC