Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7929965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:15:47+00:00 2026-06-03T20:15:47+00:00

I have a div with position:relative; contained in a div with fixed width. Inside

  • 0

I have a div with position:relative; contained in a div with fixed width. Inside it (div with position relative) there is some text. The problem is that I need to center it in vertical and in some cases the text is composed by one line and in others by two. My css code is this:

.rollover{
position: relative;
width: auto;
background-color: #000;
opacity: 0.5;
height: 40px;
overflow: hidden;
padding-left: 10px;
}

.rollover a{
font-family: lucida;
font-size: 10px;
color: #FFF;
display: block;
line-height: 11px;
float: left;
text-decoration: none;
}

and my html code is:

<div class="rollover">
    <a href="">ONE LINE TEXT</a>
</div>

or

<div class="rollover">
    <a href="">FIRST LINE<br/>SECOND LINE</a>
</div>

How can I do?

Thanks, Mattia

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:15:49+00:00Added an answer on June 3, 2026 at 8:15 pm

    Having recently faced the problem, the solution I came up with (if you have to do it in a case where you can’t use display:table-cell) is to add a little markup and to use the line-height property.

    line-height alone on the outer div works fine when there is only one line. When you face something that could be multi line, the solution is to use two line height.

    The markup used is the following:

    <div class="rollover">
        <span>
             <a href="">ONE LINE TEXT</a>
        </span>
    </div>
    <div class="rollover">
        <span>
             <a href="">FIRST LINE<br/>SECOND LINE</a>
        </span>
    </div>​
    

    Notice the added <span>.

    The trick is that the span will be a line with a 40px line-height (inherited from the rollover div). And it will have the float property as well. Because with a floated element, you can’t use vertical-align. Inside that span, we’ll have our link with a line height of 11px and a vertical align-middle. That is the middle of the span. And there it is.

    The css needed is the following:

    .rollover{
        position: relative;
        width: auto;
        background-color: #000;
        opacity: 0.5;
        height: 40px;
        overflow: hidden;
        padding-left: 10px;
        line-height:40px;
    }
    
    .rollover span{
        float: left;
    }
    .rollover a{
        font-family: lucida;
        font-size: 10px;
        color: #FFF;
        line-height: 11px;
        text-decoration: none;
        vertical-align:middle;
        display:inline-block;    
    }
    

    Note that I changed the display of a from block to inline-block, otherwise, no vertical-align.

    And a little fiddle to show it: http://jsfiddle.net/r5RFx/ (I added some borders to see the blocks on the page)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a container div with the following CSS: #container { position:relative; overflow:hidden; width:200px;
I have a problem about div position relative alignment. I want the second div
I have a div which position is fixed, within that i have a div
I have a div which is position:fixed so that it it always in the
There is an inner div of fixed width and variable height contained by an
i have a container div which is position:relative and the squares are position:absolute because
So currently I have: #div { position: relative; height: 510px; overflow-y: scroll; } However
I have the following CSS rules applied to a DIV: .icon { position:relative; display:
I have a div whose position has been fixed. Everything is fine till the
Lets say I have a div #elements with a ul inside.The div has position:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.