1

In the website I am creating I need a button which has an image (a cross) as background. I use a file *.css for all the graphics of the website, so I'd like to write the button's style in this file.

HTML code

<html>
    <head>
        <title>Prova</title>
        <link rel="stylesheet" href="grafica/style.css" type="text/css">
    </head>

    <body>
        <button id="cookieButton">
    </body>
</html>

CSS

#cookieButton
{
    background-image:url('croce2.png');
    background-repeat: no-repeat;
    background-position: center center;
    width:30px;
    height:30px;
}

this doesn't work, but if I write the same CSS code inline (as in the following code) it works

<html>
    <head>
        <title>Prova</title>
        <link rel="stylesheet" href="grafica/style.css" type="text/css">
    </head>

    <body>
        <button style="background-image:url('croce2.png'); background-repeat: no-repeat;
        background-position: center center; width:30px; height:30px">
    </body>
</html>

This is the result in the first case and in the second case

result

Why doesn't it work in the first case?

3
  • You need close sign in center in the button? Commented Apr 29, 2017 at 7:06
  • 1
    give full path background-image:url('/xx/yy.png''); Commented Apr 29, 2017 at 7:19
  • 1
    Thanks @rubin, I've already solved :) Commented Apr 29, 2017 at 7:24

3 Answers 3

2

You had the problem with the image path because your CSS file was in different directory ("grafica").

So when you tried to load the image in CSS, it couldn't find it, but in HTML (when you used inline style) it had no problems, because the HTML file and image file are in the same place.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, but there was a copy of the image in both directories
1

For me, your first code is right. Check my https://jsfiddle.net/10o8zjej/ !

background-image:url('https://upload.wikimedia.org/wikipedia/commons/thumb/7/78/Chocolat.png/220px-Chocolat.png');
background-repeat: no-repeat;
background-position: center center;
width:30px;
height:30px;

First, are you sure the path to your .css is right ? Or the path to your croce2.png is also right ?

1 Comment

Yes, I am sure.
1

HTML (You didn't Put button tag Close)

<html>
    <head>
        <title>Prova</title>
        <link rel="stylesheet" href="grafica/style.css" type="text/css">
    </head>

    <body>
    <button id="cookieButton"></button>
    </body>
</html>

Replace CSS

#cookieButton
{
    background-image:url('http://shrugs.com/img/close_delete.png');
    background-repeat: no-repeat;
    background-position: center center;
    width:30px;
    height:30px;
    background-color:none;
    outline:none;
    border:none;
}

1 Comment

Ok thanks a lot, now it works. The problem wasn't the button tag, but the image path in the CSS file (I had to write the complete path even if the image is in the same directory of the other files). I still wonder why it doesn't work if I write only the image name (they're in the same directory, it should work)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.