inline-box with image vertical-align:middle with parent box - html

Please run the demo:
* {
margin:0;
padding:0;
}
.body {
font-family:Microsoft Yahei;
font-size:16px;
background-color:lightblue;
height: 200px;
width:200px;
line-height:2;
}
.body span {
background-color:pink;
}
.body img {
width:50px;
height:50px;
}
.body .img-wrapper {
background-color:orange;
}
.body .img-wrapper {
vertical-align:middle;
}
<div class="body">
<span class="wrapper">
words-g words-g words-g
<span class="img-wrapper">
<img src="https://avatars3.githubusercontent.com/u/23273077" alt="">
s
</span>
words-g words-g words-g
</span>
</div>
The point is that I set
.body .img-wrapper {
vertical-align:middle;
}
I was expecting the red lines in below picture is in the same line:
According to the specification,
Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.
So,I think:
the vertical midpoint of the box is the first red line in the above picture and
the baseline of the parent box plus half the x-height of the parent = the second red line
But it turns out I am wrong and I guess the key is x-height of the parent.So,I found this:
So, I thought x-height of the parent in this case is not the second red line because of the existence of the image.
So,my question is :
how much is the x-height of the parent in this case? Is it changed because of the existence of the image?
Or something else is wrong?
Please notice:
I just want to get the x-height value in this case,so I can understand the vertical-align better.
I am not asking for a specific solution.
Whatever thanks for your help!

First the x-height of the element is not affected by the the image and is only defined by font-size and of course the font-family used. Then in order to get the value of the x-height you need to consider the ex unit.
Here is a better illustration taken for this answer
You may clearly see the difference between each value of vertical alignment and also notice the illustration of em and ex unit. Now in order to have the exact value of x-height, you simply need to use the ex unit.
Here is an example:
* {
margin:0;
padding:0;
}
body {
font-family:Microsoft Yahei;
font-size:16px;
background-color:lightblue;
line-height:2;
}
span {
background-color:pink;
border-right:1ex solid red;
border-left:1em solid red;
}
img {
width:50px;
height:50px;
}
<span>
words-g words-g words-g
</span>
<br>
<span>
words-g words-g words-g <img src="https://avatars3.githubusercontent.com/u/23273077" alt="">
</span>
As you can see I added a right and left border using ex and em units then if I check the computed value I can get the exact value. You will aslo notice that both span have the same value which indicate that the image has no impact on it.

Related

Alignment of p tag and br tag

This is what I want.
This is what I'm getting.
This is my code. I'm having trouble with the spacing. I can't seem to align p tag with the br tag. I have also tried experimenting with inline but no luck.
<p class="one"><b>Field of</b> Select all that apply, hold CTRL when clicking to select multiple items <br>
<b>study: </b></p><!-- This is where the issue lies. -->
You should have 2 containers, Left bold text should be in the left container with some styles. There is no good way to fix your code for solving your problem, it is easier to rewrite it at all, as for me.
Use padding for having distance between texts and text-align:right; for second line to be closer to the border. Take a look:
.main_text {
padding-left:5px;
}
.left_text {
text-align: right;
}
<div style="display:flex">
<div class='left_text'>
<b>Field of <br> study: </b>
</div>
<div class='main_text'>
<label> Select all that apply, hold CTRL when clicking to select multiple items</label>
</div>
</div>
Let's make it simple,Use float: right to align "study" correctly. And that's it!
This is your code:
.one {
width: 55px;
height: 50px;
}
.span {
float: right;
}
<b>Field of</b> Select all that apply; hold CTRL when clicking to select multiple items
<p class="one">
<b><span class="span">Study:</span></b>
</p>
CodePen: https://codepen.io/marchmello/pen/abOPJzp?editors=1100
Flexbox is perfect for this kind of placement. Use align-items:center for the first positioning
div{
display:flex;
align-items:center;
font-size:19px;
}
#a1{
display:block;
margin-right:14px;
width:64px;
}
<div>
<p id='a1' ><b>Field of <br/>Study </b></p>
<p>Select all that apply, hold CTRL when clicking to select multiple items </p>
</div>
or use align-items:flex-start if you want the placement like this:
div{
display:flex;
align-items:flex-start;
font-size:19px;
}
#a1{
display:block;
margin-right:14px;
width:64px;
}
<div>
<p id='a1' ><b>Field of <br/>Study </b></p>
<p>Select all that apply, hold CTRL when clicking to select multiple items </p>
</div>

How to get picture to align with the left set of paragraphs/go to right of?

Here is a prototype of what I am trying to implement
Here is what I currently have : JsFiddle
I am trying to get the picture of the guy on the laptop to align correctly with and to the right of the paragraph components - Business Traveller, Office Supply Purchases, etc...
What I've tried is using Align attribute, changing my img src code to
<img id="laptop" align="middle" src="zoom-39988392-3.JPG" height = "90" width ="90" />
but that didn't have any effect.
I also tried Float but that messed up my margins and the organization of my left components.
Is there a way I can do this without floating?
See the fiddle
The HTML and CSS that i've used is as follows. Used float:left
HTML
<div class="container">
<div id="choices">
<p class="choice">Business Traveller</p>
<p class="choice">Office Supply Purchases</p>
<p class="choice">Stay at home parent</p>
<p class="choice">Entertainment</p>
<p class="choice">Profile 6</p>
</div>
<div class="image"></div>
</div>
CSS
html, body, .container {
height:100%;
}
#choices {
width:30%;
float:left;
}
.choice {
margin-top:0px;
margin-left:20px;
text-align:center;
width:100%;
background-image: url("http://i.imgur.com/H43sVoi.png");
padding-top:15px;
padding-bottom:15px;
}
.image {
height:100%;
width:65%;
background-color:red;
float:left;
}
You will have to work with the height and width of each divs. I just made it roughly.
You have to create two columns. 1 column for the menu and the second column for the image. If you do this, you wont have trouble floating.

Getting <DIV>'s to vertically hug text contents

I'm attempting to get a div element to hug the contents as tightly as possible. This works fine for the width by using display:inline-block, but the height always has some extra padding/ margin/ border. How do I get the parent div to wrap the text exactly? I can get it by judicially choosing line-height, but this only works for a particular font and size.
How do I get the red box below to wrap the text as close as possible... for any input font or size?
Example:
jsfiddle, with css code
.outer {
display:block;
background:red;
}
.hug {
font-size:200%;
margin:0em;
border:0em;
padding:0em;
}
and html:
<div class="outer">
<div class="hug">
<h1>PERFECT FIT</h1>
</div>
</div>
<div class="outer">
<div class="hug">
<h2>PERFECT FIT</h2>
</div>
</div>
CSS, CCS3 solutions preferred over javascript, unless it isn't possible.
I ended up getting some decent results with this. If you alter the font size the line-height can stay as needed and hugs pretty darn close... Does it need to be to the pixel exactly?
.thisone{
display:inline-block;
background:#09f;
min-width:1px;
font-family:arial;
width:auto;
font-size:70px;
line-height:.70em;
}
DEMO
Here is something I've tried : http://jsfiddle.net/wared/CpZru/. My first attempt (pink) worked only with H2, so, I gave a closer look to this tag's default styles, and noticed that the font size was set to 1.5 (Chrome). Then I used this ratio for my second attempt (blue) : 1 / 1.5 = 0.666.... Although the result is more reliable, the ratio seems not to be valid with a different font weight (1 pixel overflows the P tag's line) or a different font family (green test).
You could calculate the ratio for each font weight/family since both parameters seem to affect the line height ratio. I'm not able to get something more interesting currently. Hope it can help in some way.
<div class="em1">
<h1>PERFECT FIT</h1>
<h2>PERFECT FIT</h2>
<p>PERFECT FIT</p>
</div>
<div class="em67">
<h1>PERFECT FIT</h1>
<h2>PERFECT FIT</h2>
<p>PERFECT FIT</p>
</div>
<div class="em67 arial">
<h1>PERFECT FIT</h1>
<h2>PERFECT FIT</h2>
<p>PERFECT FIT</p>
</div>
.em1 {
line-height: 1em;
}
.em1 * {
background: pink;
line-height: inherit;
}
.em67 * {
background: lightblue;
line-height: .67em;
}
.arial * {
font-family: Arial;
background: lightgreen;
}
.outer {
display:block;
background:red;
float:left;
clear:both;
}
.hug {
font-size:200%;
margin:0em;
border:0em;
padding:0em;
}
.hug h1,
.hug h2 {
margin:0;
padding:0;
height:1em;
line-height:1em;
}

Extra spacing below my in-line images

Background
I am creating a video gallery using the ShadowBox jQuery plugin. To do this, I am creating rows of inline images using display:inline-block. The user will be able to upload a video as well as thumbnail images to accompany the video. The thumbnail's max size is 240x160 pixels.
What I want to do is have a black border around each gallery thumbnail "slot" with the user's uploaded thumbnail residing inside of that "slot", so if the user uploads a 240x160 thumbnail, the thumbnail will fill up the "slot" completely, and if they upload a smaller image, the thumbnail will still be in the "slot" with some extra spacing around it.
Here's an example of where I am right now: http://jsfiddle.net/shaunp/HvZ5p/
The problem is that there is extra spacing below my thumbnails and I'm not sure why. If you inspect the code you will see that there is an extra 5 pixels lurking below the image and I'm not sure where it's coming from. The grey part below the image should be directly BEHIND the image so that in the case the user uploads a smaller thumbnail, there will be grey-background space around it, but for some reason it is too tall. Any suggestions?
HTML
<div class="inline">
<div class="bg-thumb">
<div class="cell-thumb">
<a href="#" rev="#nvcCaption#" class="shadow">
<img src="http://farm9.staticflickr.com/8330/8135703920_f2302b8415_m.jpg" class="thumbImg" alt="Thumb" />
</a>
</div>
</div>
<div class="vcCaption">Caption</div>
</div>
<div class="inline">
<div class="bg-thumb">
<div class="cell-thumb">
<a href="#" rev="#nvcCaption#" class="shadow">
<img src="http://farm9.staticflickr.com/8330/8135703920_f2302b8415_m.jpg" class="thumbImg" alt="Thumb" />
</a>
</div>
</div>
<div class="vcCaption">Caption</div>
</div>
CSS
body {
overflow:hidden;
margin:0 50px 0 50px;
}
.vcCaption {
text-align:center;
font-family:"HelveticaNeue-Light","Helvetica Neue",Helvetica,Arial,sans-serif;
font-size:14px;
color:#000;
margin-bottom:5px;
}
.inline {
display:inline-block;
}
.bg-thumb {
width:250px;
height:170px;
}
.bg-thumb {
text-align:center;
display:table;
margin-bottom:5px;
}
.cell-thumb {
display:table-cell;
vertical-align:middle;
border:5px solid #000;
background-color:#7f7f7f;
}
.thumbImg {
max-width:240px;
max-height:160px;
}
Add vertical-align:top to your thumbnails:
.thumbImg {
max-width:240px;
max-height:160px;
vertical-align:top;
}
jsFiddle example
The default value of vertical-align is baseline, but for your needs you'll want the images to align to the top.
Another option would be to set the font size to zero on the containing div like:
.cell-thumb {
display:table-cell;
vertical-align:middle;
border:5px solid #000;
background-color:#7f7f7f;
font-size:0;
}
jsFiddle example
Adding vertical-align: middle; to your image will solve that.
.thumbImg {
vertical-align: middle;
max-width:240px;
max-height:160px;
}
the anchor tag is by default an inline element which gives it extra spacing, set it to a block element and give it some width and height!
.cell-thumb a {
display: block;
width: 240px;
height: 160px;
}
Images will by default display as inline-block (http://dev.w3.org/html5/markup/img.html#img-display) meaning that they will sits on an inline level block - or text line if you prefer.
Either set the font-size and/or line-height to 0 or in this case simply set the image to display at block level (display: block;).

Why aren't my divs aligning?

Morning,
I have the following HTML:
<div id="sah_padding">
<div id="sah_holder">
<div id="sah_name">Hello [agent_name]</div>
<div id="sah_logout">✖</div>
</div>
You are working with [customer_name]
</div>
and I have the following CSS:
#sah_padding{
padding:10px 10px 10px 10px;
}
#sah_holder{
padding-bottom:10px;
clear:both;
}
#sah_name{
float:left;
width:50%;
}
#sah_logout{
text-align:right;
}
#logout_link{
color:#fff;
text-decoration:none;
font-weight:bold;
}
However my login link and Hello message aren't aligning correctly, the logout link is a few pixels below the hello message and I need them to be on the same horizontal line. What am I doing wrong?
if you set line-height :1 to #logout_link element, it should correct the alignment
(of course feel free to choose a different value to adjust it)
Give float to your #sah_logout also. Write like this:
#sah_logout{
float: left;
text-align:right;
}
Check this http://jsfiddle.net/QUNT2/
Check the line-height of your elements!!!
You need to set a float to #sah_logout as well:
#sah_logout{
float: left;
text-align:right;
}
You can even make it say, float: right. It's entirely your choice. Doing only a text-align: right will modify how the inner contents of the container behave, not how the div behaves within the flow.
Also, you might have a few problems with the parent div not wrapping correctly around the children divs (as all children now have float properties), so you might need to add another div, with clear: both set in its style:
<div id="sah_holder">
<div id="sah_name">Hello [agent_name]</div>
<div id="sah_logout">✖</div>
<div style="clear: both"></div>
</div>
Instead of wasting time with block level elements why don't you simply use inline elements for both the "Hello" text and the logout link? That's what inline elements are supposed to do - stay in line with each other.
<div id="sah_padding">
<div id="sah_holder">
<span id="sah_name">Hello [agent_name]</span>✖
</div>
<span>You are working with [customer_name]</span>
</div>
Its is highly unlikely that you would need any of the CSS code you previously used for elements inside sah_holder unless you want to style them differently.
check it your updated html :-
<div id="sah_padding">
<div id="sah_holder">
<div id="sah_name">Hello [agent_name]</div>
<div id="sah_logout">You are working with [customer_name]</div>
</div>
</div>
your updated css:-
#sah_padding{
padding:10px 10px 10px 10px;
}
#sah_holder{
padding-bottom:10px;
border:1px solid red;
overflow:hidden;
}
#sah_name{
float:left;
width:50%;
}
#sah_logout{
float:right;
}
.logout_link{
color:black;
text-decoration:none;
font-weight:bold;
}
or you can see the demo:- http://jsfiddle.net/WnecH/11/