CSS: Vertical-align - html

How to vertical-align without using display table/table-cell or position absolute ?
#parent {
border: 1px solid red;
height: 100vh;
}
.child {
border: 1px solid blue;
}
<div id="parent">
<div class="child">
<p>I want to be vertical aligned</p>
</div>
</div>

Here is an another option using "Flex" property.
<div id="parent">
<div class="child">
<p>I want to be vertical aligned</p>
</div>
</div>
#parent {
border: 1px solid red;
display: flex;
align-items: center;
height: 100vh;
}
.child {
border: 1px solid blue;
flex-grow: 1;
}
Codepen demo link

You can use position relative, with top of 50% and a translation of -50%.
#parent {
border: 1px solid red;
height: 100vh;
}
.child {
position: relative;
top: 50%;
transform: translate(0,-50%);
border: 1px solid blue;
}
<div id="parent">
<div class="child">
<p>I want to be vertical aligned</p>
</div>
</div>

Another method could be to use a floater div
#parent {
border: 1px solid red;
height: 100vh;
}
.floater {
float:left;
height:50%;
width:100%;
margin-bottom: -25px;
}
.child {
border: 1px solid blue;
clear: both;
height:50px;
}
<div id="parent">
<div class="floater"></div>
<div class="child">
<p>I want to be vertical aligned</p>
</div>
</div>

You can try using display:flex.
CSS
#parent {
border: 1px solid red;
height: 100vh;
display: flex;
align-items: center; /* vertical */
justify-content: center; /* horizontal */
}
.child {
border: 1px solid blue;
}
<div id="parent">
<div class="child">
<p>I want to be vertical aligned</p>
</div>
</div>

You can use display:flex;:
#parent {
border: 1px solid red;
height: 100vh;
display:flex;
align-items:center;
justify-content:center;
}

You can use like that I think
position: fixed; top: 50%;

if you do not mind browser compatibility I would go with flex - see #rblarsen, #Satheesh Kumars answers.
but if you need to expand browser support, here is a more complex but rather solid solution : tested IE9+ FF Chrome and other major browsers...
check out this fiddle : https://jsfiddle.net/kLLz0nm2/
HTML
<div class="wrapper">
<div class="content">Middle aligned</div>
<div class="middle"></div>
</div>
CSS
.wrapper{
width : 100%;
height : 100%;
text-align: center;
}
.content{
display: inline-block;
vertical-align: middle;
}
.middle{
height: 100%;
display: inline-block;
vertical-align: middle;
}
P.S - the above translate solution while fairly simple can sometimes cause poor rendering issues, check out :

Related

Float right changing position of next element

I have a simple HTML, one parent div, and two children. When I am styling one child with float set to right, the next child goes up and the margin-top doesn't apply to it, which I don't want.
Here is the sample code.
.inner1 {
width: 50%;
height: 100px;
border: 5px solid red;
float: right;
}
<div class="outer">
<div class="inner1">
</div>
<div class="inner2">
Text that goes up after float.
</div>
</div>
Can someone please suggest how to handle this situation?
Here is the JSFiddle
I want the output to be something like
Don't use float. float is deprecated. Please take note of this solution using flex.
.outer {
width: 100%;
height: 100%;
display: flex;
flex-flow: row-reverse;
align-items: center;
}
.inner1 {
width: 50%;
height: 100px;
border: 5px solid red;
}
.inner2 {
width: 50%;
text-align: center;
}
<div class="outer">
<div class="inner1">
</div>
<div class="inner2">
Text that goes up after float.
</div>
</div>
Snippet #2
.outer {
width: 100%;
height: 100%;
display: flex;
flex-flow: column;
align-items: flex-end;
}
.inner1 {
width: 50%;
height: 100px;
border: 5px solid red;
}
.inner2 {
width: 50%;
}
<div class="outer">
<div class="inner1">
</div>
<div class="inner2">
Text that goes up after float.
</div>
</div>
Try this code:
.outer{
display:flex;
flex-direction:column;
align-items:flex-end;
}
.inner1 {
width: 50%;
height: 100px;
border: 5px solid red;
}
.inner2{
width: 50%;
}
<div class="outer">
<div class="inner1">
</div>
<div class="inner2">
Text that goes up after float.
</div>
</div>
You can use clear: both; in the element what has float: right; property.

How to place two square divs side by side covering the width of their parent

I want .board element to have a square aspect ratio. I want to show two of them side by side, together covering the width of their parent.
I don't want to use width: 50%, because I want to position .wrap element with display: flex.
.board {
position: relative;
background: red;
width: 100%;
height: 0;
padding-bottom: 100%;
border: 1px solid black;
}
.wrap {
display: flex;
}
<div class="wrap">
<div class="board"></div>
<div class="board"></div>
</div>
When I do it like this, I get two divs with squashed width.
Use aspect-ratio and flex:1
.board {
position: relative;
background: red;
flex:1;
aspect-ratio:1/1;
border: 1px solid black;
}
.wrap {
display: flex;
}
<div class="wrap">
<div class="board"></div>
<div class="board"></div>
</div>
Alternatively flex:1 and padding-bottom:50%;
.board {
position: relative;
background: red;
flex: 1;
padding-bottom: 50%;
border: 1px solid black;
}
.wrap {
display: flex;
}
<div class="wrap">
<div class="board"></div>
<div class="board"></div>
</div>

Using flex align-items center to vertically center text inside div

I can't figure out what I have wrong with my css. I would think this would vertically center the text inside this div:
.content-bar {
height: 60px;
background: #f7f7f7;
overflow: hidden;
border-bottom: 1px solid #ececec;
}
.content-bar-content {
display: flex;
align-items: center;
}
<div class="content-bar">
<div class="content-bar-content">
Testing 123
</div>
</div>
However, the text is lining up to the top of the div. What am I missing?
See codepen: https://codepen.io/anon/pen/xgOwwp
You can specify the content-bar as a flexbox like below:
.content-bar {
height: 60px;
background: #f7f7f7;
overflow: hidden;
border-bottom: 1px solid #ececec;
display: flex;
align-items: center;
}
.content-bar-content {}
<div class="content-bar">
<div class="content-bar-content">
Testing 123
</div>
</div>
Or you can inherit the height of the content-bar - just add height: inherit on content-bar-content - see demo below:
.content-bar {
height: 60px;
background: #f7f7f7;
overflow: hidden;
border-bottom: 1px solid #ececec;
}
.content-bar-content {
display: flex;
align-items: center;
height: inherit;
}
<div class="content-bar">
<div class="content-bar-content">
Testing 123
</div>
</div>
check with the demo , i have swap the height
.content-bar {
background: #f7f7f7;
overflow: hidden;
border-bottom: 1px solid #ececec;
}
.content-bar-content {
display:flex;
align-items:center;
height: 60px;
}
<div class="content-bar">
<div class="content-bar-content">
Testing 123
</div>
</div>
All of your CSS must be declared in your parent container. Here is an example to center both horizontally and vertically:
.content-bar {
height: 80px;
background: #f7f7f7;
overflow: hidden;
border-bottom: 1px solid #ececec;
display:flex;
align-items:center;
justify-content: center;
}
<div class="content-bar">
<div class="content-bar-content">
Testing 123
</div>
</div>
Hope this helps!
Add height to both classes and addvertical-align: middle to content
.content-bar {
height: 60px;
background: #f7f7f7;
overflow: hidden;
border-bottom: 1px solid #ececec;
height:100%
}
.content-bar-content {
height:100%;
display:flex;
align-items:center;
vertical-align: middle;
}
<div class="content-bar">
<div class="content-bar-content">
Testing 123
</div>
</div>
Try this simple code to align text in center of the div remove fixed height and give padding
.content-bar {
padding:30px;
height:auto;
background: #f7f7f7;
overflow: hidden;
border-bottom: 1px solid #ececec;
}
.content-bar-content {
display:flex;
text-align:center;
}
<div class="content-bar">
<div class="content-bar-content">
Testing 123<br/> Testing 123<br/> Testing 123<br/> Testing 123<br/>
</div>
</div>

css max-width is not working

I have parent element width:100px and position:relative. And I have its inner element with position:absolute. I need this inner element to stretch out up to 200px, but it doesn't work. Maximum value it takes is 100% of parent.
UPD
I don't want width to be fixed. I just needed it to be up to 200px if there is content and auto if not much content there.
p.s. I need those position properties
Here's html:
<div class='parent'>
<div>element</div>
<div class="inner">
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
</div>
</div>
styling:
.parent {
width: 100px;
border: 1px solid yellow;
position: relative;
}
.inner {
max-width: 200px;
border: 1px solid red;
position: absolute;
}
.item {
float: left;
border: 1px dotted grey;
}
And jsfiddle to the example
Anyone please help
It is working fine. Your maximum width allowed is 200 pixels. See it here:
http://jsfiddle.net/jy6g2q7b/1/
Maybe you are searching only for width (is fixed)
.inner { width: 200px; }
See it working: http://jsfiddle.net/jy6g2q7b/4/
add "width: 200%" to the .inner and the max-width will still work.
.inner {
width: 200%;
max-width: 200px;
border: 1px solid red;
position: absolute;
}
http://jsfiddle.net/jy6g2q7b/2/
if you are using AngularJS, you can use ng-style = {'width':'200%'}. And if not, you can change it to inherit from parent `
.inner {
max-width: inherit;
border: 1px solid red;
position: absolute;
}
Cheers.
All that problem (odd behavior) is occurring due to this line -
.item {
float: left; /* THIS ONE */
border: 1px dotted grey;
}
you need to find some other alternative for those text blocks to align. You can use display: table-cell instead of float, but that makes the blocks go out of #content
I'll update my answer if I find any alternative.
Give position:static for the parent and then try
.parent {
border: 1px solid yellow;
position: static;
width: 100px;
}
.inner {
border: 1px solid red;
max-width: 200px;
position: absolute;
}
This is expected behaviour. The max-width value of .inner is not reached because it is limited by the width of .parent and the floated .items will only be on the same row until they hit the right edge of the containing element.
One way around this issue is to use a pseudo element to achieve a similar bordered result without the restrictive width:
Remove width from .parent, this will cause it to take up 100% of the width as it is a block level element. Remove border: 1px solid yellow; as it will no longer have the desired result
Create a new pseudo element .parent:before. Set border: 1px solid yellow; and width: 100px; to show the yellow border on this instead
Set .parent:before to be position: absolute; and have height: 100%; to position it relatively .parent and get it to fill the correct area
Now that .inner is not restricted by the width of .parent it should abide by the max-width: 200px; rule.
.parent {
position: relative;
}
.parent:before {
border: 1px solid yellow;
content: "";
display: block;
height: 100%;
position: absolute;
width: 100px;
}
.inner {
border: 1px solid red;
max-width: 200px;
position: absolute;
}
.item {
border: 1px dotted grey;
float: left;
}
<div class='parent'>
<div>element</div>
<div class="inner">
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
<div class="item">text</div>
</div>
</div>

How to make full 100% height of floated element?

I have the following html markup:
.container {
border: 1px solid green;
}
.right {
border: 1px solid #000;
float: right;
width: 40px;
}
.left {
border: 1px solid red;
overflow: hidden;
}
<div class="container">
<div class="right">Right</div>
<div class="left-container">
<div class="left">
Left fluid
<br/>
multiple rows
</div>
</div>
</div>
As you can see right block looks ugly. How could I make right element fluid height 100%?
Add the rule height:100% the right div, and remove float:right. I changed it to position:absolute, so that you didn't need the container's height.
.container {
border: 1px solid green;
position: relative;
width: 100%;
}
.right {
border: 1px solid #000;
width: 40px;
height: 100%;
position: absolute;
right: 0;
}
.left {
display: block;
border: 1px solid red;
overflow: hidden;
margin-right:40px;
}
<br><br><div class="container">
<div class="right">Right</div>
<div class="left-container">
<div class="left">
Left fluid
multiple rows a really long sentence a really long sentence a really long sentence a really long sentence a really long sentence a really long sentence.
</div>
</div>
</div>
If your application will run in a modern browser, then using flexbox is a good way to go: http://jsfiddle.net/2hn9zgog/.
HTML:
<div class="container">
<div class="right">Right</div>
<div class="left">
Left fluid
<br/>multiple rows
</div>
</div>
CSS:
.container {
display: flex;
width: 100%;
outline: 1px dotted gray;
}
.right {
order: 2;
flex: 0 0 auto;
border: 1px solid green;
}
.left {
flex: 1 0 auto;
border: 1px solid red;
}
add clear: both; after floated element.
<div class="right"></div>
<div style="clear: both"></div>
Add
html, body{
height: 100%;
min-height: 100%;
}
.your-container{
height: 100%;
}