That's the default behavior of the browser on absolute
or fixed
positioning. Although it's still inside the container div
, the width: 60%
on .title-image
no longer affects it as it's unable to find any parent element with its position set to relative
or absolute
.
这是浏览器在绝对定位或固定定位时的默认行为。尽管它仍然在容器div中,但.title-Image上的宽度:60%不再影响它,因为它找不到任何位置设置为相对或绝对的父元素。
Remember that these values will be relative to the next parent element with relative (or absolute) positioning. If there is no such parent, it will default all the way back up to the element itself meaning it will be placed relative to the page itself.
The trade-off (and most important thing to remember) about absolute
positioning is that these elements are removed from the flow of
elements on the page. An element with this type of positioning is not
affected by other elements and it doesn’t affect other elements. This
is a serious thing to consider every time you use absolute
positioning. Its overuse or improper use can limit the flexibility of
your site.
https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/#absolute
If what you want is to position the .title-image
relative to it's parent div
you must set its (the parent div
) position to relative
or absolute
.
如果您想要的是相对于其父div定位.title-Image,则必须将其(父div)位置设置为相对或绝对。
Example: https://jsfiddle.net/o9wjrcd7/
示例:https://jsfiddle.net/o9wjrcd7/
Hope that helps!
希望这能有所帮助!
just add
只需添加
Position:relative;
职位:相对;
to the parent of title-img.
you are following the course full-stack course of angilia.
TITLE-IMG的父级。你正在沿着安吉莉亚的路线全程前进。
I was also having the same doubt
我也有同样的怀疑
I had the same problem, and I wasted like a day trying to solve that issue on my own.
This problem is caused because of different version of bootstrap.
我也有同样的问题,我浪费了一天的时间试图独自解决这个问题。此问题是由不同版本的引导程序引起的。
position: absolute; changes the size of the image depending on the size of its nearest ancestor, and its parent should have a position other than static.
位置:绝对;根据其最近祖先的大小更改图像的大小,并且其父图像的位置应该不是静态的。
You need to set the position of its parent to relative in order to solve this problem.
要解决此问题,需要将其父对象的位置设置为相对。
<div class="title-image-div col-lg-6">
<img class="title-image" src="https://img.favpng.com/18/8/0/iphone-6s-plus-iphone-6-plus-apple-iphone-6s-png-favpng-2qagf951EWF4xaNNGb9WBPdNZ.jpg" alt="iphone-mockup">
</div>
<style>
.title-image-div {
position: relative;
}
</style>
An element with position: absolute;
is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).
具有位置的元素:绝对的;相对于最近定位的祖先定位(而不是相对于视口定位,如固定)。
However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.
I think you could try to position it relative and also reduce the rotation.
但是,如果绝对定位的元素没有定位的上级元素,它将使用文档正文,并随页面滚动一起移动。我想你可以试着把它放在相对的位置,同时减少旋转。
<div class="col-lg-6">
<img class="title-image" src="https://img.favpng.com/18/8/0/iphone-6s-plus-iphone-6-plus-apple-iphone-6s-png-favpng-2qagf951EWF4xaNNGb9WBPdNZ.jpg" alt="iphone-mockup">
</div>
try adding inline style to the parent of .title-image (col-lg-6) so it looks like this:
尝试将内联样式添加到.ITLE-IMAGE(ol-lg-6)的父级,使其如下所示:
<div class="col-lg-6" style="position: relative;">
<img class="title-image" src="https://img.favpng.com/18/8/0/iphone-6s-plus-iphone-6-plus-apple-iphone-6s-png-favpng-2qagf951EWF4xaNNGb9WBPdNZ.jpg" alt="iphone-mockup">
</div>
The reason for this is, angela used Bootsrap 4 in her course which give col-lg-6 default position's value of relative. meanwhile, in Bootstrap 5 col-lg-6 has default position's value of static
这样做的原因是,Angela在她的课程中使用了Bootsrap 4,这给出了ol-LG-6的默认位置值Relative。同时,在Bootstrap 5中,ol-lg-6的默认位置值为Static
CMIIW
CMIIW
/* Title Images */
.title-image-con {
position: relative;
}
.title-image {
width: 60%;
transform: rotate(25deg);
position: absolute;
}
/* Features */
#features {
padding: 7% 15%;
background-color: white;
position: relative;
z-index: 1;
}
<div class="title-image-con col-lg-6">
<img class="title-image rotate" src="images/iphone6.png" alt="iphone-
mockup">
</div>
Adding another class name to the element and make the position relative on css works for me.
向元素添加另一个类名,并使位置相对于css,对我来说是可行的。
css snippet
CSS代码片断
更多回答
Thanks for your help!
谢谢你的帮助!
Please give more information, try answering the questions made on the topic: Can the absolute position cause an image to get bigger than it originally was?
, would the sizing be thrown out even though the image was contained in a div?
, What am I doing wrong?
, don't simply write a code, give some explanations and answers :)
请给出更多的信息,尝试回答围绕这个主题提出的问题:绝对位置会导致图像变得比原来更大吗?即使图像包含在div中,大小会被抛出吗?我做错了什么?,不要简单地写代码,给出一些解释和答案:)
我是一名优秀的程序员,十分优秀!