【问题标题】:Glasses shape design with CSS使用 CSS 设计眼镜形状
【发布时间】:2021-05-20 12:53:44
【问题描述】:

我需要用CSS为一个网站的主页编写这个眼镜形状设计,我需要考虑在眼镜上方的空白处我们要放置一些动态数据。 我的问题是如何管理两侧之间的曲线。 到目前为止,我已经用 CSS 边框和 vh/vw 的指针(但不完美)对顶部圆圈进行了编码以用于响应目的,我尝试将图像用于曲线部分,但它效率不高,因为在响应版本中它不会'工作不完美。

所以我正在寻找一种基于代码的方法来处理这一部分,有什么想法吗?

你可以看到下面的代码:

@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;700&display=swap');
body {
  margin: 0;
  font-family: 'Montserrat', sans-serif;
  font-size: 16px
}

@media screen and (max-width: 1400px) {
  body {
    font-size: 12px
  }
}

.homePageHeader {
  display: grid;
  grid-template-columns: 33% 33% 33%;
  width: 100%;
  text-align: center;
  color: #ffff;
  height: 20vh;
}

.homePageWrapper {
  background: #FFF3D3 no-repeat;
  background-size: cover;
  height: 100vh;
  margin: 0;
  transition: .4s linear;
  overflow: hidden;
}

.homePageCircleWrapper {
  position: relative;
  height: 70vh;
  text-align: center;
  margin: 0 auto;
}

.homePageCircleWrapperInner {
  height: 30vw;
  width: 30vw;
  border-radius: 50%;
  border: 2.1875em solid #ff692814;
  margin: 0 auto;
  position: relative;
}

.homePageCircleHeading {
  margin-top: 5em;
  margin-bottom: 1.7em;
  color: #C86102;
}

.homePageCircleHeading h1 {
  font-weight: 300;
  font-size: 2.5em;
}

.homePageCircleAppstores img:nth-child(2) {
  margin-left: 1.25em;
}

.homePageCircleImage {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translate(-50%, 0%);
}

#circleScroll {
  position: absolute;
  height: 30vw;
  width: 30vw;
  transform: rotate(0deg);
  transition: transform 0.7s linear;
  top: 0;
  left: 0;
  text-align: center;
}

.homePageCircleImage img {
  width: 25vw;
  height: auto;
  margin-bottom: -5px;
}

.scrollPointer {
  position: absolute;
  width: 2.5em;
  /* height: 35px; */
  background: #ff692814;
  margin: 0 auto;
  left: 47%;
  top: 0;
  margin-top: -5.5em;
  border-top-right-radius: 2.5em;
  border-top-left-radius: 2.5em;
  height: 3.3em;
}

.homePageHeaderLogo {
  text-align: center;
  margin: 0 auto;
  transition: .4s;
  display: flex;
  justify-content: center;
  align-items: center;
}

.homePageHeader {
  color: #81621F;
}

.homePageHeaderCTA {
  font-weight: 500;
  font-size: 0.625em;
}

.homePageHeaderMenu {
  font-size: 0.625em;
  font-weight: 700;
}

.homePageFooter {
  padding: 1em 0;
  width: 100%;
  margin: 0 auto;
  text-align: center;
  height: 10vh;
  position: relative;
}

.homePageFooter span {
  font-size: 1em;
  font-weight: 500;
  color: rgba(129, 98, 31, 1);
}

.homePageFooter span:first-child {
  margin-right: 1.8em
}

.homePageFooterInside a {
  /* color: #ffff; */
  color: #CFCFCF;
  text-decoration: none;
}
<div class="homePageWrapper">
  <div class="custom-footer"></div>

  <div class="homePageHeader">
    <div class="homePageHeaderMenu">
      <p>MENU </p>
    </div>
    <div class="homePageHeaderLogo">
      Logo
    </div>
    <div class="homePageHeaderCTA">
      <p>Click here
    </div>
  </div>


  <div class="homePageCircleWrapper">
    <div class="homePageCircleWrapperInner">
      <div class="homePageOptions">

        <div id="circleScroll">
          <span class="scrollPointer"></span>
        </div>
      </div>
      <div class="homePageCircleHeading">
        <h1>
          A website
          <br><strong>For you</strong>
        </h1>

      </div>

      <div class="homePageCircleAppstores">

        <span>span</span> <span>span</span></br>
        <span>span</span> <span>span</span>
      </div>
      <div class="homePageCircleImage">

      </div>
    </div>


    <div class="homePageFooter">

      <div class="homePageFooterInside">
        <a href=""><span>Question</span></a>
        <a href=""> <span>Answer</span></a>
      </div>
    </div>

  </div>

设计:

【问题讨论】:

  • 为什么不使用图像?例如,SVG 会小巧、高效且精确,并且可以根据需要调整大小。
  • 是的,SVG 响应式很酷,但问题是在响应式版本中,图像不会很好地附加在圆圈中,同时调整一些空间或重叠显示
  • 您需要更具体地了解这个关于 SVG 的问题。只是试图为此使用 CSS 似乎有点过头了。我觉得像 SVG 这样的替代方案可能是一个更好的起点。 “图像”、“圆圈”、“一些空间”——更详细地说明出了什么问题将是一个好的开始:)
  • 你尝试过线性渐变和径向渐变的组合吗?
  • 好的,让我再试一次 :) 如果你现在检查代码,我用border-width: 2.1875em 构建一个圆,如果我想使用SVG,我需要使用a单独的 div 并将其设置为背景图像和绝对位置,我需要将图像连接到顶部圆圈。正确的?现在,如果我用 vw/vh 定位它,它会动态改变大小,但取决于视口,我会有不同的空间并且它不适合,请检查这张图片,你会明白我的意思:ibb.co/4FGNG4J

标签: html css css-transforms shapes css-shapes


【解决方案1】:

我更新了我的问题。现在它看起来像眼镜。这是你想要的吗?

如果你想移动眼镜,可以使用container

.firstHandle {
  height: 40px;
  width: 25px;
  border-top-right-radius: 20px;
  border-top-left-radius: 20px;
  background: gray;
  margin-left: 5.1em;
  margin-bottom: -0.5em;
}

.firstCircle {
  width: 150px;
  height: 150px;
  border: 20px solid gray;
  border-radius: 10000px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.curve {
    width: 60px;
    height: 25px;
    border-top-left-radius: 200px;
    border-top-right-radius: 200px;
    border: 15px solid gray;
    border-bottom: 0;
    transform: rotate(-90deg);
    margin-top: 0.5em;
    margin-left: 50px;
}

.secondCircle {
  width: 150px;
  height: 150px;
  border: 20px solid grey;
  background: grey;
  border-radius: 10000px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 0.5em;
}

.secondHandle {
  height: 40px;
  width: 25px;
  border-bottom-right-radius: 20px;
  border-bottom-left-radius: 20px;
  background: gray;
  margin-left: 5.1em;
  margin-top: -0.5em;
}
<div id="container">

  <div class="firstHandle"></div>

  <div class="firstCircle">
    <p>Hello World!</p>
  </div>

  <div class="curve"></div>

  <div class="secondCircle">
    <p>Hello World!</p>
  </div>
  
  <div class="secondHandle"></div>

</div>

如果您希望我更改其他任何内容,请告诉我。

【讨论】:

  • 是的,类似的东西,但我无法将其样式设置为与两个圆之间的曲线连接器一模一样
  • 这是你需要的吗?
  • 非常感谢你,是的,这就是我需要的,唯一的问题是边框颜色的不透明度为 0.4,它会暴露每个对象的重叠,所以我需要找到一种方法来解决这个问题, 我把尺寸改成动态 vw 效果很好
  • 太棒了!如果需要,我可以将边框颜色设为 0.4 的不透明度
  • 谢谢,请问您解决曲线和边框重叠的方法是什么?
【解决方案2】:

可以仅使用 CSS 渐变来绘制它。这样,它可以根据需要用作任何元素的背景图像,无需额外的 HTML 或 SVG。

这是一个名为 frame 的类 - 显然可以调整百分比以获得所需的确切比例。

* {
  margin: 0;
  padding: 0;
}
.frame {
  
  --actualh: 100vh; /* set this to what you want the height of the whole thing to be */
  --bg: #6a6a6a; /* background color */
  --frame: #838383; /* frame color */
  
 /* These are measurements taken from the image given. They will be used relatively to calculate various dimensions */ 
  --w: 11.7;
  --h: 26.7;
  
  background-color: var(--bg);
  width: calc(calc(var(--w) / var(--h)) * var(--actualh));
  height: var(--actualh);
  
  background-image:
   /* the lower lens */
   radial-gradient(circle at 50% 75%, var(--frame) 0%, var(--frame) 27%, transparent 27%, transparent 100%),
   /* the upper lens */
   radial-gradient(circle at 50% 25%, var(--bg) 0%, var(--bg) 23%, var(--frame) 23%, var(--frame) 27%, transparent 27%, transparent 100%),
   /* the bridge curved part */
   linear-gradient(to right, transparent 0%, transparent 70%, var(--bg) 70%, var(--bg) 100%),
   radial-gradient(circle, var(--bg) 0%, var(--bg) 23.93%, var(--frame) 23.93%, var(--frame) 33%, transparent 33%, transparent 100%),
   /* the ears */
   radial-gradient(circle at 50% 4%, transparent 0%, transparent 5%, var(--bg) 5%, var(--bg) 15%, transparent 15%, transparent 100%),
   radial-gradient(circle at 50% 96%, transparent 0%, transparent 5%, var(--bg) 5%, var(--bg) 15%, transparent 15%, transparent 100%),
   linear-gradient(to right, transparent 0%, transparent 40%, var(--frame) 40%, var(--frame) 60%, transparent 60%, transparent 100%);
}
&lt;div class="frame"&gt;&lt;/div&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-04
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多