Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make button width fit to the text

Tags:

html

css

While I was fiddling with this 'Fancy 3D Button' example, I found that the width seemed to be hard-coded to fit the text's width.

Here is the HTML / CSS:

body {    background-image: url(http://subtlepatterns.com/patterns/ricepaper.png)  }  a {    position: relative;    color: rgba(255, 255, 255, 1);    text-decoration: none;    background-color: rgba(219, 87, 5, 1);    font-family: 'Yanone Kaffeesatz';    font-weight: 700;    font-size: 3em;    display: block;    padding: 4px;    -webkit-border-radius: 8px;    -moz-border-radius: 8px;    border-radius: 8px;    -webkit-box-shadow: 0px 9px 0px rgba(219, 31, 5, 1), 0px 9px 25px rgba(0, 0, 0, .7);    -moz-box-shadow: 0px 9px 0px rgba(219, 31, 5, 1), 0px 9px 25px rgba(0, 0, 0, .7);    box-shadow: 0px 9px 0px rgba(219, 31, 5, 1), 0px 9px 25px rgba(0, 0, 0, .7);    margin: 100px auto;    width: 160px;    text-align: center;    -webkit-transition: all .1s ease;    -moz-transition: all .1s ease;    -ms-transition: all .1s ease;    -o-transition: all .1s ease;    transition: all .1s ease;  }  a:active {    -webkit-box-shadow: 0px 3px 0px rgba(219, 31, 5, 1), 0px 3px 6px rgba(0, 0, 0, .9);    -moz-box-shadow: 0px 3px 0px rgba(219, 31, 5, 1), 0px 3px 6px rgba(0, 0, 0, .9);    box-shadow: 0px 3px 0px rgba(219, 31, 5, 1), 0px 3px 6px rgba(0, 0, 0, .9);    position: relative;    top: 6px;  }
<link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:700' rel='stylesheet' type='text/css'>    <a href="javascript:void(0);">Push me!</a>

Normal look

If I remove the width property, the button would fill the page width.

Without the width property

Is there any way to make the button's width fit to the text, automatically?

like image 503
chenaren Avatar asked Dec 31 '14 15:12

chenaren


People also ask

How do I make text fit a button in CSS?

Remove the width and display: block and then add display: inline-block to the button. To have it remain centered you can either add text-align: center; on the body or do the same on a newly created container.

How do I change the size of the button text?

To change the font size of a button, use the font-size property.

How do I make a button full size?

display: block and width: 100% is the correct way to go full width but you need to remove the left/right margin on the button as it pushes it outside the containing element. for more information on the box-sizing property, read the MDN docs.


2 Answers

Remove the width and display: block and then add display: inline-block to the button. To have it remain centered you can either add text-align: center; on the body or do the same on a newly created container.

The advantage of this approach (as opossed to centering with auto margins) is that the button will remain centered regardless of how much text it has.

Example: http://cssdeck.com/labs/2u4kf6dv

like image 87
Natsu Avatar answered Sep 30 '22 23:09

Natsu


If you are developing to a modern browser. https://caniuse.com/#search=fit%20content

You can use:

width: fit-content; 
like image 33
Roger Cruz Avatar answered Oct 01 '22 00:10

Roger Cruz