我正面临着一个问题,在我的网页上的英雄部分的比例.HERO部分包括一个背景图像和一个使用Shape-Default类创建的自定义形状(一个大三角形/左箭头).目标是使背景图像和形状在调整浏览器窗口大小时按比例zoom .但正如你在下面看到的那样,这并不会发生.

enter image description here

随着浏览器窗口大小的调整,形状开始出血或在其自身与背景图像之间产生间隙.我已try 调整背景大小和剪辑路径属性,但问题仍然存在.

这是它的代码https://codepen.io/boostai/pen/yLZEgro

HTML

<section class="uk-section uk-padding-remove background-hero" style="background-image: url(https://picsum.photos/1600/900)">
<div class="uk-flex uk-flex-right@l">
    <div class="shape-default uk-height-large@l uk-height-unset uk-background-purple uk-flex uk-flex-top uk-flex-middle@l">
        <div class="uk-margin-auto-left uk-width-4-5@s uk-padding-small-top uk-padding-remove-top@l">

            <h1 class="uk-text-white uk-text-boldest uk-margin-small-top uk-width-4-5@l">
                Lorem Ipsum is simply dummy text of the printing and typesetting </h1>

            <div class="uk-text-white uk-text-bold uk-width-4-5@l">
                <p>It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            </div>

        </div>
    </div>
</div>
</section>

CSS

.background-position-hero {
    background-size: 63% auto;
}

.background-hero {
    background-size: 50%;
    background-repeat: no-repeat;
}

/* MOBILE */
@media screen and (max-width: 960) {
    .background-hero {
        background-image: none;
    }
}

/* ----------- Retina Screens ----------- */
@media screen and (min-device-width: 1200px) and (max-device-width: 1600px) and (-webkit-min-device-pixel-ratio: 2) and (min-resolution: 192dpi) {
    .background-hero {
        background-size: auto 100%;
        background-repeat: no-repeat;
    }
}

/* Hero
   ========================================================================== */
.homepage-hero {
    background-size: 60vw auto;
    background-repeat: no-repeat;
    background-position: left center;
    padding: 0;
}

.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none; /* This makes the overlay and its content not block user interactions */
}

@media screen and (max-width: 960px) {
    .homepage-hero {
        height: auto;
    }

    .homepage-hero a {
        position: relative;
    }

    .overlay {
        top: 18%;
        left: 25%;
        height: auto;
    }
}

.shape-default {
    width: 60vw;
    /*  min-height: 450px;*/
    max-width: 100%;
    position: relative;
    overflow: hidden;
    clip-path: polygon(15% 0%, 100% 0%, 100% 100%, 15% 100%, 5% 50%);
    z-index: 1;
    background-color: #59195d;
}

@media screen and (max-width: 960px) {
    .shape-default {
        width: 100%;
        border-radius: unset;
        min-height: 0;
        clip-path: none;
        padding: 0 15px;
    }

    .shape-default h1 {
        font-size: 1.6rem;
    }
}

/* Weight modifier
  ========================================================================== */
.uk-text-boldest {
    font-weight: 900;
}

/* Color modifiers
  ========================================================================== */
.uk-text-white {
    color: #fff !important;
}

/* Small
 ========================================================================== */

.uk-padding-small-top {
    padding-top: 15px;
}

/* Remove
 ========================================================================== */
/* Phone landscape and bigger */
@media (min-width: 640px) {
    .uk-padding-remove-top\@s {
        padding-top: 0 !important;
    }
    .uk-padding-remove-bottom\@s {
        padding-bottom: 0 !important;
    }
}

/* Tablet landscape and bigger */
@media (min-width: 960px) {
    .uk-padding-remove-top\@m {
        padding-top: 0 !important;
    }
    .uk-padding-remove-bottom\@m {
        padding-bottom: 0 !important;
    }
}
/* Desktop and bigger */
@media (min-width: 1200px) {
    .uk-padding-remove-top\@l {
        padding-top: 0 !important;
    }
    .uk-padding-remove-bottom\@l {
        padding-bottom: 0 !important;
    }
}
/* Large screen and bigger */
@media (min-width: 1600px) {
    .uk-padding-remove-top\@xl {
        padding-top: 0 !important;
    }
    .uk-padding-remove-bottom\@xl {
        padding-bottom: 0 !important;
    }
}

/* ========================================================================
   Component: Height
 ========================================================================== */
.uk-height-xlarge {
    height: 840px;
}
.uk-height-max-xlarge {
    max-height: 840px;
}
.uk-height-unset {
    height: unset;
}

/* Phone landscape and bigger */
@media (min-width: 640px) {
    .uk-height-unset\@s {
        height: unset;
    }
    .uk-height-large\@s {
        height: 450px;
    }
}
/* Tablet landscape and bigger */
@media (min-width: 960px) {
    .uk-height-unset\@m {
        height: unset;
    }
    .uk-height-large\@m {
        height: 450px;
    }
}
/* Desktop and bigger */
@media (min-width: 1200px) {
    .uk-height-unset\@l {
        height: unset;
    }
    .uk-height-large\@l {
        height: 450px;
    }
}
/* Large screen and bigger */
@media (min-width: 1600px) {
    .uk-height-unset\@xl {
        height: unset;
    }
    .uk-height-large\@xl {
        height: 450px;
    }
}

/* Alignment
 ========================================================================== */
/*
 * Align items in the cross axis of the current line of the flex container
 * Row: Vertical
 */

/* Phone landscape and bigger */
@media (min-width: 640px) {
    .uk-flex-middle\@s {
        align-items: center;
    }
}
/* Tablet landscape and bigger */
@media (min-width: 960px) {
    .uk-flex-middle\@m {
        align-items: center;
    }
}
/* Desktop and bigger */
@media (min-width: 1200px) {
    .uk-flex-middle\@l {
        align-items: center;
    }
}
/* Large screen and bigger */
@media (min-width: 1600px) {
    .uk-flex-middle\@xl {
        align-items: center;
    }
}
 

如何确保在调整浏览器窗口大小时,背景图像和形状一起zoom 并保持其比例?

推荐答案

以下是简单且响应迅速的实现:

section {
  position: relative;
  background: purple;
  padding-left: 50%;
  color: #fff;
}
section:before {
  content:"";
  position: absolute;
  inset: 0 50% 0 0;
  background: var(--img) center/cover;
  clip-path: polygon(0 0, 100% 0,calc(100% - 80px) 50%,100% 100%, 0 100%);
}
section h1 {
  font-size: 25px;
  margin: 0;
  padding: 1em;
}
section p {
  font-size: 19px;
  margin: 0;
  padding: 1em;
}
@media (max-width: 500px) {
  section {
    padding: 0;
  }
  section:before {
    content:none
  }
}
<section style="--img:url(https://picsum.photos/id/1014/800/400)">
  <h1> A Title </h1>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur non nisl non neque ornare imperdiet. Sed in lorem in est aliquam cursus quis at felis. In sit amet feugiat turpis, sit amet iaculis ligula. Suspendisse diam tellus, commodo non eros eu,
    molestie auctor risus. Duis venenatis scelerisque urna pellentesque laoreet. </p>
</section>

Html相关问答推荐

简化指标与Delta保持一致

悬停时跳转的内容

如何在div中淡入和淡出渐变背景?

如何修复与导航栏重叠的css网格?

为什么前端框架可以创建自定义属性,而我却被期望在挖掘之前添加数据

滚动两个不同高度的DIV;一个等待另一个

防止position:relative的child在出界时收缩

角化、剪裁、边缘,但仅在底部2和平滑包装上

Chrome浏览器错误:上的主题验证判断https://pagead2.googlesyndication.com/失败

如何解决水平塌陷问题?

如何使 CSS 网格项目像 Flexbox 项目一样匹配父级的高度

使用简单的 HTML 设计公司徽标和文本

如何用js和CSS在两点之间画一条线

并排对齐两个文本区域列

Github上部署需要花费几小时时间:等待github pages部署批准

如何为某些行具有 rowspan 的表的每个奇数行着色

如何使用 CSS 和 HTML 将文本放入图像中?

有没有办法让 textarea 在调整大小时不显示 CSS 转换?

CSS 斜角与 3 行对齐

我正在try 向我预先存在的导航栏添加响应式汉堡包导航,但由于某种原因它没有显示