How can I make my Button Divs all have the same width?
I'm trying to find the answer to a problem I have. What I have is many buttons and I want them to appear down the page and all be the same width. I have the following code and CSS. However they don't appear to be the full width. What I want ideally is them to be a width equal to the enclosing DIV. I also want the text to appear centered which it does now. Can anyone tell me why the buttons all appear different widths?
I have the following code:
<div style="width: 200px;">
<div class = "abc" >
<a class = "btn" href="/test" title="Classes">Test</a>
</div>
<div class = "abc" >
<a class = "btn" href="/test" title="Classes">Test sadfadsfdsafsadfsa</a>
</div>
<div class = "abc" >
<a class = "btn" href="/test" title="Classes">Test afds</a>
</div>
<div class = "abc" >
<a class = "btn" href="/test" title="Classes">Test asdfdsafadsfdsfasdfsadfdsf</a>
</div>
</div>
and the following CSS:
.abc {
float: left;
height: 25px;
padding-top: 4px;
text-align: center;
width: 190px;
}
a.btn {
border: 1px solid;
border-radius: 3px 3px 3px 3px;
cursor: default;
font-size: 12px;
padding: 3px 4px;
text-dec开发者_Go百科oration: none;
width: 180px;
}
I created a fiddle
fiddle
If you want to specify the width of an a
tag you need to set it to display: block
remove the class="abc" div, they are not needed.
Give the .btn class a display: inline-block;
(or just block
, but I dislike it).
Link (a) elements are inherently inline elements, which ignore width declarations.
By applying a display:block
to a.btn, it will accept your width declaration.
You can only specify width for block-level elements. To correct it, you can add a display:block;
or display:inline-block;
to your a.btn class
Add display:block;
to a.btn
.
you need to set a.btn display:block; since, by default, a are inline elements.
精彩评论