CSS Aligning Things Vertically and Horizontally - html

See Fiddle
I have an image and two sets of text (one is a "label" and one is a "ranking").
<div class="contentsInfo">
<img src="http://lorempixel.com/90/30" />
<span>Here is some info</span>
<span>20</span>
</div>
I would like to align each to be centered vertically, the label to show up next to the image, and for the ranking data to be right aligned in the container.
However, it is not aligning in the middle, and the info and ranking "columns" are being switched (the ranking shows up in the middle, and the label to the right side).
Here is my css:
.contentsInfo
{
font-size: 0.80em;
border-bottom: 1px grey dotted;
vertical-align: middle;
display: table;
width: 100%;
}
.contentsInfo>span
{
float: right;
margin-left: 5px;
vertical-align: middle;
display: table-cell;
}
.contentsInfo span
{
font-size: 1.1em;
}
.contentsInfo img
{
height: 30px;
vertical-align: middle;
margin-right: 2px 5px 2px 0px;
padding: 2px 0px;
}

Wrap your contents in elements and do the following:
HTML:
<div class="contentsInfo">
<div>
<img src="http://lorempixel.com/90/30" />
<span>Here is some info</span>
</div>
<div>
<span>20</span>
</div>
</div>
CSS:
body,
html {
padding: 0px;
margin: 0px;
}
.contentsInfo {
font-size: 0.80em;
border-bottom: 1px grey dotted;
vertical-align: middle;
display: table;
width: 100%;
}
.contentsInfo>div {
margin-left: 5px;
vertical-align: middle;
display: table-cell;
}
.contentsInfo > div:first-child {
width: 100%;
}
.contentsInfo > div:last-child {
white-space: nowrap;
}
.contentsInfo span {
font-size: 1.1em;
}
.contentsInfo img {
height: 30px;
vertical-align: middle;
margin-right: 2px 5px 2px 0px;
padding: 2px 0px;
}
Working Fiddle

You can do like following way using display:flex. I have added on class ranking to align it right side.
*{
margin:0;
}
.contentsInfo
{
font-size: 0.80em;
border-bottom: 1px grey dotted;
display: flex;
align-items: center;
}
.contentsInfo>span
{
margin-left: 5px;
display: flex;
flex: 1 1 auto;
margin-right: 5px;
}
.ranking{
justify-content: flex-end;
}
.contentsInfo span
{
font-size: 1.1em;
}
.contentsInfo img
{
height: 30px;
padding: 2px 0px;
}
<div class="contentsInfo">
<img src="http://lorempixel.com/90/30" />
<span>Here is some info</span>
<span class="ranking">20</span>
</div>
Fiddle

Related

How to make border as big as text in it

So it looks like this:
this is the example code of a message:
.allMsg {
width: 100%;
}
.self {
border-radius: 1rem;
background-color: #28a745;
text-align: right;
padding-left: 5px;
padding-right: 5px;
margin-left: 5px;
margin-right: 5px;
}
.friend {
text-align: left;
}
#chatWith {
text-align: center;
font-family: sans-serif;
font-size: 40px;
padding-bottom: 15px;
border-bottom: 1px solid #eee;
}
<div class='allMsg'>
<p class='self chatMsg'>Hello</p>
</div>
How can i make the border as big as the text inside ... I thought the padding was going to work but unfortunately it didn't so please help me.
It's possible if you wrap your messages inside another element. So let's say all messages have a full-width element, but friends messages will aligned to the left and have a blue background, while yours will aligned to the right and have a green background. If you don't want to change your markup so much, the easiest is to wrap your messages inside a span, than you doesn't need to change anything else in your html.
.allMsg {
width: 100%;
}
.self span, .friend span {
border-radius: 1rem;
padding-left: 5px;
padding-right: 5px;
margin-left: 5px;
margin-right: 5px;
}
.self span {
background-color: #28a745;
}
.friend span {
background-color: #2845a7;
}
.self {
text-align: right;
}
.friend {
text-align: left;
}
<div class='allMsg'>
<p class='chatMsg friend'>
<span>hello</span>
</p>
<p class='chatMsg self'>
<span>hy</span>
</p>
<p class='chatMsg friend'>
<span>how are you friend?</span>
</p>
<p class='chatMsg self'>
<span>i'm fine thanks</span>
</p>
</div>
You could use flexbox on the container:
.allMsg {
width: 100%;
display: flex;
flex-flow: column nowrap;
align-items: flex-end;
}
Example:
.allMsg {
width: 100%;
display: flex;
flex-flow: column nowrap;
align-items: flex-end;
}
.self {
border-radius: 1rem;
background-color: #28a745;
text-align: right;
padding-left: 5px;
padding-right: 5px;
margin-left: 5px;
margin-right: 5px;
}
.friend {
text-align: left;
}
#chatWith {
text-align: center;
font-family: sans-serif;
font-size: 40px;
padding-bottom: 15px;
border-bottom: 1px solid #eee;
}
<div class='allMsg'>
<p class='self chatMsg'>Hello</p>
<p class='self chatMsg'>This is a test</p>
</div>

Vertical align left and right text inside a div

I am trying to create a headline and align one text to the left and another to the right and I would like to know if there is another way apart from line-height to achieve that?
.popular {
margin-left: 15px;
float: left;
}
.popular-day {
margin-right: 15px;
float: right
}
.menu-headline {
height: 100px;
outline: 1px solid black;
width: 500px;
text-align: middle;
}
<div class='menu-headline'>
<p class='popular'>Popular</p>
<p class='popular-day'>Today</p>
</div>
It can be done easily with flexbox.
.menu-headline {
outline: 1px solid black;
width: 500px;
height: 100px;
padding: 0 15px;
box-sizing: border-box;
display: flex;
justify-content: space-between;
align-items: center;
}
<div class='menu-headline'>
<p class='popular'>Popular</p>
<p class='popular-day'>Today</p>
</div>
Or using CSS table to support older IE.
.menu-headline {
outline: 1px solid black;
width: 500px;
height: 100px;
display: table;
}
.menu-headline > p {
display: table-cell;
vertical-align: middle;
padding: 0 15px;
}
.popular-day {
text-align: right;
}
<div class='menu-headline'>
<p class='popular'>Popular</p>
<p class='popular-day'>Today</p>
</div>

text-align not working with vertical-align

I have 6 divs that each contain a <p> and a link, and I want them to both be centered, with the <p> at the top and the link at the bottom. This is my html (it's basically the same for all 6):
<div id="home">
<p>P</p>
<a class="nav" href="index.html">home</a>
</div>
(all 6 are wrapped in a <header>)
This is my css:
header div {
width: 133.33px;
height: 145px;
text-align: center;
font-size: 18px;
padding-bottom: 3px;
font-weight: lighter;
float: left;
display: table;
}
header div a {
text-decoration: none;
color: #474747;
vertical-align: bottom;
text-align: center;
display: table-cell;
}
header div p {
font-size: 60px;
padding: 0;
text-align: center;
vertical-align: top;
display: table-cell;
}
I can't get the text-align: center to work at the same time as the vertical-align. Is there anyway to get the top/bottom positioning without using vertical-align, or to get the text-align to work?
Here's the code you're looking for, I hope: (These are not the droids you're looking for)
.wrapper {
text-align: center;
height: 200px;
width: 200px;
border: 1px solid black;
display: table;
}
.container {
display: table-cell;
vertical-align: bottom;
}
<div class="wrapper">
<div class="container">
<p>Hello World!</p>
I am a Link!
</div>
</div>
following Alohci's comment, if you'd like the <p> tags at the top and <a> tags at the bottom you should use the position property as follows:
.wrapper {
position: relative;
text-align: center;
height: 200px;
max-height: 200px;
width: 200px;
max-width: 200px;
border: 1px solid black;
display: table;
}
.container p {
display: inline;
vertical-align: top;
}
.container a {
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
<div class="wrapper">
<div class="container">
<p>Hello World!</p>
I am a Link!
</div>
</div>
Use Following CSS,
You Can Get a <p> and a link both be at centered,
div {
width: 133.33px;
height: 145px;
text-align: center;
font-size: 18px;
padding-bottom: 3px;
font-weight: lighter;
float: left;
display: table;
}
div a {
text-decoration: none;
color: #474747;
vertical-align: middle;
display: table-cell;
}
div p {
font-size: 60px;
padding: 0;
text-align: center;
}
Here is the Jsfiddle solution link.
http://jsfiddle.net/rbdqtxyf/

Why display: table doesn't center the div?

.headers
{
background-color: #ff0000;
width: 100%;
overflow: hidden;
display: table;
}
.logo
{
height: 35px;
border: 1px solid #00ff00;
float: left;
width: 30px;
}
.slogan
{
float: right;
display: table-cell;
vertical-align: middle;
}
<div class="headers">
<div class="logo"></div>
<div class="slogan">IIN</div>
</div>
How do i center my div[slogan] without using negative margin/top 50%?
you don't need floats when using display: table/table-cell ... this should center the .slogan div using the table-cell layout.
.headers
{
background-color: #ff0000;
width: 100%;
overflow: hidden;
display: table;
}
.logo
{
height: 35px;
border: 1px solid #00ff00;
/*float: left; NOT THIS */
width: 30px;
display: table-cell; /* THIS */
}
.slogan
{
/*float: right; NOT THIS */
display: table-cell;
vertical-align: middle;
text-align: right; /* THIS */
}
<div class="headers">
<div class="logo"></div>
<div class="slogan">IIN</div>
</div>
If you are looking to middle slogan vertically then please check the fiddle https://jsfiddle.net/nileshmahaja/e6byt6zt/
CSS
.headers
{
background-color: #ff0000;
width: 100%;
overflow: hidden;
display: table;
vertical-align:middle;
}
.logo
{
height: 35px;
border: 1px solid #00ff00;
float: left;
width: 30px;
}
.slogan
{
display: table-cell;
vertical-align: middle;
text-align:right;
}
change your slogan class with this
.slogan {
display: table-cell;
text-align: center;
vertical-align: middle;
width: 100%;
}
You should use 'margin: 0 auto ;' on the . If you want to align text inside the div you can just use 'text-align: center ;'.
Check this post click

Vertically center div contained in floating div

I have a div that's inside a float:left div and it doesn't want to vertically align. I thought I could completely fill the floated div with the inner div and set that one to display: table-cell, but it doesn't seem to want to both FILL the div and allow this display?
JSFiddle Here
<div id="tabs-3" aria-labelledby="ui-id-3" class="ui-tabs-panel ui-widget-content ui-corner-bottom" role="tabpanel" aria-expanded="true" aria-hidden="false" style="">
<div id="DivToCenter">
<div id="ButtonContainer">
<ul>
<li><div class="TemplateButton">Report an Issue</div></li>
<li><div class="TemplateButton">Report an Issue</div></li>
<li><div class="TemplateButton">Report an Issue</div></li>
<li><div class="TemplateButton">Report an Issue</div></li>
</ul>
</div>
</div>
</div>
Styling:
#tabs-3 { /* Tab Content Panel (Right Panel)*/
float: left;
min-height: 300px; /* How tall is our tab box, minimum?*/
text-align: center;
background-color: pink;
}
#DivToCenter {
width: 100%;
height: 100%;
background: red;
display: table-cell;
vertical-align: middle;
text-align: center;
}
#ButtonContainer {
width: 100%;
overflow: auto;
padding-top: 10px;
padding-bottom: 10px;
background: green;
display: table-cell;
vertical-align: middle;
text-align: center;
}
#ButtonContainer > ul {
padding: 0;
margin: 0;
display: inline-block;
}
#ButtonContainer > ul > li {
display: inline-block; /*Puts the buttons in a line*/
}
.TemplateButton {
padding-left: 5px;
padding-right: 5px;
}
.ui-tabs-panel.ui-widget-content .TemplateButton > a {
/* SHAPE */
width: 119px;
height: 119px;
padding: 15px;
margin-left: 3px;
margin-right: 3px;
border: 1px solid white;
border-radius: 15px;
-moz-border-radius: 15px;
/* ALIGNMENT */
display: table-cell;
vertical-align: middle;
text-align: center;
/* STYLING */
font-weight: bold;
font-size: 1.25em;
color: white;
background-color: #0c428f;
}
.ui-tabs-panel.ui-widget-content .TemplateButton > a:hover {
background-color: #6795bb;
}
Just add display: table; to #tabs-3
Updated Demo