首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >css:在父级中具有错误大小的绝对定位div

css:在父级中具有错误大小的绝对定位div
EN

Stack Overflow用户
提问于 2021-01-09 21:18:46
回答 1查看 64关注 0票数 2

我试图在css网格中放置一个图像(实际上是雪碧-工作表)。

为了能够css-动画雪碧-工作表,我必须把它的位置定义为‘绝对’。我在一个非常简单的代码中复制了我的问题:

首先,我有一个sprite-sheet组件,它包含sprite-sheet和compo映像组件,它们应该模拟另一个持有这个sprite-sheet的组件。

雪碧-薄荷.组件.:

代码语言:javascript
复制
import { Component } from '@angular/core';

    @Component({
      selector: 'sprite-sheet',
      styleUrls: ['./sprite-sheet.component.scss'],
      templateUrl: './sprite-sheet.component.html',
    })
    export class SpriteSheetComponent {
      constructor() {
      }
    }

sprite-sheet.component.html

代码语言:javascript
复制
<div></div>

sprite-sheet.component.css

代码语言:javascript
复制
:host {
  height: 100%;
  width: 100%;
}

div
{
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0px;
  left: 0px;
  background-repeat: no-repeat !important;
  background-position-x: 0%;
  background-image: url('/assets/icon/favicon.png')
}

component-with-image.ts

代码语言:javascript
复制
import { Component } from "@angular/core";

@Component({
    selector: 'compo-image',
    templateUrl: `./component-with-image.html`,
    styleUrls: ['./component-with-image.css']
})
export class ComponentWithImage {
}

component-with-image.html

component-with-image.css

代码语言:javascript
复制
div {
    height: 100%;
    width: 100%;
}

然后,我尝试在我的应用程序中显示这个图像:

component-with-image.html

代码语言:javascript
复制
<div>
  some-text
</div>
<div> <!-- should be a css grid -->
  <compo-image></compo-image>
</div>

app.component.css

代码语言:javascript
复制
div {
    width: 100px;
    height: 100px;
}

compo-image {
    width: 100%;
    height: 100%;
}

但这就是我得到的:

酷,这是因为我的绝对定位图像相对于根组件,因为我没有为其他组件指定位置。

因此,我添加了一个名为sprite-sheet 2的包装组件,使其具有“相对”位置,并承载真正的sprite-sheet组件。

sprite-sheet.component2.ts

代码语言:javascript
复制
import { Component } from '@angular/core';

@Component({
  selector: 'sprite-sheet2',
  styleUrls: ['./sprite-sheet.component2.scss'],
  templateUrl: './sprite-sheet.component2.html',
})
export class SpriteSheetComponent2 {
  constructor() {
  }
}

sprite-sheet.component2.ts

代码语言:javascript
复制
:host {
    position: relative;
    height: 100%;
    width: 100%;
}

sprite-sheet {
    height: 100%;
    width: 100%;
}

sprite-sheet2.component2.html

代码语言:javascript
复制
<sprite-sheet></sprite-sheet>

但我明白了:

雪碧-sheet2 2上面的所有东西都定义了宽度和高度,而sprite-sheet2 2和它包含的所有东西都有0x0大小,尽管这些组件的宽度和高度= 100%。

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-09 22:13:27

角组件host没有默认的显示display:block,因此您需要自己指定它。

代码语言:javascript
复制
:host {
    display: block; // or anything else
    position: relative;
    height: 100%;
    width: 100%;
}

但是可以在angular.json中设置ng g c XXXX命令生成的新组件的默认显示。

代码语言:javascript
复制
"schematics": {
    "@schematics/angular:component": {
      "displayBlock": true
   }
 }

相关问题:

Angular component default style css display block

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65647714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档