tags:

views:

120

answers:

4

Hi,
I have this HTML

<div class="someClass">
   <ul>
      <li><a href="index.html">1</a></li>
      <li><a href="index.html">2</a></li>
      <li><a href="index.html">3</a></li>
      <li><a href="index.html">4</a></li>
      <li><a href="index.html">5</a></li>
   </ul>
</div>

Problem:

I want to make the <a>s to shape like a square, with height = 20px and width = 20px. I can make it have the height and width if I make it float: left. In there the problem occur cause I need the <ul> to be centered but it won't because of the float:left of the <a>.

how can I make it centered with fix height and width of the anchors?

note: anchors don't have fixed length... the sample is just 5, but it can also grow to 7, or 9. I also need the links to be inline in view.

+3  A: 

a is an inline element out of the box, which typically cannot have width and height applied to it.

If you forcibly set display: block in its style declaration, it'll behave like a block level element, and you can set its width and height.

David Hedlund
ahh thanks for the quick reply. I have done your suggestion but it has to be displayed inline... any other way I can make it display inline? I'm really stuck...
Reigel
`display: inline-block;` - but take care, it works in this case because a is naturally inline, but for elements that are naturally block elements inline-block doesn't work for IE7: http://www.quirksmode.org/css/display.html#t03
ANeves
+3  A: 

This will make your li's 20x20 and align them to center:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <style type="text/css">
    .someClass ul {line-height:20px;text-align:center;width:105px;margin:0 auto;overflow:hidden;}
    .someClass ul li {display:block;width:20px;height:20px;float:left;background:blue;margin-right:1px;}
    .someClass ul li a {color:#fff;}
    </style>
</head>
<body>
    <div class="someClass">
       <ul>
          <li><a href="index.html">1</a></li>
          <li><a href="index.html">2</a></li>
          <li><a href="index.html">3</a></li>
          <li><a href="index.html">4</a></li>
          <li><a href="index.html">5</a></li>
       </ul>
    </div>
</body>
</html>

This is another approach - it sets everything inline with a 20px line height and can have any number of items:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <style type="text/css">
    .someClass ul {line-height:20px;text-align:center;}
    .someClass ul li {display:inline;}
    .someClass ul li a {display:inline}
    </style>
</head>
<body>
    <div class="someClass">
       <ul>
          <li><a href="index.html">1</a></li>
          <li><a href="index.html">2</a></li>
          <li><a href="index.html">3</a></li>
          <li><a href="index.html">4</a></li>
          <li><a href="index.html">5</a></li>
       </ul>
    </div>
</body>
</html>
easwee
+1, seems to work as intended.
ANeves
+3  A: 

Use display:inline-block :

.someClass ul
{
  margin: 0;
  padding: 0;
  list-style-type: none;
  text-align: center;
}

.someClass ul li { display: inline; }

.someClass ul li a
{
  display: inline-block;
  text-decoration: none;
  width: 20px;
  height: 20px;
}
Voyta
+1: perfect! But, repeating the comment on that other answer: pay attention that inline-block doesn't work on IE7 for elements that are naturally block elements: http://www.quirksmode.org/css/display.html#t03
ANeves
+1  A: 

Ok, done here is your answer

this is your markup

<div class="someClass">
   <ul>
      <li><a href="index.html">1</a></li>
      <li><a href="index.html">2</a></li>
      <li><a href="index.html">3</a></li>
      <li><a href="index.html">4</a></li>
      <li><a href="index.html">5</a></li>
   </ul>
</div>

Your CSS must be something like this

.someclass { 
    width:100%;
    overflow:hidden;
}
.someclass ul {
    position:relative;
    float:left;
    left:50%;
    list-style:none;
}

.someclass ul li {
    position:relative;
    float:left;
    right:50%;
}

.someclass ul li a {
     display:block;
     height:100px;
     width:100px;
     border: 1px #f00 solid;
}

This trick is completely flexible does not depend on how big your <UL> is

Starx
-1, this doesn't even answer the first line of the question: `I want to make the <a>s to shape like a square, with height = 20px and width = 20px.`
ANeves
ok updated my answer
Starx
+0: Could be improved, but seems to work as intended - removed -1.
ANeves
wow! this is really great! thanks for the help.
Reigel