How to align text vertically equal top CSS - html

I am trying to align the text vertically equal using css, but there is some sort of space in the right side span.
Have added the code and fiddle link.
p{
clear: both;
margin: 0;
padding: 0;
display: table;
}
span.blue{
background: blue;
}
span.green{
background: green;
}
span.black{
background: black;
}
span.circle{
width: 15px;
height: 15px;
-webkit-border-radius: 15px;
border-radius: 15px;
display: inline-block;
margin-right: 12px;
/* float: left; */
vertical-align: top;
}
span.desc{
/* float: left; */
width: 115px;
vertical-align: top;
display: inline-block;
}
<div class="container">
<p>
<span class="blue circle"></span>
<span class="desc">Blue text</span>
</p>
<p>
<span class="green circle"></span>
<span class="desc">Green text</span>
</p>
<p>
<span class="black circle"></span>
<span class="desc">black text</span>
</p>
</div>
Fiddle Link

I think I see the small misalignment you're talking about. Is this better ? I aligned both the cirlce and the text using
vertical-align: middle;
instead of
vertical-align: top;
Edit
As vertical-align: top has to be kept, I instead added a margin-top: 2px to the circles. This places them at the same height as the text and both are still aligned at top.
p{
clear: both;
margin: 0;
padding: 0;
display: table;
}
span.blue{
background: blue;
}
span.green{
background: green;
}
span.black{
background: black;
}
span.circle{
width: 15px;
height: 15px;
-webkit-border-radius: 15px;
border-radius: 15px;
display: inline-block;
margin-right: 12px;
/* float: left; */
vertical-align: top;
margin-top: 2px;
}
span.desc{
/* float: left; */
width: 115px;
vertical-align: top;
display: inline-block;
}
<div class="container">
<p>
<span class="blue circle"></span>
<span class="desc">Blue text</span>
</p>
<p>
<span class="green circle"></span>
<span class="desc">Green text</span>
</p>
<p>
<span class="black circle"></span>
<span class="desc">black text</span>
</p>
</div>

Use vertical-align: middle or you can use the same line-height and font-size to solve the issue as in the snippet below.
p span {
font-size: 16px;
line-height: 16px;
}
Explanation:
Keeping the same line-height and font-size will ensure that vertical-align: top will work perfectly fine.
snippet below:
p{
clear: both;
margin: 0;
padding: 0;
display: table;
}
span.blue{
background: blue;
}
span.green{
background: green;
}
span.black{
background: black;
}
span.circle{
width: 15px;
height: 15px;
-webkit-border-radius: 15px;
border-radius: 15px;
display: inline-block;
margin-right: 12px;
/* float: left; */
vertical-align: top;
}
span.desc{
/* float: left; */
width: 115px;
vertical-align: top;
display: inline-block;
}
p span {
font-size: 16px;
line-height: 16px;
}
<div class="container">
<p>
<span class="blue circle"></span>
<span class="desc">Blue text</span>
</p>
<p>
<span class="green circle"></span>
<span class="desc">Green text</span>
</p>
<p>
<span class="black circle"></span>
<span class="desc">black text</span>
</p>
</div>

Your issue looks to be that the height of the text is greater then that of the circle. Flexbox could help with this, make the p display flex and set the circles to align center.
p{
clear: both;
margin: 0;
padding: 0;
display: flex;
}
span.blue{
background: blue;
}
span.green{
background: green;
}
span.black{
background: black;
}
span.circle{
align-self:center;
width: 15px;
height: 15px;
-webkit-border-radius: 15px;
border-radius: 15px;
display: inline-block;
margin-right: 12px;
/* float: left; */
vertical-align: top;
}
span.desc{
/* float: left; */
width: 115px;
vertical-align: top;
display: inline-block;
}

Related

How does one vertically center an Image inside parent div that's using css display:table? [duplicate]

This question already has answers here:
How do I vertically center text with CSS? [duplicate]
(37 answers)
Closed 5 years ago.
I want to try and have the div that contains the image on the left to be centered vertically. I have tried numerous things but I keep running into issues.
weekly-middle is the container div to the div which is left which contains the image that I want centered.
The text is already centered.
.weekly-middle {
text-align: center;
width: 100%;
/* height:100%;*/
border: none;
border-collapse: collapse;
margin: 0px;
padding-top: 56px;
padding-bottom: 56px;
content: "";
display: table;
/*clear: both;*/
}
.left {
text-align: left;
padding: 20px;
width: 100px;
float: left;
/* padding-top: 56px;
padding-bottom: 56px;*/
vertical-align: middle;
height: 300px
}
.right {
margin-left: 100px;
float: left;
padding-top: 56px;
padding-bottom: 56px;
}
<div class="weekly-middle teal">
<div class="left">
<img width="50" height="50" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Wiktionary_small.svg/350px-Wiktionary_small.svg.png" />
</div>
<div class="right">
<h1>Some Title</h1>
<p class="title">Problems Solved</p>
</div>
</div>
Solution provided in comment below
Refer to Joseph Marikle comment with link to JSFiddle.
Use flexbox. It's a very powerfull CSS tool :
.left {
text-align: left;
padding: 20px;
width: 100px;
float: left;
/* padding-top: 56px;
padding-bottom: 56px;*/
vertical-align: middle;
height: 300px;
display:flex;
flex-direction:column;
justify-content:center;
}
you can try following code
<div class="left">
<div class="display-table">
<div class="display-table-cell">
<img width="50" height="50" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Wiktionary_small.svg/350px-Wiktionary_small.svg.png" />
</div>
</div>
and for the css you can write:
.display-table{
display: table;
width:100%;
}
.display-table-cell{
display:table-cell;
vertical-align: middle;
text-align:left;
height: 100px;// your choose
}
add this to your css, hope it helps:
.weekly-middle img {
margin-top:50%;
}
.weekly-middle {
text-align: center;
width: 100%;
/* height:100%;*/
border: none;
border-collapse: collapse;
margin: 0px;
padding-top: 56px;
padding-bottom: 56px;
content: "";
display: table;
/*clear: both;*/
}
.weekly-middle img {
margin-top:50%;
}
.left {
text-align: left;
padding: 20px;
width: 100px;
float: left;
/* padding-top: 56px;
padding-bottom: 56px;*/
vertical-align: middle;
height: 300px
}
.right {
margin-left: 100px;
float: left;
padding-top: 56px;
padding-bottom: 56px;
}
<div class="weekly-middle teal">
<div class="left">
<img width="50" height="50" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Wiktionary_small.svg/350px-Wiktionary_small.svg.png" />
</div>
<div class="right">
<h1>Some Title</h1>
<p class="title">Problems Solved</p>
</div>
</div>
I think you should be able to just remove the 'left' and 'right' classes from the inner divs and it will work.
See if what you're looking for is below.
.weekly-middle {
text-align: center;
width: 100%;
/* height:100%;*/
border: none;
border-collapse: collapse;
margin: 0px;
padding-top: 56px;
padding-bottom: 56px;
content: "";
display: table;
/*clear: both;*/
}
.left {
text-align: left;
padding: 20px;
width: 100px;
float: left;
/* padding-top: 56px;
padding-bottom: 56px;*/
vertical-align: middle;
height: 300px
}
.right {
margin-left: 100px;
float: left;
padding-top: 56px;
padding-bottom: 56px;
}
<div class="weekly-middle teal">
<div class="">
<img width="50" height="50" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Wiktionary_small.svg/350px-Wiktionary_small.svg.png" />
</div>
<div class="">
<h1>Some Title</h1>
<p class="title">Problems Solved</p>
</div>
</div>

Why are the divs not expanding to the full width of the parent div

Fiddle: https://jsfiddle.net/eze5x9t9/
HTML:
<div style="width: 100%; overflow: hidden; height: 65px; background: #00CC00;">
<div style="width: 60%; overflow: hidden; float: left; background: #3074A3; color: #EDEDED; height: 65px; text-align: center; display: table; vertical-align: middle;">
<span style="font-size: 35px;display: table-cell;vertical-align: middle;">My Name</span>
</div>
<div style="width: 40%; overflow: hidden; float: left; background: #266996; color: #EDEDED; height: 65px; text-align: center; display: table; vertical-align: middle;">
<span style="font-size: 20px;display: table-cell;vertical-align: middle;">My Job</span>
</div>
</div>
Screenshot:
Why is there a green space at the end? The fiddle was done in Chrome.
It's a common BUG for the WebKit browsers, no fixing actually.
reference:
https://lists.webkit.org/pipermail/webkit-unassigned/2006-January/002684.html
https://css-tricks.com/percentage-bugs-in-webkit/
http://www.screenr.com/pvB8
Somehow in Chrome the outer div is exactly one pixel wider than the contained divs.
You could however solve that by not using display: table; and display: table-cell; (if you only did that to make the vertical centering work) like so:
<div style="width: 100%; height: 65px; background: #00CC00;">
<div style="width: 60%; float: left; background: #3074A3; color: #EDEDED; height: 65px; text-align: center;">
<span style="font-size: 35px; line-height: 65px;">My Name</span>
</div>
<div style="width: 40%; float: left; background: #266996; color: #EDEDED; height: 65px; text-align: center;">
<span style="font-size: 20px; line-height: 65px;">My Job</span>
</div>
</div>
Edit: A second code snippet showcasing the standard method for vertical centering using absolute positioning and transform: translate:
.outer {
height: 65px;
background-color: #00cc00;
display: flex;
}
.inner {
width: 60%;
float: left;
background: #3074A3;
color: #EDEDED;
height: 65px;
text-align: center;
font-size: 35px;
position: relative;
}
.inner + .inner {
width: 40%;
background: #266996;
font-size: 20px;
}
.inner > span {
display: inline-block;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div class="outer">
<div class="inner">
<span>My Name</span>
</div>
<div class="inner">
<span>My Job<br />Good job!</span>
</div>
</div>

Spans vertical align with float

I have a problem with vertically align 3 spans inside a div. It's easy to achieve, but vertical align doesn't work when i use float. I want that lightblue bar to be vertically centered. Code:
.container {
}
.text-1 {
float: left;
padding-right: 10px;
}
.bar {
background-color: lightblue;
border-radius: 5px;
float: left;
height: 5px;
width: 150px;
}
.text-2 {
padding-left: 10px;
}
<div class="container">
<span class="text-1">Text 1</span>
<span class="bar"> </span>
<span class="text-2">Text 2</span>
</div>
Thank you very much for your help.
JSFiddle
You can use display: inline-block; along with vertical-align: middle; on your <span> elements instead of float. This way they are positioned next to each other too and you can apply the vertical alignment:
.container span {
display: inline-block;
vertical-align: middle;
}
.text-1 {
padding-right: 10px;
}
.bar {
background-color: lightblue;
border-radius: 5px;
height: 5px;
width: 150px;
}
.text-2 {
padding-left: 10px;
}
<div class="container">
<span class="text-1">Text 1</span>
<span class="bar"> </span>
<span class="text-2">Text 2</span>
</div>

Vertical aligning the text in a container

I am trying to align span elements which are adjacent to a floating element.
Here is the fiddle
.heading {
background-color: tomato;
}
.heading::after {
content: "";
display: block;
clear: both;
}
.heading > * {
display: inline-block;
vertical-align: middle;
color: beige;
padding: 10px;
font-family: Calibri;
}
.button {
float: right;
background-color: firebrick;
font-family: Tahoma;
}
<div class="heading"> <span> some icon here</span>
<!--
--><span>some text here</span>
<div class="button">Go</div>
</div>
I am okay to change the HTML.
Some considerations: (Added after seeing below answers)
Font size and height of the each child elements are different.
If I use display: table-cell, the child elements width are stretching in the parent container. (I need to just align the child elements vertically)
PS: I am not looking for display: flex solution.
I would love to go with the display: table-cell; solution but if it's just one line than you can use line-height here. (Just not to complicate the process using display: table-cell; as there are certain limitations such as you cannot use margin and stuff)
.heading {
background-color: tomato;
line-height: 40px;
}
.button {
float: right;
padding: 0 10px;
background-color: firebrick;
}
Demo (line-height solution)
So basically what am doing is getting rid of your custom padding for the button and using a line-height instead. If you think you can have more than one line, than making both the sections as display: table-cell; will make much more sense.
As you commented a case where you have different font-size which doesn't make much sense to me, so a solution for that is to use display: table-cell; and tweak your markup a bit
Demo 2 (display: table-cell; solution)
<div class="heading">
<div>
<span> some icon here</span>
<span>some text here</span>
</div>
<div>
<div class="button">Go</div>
</div>
</div>
.heading {
background-color: tomato;
line-height: 40px;
}
.heading > div {
display: table-cell;
vertical-align: middle;
}
.heading > div:first-child {
width: 100%;
}
.heading span {
display: inline-block;
vertical-align: top;
}
.heading span:first-child {
font-size: 30px;
}
.button {
padding: 0 10px;
background-color: firebrick;
}
Add this in to your css:
.heading span {
line-height: 34px;
}
I think you try to display: table-cell property
as like this
.heading {
background-color: tomato;
display: table;
width:100%;
}
/* clear fix */
.heading::after {
content:"";
display: block;
clear: both;
}
.heading > * {
display: table-cell;
vertical-align: middle;
color: beige;
}
.button {
float: right;
padding: 10px;
background-color: firebrick;
}
/* my attempt to align */
.heading::before {
content:"";
height: 100%;
width: 0px;
}
<div class="heading"> <span> some icon here</span>
<span>some text here</span>
<div class="button">Go</div>
</div>
Try
position: relative;
top: 50%;
transform: translateY(-50%);
I would suggest using a padding and make sure the height of the button is set.
.heading .mytext {
height: 20px;
float: left;
padding-bottom: 10px;
padding-top: 10px;
}
.button {
float: right;
padding: 10px;
height: 20px;
background-color: firebrick;
}
with html
<div class="heading">
<div class="mytext">
<span> some icon here</span>
<span>some text here</span>
</div>
<div class="button">Go</div>
</div>
See: http://jsfiddle.net/w7vngc43/20/
I modified Mr.Alien's solution of display: table-cell without using line-height. Here is the code:
HTML:
<div class="heading">
<div class="left"> <span class="left-one"> some icon here</span>
<span class="left-two">some text here</span>
</div>
<div class="right">
<button class="button-cancel">Cancel</button>
<button class="button-go">Go</button>
</div>
</div>
CSS
.heading {
background-color: tomato;
}
.heading > div {
display: table-cell;
vertical-align: middle;
}
.left {
width: 100%;
}
.left span {
display: inline-block;
vertical-align: middle;
}
.left-one {
font-size: 30px;
}
button {
padding: 10px 15px;
background-color: firebrick;
border: none;
margin: 0;
}
.right {
white-space: nowrap;
}
Working Fiddle

CSS table cell alignment and ellipsis not working

I have some HTML code that is not hidden and cut off using the CSS ellipsis.
I have tried many things to fix this issue, but nothing works for me (and it is killing me I cannot fix such a simple issue). I have read all the SO posts about the CSS ellipsis.
Here is a visual representation of what I have:
As shown the 11/2001 (2 annees, 10 mois) is dropped to the next line and the ellipsis does not take effect.
I am trying to keep the 11/2001 (2 annees, 10 mois) next to the prompt Date d'achevement and to be cut off (hidden) with the ellipsis if the value is too long, as it is in this case.
Here is my HTML
<div id="live_preview" class="livePreview_resumeWrapper1">
<div class="resumeStyleWrapper25">
<div class="resumeStyleOptimisedContainer25">
<div class="resumeStyleStandardHeadings25">Emploi Détails d'histoire</div>
</div>
<div class="resumeStyleWrapper25">
<div class="resumeStyleStandardTableRow25">
<div class="resumeStyleStandardContainer25">
<div class="resumeStyleStandardTableRow25">
<div class="resumeStyleStandardLabels25">employeur</div>
<div class="resumeStyleStandardLabelContent25">french</div>
</div>
<div class="resumeStyleStandardTableRow25">
<div class="resumeStyleStandardLabels25">Date de demarrage</div>
<div class="resumeStyleStandardLabels25">
<div class="resumeStyleDateStartContent25">01/2009</div>
<div class="resumeStyleFinishDateLabel25">Date d'achevement</div>
<div class="resumeStyleDateFinishContent25">
<div class="ellipsis">11/2011 (2 annees, 10 mois)</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Here is my CSS
.livePreview_resumeWrapper1 {
border: 1px solid #000;
box-shadow: 10px 10px 5px 0px #888888;
direction: ltr;
padding: 20px;
width: 93%;
}
.resumeStyleWrapper25 {
border-spacing: 0px 0px;
display: table;
font-family: verdana;
font-size: 12px;
width: 100%;
}
.resumeStyleOptimisedContainer25 {
/* THIS CLASS ALTERNATES BETWEEN A ROW AND A NON-ROW DEPENDING ON THE STYLE REQUIREMENTS */
background-color: #000;
display: table-cell;
font-weight: bold;
min-width: 149px;
padding: 2px;
text-transform: uppercase;
vertical-align: top;
width: 18%;
}
.resumeStyleStandardHeadings25 {
background-color: #000;
color: #fff;
display: table-cell;
font-weight: bold;
min-width: 149px;
padding: 2px;
text-transform: uppercase;
vertical-align: top;
width: 18%;
}
.resumeStyleStandardContainer25 {
padding-left: 5px;
width: 100%;
}
.resumeStyleStandardTableRow25 {
display: table-row;
}
.resumeStyleStandardLabels25 {
direction: ltr;
display: table-cell;
font-weight: bold;
height: 100%;
min-height: 25px;
overflow: hidden;
padding: 2px;
padding-right: 10px;
text-align: right;
vertical-align: top;
white-space: nowrap;
}
.resumeStyleDateStartContent25 {
direction: ltr;
display: table-cell;
float: left;
padding: 2px;
text-align: left;
vertical-align: top;
width: 20%;
}
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.resumeStyleFinishDateLabel25 {
background-color: #fff;
color: #000;
direction: ltr;
display: table-cell;
float: left;
font-weight: bold;
padding: 2px;
padding-right: 10px;
text-align: left;
vertical-align: top;
}
.resumeStyleDateFinishContent25 {
direction: ltr;
display: table-cell;
float: left;
padding: 2px;
text-align: left;
vertical-align: top;
}
Can anyone point out what I am doing incorrectly?
EDIT
I have updated the HTML & CSS as requested in the comments.
There is a problem of the CSS table structure, you have some table cells stay directly under the element that is also set to table cell, which is invalid. You can wrapped the inner table cell elements into a container and set it as display:table, which would fix the issue.
It's this part:
<div class="table">
<div class="resumeStyleStandardLabelContent25">
<div class="ellipsis">01/2009 Date d'achevement</div>
</div>
<div class="resumeStyleStandardLabelContent25">
<div class="ellipsis">11/2011 (2 annees, 10 mois)</div>
</div>
</div>
In general the correct CSS table layout is like this, but row isn't needed if there is only one row.
<div style="display:table">
<div style="display:table-row">
<div style="display:table-cell">
...
When you use CSS text-overflow:ellipsis in table, it needs to work with fixed table layout, and has width value set, either fixed or percentage are both fine.
It's like this:
.table {
display: table;
width: 100%;
table-layout: fixed;
}
Lastly:
Remove all the float properties from the table cells, they don't work together.
Here is the updated code snippet:
body {width: 500px;} /*for demo only*/
.livePreview_resumeWrapper1 {
border: 1px solid #000;
box-shadow: 10px 10px 5px 0px #888888;
direction: ltr;
padding: 20px;
width: 93%;
}
.resumeStyleWrapper25 {
border-spacing: 0px 0px;
display: table;
font-family: verdana;
font-size: 12px;
width: 100%;
}
.resumeStyleOptimisedContainer25 {
/* THIS CLASS ALTERNATES BETWEEN A ROW AND A NON-ROW DEPENDING ON THE STYLE REQUIREMENTS */
background-color: #000;
display: table-cell;
font-weight: bold;
min-width: 149px;
padding: 2px;
text-transform: uppercase;
vertical-align: top;
width: 18%;
}
.resumeStyleStandardHeadings25 {
background-color: #000;
color: #fff;
display: table-cell;
font-weight: bold;
min-width: 149px;
padding: 2px;
text-transform: uppercase;
vertical-align: top;
width: 18%;
}
.resumeStyleStandardContainer25 {
padding-left: 5px;
width: 100%;
}
.resumeStyleStandardTableRow25 {
display: table-row;
}
.resumeStyleStandardLabels25 {
direction: ltr;
display: table-cell;
font-weight: bold;
height: 100%;
min-height: 25px;
overflow: hidden;
padding: 2px;
padding-right: 10px;
text-align: right;
vertical-align: top;
white-space: nowrap;
}
.resumeStyleDateStartContent25 {
direction: ltr;
display: table-cell;
/* float: left; */
padding: 2px;
text-align: left;
vertical-align: top;
width: 20%;
}
.table {
display: table;
width: 100%;
table-layout: fixed;
}
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.resumeStyleFinishDateLabel25 {
background-color: #fff;
color: #000;
direction: ltr;
display: table-cell;
/* float: left; */
font-weight: bold;
padding: 2px;
padding-right: 10px;
text-align: left;
vertical-align: top;
}
.resumeStyleDateFinishContent25 {
direction: ltr;
display: table-cell;
/* float: left; */
padding: 2px;
text-align: left;
vertical-align: top;
}
<div id="live_preview" class="livePreview_resumeWrapper1">
<div class="resumeStyleWrapper25">
<div class="resumeStyleOptimisedContainer25">
<div class="resumeStyleStandardHeadings25">Emploi Détails d'histoire</div>
</div>
<div class="resumeStyleWrapper25">
<div class="resumeStyleStandardTableRow25">
<div class="resumeStyleStandardContainer25">
<div class="resumeStyleStandardTableRow25">
<div class="resumeStyleStandardLabels25">employeur</div>
<div class="resumeStyleStandardLabelContent25">french</div>
</div>
<div class="resumeStyleStandardTableRow25">
<div class="resumeStyleStandardLabels25">Date de demarrage</div>
<div class="resumeStyleStandardLabelContent25">
<div class="table">
<div class="resumeStyleStandardLabelContent25">
<div class="ellipsis">01/2009 Date d'achevement</div>
</div>
<div class="resumeStyleStandardLabelContent25">
<div class="ellipsis">11/2011 (2 annees, 10 mois)</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Or, view the JsFiddle demo, so you can resize the frame easily to check:
Working Demo Here