Am trying vertical align some text within a div to the center but I am unable to do so. I've tried using the display method to be table and table cell but this doesn't seem to work. Please help.
Fiddle
HTML:
<div class="vc_row wpb_row vc_row-fluid blunetser-banner vc_custom_1415005990938">
<div class="vc_col-sm-12 wpb_column vc_column_container">
<div class="wpb_wrapper">
<div class="vc_row wpb_row vc_inner vc_row-fluid blunet-serv-banner-row">
<div class="wpb_wrapper">
<div class="wpb_text_column wpb_content_element ">
<div class="wpb_wrapper">
<p>Some text will go here</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
CSS:
.blunetser-banner {
display: table;
height: 300px;
background: #eee;
}
.blunet-serv-banner-row {
display: table-cell;
vertical-align: middle;
}
This is my favorite solution for this issue (simple and very well browser supported):
html
<div class="vcenter">
<p>Text</p>
</div>
css
.vcenter{ //change this
width: 300px;
height: 300px;
}
.vcenter:before {
content: " ";
display: inline-block;
height: 100%;
vertical-align: middle;
}
.vcenter :first-child {
display:inline-block;
vertical-align:middle;
}
Fiddle: http://jsfiddle.net/u5x7ta0w/2/
So, for your case:
.wpb_wrapper{
width: 300px;
height: 300px;
}
.wpb_wrapper:before {
content: " ";
display: inline-block;
height: 100%;
vertical-align: middle;
}
.wpb_wrapper :first-child {
display:inline-block;
vertical-align:middle;
}
I think you used the wrong class names in your stylesheet?
http://jsfiddle.net/f72ocv0x/5/
.wpb_text_column {
display: table;
height: 300px;
background: #eee;
}
.wpb_wrapper {
display: table-cell;
vertical-align: middle;
}
Because .blunet-serv-banner-row is not the direct child of .blunetser-banne, it doesn't take up 100% height.
It should be :
.vc_column_container {
display: table-cell;
vertical-align: middle;
}
See updated fiddle here.
you have to give the div with the text in it a line-height if u give the div the line height 300px it will be in the middle
.blunetser-banner {
display: table;
height: 300px;
background: #eee;
line-height: 300px;
}
line-height needs to be the same as the height of the div
http://jsfiddle.net/f72ocv0x/6/
Related
Hi Everybody and tks in advance for your help... I'm looking for the best practice to resolve a simple question:
.left {
float: left;
width: 79%;
margin-right: 1%;
}
.left img {
float: right;
}
.right {
float: right;
width: 20%;
}
<div class="main">
<div class="left">
<img src="http://placehold.it/200x200" />
</div>
<div class="right">A TEXT</div>
</div>
How should I center the text vertically at the middle of the image (obviusly not using px in margin-top or bottom, because the width/height of the image will be dinamic).
Thanks!
You could use flexbox. See the example:
.main{
display: flex;
align-items: center;
}
https://jsfiddle.net/zb2ozq1k/1/
I'd get rid of float and use table-cells instead. Then, use vertical-align:middle for the text.
Like this:
.main{
display: inline-table;
border: 1px solid blue;
}
.left {
width: 79%;
margin-right: 1%;
display: table-cell;
border: 1px solid green;
}
.right {
width: 20%;
border: 1px solid red;
display: table-cell;
vertical-align: middle;
}
<div class="main">
<div class="left">
<img src="http://placehold.it/200x200" />
</div>
<div class="right">A TEXT</div>
</div>
h1 {
display: inline;
vertical-align:top;
}
<div class="middle">
<img src="http://placekitten.com/200/150" >
<h1>A TEXT</h1>
</div>
I'm having a bit of difficulty in displaying a table. I use display:table and display:table-cell a lot for sections usually. Especially when I just want to center the content of a section vertically. So to say, I have the following HTML:
<div class="wrapper">
<div class="cell">
<div class="red">
</div>
</div>
</div>
and the following css applied to the html:
html,body {
height: 100%;
}
.wrapper {
display: table;
width: 100%;
height: 100%;
}
.wrapper * {
width: 100%;
height: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
}
.red {
margin-right: auto;
margin-left: auto;
background: red;
height: 200px;
width: 200px;
}
Now there is a small problem. I want to add a section header to this particular section and the section header has to be a child of .wrapper, so the HTML changes as below :
<div class="wrapper">
<div class="section-heading">
<h1>section heading</h1>
</div>
<div class="cell">
<div class="red">
</div>
</div>
</div>
Now the problem with using display table and table-cell is that when I add a header to the section, I can't add it without it affecting the other child elements of .wrapper . So how do I add a heading (when the heading is added in the above HTML the .cell div seems to be moving horizontally slightly)?
Of course I could use absolute positioning, but I was just wondering, is there something that can be done, without taking the heading element out of the flow?
FIDDLE HERE
Did you try adding display:table-row to the section-heading?
.wrapper > .section-heading{
display:table-row;
height:auto;
}
Fiddle: http://jsfiddle.net/7xo353hg/4/
You can make heading container display: table-row:
.section-heading {
display: table-row;
text-align: center;
}
Check the demo:
html, body {
height: 100%;
}
.wrapper {
display: table;
width: 100%;
height: 100%;
}
.wrapper * {
width: 100%;
height: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
}
.red {
margin-right: auto;
margin-left: auto;
background: red;
height: 200px;
width: 200px;
}
.section-heading {
display: table-row;
text-align: center;
}
<div class="wrapper">
<div class="section-heading">
<h1>section heading</h1>
</div>
<div class="cell">
<div class="red"></div>
</div>
</div>
I've searched it at online and found some solution. But, nothing works at my project. At most of the solution, I've found:
<div class="a">
<div class="b">
Unknown stuff to be centered.
</div>
</div>
.a {
display: table;
width: 100%;
}
.b {
display: table-cell;
text-align: center;
vertical-align: middle;
}
By applying this technique, I've tried to build something like this: http://jsfiddle.net/L2GZx/1/
The text of left column only needed to be aligned middle vertically. But, it's not working with that technique:
<div class="row">
<div class="left">
<p>Sample Text</p>
</div>
<div class="right">
<p>Text</p>
<p>Input Element</p>
<p>Table</p>
<p>Image</p>
</div>
</div>
<div class="row">
<div class="left">
<p>Sample Text Sample Text Sample Text Sample Text Sample Text </p>
</div>
<div class="right">
<p>Text</p>
<p>Input Element</p>
<p>Table</p>
<p>Image</p>
</div>
</div>
<div class="row">
<div class="left">
<p>Sample Text</p>
</div>
<div class="right">
<p>Text</p>
<p>Input Element</p>
<p>Table</p>
<p>Image</p>
</div>
</div>
CSS:
.row {
width: 100%;
background: #ccc;
border-bottom: 1px solid #999;
display: table;
}
.left {
float: left;
width: 40%;
padding: 20px;
display: table-cell;
vertical-align: middle;
}
.right {
float: right;
background: #fff;
width: 40%;
padding: 20px;
}
How can I make the text of left-column aligned middle vertically? Note: I can't use any fixed height as content of each row will be different
Remove the floats. Floated elements can not also be displayed as table-cells. See updated Fiddle.
.row {
width: 100%;
background: #ccc;
border-bottom: 1px solid #999;
display: table;
}
.left {
width: 40%;
padding: 20px;
display: table-cell;
vertical-align: middle;
}
.right {
display: table-cell;
background: #fff;
width: 40%;
padding: 20px;
}
.left {
width: 40%;
padding: 20px;
display: table-cell;
vertical-align: middle;
}
removing" float:left " from .left style solves that issue, but using table and div together is not that good.Working Example
An alternative that I prefer in a situation like this is:
To not use display: table-cell, but rather use display:
inline-block.
To then use vertical-align: middle on the element.
Sample (revised) markup / css:
<div class="row">
<div class="left">
<p>Sample Text</p>
</div>
<div class="right">
<p>Text</p>
<p>Input Element</p>
<p>Table</p>
<p>Image</p>
</div>
</div>
CSS:
.row {
width: 100%;
background: #ccc;
border-bottom: 1px solid #999;
}
.row > div {
display: inline-block;
/* below 2 lines are IE7 hack to make inline-block work */
zoom: 1;
*display: inline;
/* below is consolidated css for both left / right divs */
width: 40%;
padding: 20px;
}
.left {
vertical-align: middle; /* or top or bottom */
}
.right {
background: #fff;
vertical-align: top; /* or middle or bottom */
}
All you have to do is to add a line-height to the left column and it will be automatically aligned (without vertical-align so you can remove it).
Here it is:
.left {
float: left;
width: 40%;
padding: 20px;
display: table-cell;
line-height:150px;
}
And here is your updated FIDDLE
Using your first example, try something like this. I'll explain how it works in the CSS.
<div class="a">
<div class="b">
Unknown stuff to be centered.
</div>
</div>
.a {
width: 100%;
position: relative; /* We want our parent div to be the basis of our absolute positioned child div */
/* You can set your height here to whatever you want */
}
.b {
position: absolute;
width: 100%; /* Set to be the full width, so that our text is aligned centered */
text-align: center;
top: 50%; /* Positions the top of the div at 50% of the parent height */
left: 0; /* Assures that the child div will be left-most aligned */
margin-top: -.5em; /* Moves the top of our div up by half of the set font size */
height: 1em; /* Sets our height to the height of the desired font */
}
Here is the JSFiddle link to see a live example: http://jsfiddle.net/L2GZx/20/
This is one of the best solutions to absolutely center text inside of a webpage. It does have it's limitations however seeing how it won't react to other elements inside the same parent and it also has to have a set height. So multiline text will have it's shortcomings with this method.
I hope this helps!
I am trying to simply align two elements vertically, they are inside a div. I have several images that all have different heights.
HTML
<div class="companyBox">
<img src="http://dummyimage.com/145x150/555555/000000&text=image" />
<div class="showPlans">
<p>Show Plans</p>
</div>
</div>
CSS
.companyBox {
height: 250px;
background-color: #999;
text-align: center;
}
.companyBox img {
vertical-align: middle;
}
.companyBox .showPlans {
vertical-align: middle;
}
Here is a http://jsfiddle.net/hQab5/
Thank you for any help, I don't understand why I am having trouble with something that I would have considered simple.
Fiddle
HTML:
<div class="companyBox">
<div class="showPlans">
<img src="http://dummyimage.com/145x150/555555/000000&text=image" />
<p>Show Plans</p>
</div>
</div>
CSS:
.companyBox {
height: 250px;
background-color: #999;
text-align: center;
width: 100%;
display: table;
}
.companyBox img {
vertical-align: middle;
margin: 0 auto;
}
.companyBox .showPlans {
vertical-align: middle;
display: table-cell;
}
I am trying to create rows of links in html+css, the format of which should look like this:
(Psuedocode)
<body class="class_with_minmaxwidth_set_in_css">
<div class="container">
<div class="row">
<div class="fixed_left_40px"></div>
<div class="fluid_center_which_changes_with_window_size"></div>
<div class="fixed_right_40px"></div>
</div>
... repeat more row divs
</div>
</body>
I have tried various combinations of using floats, positions and displays for each of the three divs within, but still am not able to set the right combination. Help will be highly appreciated!
display: table is perfect for this:
DEMO
<div class="container">
<div class="row">
<div class="fixed">40px</div>
<div class="fluid">fluid</div>
<div class="fixed">40px</div>
</div>
</div>
.container {
display: table;
width: 100%;
}
.row {
display: table-row;
}
.row div {
display: table-cell;
}
.row div.fixed {
width: 40px;
}
.row div.fluid {
background: lightGreen;
}
If you want to align div elements in a line, use display: inline:block
<div class="row">
<div class="fixed_left_40px"></div>
<div class="fluid_center_which_changes_with_window_size"></div>
<div class="fixed_right_40px"></div>
</div>
CSS:
.row div{
display: inline-block;
height: 40px;
}
.fixed_left_40px{
width: 40px;
background: blue;
}
.fixed_right_40px{
background: red;
width: 40px;
}
Working Fiddle
.row div {
display: inline-block;
}
.row .fixed_left_40px {
float: left;
width: 40px;
}
.row .fixed_right_40px {
float: right;
width: 40px;
}
see http://jsfiddle.net/Bsrur/