vertically align a div and a span using css - html

please see the jsfiddle project here . How to horizontally align the box and the label?
.box {
display: inline-block;
height: 20px;
width: 30px;
border: 1px solid;
}
.legend {
horizontal-align: middle;
}
<div class="legend">
<div class="box" style="background-color: red;">
</div>
<span>Alabama</span>
</div>

I assume you mean vertically align? If you are simply trying to align, vertically, two div of unequal height then display: inline-block is the simplest and most effective method. You almost had it with your code, but float breaks it. Don't use float!
.box {
background-color: red;
border: 1px solid;
height: 20px;
width: 30px;
}
.box,
.legend {
display: inline-block;
vertical-align: middle;
}
<div class="box"></div>
<div class="legend">Alabama</div>
enter link description here

Setting the vertical-align property on the .box div would seem to make the most sense and cause the least issues.
.box {
display: inline-block;
height: 20px;
width: 30px;
border: 1px solid;
vertical-align: middle;
}
<div class="legend">
<div class="box" style="background-color: red;">
</div>
<span>Alabama</span>
</div>

You can float the box instead of displaying it inline. There's a couple different ways to align these.
https://jsfiddle.net/cckxkvov/5/
.box {
float: left;
height: 20px;
width: 30px;
border: 1px solid;
}
.legend {
horizontal-align: middle;
}
span {
display: inline-block;
line-height: 20px;
}

Why not use flexbox?
If you're trying to vertically align the two items within .legend then just apply these styles:
.legend {
display: flex;
align-items: center;
}
Keep in mind you will need to apply prefixes for cross-browser compatibility.

Related

Centering a div to the viewport with other divs on both sides

I'm currently designing a header bar for a site I'm working on. I would like the header to show a logo at the left, the title in the center, and the user account on the right.
This mockup is what I'm envisioning. Note that the dotted boxes denote a div.
I've made some progress on creating it in HTML/CSS, but I can't seem to get the title to center to the viewport.
As you can see, the title is centering itself between the logo and the account info divs, instead of centering itself on the viewport. This ends up making the page just look a little bit off, and so I would really like to center the title to the viewport.
Here's my code:
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display: inline-block;
text-align: center;
}
.logoArea {
display: inline;
text-align: center;
float: left;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
display: inline-block;
text-align: center;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.siteTitle {
color: white;
}
.pageTitle {
color: white;
}
.userAccountArea {
display: inline;
text-align: center;
float: right;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}
<div className="headerBarArea">
<div className="logoArea">
<img src="assets/mines_logo_stacked.png" className="minesLogo" />
</div>
<div className="titleArea">
<h2 className="siteTitle">This is my site Title</h2>
<h3 className="pageTitle">Page Title</h3>
</div>
<div className="userAccountArea">
<img src="assets/user_account.png" className="userAccountIcon" />
<span className="UserAccountText">John Smith (Student)</span>
</div>
</div>
Any ideas on what I could do to make the title div centered to the viewport, instead of centering between the two divs?
html code
<div class="flex-container">
<div class="flex-item">1</div>
<div class="align-self-center">
<span class="siteTitle">This is my site Title</span>
<br>
<div class="text-center ">Page Title</div>
</div>
<div class="flex-item align-self-end third-item">3</div>
</div>
CSS code
.flex-container {
/* We first create a flex layout context */
display: flex;
/* Then we define the flow direction
and if we allow the items to wrap
* Remember this is the same as:
* flex-direction: row;
* flex-wrap: wrap;
*/
flex-flow: row wrap;
/* Then we define how is distributed the remaining space */
justify-content: space-between;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin-top: 10px;
line-height: 150px;
color: white;
font-weight: bold;
text-align: center;
}
.third-item {
height: 100px;
}
.text-center{
text-align: center;
}
.align-self-end{
align-self:end;
}
.align-self-center{
align-self:center;
}
code output
code solution
used flex to place the items used .flex-container as parent div where flex items are placed in .justify-content: space-between; is used to place space in between the items. align-self:center; is used to place Page Title at center
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
codepen
.headerBarArea{
display:flex;
justify-content: space-between;
align-items: center
}
It is an easy way to the layout.
you can try it.
Try adding margin-left:auto; and margin-right:auto; to the .titleArea class .
I would suggest using a flex-box though.
Replace your css code with this to make title div centered in all the viewport.
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display:flex;
justify-content: space-between;
}
.logoArea , .titleArea , .userAccountArea {
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
text-align: center;
}
.siteTitle , .pageTitle{
color: white;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}

Vertically Centering In-line Block Elements Of Different Heights

I'm attempting to vertically align 2 divs with different heights and widths, which I've horizontally centered on the page. Text-align doesn't appear to do anything, and I was hoping that there was a solution that I'm missing.
CSS
.center {
text-align: center;
}
div#map {
background: blue;
display: inline-block;
height: 250px;
margin: 10px 15px;
width: 300px;
}
div.contact {
display: inline-block;
margin: 10px 15px;
overflow: auto;
text-align: center;
width: 350px;
}
HTML
<div class="center">
<div id="map">...</div>
<div class="contact">...</div>
</div>
Just add vertical-align: middle:
div#map,
div.contact {
vertical-align: middle;
display: inline-block;
}
.center {
text-align: center;
}
div#map {
vertical-align: middle;
background: blue;
display: inline-block;
height: 250px;
margin: 10px 15px;
width: 300px;
}
div.contact {
vertical-align: middle;
display: inline-block;
background: green;
margin: 10px 15px;
overflow: auto;
text-align: center;
width: 350px;
}
<div class="center">
<div id="map">...</div>
<div class="contact">...</div>
</div>
I'd set text-align: left for the .contact div and vertical-align: middle for both (since they are inline-blocks)
http://codepen.io/anon/pen/QdNaoJ
You can use flexbox
.center {
align-items: center;
}
You can find the flexbox documentation here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
User this in your css
div#map,div.contact {
display: inline-block;
vertical-align:middle;
}
vertical-align:middle; will aling the data in middle between your div

How to vertically position some text at the centre of a circle div? [duplicate]

This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 6 years ago.
I'm trying to make a circle shaped div with some text directly in the middle. I can get it horizontally in the middle, easily but I am having loads of trouble getting it in the vertical center, I've tried doing vertical-align, padding, display etc but it'll always stay right at the top.
Here's the offending HTML:
<div class="widget">
<p id='case'>{27 cases}</p>
</div>
And here's the CSS for that div:
.widget {
background-color: #ff1122;
color: yellow;
width: 150px;
height: 150px;
border-radius: 100%;
text-align: center;
font-size: 24px;
vertical-align: middle;
}
I'm trying to get the text precisely in the middle of the circle vertically. How can I do this?
Take a look at this:
.widget {
background-color: #f12;
color: yellow;
width: 150px;
height: 150px;
border-radius: 100%;
text-align: center;
font-size: 24px;
}
.widget * {
display:inline-block;
vertical-align: middle;
}
.widget::before {
content: "";
display:inline-block;
width: 0;
height: 100%;
vertical-align: middle;
}
<div class="widget">
<p id='case'>{27 cases}</p>
</div>
Make the circle display: table; and the inside p have display: table-cell; and vertical-align: middle;.
.widget {
background-color: #ff1122;
color: yellow;
width: 150px;
height: 150px;
border-radius: 100%;
text-align: center;
font-size: 24px;
display: table;
}
.widget p {
display: table-cell;
vertical-align: middle;
}
<div class="widget">
<p id='case'>{27 cases}</p>
</div>
You need to add the line-height property. Make the line-height equal to the height of the circle, in this case 150px.
line-height:150px;
.widget {
background-color: #ff1122;
color: yellow;
width: 150px;
height: 150px;
border-radius: 100%;
text-align: center;
font-size: 24px;
vertical-align: middle;
line-height:150px;
}
<div class="widget">
<p id='case'>{27 cases}</p>
</div>
If you are open to using flex-box you can also do this instead. This will work for multiple lines.
.widget {
background-color: #ff1122;
color: yellow;
width: 150px;
height: 150px;
border-radius: 100%;
text-align: center;
font-size: 24px;
display: flex;
flex-direction: column;
justify-content: center;
}
<div class="widget">
<p id='case'>This is multiple line text.</p>
</div>

how to center align the label for form

I am using my own classes and tags for HTML and CSS forms. I want to align the label text in middle.
I tried to use it using line height but it get messier when the text length is heavy.
and is it possible to use vertical align: middle; Because that's not even working
HTML :
<div class="type-details">
<div class="col-xs-12 no-padding text-group">
<span class="col-md-4 no-padding form-label">Placeholder:</span>
<div class="col-md-7 no-padding">
<input class="form-text" type="text">
</div>
</div>
</div>
CSS:
.type-details {
border-bottom: 1px solid #e9eef0;
padding: 20px;
width: 60%;
float: left;
}
.form-label {
float: left;
min-width: 17px;
font-size: 11px;
text-transform: uppercase;
color: #62696d;
margin-right: 15px;
}
.no-padding {
padding: 0px;
}
input.form-text {
background: #fff;
border: 1px solid #dae2e6;
font-size: 12px;
color: #62696d;
padding: 6px 8px;
box-shadow: none;
border-radius: 0;
line-height: normal;
display: inline-block;
width: 100%;
margin-bottom: 0px;
}
Check this fiddle https://jsfiddle.net/kuascjpo/2/ I want to align the span="form-label" vertically center
There are a few ways you can do this. Here is a fiddle: https://jsfiddle.net/sheriffderek/b9jg33b3/
Markup
<form action="" class="your-thing">
<label class="input-w">
<span class="label">First<br> name:</span>
<input type="text" placeholder='sheriff'>
</label>
<label class="input-w">
<span class="label">Last name:</span>
<input type="text" placeholder='derek'>
</label>
</form>
1. With display: inline-block;
The trick is that you need the element to be display inline-block. This way, you can use the vertical-align: middle; property of inline elements - and have the other properties of block elements too. Also, you don't want to use floats in this case. So,
.inline-block .input-w {
display: block; // to stack them
width: 100%;
}
.inline-block .label, .inline-block input {
/* float: none; you may need this to overide previous float rules */
display: inline-block;
vertical-align: middle;
}
2. The other way would be to use flexbox display: flex;
This involves a little more concern about the markup and the parent of these inputs.
.flex-box {
display: flex;
flex-direction: column;
}
.flex-box .input-w {
display: flex;
flex-direction: row;
}
They both have their strengths and side-affects. : )
A simple approach/hack that works is to wrap the text inside the <label></label> tags in <p></p> tags, at which point, the text-align: center; property will work to center the text.
label{
text-align: center;
}
<label><p>Your Text</p></label>
https://jsfiddle.net/cmckay/2xydfs87/
.text-group {
display:flex;
align-items:center;
}
vertical-align: baseline|length|sub|super|top|text-top|middle|bottom|text-bottom|initial|inherit;
http://www.w3schools.com/cssref/pr_pos_vertical-align.asp

Horizontally and Vertically align a span element

Demo
<div class="subject" index= "0">
<span class="subject_name">FIFA</span>
<span class="subject_completion">55%</span>
</div>
.subject span
{
display: table-cell;
vertical-align: middle;
}
Why it is not vertically aligning my span div? How do i align it vertically which should not affect horizontal aligning also?
I am not preferring using top, margin-top, padding-top. I am preferring something which should work even size of my circle changes.
I am free to modify html also, but i am first preferring span instead div.
Please suggest some solutions.
You don't need any special rules at all for the spans. You can just add these three rules to the container:
.subject {
display: flex;
align-items: center;
justify-content: center;
}
.user_body_content_container
{
display: table;
}
.subject_container
{
width: 200px;
height: 250px;
border: 1px solid #eee;
display: table-cell;
}
.subject
{
border-radius: 50%;
border: 1px solid #653;
width: 175px;
height: 175px;
margin: auto;
margin-top: 25%;
display: flex;
align-items: center;
justify-content: center;
}
<div class="user_body_content_container">
<div class="subject_container" id="subject_container0" index="0">
<div class="subject" index= "0">
<span class="subject_name">FIFA</span>
<span class="subject_completion">55%</span>
</div>
</div>
</div>
you have too many display: table and display: table-cell for the task you're doing.
try
.user_body_content_container
{
}
.subject_container
{
width: 200px;
height: 250px;
border: 1px solid #eee;
/*display: table-cell;*/
/*remove above*/
}
.subject
{
border-radius: 50%;
border: 1px solid #653;
width: 175px;
height: 175px;
margin: auto;
margin-top: 25%;
display: table;
text-align: center;
}
.subject span
{
display: table-cell;
vertical-align: middle;
}
Jsfiddle
You can use following CSS classes.
.user_body_content_container
{
display: table;
}
.subject_container
{
width: 200px;
height: 250px;
border: 1px solid #eee;
display: table-cell;
position:absolute;
}
.subject
{
border-radius: 50%;
border: 1px solid #653;
width: 175px;
height: 175px;
margin: auto;
margin-top: 25%;
}
.subject span
{
display:block;
position: relative;
top:50%;
text-align:center;
}
So here's the solution I came up with:
css:
body, html{
margin:0; padding:0;height:100%
}
.user_body_content_container{
height:100%;
}
.subject_container{
display:table;
width:100%;
height:100%;
vertical-align:middle
}
.subject
{
display: table-cell;
vertical-align: middle;
text-align:center;}
.subject span{border:1px solid black;width:175px; display:inline-block}
/*no height given, to give it room to adjust for long texts*/
tested it with long text and short text and it appears to be working.
Note: 100% heights are required in the container `div's and body to make the page full height.
here's the codepen: http://tinyurl.com/klzknog
There are three ways for vertical align: flexbox, display: table-cell and position.
HTML
<div class="container one">
<p>I'm a text</p>
</div>
<div class="container two">
<p>I'm a text</p>
</div>
<div class="container three">
<p>I'm a text</p>
</div>
CSS (Sass)
.container {
width: 200px;
height: 200px;
border: 1px solid tomato;
float: left;
margin-right:1em;
}
.one {
display: table;
p {
display:table-cell;
vertical-align: middle;
}
}
.two {
display: flex;
align-items: center;
}
.three {
position: relative;
p {
position: absolute;
top: 50%;
transform: translateY(-50%);
margin: 0;
}
}
Demo
Add the following style to the parent class
.subject{
text-align: center;
display: table-cell;
vertical-align: middle;
}
this would align the span both horizontally and vertically