2

How to make text over the image/button using html/css.

Eg: I have a button(Login Button). I want the text "Login" to be placed over that button. The text should be center of the image/button.

Madhan
  • 81
  • 1
  • 2
  • 7
  • You can have a div with background image and content as Login which can be aligned in center position. – sasi May 19 '15 at 11:24
  • Duplicate of this? http://stackoverflow.com/questions/8708945/how-to-position-text-over-an-image-in-css – mplungjan May 19 '15 at 11:26

8 Answers8

5

You can simply set the background-image property of the relevant button (or other element if you are using in place of a button).

Note that you can control height/vertical centering of the text using line-height, however, if the text is multi-line this may have other issues, so you may wish to resort to using padding, or a sub element with top:50%;tranform:translateY(-50%);

If using a semantically valid button element, text is center aligned by default, otherwise simply set text-align:center

button {
  line-height: 50px;
  width: 300px;
}
button.imgButton {
  color: white;
  background-image: url(http://abdpbt.com/tech/wp-content/uploads/2009/09/gradient.jpg);
  background-size:cover; /* <-- scale the image to cover the button */
  background-position:center center; /* <-- center the image in the button */
}
<button>Normal Button</button>

<button class='imgButton'>Image Button</button>
SW4
  • 69,876
  • 20
  • 132
  • 137
  • much cleaner than my approach :) – jbutler483 May 19 '15 at 11:27
  • @SW4 OP needs to align the text on button, I don't think there will be solution required for multiline. then button element doesn't make any sense. – Kheema Pandey May 19 '15 at 11:37
  • @KheemaPandey: There are many reasons why text on a button may overlap, which would be affected by `line-height` (as SW4 has mentioned) – jbutler483 May 19 '15 at 11:39
  • @jbutler483 you didn't understand what i am saying..Does Button have really a multiline text.... Atleast what i understood button should be user focused because they push User to perform any action.. so short text is better option. – Kheema Pandey May 19 '15 at 11:43
  • @KheemaPandey: yes, button elements do support multilined text? Are you possibly mixing up with ``? There are many cases where text might want to be wrapped. consider a 'narrow' button: like [this](http://jsfiddle.net/jbutler483/hsjfe4ze/), for example – jbutler483 May 19 '15 at 11:45
  • I know button support multiline text, but for end user to things clear and perform a better `call to action` button text should be short. – Kheema Pandey May 19 '15 at 11:47
  • @jbutler483 In past we all faced same situation when we use a background image for button and with long text its break..I am not against the solution.. just want to share to improved the overall user experience the text should be short and effective.. – Kheema Pandey May 19 '15 at 11:51
  • 1
    I guess I am going on now off-topic.. excuse me if I am making a nonsense arguments. – Kheema Pandey May 19 '15 at 11:51
  • 1
    @KheemaPandey, note that for semantic purposes, `button` should be used where possible for a button – SW4 May 19 '15 at 12:13
2

Try this,

HTML Code:

<input type="submit" value="Login" class="myButton"/>

CSS:

.myButton {
    background-color:#44c767;
    border-radius:28px;
    border:1px solid #18ab29;
    display:inline-block;
    cursor:pointer;
    color:#ffffff;
    font-family:Arial;
    font-size:17px;
    padding:16px 31px;
    text-decoration:none;
    text-shadow:0px 1px 0px #2f6627;
}
.myButton:hover {
    background-color:#5cbf2a;
}
.myButton:active {
    position:relative;
    top:1px;
}
Alex
  • 8,461
  • 6
  • 37
  • 49
Mihir
  • 572
  • 1
  • 6
  • 24
0

Centering in front of an image

If you need to center text over an image, you might want to wrap your img in a div, use positioning on the img itself as well as the text.

You would then be able to center this using transforms (supporting multi-lined text).

demo

.wrap {
  position: relative;
  display: inline-block;
  height: 300px;
  width: 300px;
}
img {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
}
p {
  position: absolute;
  top: 50%;
  left: 50%;
  display: inline-block;
  transform: translate(-50%, -50%);
  font-size: 20px;
  color: tomato;
  font-weight: bold;
}
<div class="wrap">
  <img src="http://lorempixel.com/300/300" />
  <p>some text</p>
</div>

Images with buttons

Since button elements are centered automatically by default, you're issue would be to add an image to the button.

This can be achieved using the background-image property.

So in your CSS use something like:

background-image:url(http://lorempixel.com/200/200);

adjusting the size using

background-size:100% 100%;

to stretch it to the full size of the button.

jbutler483
  • 24,074
  • 9
  • 92
  • 145
0

Encapsulate the text between the button tags. In CSS, select the button and do something like this

#button {
    text-align: center;
}
Arthelon
  • 36
  • 1
  • 5
0

Here you can find your code

take a look at bootstrap framework as well

http://jsfiddle.net/Lw2nhc26/

<div class="button">
    Button
</div>

.button {
    padding:10px;
    background-color:#000000;
    width 100%;
    color:#FFFFFF;
    text-align: center;
    border-radius:5px;
}
Stefano Vuerich
  • 996
  • 1
  • 7
  • 28
0

Use this code. It will take centre alignment by default.

<button type="button">This is Button</button>
jbutler483
  • 24,074
  • 9
  • 92
  • 145
iLion
  • 106
  • 1
  • 12
0

As I mentioned in my comment, you can have a div with background image.

HTML

<div>
    Login
</div>

CSS

div{
    background-image:url("http://www.noao.edu/image_gallery/images/d4/androa.jpg");
    width: 200px;
    height: 100px;
    color: white;
    text-align: center;
    line-height: 100px;
    font-weight: bold;
}

Fiddle

sasi
  • 512
  • 4
  • 27
-1

How about using a table? You can find an example here: https://www.w3schools.com/html/html_tables.asp

Otherwise it might look something like this:

<table class="form-group">
    <tr>
        <th><h6><label for="Machine Name" class="col col-form-label">Machine Name</label></h6></th>
    </tr>
    <tr class="col-12 col-md-2">
        <td><asp:TextBox ID="txtMachineName" ClientIDMode="Static" CssClass="form-control" MaxLength="256" runat="server" required="required"></asp:TextBox></td>
    </tr>

    <tr>
        <th><h6><label for="serialNumber" class="col col-form-label">Serial Number</label></h6></th>
    </tr>
    <tr>
        <td><asp:TextBox ID="serialNumber" ClientIDMode="Static" CssClass="form-control" MaxLength="256" runat="server" required="required"></asp:TextBox></td>
    </tr>
</table>
Brian Sunbury
  • 45
  • 2
  • 9