使用css将图像定位在链接中

时间:2016-12-23 13:04:09

标签: html css hyperlink positioning css-transforms

我在定位包含在链接中的图像时遇到问题。

这是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;
}

这不起作用。图像拒绝放置在中心,只是保持在左侧(参见图像:我设置了背景颜色,以确保元素跨越父元素宽度的100%)。

我也尝试过:

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

图像:

enter image description here

我想在中间放置iTunes徽标。

7 个答案:

答案 0 :(得分:1)

只需在您的css规则下方删除。

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>

答案 1 :(得分:0)

您可以更改此CSS:

#icon {
    width: 60%;
}

到此:

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

有关工作代码,请参阅此fiddle。图标不会显示,因为它是相对文件名,但如果您检查它,您可以看到它在包含元素中居中。

答案 2 :(得分:0)

尝试对齐center

#order{
   text-align:center;
}

答案 3 :(得分:0)

尝试使用margin: 0 auto,例如:

#order img {
  margin: 0 auto;
}

查看下面的代码段(使用全屏):

@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>

希望这有帮助!

答案 4 :(得分:0)

使用:CSS

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

答案 5 :(得分:0)

删除它:

img { 
   display:block;
}

答案 6 :(得分:0)

请将此代码用于显示中心图像

&#13;
&#13;
@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;
}
&#13;
<!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>
&#13;
&#13;
&#13;