Is there a similar technique like float and clearfix but for vertical alignment.
I want to separate different elements (either inline or floating left) such that they are always aligned either to the top or bottom.
In this jsFiddle I want the red and the green to be horizontally aligned. I cannot change the CSS or the existing divs. I can only wrap .s1 and .s2
Ok.. If you know the class name you can do like this:
SEE DEMO 1
Here the css:
.s1 {
display: inline-block;
font-size: 10px;
height: 20px;
}
.s2 {
display: inline-block;
background: green;
font-size: 20px;
height: 20px;
}
.s3 {
background: red;
height: 20px;
}
/* PUT THIS IN AN EXTRA FILE OR UNDER THE ABOVE STYLE */
.s1 {
vertical-align: bottom;
}
.s2 {
vertical-align: middle;
}
Or if you can wrap the file you can float the div's like this:
SEE DEMO 2
HTML
<div class="wrap_1"> <!-- Wrap 1 -->
<div class="s1">
<div class="s3">asdf</div>
</div>
</div>
<div class="wrap_2"> <!-- Wrap 2 -->
<div class="s2">
<div>qwer</div>
</div>
</div>
CSS
.s1 {
display: inline-block;
font-size: 10px;
height: 20px;
}
.s2 {
display: inline-block;
background: green;
font-size: 20px;
height: 20px;
}
.s3 {
background: red;
height: 20px;
}
/* FLOAT THE DIV */
.wrap_1 div, .wrap_2 div {
float: left;
}
Let me know if solved your issue!
Related
I have equal height columns with centered content:
http://codepen.io/anon/pen/BzKwgE
<div class="cont">
<div class="item item-first">
<p>First</p>
</div>
<div class="item item-second">
<p>Second Second Second Second Second Second Second Second Second Second Second Second Second Second Second Second Second </p>
</div>
<div class="item item-third">
<p>Third</p>
</div>
</div>
* {
box-sizing: border-box;
}
.cont {
width: 70%;
display: table;
}
.item {
display: table-cell;
vertical-align: middle;
text-align: center;
padding: 20px;
width: 33%;
}
.item-first {
background: blue;
}
.item-second {
background: green;
}
.item-third {
background: blue;
}
This is working great. However I also need my columns to have a 16x9 aspect ratio. In rare cases there will be a lot of content, in which case its OK to change the aespect ratio.
Ive got this working below however it stops the content being vertically centered:
http://codepen.io/anon/pen/pbyWMj
* {
box-siing: border-box;
}
.cont {
width: 70%;
display: table;
}
.item {
display: table-cell;
vertical-align: middle;
text-align: center;
padding: 20px;
width: 33%;
}
.item:before {
padding-bottom: 56.25%; // 16:9 ratio
display: block;
content: '';
float: left;
width: 1px;
}
.item-first {
background: blue;
}
.item-second {
background: green;
}
.item-third {
background: blue;
}
I can see that this is happening due to the padding hack. Is there a way to have equal height columns, vertically centered content, and the 16x9 aspect ratio?
Im supporting IE9. Ideally it would look the same, but a usable fallback is also acceptable.
You can use inline-block and vertical-align on the elements inside each item. Try this:
.item {
display:table-cell;
vertical-align:middle;
text-align: center;
width: 33%;
}
.item:before {
padding-bottom: 56.25%;
display: inline-block;
content: '';
width: 0;
vertical-align:middle;
margin-right:-4px;
}
.item p {
display:inline-block;
vertical-align:middle;
padding:20px;
}
Codepen Update
So, I have three divs:
<div class="takeremaining">
<div class="centeredcontent">
This is my centered content
</div>
</div>
<div class="dynamicallyallocated">
This is my dynamic content
</div>
I'd like the rightmost div dynamicallyallocated to be dynamically sized based on the content using display: inline-block; and the other div takeremaining to take the remaining space in the parent div. I've tried this with my css:
.takeremaining {
display: inline-block;
width: 100%;
float: left;
background-color: #0000ff;
}
.centeredcontent {
display: table;
margin: 0 auto;
background-color: #00ffff;
}
.dynamicallyallocated {
display: inline-block;
float: right;
background-color: #00ff00
}
but, as you can see by this JSFiddle demo, the div dynamicallyallocated is bumped beneath takeremaining. I believe this is because of width: 100%; in takeremaining, but I'm not sure how to give it a dynamic width based on the conditional width of dynamicallyallocated. What would you suggest?
Here is a solution for you.
.container {
display: table;
width: 100%;
}
.takeremaining {
display: table-cell;
width: 100%;
text-align: center;
background-color: #0000ff;
}
.centeredcontent {
display: inline-block;
background-color: #00ffff;
}
.dynamicallyallocated {
display: table-cell;
width: 0;
background-color: #00ff00;
white-space: nowrap
}
<div class="container">
<div class="takeremaining">
<div class="centeredcontent">
This is my centered content
</div>
</div>
<div class="dynamicallyallocated">
This is my dynamic content
</div>
</div>
I want three divisions side bu side with the middle explanding and the other two positioned at the ends. So here is what I tried. The padding rule disturbs the positioning but its necessary. I want approach which works in all major browsers(So ruling out flexbox)
.Button {
width: 80%; /*Useless Rule*/
}
.Button > .left {
float: left;
background-color: blue;
padding: 5px;
}
.Button > .right {
float: right;
background-color: red;
padding: 5px;
}
.Button> .middle {
width: 100%;
background-color: green;
padding: 5px;
}
<html>
<body>
<div class="Button">
<div class="left"><</div>
<div class="right">></div>
<div class="middle">Middle</div>
</div>
</body>
</html>
I like to use the display: table on the parent, and the display: table-cell on the children. Then give the first and third child a width of 1px. It will then be only as width as its content.
.button {
display: table;
width: 100%;
}
.button>div {
display: table-cell;
text-align: center;
background: lightblue;
}
.button>div:nth-child(1),
.button>div:nth-child(3) {
width: 1px;
background: lightgreen;
}
<div class="button">
<div><</div>
<div>Middle</div>
<div>></div>
</div>
I am trying to place two img tags onto of each other but have them both centered.
Here is where im trying to do it: http://nathanturnbull.me/scrolldiv/home.html
The Html:
<div id="divs" class="div1">
<div class="textcont">
<img class="imglogo" src="gpk.gif" alt="GPK NET">
<div class="textcont" >
<img class="imgbutton" src="gpk.gif">
</div>
</div>
</div>
The css:
#divs, #div4 {
height: 100%;
width: 100%;
overflow: hidden;
background-size: cover;
font-family: 'Cousine';
font-size: 80px;
color: #ffffff;
text-align: center;
}
.div1 {
display: table;
}
.textcont {
display: table-cell;
vertical-align: middle;
background-color: transparent;
}
.imgbutton {
height: 42px;
width: 84px;
}
.container {
text-align: center;
}
.container IMG {
display: inline; /* browser default */
}
or
IMG {
display: block;
margin: 0 auto;
}
Your images should be horizontally centred inside their container.
Remove the display:table-cell; statement from your .textcont CSS class.
See example.
i have a div that has a specific height and the content inside adjusts according to height VERTICALLY now what i did is i added the property display-table; to the parent div and display: table-cell;vertical-align: middle; to the child div now what happens is my div is vertically aligned and looking good but the h1 inside the child i not exactly as centered aligned as the button i figured adding some padding top or margin top might solve the issue but it is not accepting either of these here's my code
html
<div class="all-smiles" id="parent">
<div id="child">
<div class="container" align="center">
<div class="row">
<h1>All Smiles Welcome</h1>
<button>BOOK AN APPOINTMENT</button>
</div>
</div>
</div>
</div>
css
#parent {
display: table;
width: 100%;
height: 250px;
}
.all-smiles {
background-color: #b7181c;
}
.all-smiles h1 {
color: white;
display: inline;
margin-right: 3%;
}
.all-smiles button {
padding: 12px;
background: 0;
color: white;
border: 1px solid #fff;
text-decoration: none;
font-family: young;
}
#child {
display: table-cell;
vertical-align: middle;
}
FIDDLE
You need to specify these two:
Vertical Alignment as middle
Line height and 1
Code:
.all-smiles h1 {
color: white;
display: inline;
margin-right: 3%;
vertical-align: middle;
line-height: 1;
}
Preview
Fiddle: https://jsfiddle.net/wd3sr2xv/