【问题标题】:How to stick image to border of div?如何将图像粘贴到div的边框?
【发布时间】:2015-06-22 00:02:16
【问题描述】:

我正在为此苦苦挣扎,因为将图像定位到 div 的顶部非常容易,但在 div 的底部进行定位并不是那么容易。如您所见,该矩形是一个顶部带有图像框架的 div。

我想在该 div 的底部放置另一个框架,以使其看起来更好,但我只能将其放置在 div 内,但不能超过边框。

查看此图片以了解我的意思:

HTML

<header>

    <div id="banner">
    <img src="img/darkframe.png">
    <img src="img/darkframe2.png">


    </div>

</header>

CSS

 * {
    margin: 0;
    padding: 0;
}
html {
    overflow-y: scroll;
}
body {
    background-image: url("/img/poe-wallpaper-5.jpg");
    font-size: 13px;
    color: #938063;
    font-family: Fontin-Bold;
    background-repeat: no-repeat;
    /*background-position: right top;*/
    background-attachment: scroll;
    background-position: center top;
    background-color: black;
}
ol, ul {
    list-style: none;
    margin: 0;
}
ul li {
    margin: 0;
    padding: 0;
}
h1 {
    margin-bottom: 10px;
    color: #111111;
}
a, img {
    outline: none;
    border: none;
    color: #000;
    font-weight: bold;
    text-transform: uppercase;
}
p {
    margin: 0 0 10px;
    line-height: 1.4em;
    font-size: 1.2em;
}
img {
    display: block;
    margin-bottom: 10px;
}
aside {
    font-style: italic;
    font-size: 0.9em;
}
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section, {
display: block;
}

/* Structure */
#wrapper {
    width: 96%;
    max-width: 920px;
    margin: auto;
    padding: 5%;
}

#main {
    padding-top: 10px;
    width: 100%;
    margin-right: 5%;
    float: left;
}

aside {
    width: 35%;
    float: right;
}

/* Logo H1
 header h1 {
 height: 70px;
 width: 160px;
 float: left;
 display: block;
 background: url(../images/demo.gif) 0 0 no-repeat;
 text-indent: -9999px;
 }*/

#skipTo {
    display: none;
}
#skipTo li {
    background: #b1fffc;
}

/* Banner */
#banner {

    float: none;
    margin:90px 0 100px 0;
    width: 100%;
    border: 1px solid #938063;
    background-color: #1a1b18;
    position: relative;
    padding-top: 25px;
    padding-bottom: 85px;
    border-radius: 10px;


}   

#banner h1 {
    font-family: 'fontinbold';
    font-weight: bold;
    text-align: center;
    font-size: 350%;
    color: #938063;
    margin-left: 10px;
    margin-right: 10px;
    margin-top: 30px;
}
#banner h2 {
    font-family: 'fontinbold';
    font-weight: bold;
    text-align: center;
    font-size: 350%;
    color: #938063;
    margin-left: 10px;
    margin-right: 10px;
}
#banner h3 {
    font-family: 'fontinbold';
    font-weight: bold;
    text-align: center;
    font-size: 350%;
    color: #938063;
    margin-left: 10px;
    margin-right: 10px;
}
#banner img[src="img/darkframe.png"] {
    position: absolute;
    max-width: 100%;
    max-height: 100%;
    top: -47px;
    height: 80px;
    width: 1000px;
}
#banner img[src="img/darkframe2.png"] {
    position: absolute;
    height: auto;
    width: 100%;
    bottom:
}

/* Media Queries */
@media screen and (max-width: 480px) {

    #skipTo {
        display: block;
    }

    header nav, #main, aside {
        float: left;
        clear: left;
        margin: 0 0 10px;
        width: 100%;
    }
    header nav li {
        margin: 0;
        background: #efefef;
        display: block;
        margin-bottom: 3px;
    }
    header nav a {
        display: block;
        padding: 10px;
        text-align: center;
    }
}

#talk2 {
    float: none;
    width: 100%;
    margin: 0 auto;
    border: 1px solid #938063;
    background-color: #1a1b18;
    position: relative;
    margin-bottom: 50px;
    border-radius: 10px;
}

#talk2 img[src="img/darkframe.png"] {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    top: -77px;
    height: 80px;
    width: 100%;
    padding-top: 25px;
}

#talk2 img[src="img/exastashfull.jpg"] {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    height: 250px;
    width: 600px;
    margin-left: auto;
    margin-right: auto;
    padding-top: 100px;
}
#talk2 img[src="img/iknow.png"] {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    height: 80px;
    width: 220px;
    margin-left: auto;
    margin-right: auto;
}
#talk2 img[src="img/sowhat.png"] {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    height: 80px;
    width: 220px;
    margin-left: auto;
    margin-right: auto;
    padding-top: 80px;
}
#talk2 img[src="img/then.png"] {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    height: 80px;
    width: 220px;
    margin-left: auto;
    margin-right: auto;
    padding-top: 80px;
}

#talk2 img[src="img/getnow.png"] {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    height: 100px;
    width: 500px;
    margin-left: auto;
    margin-right: auto;
    padding-top: 80px;
}
#talk2 img[src="img/buynowred.png"] {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    height: 120px;
    width: 250px;
    margin-left: auto;
    margin-right: auto;
    padding-top: 40px;
}

#talk2 p {

    font-family: 'fontinbold';
    text-align: center;
    font-size: 22px;
    color: #938063;
    margin-left: 30px;
    margin-right: 30px;
    margin-top: 40px;
    margin-bottom: 30px;
}

#formdiv {
    width: 100%;
    margin 0: auto;
    text-align: center;
}

#stash img {
    position: relative;
    max-width: 100%;
    max-height: 100%;
}

【问题讨论】:

  • 将 top:100% 放在横幅 div 上没有任何作用
  • 检查我的答案是不是你想要的。

标签: html css


【解决方案1】:

只需将position:absolute;bottom:0; 设置为img

还将position:relative; 设置为img 容器。

#banner {
    float: none;
    margin:90px 0 100px 0;
    width: 100%;
    border: 1px solid #938063;
    background-color: #1a1b18;
    position: relative;
    padding-top: 25px;
    padding-bottom: 85px;
    border-radius: 10px;
}
#banner img {
    width: 100%;
    height: auto;
    position:absolute;
    bottom:0;
}

演示http://jsfiddle.net/a_incarnati/fua75hpv/3/

还可以查看这个其他演示,也许更清楚:http://jsfiddle.net/a_incarnati/fua75hpv/4/

快速编辑您的代码,其中我删除了应用于每个 img 的 margin:10px,然后删除了应用于每个图像的所有其他属性,并使用 bottom:0 绝对定位它们。

演示 2: https://jsfiddle.net/a_incarnati/v575uvyb/3/

DEMO 3 https://jsfiddle.net/a_incarnati/v575uvyb/6/

【讨论】:

  • 你的演示是我需要的,但我仍然得到这个结果:s7.postimg.org/5gljdq9vf/frames3.png
  • 您能否发布更多代码,例如更多 html 以了解应用了哪些样式...我注意到在您的演示中有一些语法错误和一些非封闭括号,我将它们修复在我的演示。
  • 我已经在我的初始帖子上发布了整个 CSS 表
  • 好的,看看下一个演示 :)
  • 好的,现在我把金色边框改了,看起来更好了,而且还粘在了底部。你能准确地告诉我你改变了什么我可以学习吗?另外,我想让框架伸出 div,但只在侧面,像这样:s21.postimg.org/oyhsdiixz/frames4.png你知道这是否可能吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 2018-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多