【问题标题】:Can you give me a sample code for this image你能给我这个图像的示例代码吗
【发布时间】:2021-05-28 07:19:46
【问题描述】:

我一直在尝试仅使用 HTML 和 CSS 重新创建图片/设计,但在定位方面我一直卡住。 The image I'm trying to recreate

有人可以给我一些建议吗?

这是我的 CSS 代码。 我知道我搞砸了定位,但我没有任何其他想法

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&family=Lexend+Deca&display=swap');
.main{
   width: 1440px;
    background-color: hsl(233, 47%, 7%);
    transform: translate(0%, 0%);
}
.inner{
    width: fit-content;
    height: fit-content;
    border-radius: 10px;
    border: purple 50px;
    background-color: #333;
    margin: 20%;
    margin-top: 30%;
}
.texts{
    background-color: hsl(244, 38%, 16%);
    font-family: 'Lexend Deca', sans-serif;
    width: 47.5%;
    transform:translateY(60%);
    height: 40%;
}
.mainhead{
    color: hsl(0, 0%, 100%);
    font-family: 'Inter', sans-serif;
}
.insights{
    color: hsl(277, 64%, 61%);
}
.mainparagraph, .stats{
    color: hsla(0, 0%, 100%, 0.75);
}
.stathead{
    color: hsla(0, 0%, 100%, 0.6);
}
p {
    font-size: 15px;
}
.image{
    width:fit-content;
    height: 40%;
    background-color: hsl(277, 98%, 38%);
    transform: translate(90%, -40%);
}
img {
    opacity: 0.6;
}
<body>
    <section class="main">
        <section class="inner">
            <section class="texts">
                <h1 class="mainhead">
                    Get <span class="insights">insights </span>that help your business grow.
                </h1>
                <p class="mainparagraph">
                    Discover the benefits of data analytics and make better decisions regarding revenue, customer experience, and overall efficiency.
                </p>
                <section class="bottom">
                    <h1 class="stathead">10k+</h1>
                    <p class="stats">COMPANIES</p>
                </section>
                <section class="bottom">
                    <h1 class="stathead">314</h1>
                    <p class="stats">TEMPLATES</p>
                </section>
                <section class="bottom">
                    <h1 class="stathead">12M+</h1>
                    <p class="stats">QUERIES</p>
                </section>
            </section>
            <section class="image">
                <img src="images/image-header-desktop.jpg" alt="">
            </section>
        </section>
    </section>
</body>

【问题讨论】:

  • 这是一个开始,我试图将代码保持在最低限度,以便您可以理解它jsfiddle.net/dyj419na 在构建 UI 时,您首先将其分解为这样 i.stack.imgur.com/uXDI0.jpg 它会让您轻松合作
  • 非常感谢!!我真的很感激,但你能向我解释一下整个 flex 概念吗?
  • 或推荐我到哪里可以阅读到它
  • 那里有很多文章,快速谷歌搜索可以让你开始,但当然你从 MDN 开始,我认为你多年来会经常访问 developer.mozilla.org/en-US/docs/Web/CSS/…跨度>

标签: html css css-position


【解决方案1】:

检查下面的这个 sn-p。可以使用Flexbox 或CSS Grid 进行定位。这里使用前者。我冒昧地将 HTML 和 CSS 修改为这样的扩展,以便它(紧密)匹配您的图像。

我建议您仔细阅读何时应该使用什么 HTML 元素,例如 &lt;section&gt;。首先查看 Flexbox,然后查看 CSS Grid,它们对于创建布局很重要,如果你掌握了它们,你的生活会变得更轻松。

图片是由filter、叠加层和mix-blend-mode的组合完成的

一定要查看@zohirsalak 发布的关于将设计拆分成小块的链接。这是一个非常好的可视化和值得学习的课程。

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&family=Lexend+Deca&display=swap');

*, *::before, *::after {
  margin: 0;
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
}

body {
  background-color: black;
  padding: 1rem;
}

.insights {
  display: flex;
  border-radius: 10px;
  background-color: hsl(244, 38%, 16%);
  width: 1000px;
  overflow: hidden;
}

.insights .texts {
  flex: 0 0 50%;
  background-color: hsl(244, 38%, 16%);
  font-family: 'Lexend Deca', sans-serif;
  padding: 4rem;
}

.insights h1 {
  font-family: 'Inter', sans-serif;
  font-size: 2em;
  color: #fff;
  margin-bottom: 2rem;
}

.insights .highlight {
  color: hsl(277, 64%, 61%);
}

.insights p {
  font-size: 1em;
  line-height: 1.5;
  margin: 2rem 0;
  color: hsla(0, 0%, 100%, 0.75);
}

.insights .stats {
  display: flex;
  list-style: none;
  padding: 0;
  margin: 4rem 0 0;
  gap: 1rem;
}

.insights .stats strong {
  font-size: 1.5em;
  color: #fff;
}

.insights .stats span {
  font-size: 0.75em;
  color: hsla(0, 0%, 100%, 0.75);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.insights .image {
  flex: 0 0 50%;
  position: relative;
}

.insights .image::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: hsl(277, 64%, 61%);
  mix-blend-mode: multiply;
}

.insights .image img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: grayscale(100%);
}
<section class="insights">
  <div class="texts">
    <h1>Get <span class="highlight">insights </span>that help your business grow.
    </h1>
    
    <p>
      Discover the benefits of data analytics and make better decisions regarding revenue, customer experience, and overall efficiency.
    </p>
    
    <ul class="stats">
      <li>
        <strong>10k+</strong>
        <span>Companies</span>
      </li>
      
      <li>
        <strong>314</strong>
        <span>Templates</span>
      </li>
      
      <li>
        <strong>12M+</strong>
        <span>Queries</span>
      </li>
    </ul>
  </div>
  
  <figure class="image">
    <img src="https://images.pexels.com/photos/3184357/pexels-photo-3184357.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260" alt="">
  </figure>
</section>

【讨论】:

  • 非常感谢你能给我解释一下 flex 的概念吗?或向我推荐我可以了解更多信息的地方
  • 答案描述中的链接指向 CSS Tricks 帖子,其中详细解释了所有 Flexbox 属性。 IMO 这是关于 Flexbox 的最好的文章。但又来了:CSS Tricks: A guide to Flexbox
猜你喜欢
  • 1970-01-01
  • 2016-08-07
  • 2018-05-21
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多