Problem vertically aligning text of an element that spans two lines - html

I want to align some text in the middle of my element using CSS. This is my markup:
<div id="testimonial">
<span class="quote">Some random text that spans two lines</span>
</div>
And the relevant CSS:
#testimonial {
background: url('images/testimonial.png') no-repeat;
width: 898px;
height: 138px;
margin: 0 auto;
margin-top: 10px;
text-align: center;
padding: 0px 30px 0px 30px;
}
.quote {
font-size: 32px;
font-family: "Times New Roman", Verdanna, Arial, sans-serif;
vertical-align: middle;
font-style: italic;
color: #676767;
text-shadow: 1px 1px #e7e7e7;
}
Usually to get .quote in the vertical middle of #testimonial, I'd do:
.quote { line-height: 138px; }
But this breaks the layout because the text in .quote spans more than one line.
As you can see I've tried doing vertical-align: middle; and that doesn't work either.
Any help is appreciated. Cheers.

I recently found out that vertical centering of something which has undefined dimensions goes very well with vertical-align: middle; in combination with line-height: 0;.
Check out this demonstration fiddle.
HTML:
<div id="testimonial">
<span><span class="quote">Some random text<br />that spans two lines</span></span>
</div>
CSS:
#testimonial {
background: #333 url('images/testimonial.png') no-repeat;
width: 898px;
height: 138px;
margin: 0 auto;
margin-top: 10px;
text-align: center;
padding: 0 30px 0 30px;
line-height: 138px;
}
#testimonial>span {
display: inline-block;
line-height: 0;
vertical-align: middle;
}
.quote {
font-size: 32px;
font-family: "Times New Roman", Verdanna, Arial, sans-serif;
font-style: italic;
color: #676767;
text-shadow: 1px 1px #e7e7e7;
line-height: 32px;
}

You can do it simpler with single span
http://jsfiddle.net/7ebLd/
#testimonial {
height: 138px;
line-height: 138px;
}
span {
display: inline-block;
line-height: 19px;
vertical-align: middle;
}

as nobody's answered it with the table cell solution yet..
here you go - with an an IE6/7 solution too (though it involves a yukky HTML hack) as #thirtydot says in comments, table display properties are not supported by IE7 and below -
if you don't want/like the extra HTML element, you could just give IE7 and below some top padding on the .quote - so that while it wouldn't be vertically centered accurately it may be an acceptable fall back
CSS:
#testimonial {
background: #eee url('images/testimonial.png') no-repeat;
width: 898px;
height: 138px;
margin: 10px auto 0;
padding: 0 30px;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.quote {
font-size: 32px;
font-family: "Times New Roman", Verdana, Arial, sans-serif;
font-style: italic;
color: #676767;
text-shadow: 1px 1px #e7e7e7;
}
IE CSS:
<!--[if lte IE 7]>
<style type="text/css" media="screen">
#testimonial i {
display: inline-block;
height: 100%;
vertical-align: middle;
}
.quote {
display: inline-block;
width: 100%;
vertical-align: middle;
}
</style>
<![endif]-->
HTML:
<div id="testimonial">
<i></i>
<span class="quote">Some random text <br> that spans two lines</span>
</div>

This website offers a plethora of options for vertical centering with css.
If you can set a height on .quote, I think Method 2 would work in this situation:
.quote {
position:absolute;
top:50%;
height:240px;
margin-top:-120px; /* negative half of the height */
}
Another option is to use display:table-cell; vertical-align:middle; method in CSS, but this option does not work in IE, so you'll have to also set an IE-specific version.

This site:
http://www.emblematiq.com/blog/vertical_align_with_css/
With the result:
http://www.emblematiq.com/blog/vertical_align_with_css/assets/03.html
Uses the following markup/css to center vertically multiple lines using the display: table-cell, vertically-align: middle method:
<div id="wrapper">
<img src="0.gif" alt="stuff" id="fixed" />
<div id="floating"><div><div>
<p>Middle aligned text goes right here and it does wrap nicely at the end of the line</p>
</div></div></div>
</div>
p {
margin:0;
padding:0;
}
#wrapper {
width:550px;
border:1px solid #666;
padding:10px;
height:300px;
}
#fixed {
float:right;
width:200px;
height:300px;
background:#666;
display:block;
}
#wrapper>#floating { /*display:table for Mozilla & Opera*/
display:table;
position:static;
}
#floating { /*for IE*/
width:300px;
height:100%;
background:#EAEAEA;
position:relative;
}
#floating div { /*for IE*/
position:absolute;
top:50%;
}
#floating>div { /*for Mozilla and Opera*/
display:table-cell;
vertical-align:middle;
position:static;
}
#floating div div {
position:relative;
top:-50%;
}

Related

Trying to modify my CSS Logo with two lines of text in a circle

I'm trying to fix my website main logo. The name is Cerebro Design, and I would like to put Cerebro up and Design down, exactly like this:
This is the CSS code I have so far:
<div style="margin:auto;
width:500px;
height:500px;
border-radius:250px;
font-size:50px;
color:#fff;
line-height:500px;
text-align:center;
background:#000;">
CEREBRO DESIGN
</div>
#logo{
margin:auto;
width:500px;
height:500px;
border-radius:250px;
font-size:80px;
color:#fff;
text-align:center;
background:#000;
}
#logo-text{
margin-left:70px;
padding-top: 160px;
max-width:30px;
}
<div id="logo">
<div id="logo-text">CEREBRO DESIGN.</div>
</div>
Then you just add the correct font and you should be good to go.
As a note, is better to use external or internal (<style>...</style>) css than using style="..." on an element.
i would use an image for the logo, but if you want to go css way, this could work for you.
Demo
div {
margin:auto;
width:500px;
height:500px;
border-radius:250px;
font-size:50px;
color:#fff;
line-height:500px;
text-align:center;
background:#000;
display: inline-block;
position: relative;
overflow: hidden;
}
div:before, div:after{
display:inline-block;
position: absolute;
color: white;
font-size: 95px;
width:500px;
height:500px;
text-align: center;
left: 0;
}
div:before{
content: 'CEREBRO';
top: -10%;
}
div:after{
content: 'DESIGN.';
top: 10%;
}
Can you share the font you are using? My quick aproach (I'm in a small laptop) would be something like this:
HTML:
<div class="cerebro-logo">
<span class="padding">Cerebro
<span class="design-text">Design .</span>
</span>
</div>
CSS:
.cerebro-logo {
margin:auto;
width:500px;
height:500px;
border-radius:250px;
font-size:70px;
font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
font-weight: 600;
color:#fff;
line-height:80px;
letter-spacing: 10px;
text-align:left;
background:#000;
text-transform: uppercase;
}
.padding {
padding-top: 165px;
padding-left: 50px;
float: left;
}
.design-text {
letter-spacing: 13px;
}
Tip: You can use letter-spacing (for example) to get that spacing effect on the "Design .".
JSFIDDLE link: http://jsfiddle.net/f5qrbbx5/
Just add your text, no need to calculate any margin or padding.
.circle {
width: 500px;
height: 500px;
margin: 20px auto;
background: #000;
border-radius: 50%;
color: #fff;
font-family: "Trebuchet MS", Helvetica, sans-serif;
font-size: 94px;
text-align: center;
white-space: nowrap;
}
.circle:before {
content: '';
display: inline-block;
height: 100%;
margin-right: -0.25em;
vertical-align: middle;
}
.circle > * {
display: inline-block;
max-width: 80%;
text-align: left;
vertical-align: middle;
white-space: normal;
}
<div class="circle">
<span>CEREBRO<br>DESIGN.</span>
</div>

Bottom alignment

I have two DIVs, one is left floated with bigger font size and the other one is right floated with smaller fonts. I want to align the smaller fonted DIV to the bottom aligned with the bigger sized text. Not able to achieve it.
and the css
.floatLeft {
float: left;
}
.floatRight {
float: right;
}
.font12 {
font-size: 12px;
}
<div class="floatLeft">Activity</div>
<div class="floatRight font12 ">View timeline / Filter activities</div>
Answer changed to allow for float as per your request.
Please see FIDDLE.
HTML
<div class="big">Activity</div>
<div class="small">
<a href="javascript:void(0);">View timeline / Filter activities
</a>
</div>
CSS
.big {
float: left;
font-size: 2em
}
.small {
float: right;
position: relative;
font-size: 1em;
top: 13px;
}
Demo page
CSS needed:
.big {
display:inline-block;
vertical-align:bottom;
/* just for demo: */
font-size: 2em;
height:200px;
background:#FADDFF;
}
.small {
display:inline-block;
vertical-align:bottom;
/* just for demo: */
font-size: 1em;
background:#D2E9F7;
height:120px;
}
so making the two elements inline-block displayed, and vertical-align with value of bottom, they will be on the same level, and they're line of reference will be the bottom of them both
Additional to the above response, you can use display: table-cell to fit both div's to the same height:
.floatLeft {
border: 1px solid black;
font-size: 30px;
width: 200px;
display: table-cell;
vertical-align: text-bottom;
}
.floatRight {
border: 1px solid black;
font-size: 12px;
display: table-cell;
vertical-align: text-bottom;
width: 200px;
}
<div class="floatLeft">Activity</div>
<div class="floatRight font12 ">View timeline / Filter activities</div>
Here is the jsfiddle: https://jsfiddle.net/x3m72kqh/
JSFiddle: Solution in JSFiddle
Here is a solution with inline-block :
.big {
display:inline-block;
width: 50%;
float: left;
font-size: 2em
}
.small {
display:inline-block;
width: 50%;
font-size: 1em
}
a {
float:right;
}

text-align:center doesn't work

I have website http://11klassniki.ru and I try to put text in in the middle using text-align:center but it doesn't work.
#konkurs_ege {
position:absolute;
top:10px;
left:380px;
width:140px;
height:80px;
background-image:url('http://11klassniki.ru/banners/konkurs_ege.jpg');
}
#konkurs_ege a {
text-decoration: none;
text-shadow: -1px -1px 0 #000000;
text-transform: uppercase;
font: 16px Arial, sans-serif;
font-weight:700;
width:100%;
text-align:center;
vertical-align: middle;
}
Here is code
<div id="konkurs_ege">
<a href='http://11klassniki.ru/view_post.php?id=144'>Konkurs!<br>how I made<br>IT</a>
</div>
I would like to have text: "Konkurs! how I made IT" in the middle of box (width:140px;
height:80px).
You need to have display: block.
.secondArticle a {
display: block;
text-align: center;
}
If you do not want to center the paragraph below the image, you can use span tag.
.secondArticle a span {
display: block;
text-align: center;
}
I believe the issue is that you're applying the text-center to the a tag, instead of the container. If you add it to the container ( konjurs_ege ), it should work. Worked for me on Chrome and FF.
#konkurs_ege {
position: absolute;
top: 10px;
left: 380px;
width: 140px;
height: 80px;
text-align: center;
background-image: url('http://11klassniki.ru/banners/konkurs_ege.jpg');
}

Create a horizontal rule with text in the middle? [duplicate]

This question already has answers here:
CSS technique for a horizontal line with words in the middle
(34 answers)
Closed 3 years ago.
I would like to have a divider on my page that looks like this:
What is the best way to do that?
html
<h3><span>My latest work</span></h3>
css
h3 {
position:relative;
text-align:center;}
h3 span {
display:inline-block;
padding:0 10px;
background:#fff;
}
h3:before {
content:"";
display:block;
position:absolute;
z-index:-1;
left:0;
right:0;
top:50%;
height:1px;
background:#ccc;
}
We can do this without images or masking lines like so:
HTML
<div class="rule">
<div class="line"><div></div></div>
<div class="words">words are cool</div>
<div class="line"><div></div></div>
</div>
CSS
.rule {
display: table;
}
.rule>div {
display: table-cell;
white-space:nowrap;
}
.line>div {
border-bottom: 1px solid silver;
height: 1px;
}
.words {
padding: 0 5px;
}
.line {
width: 50%;
vertical-align: middle;
}
Demo: http://jsfiddle.net/5tqE5/1/
This uses attr() which is not supported in older browsers. It could be replaced with an extra element.
<div class="lines" data-text="Some Text Goes Here"></div>
.lines {
position: relative;
font-size: 20px;
font-family: sans-serif;
margin: 0 auto;
border-top: 1px solid silver;
margin-top: 20px;
}
.lines:before{
content: attr(data-text);
background-color: #fff;
position: absolute;
text-align: center;
left: 50%;
width: 220px;
margin-left: -110px;
padding: 10px;
top: -20px;
}
Dunno about the 'best' - you've given no terms by which to assess that. Smallest, fastest, most compatible, etc, etc.
Anyhoo, I just took a 1-pixel wide slice of your image and saved it. I then use it as the background-image of the div.
CSS:
#myDiv
{
background: url(horizline1x41px.png) repeat;
text-align: center;
line-height: 41px;
}
#myDiv span
{
padding-left: 16px;
padding-right: 16px;
background: white;
font-weight: bold;
font-size: 1.5em;
}
HTML:
<div id='myDiv'><span>OUR LATEST WORK</span></div>
Demo: http://jsfiddle.net/8zve4/
I don't like the extra markup, but this should work.
CSS:
.hline {
border: 1px solid #EEE;
color: #666;
font-family: helvetica;
font-weight: bold;
font-variant: small-caps;
letter-spacing: .1em;
line-height: 0px;
text-align: center;
text-transform: uppercase;
}
.hline > span {
background-color: #FFF;
padding: 0px 1em;
}
HTML:
<div class="hline"><span>Our latest work</span></div>

Simple css positioning (I think)

I've been meaning to replace the tables in my site with css positioning and have been trying to teach myself through tutorials etc. I've had some early success but it all came crashing down when I tried to create a sidebar. I'm hoping the problem has some kind of simple solution. The relative/absolute positioning of the elements is not going anywhere close to what I wanted to do. My goal is to have a sidebar with images that stack (float?) from top to bottom, with the middle elements being part of an unordered list. I got it to work once but now that stack on top of each other. It has to be the way I am setting the float and the absolute/relative positioning. After reading some articles here I tried adding a div wrapper to put them inside but I think I got myself even more confused. Is it possible someone could nudge me in the right direction? Here is the code:
CSS
body
{
background: #b6b7bc;
font-size: .80em;
font-family: "Helvetica Neue", "Lucida Grande", "Segoe UI", Arial, Helvetica, Verdana, sans-serif;
margin: 50px;
padding: 0px;
color: #696969;
height: 160px;
}
a:link, a:visited
{
color: #034af3;
}
a:hover
{
color: #1d60ff;
text-decoration: none;
}
a:active
{
color: #034af3;
}
p
{
margin-bottom: 10px;
line-height: 1.6em;
}
/* HEADINGS ----------------------------------------------------------*/
h1, h2, h3, h4, h5, h6
{
font-size: 1.5em;
color: #666666;
font-variant: small-caps;
text-transform: none;
font-weight: 200;
margin-bottom: 0px;
}
h1
{
font-size: 1.6em;
padding-bottom: 0px;
margin-bottom: 0px;
}
h2
{
font-size: 1.5em;
font-weight: 600;
}
h3
{
font-size: 1.2em;
}
h4
{
font-size: 1.1em;
}
h5, h6
{
font-size: 1em;
}
/* PRIMARY LAYOUT ELEMENTS ---------------------------------------------------------*/
.page
{
width: 960px;
background-color: #fff;
margin: 20px auto 0px auto;
border: 1px solid #496077;
}
.header
{
position: relative;
margin: 0px;
padding: 0px;
background: #4b6c9e;
width: 100%;
top: 0px;
left: 0px;
}
.header h1
{
font-weight: 700;
margin: 0px;
padding: 0px 0px 0px 20px;
color: #f9f9f9;
border: none;
line-height: 2em;
font-size: 2em;
}
.main
{
padding: 0px 12px;
margin: 0px 4px 4px 4px;
min-height: 420px;
width: 500px;
float: left;
}
.leftCol
{
padding: 6px 0px;
margin: 12px 8px 8px 8px;
width: 200px;
min-height: 200px;
}
.footer
{
color: #4e5766;
padding: 8px 0px 0px 0px;
margin: 0px auto;
text-align: center;
line-height: normal;
}
/* MISC ----------------------------------------------------------*/
.clear
{
clear: both;
width: 936px;
height: 35px;
}
.title
{
display: block;
float: left;
text-align: justify;
}
.bold
{
font-weight: bold;
}
p.clear
{
clear: both;
height: 0;
margin: 0;
padding: 0;
}
#wrapper
{
position:relative;
height: 500px;
width: 900px;
}
#insidemain
{
position:absolute;
float: left;
width: 500px;
height 180px;
}
/* ---------------- Sidebar Items ---------------------*/
#sidebar /* Sidebar container */
{
position:absolute;
border-top: 1px solid #99CC33;
border-left: 1px solid #99CC33;
height: 300px;
width: 180px;
margin-right: 5px;
padding: 5px 0 0 5px;
}
#sidebarHeader
{
position:absolute;
height: 37px;
width: 172px;
float: left;
background-image: url(../img/TopMenu.jpg);
background-repeat:no-repeat;
}
#sidebarItems ul
{
position:absolute;
height: 27px;
width: 172px;
float:left;
background-image: url(../img/MenuItems.jpg);
background-repeat:no-repeat;
/*left: 6px;
top: 45px;*/
background-position: 0px -27px;
}
#sidebarFooter
{
position:absolute;
height: 46px;
width: 172px;
float:left;
background-image: url(../img/BottomMenu.jpg);
background-repeat:no-repeat;
}
And the HTML:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
<link href="Styles/Simple.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="page">
<div class="header">header
<div class="title">
<h1>
Test Page
</h1>
</div>
</div>
<p class = "clear">clear</p>
<div id="wrapper">
<div id="sidebar">
<div id="sidebarHeader">
</div>
<div id="sidebarItems">
<ul>
<li>test item</li>
</ul>
</div>
<div id="sidebarFooter">
</div>
</div>
</div>
<div id="insidemain">
main
</div>
</div>
<div class="clear">clear</div>
<div class="footer">
<a href="http://www.google.com/">
Blah blah test to see how far this will go across the page blah blha lorem ipsum and various other stuff that is meaningless etc
</a>
</div>
</body>
</html>
Typically (for non-responsive sites especially), you'd have your .wrapper div around the entire content (header, content, sidebar, footer, etc). Then set your .wrappers width. Your .sidebar would have a set width and it would either float: left; or float: right; depending on the side you want it on. Set your .content div's width which would be less than or equal to your .wrapper width - your .sidebar width. Then add your .clearfix below so the .footer falls beneath everything. In most cases (at least for the large page chunks) you can avoid position:absolute; which helps make things more easily fall into place.
You really shouldn't have to float your div's or list. Those are block elements by default and will stack vertically regardless.
Also, as Scrimothy mentioned, you do not want absolutely positioned elements as that will take the element out of the page flow. In other words, they no longer take up "real" space in the page, and instead render at whatever coordinates you position them.
Similarly, floats also take up no space, except with other floated elements. That's why some UI developers will float almost every element on the page and "clear" them using a footer or at key breaks in the page. I personally don't recommend positioning in that fashion, but to each his own.
See if this quick tutorial helps you with some key positioning concepts: HERE
Don't target the same element with both float and position:absolute. It doesn't make much sense. Anywhere where you have float, you should get rid of position:absolute
Next, get rid of those silly class="clear" elements. Instead, target .footer with clear:both and .page with overflow-y:hidden;