positioning an image inside a link with css

1.8k views Asked by At

I am having problems positioning an image wrapped in a link.

here is the html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Albums</title
        <link rel="stylesheet" type="text/css" href="../../css/BackButton.css" />
        <link rel="stylesheet" type="text/css" href="MotusStyles.css" />
    </head>
    <body>
        <article>
            <div id="container">

                <div id="albumSky" class="albums">
                    <img src="../Images_Albums/PortadaMotus.jpg">
                </div>

                <div id="songs">

                    <p>01 song</p>

                    <p>02 song</p>

                    <p>03 song</p>

                    <p>04 song</p>

                    <p>05 song</p>

                    <p>06 song</p>

                    <p>07 song</p>

                    <p>08 song</p>

                    <p>09 song</p>

                    <p>10 song</p>

        </div>

                <div id="order">
                    <a href="https://itunes.apple.com/gb/album/motus-et-bouche-cousue/id595359048?app=music&ign-mpt=uo%3D4"><img id="icon" src="apple.svg"></a>
                </div>

            </div>

            <div id="credits">
                <p>someone, voc, piano / someone, bss</p>
            </div>

        </article>

        <div id="info">
            <a href="../index.html"><p class="backContact">[back]</p></a>
        </div>
    </body>
</html>

css

@font-face {font-family: QuaestorSans;
          src:  url("../fonts/QuaestorSans-Rg.otf") format("opentype"),
                url("../fonts/QuaestorSans.ttf") format("opentype");
}

html, body {
    height: 100%;
    margin:0;
    padding:0;
}

body {
    background: url(../Images_Albums/FondBack.jpg) no-repeat;
    background-size: 100%;
/*  background-color: red;*/
}

article {
    position: absolute;
    width: 32%;
    height: 100%;
    left: 50%;
    transform: translate(-50%, 0);
    background: url(../Images_Albums/FondFront.jpg) no-repeat;
    background-size: 100%;
}

#container {
    margin-top: 9%;
    position: absolute;
    width: 50%;
    left: 50%;
    transform: translate(-50%, 0);
  background-color: green;

}

img {
    width: 100%;
    display: block;
    margin-bottom: 15%;
}


a {
    text-decoration: none;
}

#songs {
    position: relative;
    height: 185px;
    overflow: scroll;
    overflow-x:hidden;
    width:80%;
    left: 50%;
    transform: translate(-50%, 0);
}


#songs p {
  margin: 0;
  margin-bottom: 7%;
  color: white;
  font-family: QuaestorSans;
  font-size: 1em;
  line-height: 1.3;
  width: 100%;
}


/* SCROLLBAR */

#songs::-webkit-scrollbar {
    width: 7px;
}


/* Track */
#songs::-webkit-scrollbar-track {
    background: #8fdcf6;
    border: 3px solid transparent;
    background-clip: content-box;
}


/* Handle */
#songs::-webkit-scrollbar-thumb {
    background: #c7edfa;
    border-radius:10px;
}


#songs::-webkit-scrollbar-thumb:hover{
    background-color:#c7edfa;
    border:1px solid #333333;
}


#songs::-webkit-scrollbar-thumb:active{
    background-color:#c7edfa;
    border:1px solid #333333;
}


#order {
    margin-top: 20%;
    position: relative;
    font-family: QuaestorSans;
    font-size: 1.2em;
    text-align: center;
    width: 100%;
    height: 50px;
    background-color: red;
}

#icon {
    width: 60%;
}

/*#order a {
  display:block;
  left: 50%;
  overflow:hidden;
  background: url(apple.svg) no-repeat;
  background-color: red;
  background-position: center;
  width:132px;
  height:48px;
  background-size:contain;

}*/


#credits  {
    margin-top: 135%;
    width: 65%;
    position: absolute;
    font-family: QuaestorSans;
    font-size: 1em;
    line-height: 1.3;
    margin-bottom: 1%;
    left: 50%;
    transform: translate(-50%, 0);
}


#credits  p{
    margin: 0;
    line-height: 1.5;
    color: #6ec7e9;
}


.backContact {
    color: #639cad;
}


.backContact:hover {

    color: #c7ecff;
}

That doesn't work. The image refuses to be placed in the center and just stays positioned to the left (see image: i have set background colours to make sure the element spans over the 100% of the width of the parent element).

I have also tried:

#order a {left:50%;
transform: translate(-50%, 0);
}

Image:

enter image description here

I want the iTunes logo in the middle.

7

There are 7 answers

0
Pravin Vavadiya On BEST ANSWER

Just Remove below your css rules.

img{display:block;}

@font-face {font-family: QuaestorSans;
          src:  url("../fonts/QuaestorSans-Rg.otf") format("opentype"),
                url("../fonts/QuaestorSans.ttf") format("opentype");
}

html, body {
    height: 100%;
    margin:0;
    padding:0;
}

body {
    background: url(../Images_Albums/FondBack.jpg) no-repeat;
    background-size: 100%;
/*  background-color: red;*/
}

article {
    position: absolute;
    width: 32%;
    height: 100%;
    left: 50%;
    transform: translate(-50%, 0);
    background: url(../Images_Albums/FondFront.jpg) no-repeat;
    background-size: 100%;
}

#container {
    margin-top: 9%;
    position: absolute;
    width: 50%;
    left: 50%;
    transform: translate(-50%, 0);
  background-color: green;

}

img {
    width: 100%;
    margin-bottom: 15%;
}


a {
    text-decoration: none;
}

#songs {
    position: relative;
    height: 185px;
    overflow: scroll;
    overflow-x:hidden;
    width:80%;
    left: 50%;
    transform: translate(-50%, 0);
}


#songs p {
  margin: 0;
  margin-bottom: 7%;
  color: white;
  font-family: QuaestorSans;
  font-size: 1em;
  line-height: 1.3;
  width: 100%;
}


/* SCROLLBAR */

#songs::-webkit-scrollbar {
    width: 7px;
}


/* Track */
#songs::-webkit-scrollbar-track {
    background: #8fdcf6;
    border: 3px solid transparent;
    background-clip: content-box;
}


/* Handle */
#songs::-webkit-scrollbar-thumb {
    background: #c7edfa;
    border-radius:10px;
}


#songs::-webkit-scrollbar-thumb:hover{
    background-color:#c7edfa;
    border:1px solid #333333;
}


#songs::-webkit-scrollbar-thumb:active{
    background-color:#c7edfa;
    border:1px solid #333333;
}


#order {
    margin-top: 20%;
    position: relative;
    font-family: QuaestorSans;
    font-size: 1.2em;
    text-align: center;
    width: 100%;
    height: 50px;
    background-color: red;
}

#icon {
    width: 60%;
}

/*#order a {
  display:block;
  left: 50%;
  overflow:hidden;
  background: url(apple.svg) no-repeat;
  background-color: red;
  background-position: center;
  width:132px;
  height:48px;
  background-size:contain;

}*/


#credits  {
    margin-top: 135%;
    width: 65%;
    position: absolute;
    font-family: QuaestorSans;
    font-size: 1em;
    line-height: 1.3;
    margin-bottom: 1%;
    left: 50%;
    transform: translate(-50%, 0);
}


#credits  p{
    margin: 0;
    line-height: 1.5;
    color: #6ec7e9;
}


.backContact {
    color: #639cad;
}


.backContact:hover {

    color: #c7ecff;
}

#order img {
  margin: 0 auto;
}
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Albums</title
        <link rel="stylesheet" type="text/css" href="../../css/BackButton.css" />
        <link rel="stylesheet" type="text/css" href="MotusStyles.css" />
    </head>
    <body>
        <article>
            <div id="container">

                <div id="albumSky" class="albums">
                    <img src="../Images_Albums/PortadaMotus.jpg">
                </div>

                <div id="songs">

                    <p>01 song</p>

                    <p>02 song</p>

                    <p>03 song</p>

                    <p>04 song</p>

                    <p>05 song</p>

                    <p>06 song</p>

                    <p>07 song</p>

                    <p>08 song</p>

                    <p>09 song</p>

                    <p>10 song</p>

        </div>

                <div id="order">
                    <a href="https://itunes.apple.com/gb/album/motus-et-bouche-cousue/id595359048?app=music&ign-mpt=uo%3D4"><img id="icon" src="http://placehold.it/100x40"></a>
                </div>

            </div>

            <div id="credits">
                <p>someone, voc, piano / someone, bss</p>
            </div>

        </article>

        <div id="info">
            <a href="../index.html"><p class="backContact">[back]</p></a>
        </div>
    </body>
</html>

0
David Jones On

You can change this CSS:

#icon {
    width: 60%;
}

to this:

#icon {
  margin: 0 auto;
    width: 60%;
}

See this fiddle for the working code. The icon doesn't appear because its a relative filename but if you inspect it you can see that it is centered within the containing element.

0
Lalji Tadhani On

try to align center

#order{
   text-align:center;
}
0
Saurav Rastogi On

Try using margin: 0 auto, like:

#order img {
  margin: 0 auto;
}

Have a look at the snippet below (use full screen):

@font-face {font-family: QuaestorSans;
          src:  url("../fonts/QuaestorSans-Rg.otf") format("opentype"),
                url("../fonts/QuaestorSans.ttf") format("opentype");
}

html, body {
    height: 100%;
    margin:0;
    padding:0;
}

body {
    background: url(../Images_Albums/FondBack.jpg) no-repeat;
    background-size: 100%;
/*  background-color: red;*/
}

article {
    position: absolute;
    width: 32%;
    height: 100%;
    left: 50%;
    transform: translate(-50%, 0);
    background: url(../Images_Albums/FondFront.jpg) no-repeat;
    background-size: 100%;
}

#container {
    margin-top: 9%;
    position: absolute;
    width: 50%;
    left: 50%;
    transform: translate(-50%, 0);
  background-color: green;

}

img {
    width: 100%;
    display: block;
    margin-bottom: 15%;
}


a {
    text-decoration: none;
}

#songs {
    position: relative;
    height: 185px;
    overflow: scroll;
    overflow-x:hidden;
    width:80%;
    left: 50%;
    transform: translate(-50%, 0);
}


#songs p {
  margin: 0;
  margin-bottom: 7%;
  color: white;
  font-family: QuaestorSans;
  font-size: 1em;
  line-height: 1.3;
  width: 100%;
}


/* SCROLLBAR */

#songs::-webkit-scrollbar {
    width: 7px;
}


/* Track */
#songs::-webkit-scrollbar-track {
    background: #8fdcf6;
    border: 3px solid transparent;
    background-clip: content-box;
}


/* Handle */
#songs::-webkit-scrollbar-thumb {
    background: #c7edfa;
    border-radius:10px;
}


#songs::-webkit-scrollbar-thumb:hover{
    background-color:#c7edfa;
    border:1px solid #333333;
}


#songs::-webkit-scrollbar-thumb:active{
    background-color:#c7edfa;
    border:1px solid #333333;
}


#order {
    margin-top: 20%;
    position: relative;
    font-family: QuaestorSans;
    font-size: 1.2em;
    text-align: center;
    width: 100%;
    height: 50px;
    background-color: red;
}

#icon {
    width: 60%;
}

/*#order a {
  display:block;
  left: 50%;
  overflow:hidden;
  background: url(apple.svg) no-repeat;
  background-color: red;
  background-position: center;
  width:132px;
  height:48px;
  background-size:contain;

}*/


#credits  {
    margin-top: 135%;
    width: 65%;
    position: absolute;
    font-family: QuaestorSans;
    font-size: 1em;
    line-height: 1.3;
    margin-bottom: 1%;
    left: 50%;
    transform: translate(-50%, 0);
}


#credits  p{
    margin: 0;
    line-height: 1.5;
    color: #6ec7e9;
}


.backContact {
    color: #639cad;
}


.backContact:hover {

    color: #c7ecff;
}

#order img {
  margin: 0 auto;
}
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Albums</title
        <link rel="stylesheet" type="text/css" href="../../css/BackButton.css" />
        <link rel="stylesheet" type="text/css" href="MotusStyles.css" />
    </head>
    <body>
        <article>
            <div id="container">

                <div id="albumSky" class="albums">
                    <img src="../Images_Albums/PortadaMotus.jpg">
                </div>

                <div id="songs">

                    <p>01 song</p>

                    <p>02 song</p>

                    <p>03 song</p>

                    <p>04 song</p>

                    <p>05 song</p>

                    <p>06 song</p>

                    <p>07 song</p>

                    <p>08 song</p>

                    <p>09 song</p>

                    <p>10 song</p>

        </div>

                <div id="order">
                    <a href="https://itunes.apple.com/gb/album/motus-et-bouche-cousue/id595359048?app=music&ign-mpt=uo%3D4"><img id="icon" src="http://www.cseashawaii.org/wp-content/uploads/2016/02/listen-on-apple-music.png"></a>
                </div>

            </div>

            <div id="credits">
                <p>someone, voc, piano / someone, bss</p>
            </div>

        </article>

        <div id="info">
            <a href="../index.html"><p class="backContact">[back]</p></a>
        </div>
    </body>
</html>

Hope this helps!

0
Mohit Yadav On

use this:CSS

#icon{
 max-width:60%;
 margin:auto;
}
0
Jeroen On

Remove this:

img { 
   display:block;
}
0
Sawan mishra On

Please you this code for display center image

@font-face {font-family: QuaestorSans;
          src:  url("../fonts/QuaestorSans-Rg.otf") format("opentype"),
                url("../fonts/QuaestorSans.ttf") format("opentype");
}

html, body {
    height: 100%;
    margin:0;
    padding:0;
}

body {
    background: url(../Images_Albums/FondBack.jpg) no-repeat;
    background-size: 100%;
/*  background-color: red;*/
}

article {
    position: absolute;
    width: 32%;
    height: 100%;
    left: 50%;
    transform: translate(-50%, 0);
    background: url(../Images_Albums/FondFront.jpg) no-repeat;
    background-size: 100%;
}

#container {
    margin-top: 9%;
    position: absolute;
    width: 50%;
    left: 50%;
    transform: translate(-50%, 0);
  background-color: green;

}

img {
    width: 100%;
    display: block;
    margin-bottom: 15%;
}


a {
    text-decoration: none;
}

#songs {
    position: relative;
    height: 185px;
    overflow: scroll;
    overflow-x:hidden;
    width:80%;
    left: 50%;
    transform: translate(-50%, 0);
}


#songs p {
  margin: 0;
  margin-bottom: 7%;
  color: white;
  font-family: QuaestorSans;
  font-size: 1em;
  line-height: 1.3;
  width: 100%;
}


/* SCROLLBAR */

#songs::-webkit-scrollbar {
    width: 7px;
}


/* Track */
#songs::-webkit-scrollbar-track {
    background: #8fdcf6;
    border: 3px solid transparent;
    background-clip: content-box;
}


/* Handle */
#songs::-webkit-scrollbar-thumb {
    background: #c7edfa;
    border-radius:10px;
}


#songs::-webkit-scrollbar-thumb:hover{
    background-color:#c7edfa;
    border:1px solid #333333;
}


#songs::-webkit-scrollbar-thumb:active{
    background-color:#c7edfa;
    border:1px solid #333333;
}


#order {
    margin-top: 20%;
    position: relative;
    font-family: QuaestorSans;
    font-size: 1.2em;
    text-align: center;
    width: 100%;
    height: 50px;
    background-color: red;
}

#icon {
       width: 50%;
    margin: auto;
    padding: 10px;
}

#credits  {
    margin-top: 135%;
    width: 65%;
    position: absolute;
    font-family: QuaestorSans;
    font-size: 1em;
    line-height: 1.3;
    margin-bottom: 1%;
    left: 50%;
    transform: translate(-50%, 0);
}


#credits  p{
    margin: 0;
    line-height: 1.5;
    color: #6ec7e9;
}


.backContact {
    color: #639cad;
}


.backContact:hover {

    color: #c7ecff;
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Albums</title
        <link rel="stylesheet" type="text/css" href="../../css/BackButton.css" />
        <link rel="stylesheet" type="text/css" href="MotusStyles.css" />
    </head>
    <body>
        <article>
            <div id="container">

                <div id="albumSky" class="albums">
                    <img src="../Images_Albums/PortadaMotus.jpg">
                </div>

                <div id="songs">

                    <p>01 song</p>

                    <p>02 song</p>

                    <p>03 song</p>

                    <p>04 song</p>

                    <p>05 song</p>

                    <p>06 song</p>

                    <p>07 song</p>

                    <p>08 song</p>

                    <p>09 song</p>

                    <p>10 song</p>

        </div>

                <div id="order">
                    <a href="https://itunes.apple.com/gb/album/motus-et-bouche-cousue/id595359048?app=music&ign-mpt=uo%3D4"><img id="icon" src="apple.svg"></a>
                </div>
<img id="icon" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Apple_logo_black.svg/2000px-Apple_logo_black.svg.png">

            <div id="credits">
                <p>someone, voc, piano / someone, bss</p>
            </div>

        </article>

        <div id="info">
            <a href="../index.html"><p class="backContact">[back]</p></a>
        </div>
    </body>
</html>