vertically align div inside another div with a dynamic height - html

I have a html page with a page title.
The height of the page title is determined by the height of the users photograph. The users photograph can be any size.
The issue that I cannot solve myself is that I am trying to vertical-align the text title to the middle of the title holder, but I am not able to figure this out if the photo has a dynamic height (the max-height is 149px). Assigning a specific height to the div does allow the middle vertical alignment, but as the users photograph can be any height, assigning a set height can make the appearance seem odd.
Here is what I am trying to achieve:
Does someone know how to vertical align middle the users name with a dynamic height? I have read several similar SO posts, tried several things but I cannot get this to work.
Here is my html code:
<div class="resumeStyleNameTitleWrapper">
<div class="resumeStyleNameTitle">
<div class="resumeStyleNameTitleInner">
<div class="resumeStyleNameTitleFontChange">Users Name</div>
</div>
</div>
<div class="resumeStyleNameTitlePhotograph">
<div class="resumeStyleNameTitlePhotographInner">
{# image has max-height: 149px & max-width: 149px; assigned in the css file #}
<img id="id_name_details_photograph" class="name_details_photograph_preview_dimensions" src="{{ name_details_photograph_url }}" />
</div>
</div>
</div>
Here is my CSS:
.resumeStyleNameTitleWrapper {
display: table-cell;
width: 100%;
}
.resumeStyleNameTitle {
direction: ltr;
display: table-cell;
float: left;
text-align: left;
width: calc(100% - 159px); /*less the width of the photograph plus 10px */
background-color: lime;
}
.resumeStyleNameTitleInner {
direction: ltr;
display: table-cell;
max-height: 149px;
text-align: left;
vertical-align: middle;
width: 100%;
background-color: orange;
}
.resumeStyleNameTitleFontChange {
direction: ltr;
font-size: 32px;
font-weight: bold;
text-align: left;
text-transform: uppercase;
width: 100%;
}
.resumeStyleNameTitlePhotograph {
direction: ltr;
display: table-cell;
float: right;
max-height: 149px;
max-width: 149px;
text-align: right;
vertical-align: top;
}
.resumeStyleNameTitlePhotographInner {
display: inline-block;
vertical-align: middle;
}
.name_details_photograph_preview_dimensions {
max-height: 149px;
max-width: 149px;
}

Example: http://jsfiddle.net/kevalbhatt18/78Lzadtt/2/
I solved your Problem by putting resumeStyleNameTitlePhotograph clss in to resumeStyleNameTitleInner
<div class="resumeStyleNameTitleWrapper">
<div class="resumeStyleNameTitle">
<div class="resumeStyleNameTitleInner">
<div class="resumeStyleNameTitleFontChange">Users Name</div>
</div>
<div class="resumeStyleNameTitlePhotograph">
<div class="resumeStyleNameTitlePhotographInner">
<img id="id_name_details_photograph" class="name_details_photograph_preview_dimensions" src="">
</div>
</div>
</div>
</div>
So now if your image size change then your text div will also change its height depending upon image div size

Simplify your markup
One div containing a text element and an img:
The parent div is given display: table and text-align: right to align the image to the right
The names element (h2 in my example) is given display: table-cell and vertical-align: middle to keep it vertically centered. text-align: left brings the name to the left
The image is given vertical-align to remove the default baseline gap
Full Example
Compatibility: display: table is recognised in IE8+ and all modern browsers.
* {
margin: 0;
}
.name {
background: #F00;
display: table;
width: 100%;
text-align: right;
}
.name > img {
max-height: 149px;
max-width: 149px;
vertical-align: middle;
}
.name > h2 {
text-align: left;
display: table-cell;
vertical-align: middle;
padding: 0 10px;
}
<div class="name">
<h2>John Smith</h2>
<img src="http://www.placehold.it/200X100" />
</div>

Try this
.container {
display: table;
width: 80%;
background: green;
padding: 5px;
}
.first-child {
display: table-cell;
text-align: center;
vertical-align: middle;
background: yellow;
}
.second-child {
display: table-cell;
text-align: center;
vertical-align: middle;
}
.text {
color: red;
font-size: 20px;
}
<div class="container">
<div class="first-child">
<div class="text">
Username
</div>
</div>
<div class="second-child">
<img src="http://placehold.it/350x150" />
</div>
</div>
<br/>
<div class="container">
<div class="first-child">
<div class="text">
Username
</div>
</div>
<div class="second-child">
<img src="http://placehold.it/350x200" />
</div>
</div>
<br/>
<div class="container">
<div class="first-child">
<div class="text">
Username
</div>
</div>
<div class="second-child">
<img src="http://placehold.it/350x100" />
</div>
</div>

I will simply use table to accomplish this task. Neat and simple. No messing around with CSS, and I don't have to worry about browser compatibility.
Just ensure to set valign (vertical alignment) to middle on the column for username.
<table width="100%" border="0">
<tr>
<td width="52%" valign="middle">Username</td>
<td width="29%"> </td>
<td width="19%"><!-- IMAGE HERE --></div></td>
</tr>
</table>
HERE IS A FIDDLE

Related

Display property issue

I want the two div containers inside the .firstcontainer div to appear side by side so I gave them the display: inline-block, but that doesn't seem to work.
.firstcontainer {
display: block;
}
.imagecontainer {
display: inline-block;
margin-left: 70px;
}
.SeasonCounterContainer {
margin-left: 20px;
display: inline-block;
}
<div class="centralcontainer">
<div class="firstcontainer">
<div class="imagecontainer">
<img src="http://subs-40ea.kxcdn.com/posters/better-call-saul.jpg" alt="">
</div>
<div class="SeasonCounterContainer">
<p>46 min | Crime, Drama</p>
</div>
</div>
</div>
Note: I've already tried different solutions but none worked for me.
You have to use a width setting on those inline-blocks to get them side by side, since the default width of inline-blocks is the width of their contents, up to 100% of the parent. Also the image width needs to be restricted to not become more than the width of its container.
.firstcontainer {
display: block;
}
.imagecontainer {
display: inline-block;
margin-left: 70px;
width: 200px;
}
.imagecontainer img {
width: 100%;
height: auto;
}
.SeasonCounterContainer {
margin-left: 20px;
display: inline-block;
width: 150px;
}
<div class="centralcontainer">
<div class="firstcontainer">
<div class="imagecontainer">
<img src="http://placehold.it/600x400/fa0">
</div>
<div class="SeasonCounterContainer">
<p>46 min | Crime, Drama</p>
</div>
</div>
</div>

Div is not centering inside Div

.wrapper {
width: 100%;
height: 150px;
}
.iconBar div {
float: left;
font-size: 18px;
}
div.iconBar {
display: table;
margin: 0 auto;
}
.iconBar img {
height: 35px;
width: 35px;
vertical-align: middle;
}
<div class="wrapper">
<div class="iconBar">
<div>
<img src="http://eventtoursolutions.com/wp-content/uploads/2017/02/convenient.png"> Convinient, All-Inclusive Weekly Price
</div>
<div>
<img src="http://eventtoursolutions.com/wp-content/uploads/2017/02/Insurance.png"> Insurance
</div>
<div>
<img src="http://eventtoursolutions.com/wp-content/uploads/2017/02/mileage.png"> Milage Allowance & Expenses
</div>
<div>
<img src="http://eventtoursolutions.com/wp-content/uploads/2017/02/tractorTrailer.png"> Tractor & Expandable Trailer
</div>
<div>
<img src="http://eventtoursolutions.com/wp-content/uploads/2017/02/professionalDriver.png"> Professional Driver
</div>
</div>
</div>
Why is my div not centering? I am having trouble making the div center. I tried a bunch of solutions from other people and i got it to work in one of the tests, but this one is not working?
Edit: iconBar Div, i want the text and icons to be centered. It needs to look like the bottom picture
Going by your image remove float:left and add display: inline-block; to .iconBar div {...} and add text-align: center; to div.iconBar {...}
.wrapper {
width: 100%;
height: 150px;
}
.iconBar div {
display: inline-block;
font-size: 18px;
}
div.iconBar {
display: table;
text-align: center;
margin: 0 auto;
}
.iconBar img {
height: 35px;
width: 35px;
vertical-align: middle;
}
<div class="wrapper">
<div class="iconBar">
<div>
<img src="http://eventtoursolutions.com/wp-content/uploads/2017/02/convenient.png"> Convinient, All-Inclusive Weekly Price
</div>
<div>
<img src="http://eventtoursolutions.com/wp-content/uploads/2017/02/Insurance.png"> Insurance
</div>
<div>
<img src="http://eventtoursolutions.com/wp-content/uploads/2017/02/mileage.png"> Milage Allowance & Expenses
</div>
<div>
<img src="http://eventtoursolutions.com/wp-content/uploads/2017/02/tractorTrailer.png"> Tractor & Expandable Trailer
</div>
<div>
<img src="http://eventtoursolutions.com/wp-content/uploads/2017/02/professionalDriver.png"> Professional Driver
</div>
</div>
</div>

Two images one left one right and center vertically

I have this code, which aligns two images to left and right side of the page with space between:
.center {
width: 100%;
display: table;
}
.leftk {
display: table-cell;
}
.rightk {
display: table-cell;
text-align: right;
vertical-align: middle;
}
<div class="center">
<div class="leftk">
<img src="http://loremflickr.com/300/200" />
</div>
<div class="rightk">
<img src="http://loremflickr.com/400/100" />
</div>
</div>
<span>Text</span>
I tried to do the same with inline-block, but I am unable to align vertically right image to center. Can you please show me a quick example to how to get same result with inline-block?
Edit: If page size is smaller than image width, I want to display them under each other.
Here is how you do it with inline block:
jsFiddle
.center {
font-size: 0; /*remove white space*/
text-align: justify;
}
.center:after {
content: "";
display: inline-block;
width: 100%;
}
.leftk,
.rightk {
font-size: 16px;
display: inline-block;
vertical-align: middle;
}
<div class="center">
<div class="leftk">
<img src="//dummyimage.com/200x200" />
</div>
<div class="rightk">
<img src="//dummyimage.com/100x100" />
</div>
</div>
you can use css3 flexbox instead,
use display:flex instead of display:table
add justify-content:space-between;
it's works fine.
.center {
width: 100%;
display: flex;
justify-content:space-between;
align-items:center;
}
.leftk>img {
width:100px;
height:100px;
}
.rightk>img {
width:50px;
height:50px;
}
<div class="center">
<div class="leftk" style="display: table-cell;">
<img src="https://i.stack.imgur.com/wwy2w.jpg"/>
</div>
<div class="rightk">
<img src="https://i.stack.imgur.com/wwy2w.jpg"/>
</div>
</div>
<span>Text</span>

Alignment changes on entering text within a div with display inline-block

Stuck on a problem where when I enter text in a div with display property set to inline-block. Here's the HTML:
<div class="row" id="section">
<div class="sectionheading">
</div>
<div class="sectionheading">
</div>
<div class="sectionheading">
</div>
<div class="sectionheading" id="sectionheading">
<span>Text</span>
</div>
</div>
CSS:
.sectionheading {
display: inline-block;
width: 6px;
background-color: #df5e5e;
height:35px;
}
#sectionheading {
width: 150px !important;
}
#section {
margin-left:10px;
margin-top: 40px;
}
The problem is with the div having id 'sectionheading'. When I have text in it like in the HTML given it shifts downwards for some reason, however when the div is empty it is aligned properly with the other divs. What's the problem here?
Use vertical-align: top; will solve your issue.
If you are using display: inline-block; you need to set vertical:align property of the div. Because default it's vertical-align value is baseline.
.sectionheading {
background-color: #df5e5e;
display: inline-block;
height: 35px;
vertical-align: top;
width: 6px;
}
Check Fiddle Here.
Try like this: Demo
.sectionheading {
vertical-align:top;
}
Another option for solve this issue..
.sectionheading {
display: inline-block;
width: 6px;
background-color: #df5e5e;
height:35px;
float: left;
margin-right:10px;
}
#sectionheading {
width: 150px !important;
}
#section {
margin-left:10px;
margin-top: 40px;
}
<div class="row" id="section">
<div class="sectionheading">
</div>
<div class="sectionheading">
</div>
<div class="sectionheading">
</div>
<div class="sectionheading" id="sectionheading">
<span>Text</span>
</div>
</div>
The reason why such happens when there is text is because there is a height given by default and with no proper padding height added. You can either add vertical-align: top; or add line-height: 24px; and remove height.
Fiddle : Example

Vertically aligned image and text div

UPDATE: The answers have got me close, but they still don't align vertically as the text div is larger, how can I make them both the same height and therefore align?
I would like to have two DIVs next to each other, one containing an image and one containing text, both sitting in a container DIV.
The image should be 15% of the width of the container div, with the text using the remaining 85%
The image and text should be aligned vertically within their respective DIVs, so it looks like they are aligned with each other.
I've tried to work this out but can't seem to do it! Can anyone help?
#picture {
float: left;
width: 15%;
line-height: auto;
}
#text {
width: auto;
padding-left: 16%;
line-height: auto;
vertical-align: middle;
margin-top: auto;
margin-bottom: auto;
}
#text p {
display: inline-block;
vertical-align: middle;
line-height: normal;
}
and
<div id="quotes">
<div id="picture">
<img style="width: 100%; vertical-align: middle" src="tom.jpg" >
</div>
<div id="text">
<p>"Christiaan was one of the stand out candidates throughout, therefore there was no hesitation in offering him a place on this highly sort after scheme..."</p>
</div>
</div>
Here's a fiddle with your code in it: http://jsfiddle.net/hQ6Vw/1/
The only changes I made was to assign matching top/bottom margins to the img and p tags. I think that will give you the effect you're looking for.
If you use float and verticl-align, those two won'nt work together.
Float extract itself from regular flow and go slide on one side or the other on top of next line right after any content within the regular flow.
Vertical-align works:
in betweem inline-boxes (inline-block-level element or displayed so with display:inline-block;)
inside td or it's CSS default display : display:table-cell;
here jsfiddle #TXChetG updated
Using display:inline-block; http://jsfiddle.net/GCyrillus/hQ6Vw/2/
Using display:table/* table-cell*/;
http://jsfiddle.net/GCyrillus/hQ6Vw/3/
This should get you close:
<div>
<div style="background: grey; width: 15%; float:left"></div>
<div style="background: blue; width: 85%; float:left"></div>
</div>
Replace the grey background div with your image and the blue with your text.
Check this out
HTML:
<section>
<div id="one"></div>
<div id="two"></div>
</section>
CSS:
section {
width: 80%;
height: 200px;
background: aqua;
margin: auto;
padding: 10px;
}
div#one {
width: 15%;
height: 200px;
background: red;
float: left;
}
div#two {
margin-left: 15%;
height: 200px;
background: black;
}
Is this what you mean?
html
<div class="container">
<div class="images">
<img src="http://jsfiddle.net/img/logo.png" style="background-color:black">
</div>
<div class="text">
Example
</div>
</div>
<div class="container">
<div class="images">
<img src="http://jsfiddle.net/img/logo.png" style="background-color:black">
</div>
<div class="text">
Example
</div>
</div>
css
.container {
clear: both;
}
.images {
width: 15%;
float: left;
vertical-align: text-top;
}
.text {
width: 85%;
float: right;
vertical-align:text-top;
}
Why not just set the #text p display to display: inline or display:block; or use margins to align them?
<div id="quotes">
<div id="picture">
<img src="tom.jpg" />
</div>
<div id="text">
<p>"Christiaan was one of the stand out candidates throughout, therefore there was no hesitation in offering him a place on this highly sort after scheme..."</p>
</div>
</div>
Display the container div as table and the text and image divs as table-cell to make them the same heights. You can then centre the image vertically through vertical-align:middle.
#quotes {
display:table;
}
#picture {
width: 15%;
display:table-cell;
vertical-align: middle;
}
#text {
display:table-cell;
width:85%;
padding-left: 16%;
}
#picture img {
width: 100%;
}
http://jsfiddle.net/X3WsV/1/