【问题标题】:Background gradient problems背景梯度问题
【发布时间】:2019-05-17 18:32:16
【问题描述】:

我尝试为我的网站制作渐变背景。你能帮我么?我现在正在学习 CSS/HTML。所以背景应该有一个从红色到黄色的渐变。我在等你的答案c;

这是我的个人网站

body {
  background-image: linear-gradient(to bottom, red, yellow);
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#card {
  position: relative;
  box-shadow: 5px 5px 10px rgba(0, 0, 0, .5);
  background-color: #ffffff;
  padding: 75px;
  border-radius: 35px 35px;
  -webkit-transition-duration: 0.2s;
  transition-duration: 0.2s;
}

#card:hover {
  box-shadow: 0 25px 50px black;
}

#person {
  background-image: linear-gradient(to bottom, #c94370, #3a679a);
  font-size: 67px;
  font-size-adjust: 0.58;
  font-family: 'Open Sans', bold;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

#profile {
  display: block;
  height: 320px;
  margin-left: auto;
  margin-right: auto;
  border-radius: 100% 100%;
  box-shadow: 0 0 25px rgba(0, 0, 0, 0.5);
  -webkit-transition-duration: 0.2s;
  transition-duration: 0.2s;
}

#profile:hover {
  box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
}

#mail {
  display: block;
  border: 5px solid #303030;
  font-family: 'Open Sans', bold;
  border-radius: 15px 15px;
  background-color: #ffffff;
  padding: 15px 42px;
  font-size: 36px;
  color: #303030;
  text-align: center;
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
  margin-left: auto;
  margin-right: auto;
}

#mail:hover {
  color: white;
  background-color: #303030;
}
<!DOCTYPE html>
<html>

<head>
  <title>Hello World</title>
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i&display=swap" rel="stylesheet">
  <link rel="shortcut icon" href="oskar.png" type="image/png" />
  <link rel="icon" href="oskar.png" type="image/png" />
  <link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
  <div id="card">
    <img src="oskar.png" id="profile"> <br>
    <b id="person">Oskar </b><br><br>
    <button id="mail" onclick="window.location.href='mailto:mail@mail.mail?subject=Die%20Website%20ist%20krass'">E-Mail</button>
  </div>
</body>

</html>

我希望有红色和黄色的渐变背景

【问题讨论】:

    标签: html css background gradient


    【解决方案1】:

    您的 CSS 应如下所示:

    body {
      background-image: linear-gradient(to bottom, red, yellow);
      background-repeat: no-repeat;
      background-attachment: fixed;
      position: static;
    }
    
    #card {
      position: relative;
      width: 300px;
      height: 500px;
      margin: auto;
      box-shadow: 5px 5px 10px rgba(0, 0, 0, .5);
      background-color: #ffffff;
      padding: 75px;
      border-radius: 35px 35px;
      -webkit-transition-duration: 0.2s;
      transition-duration: 0.2s;
    }
    

    position: absolute 打乱了背景的显示。这会相对于它的父元素定位一个元素。由于 body 的父级是 html 标记,我不太确定这会将 body 放置在哪里,但它不是您想要的位置。

    请看这里:https://www.w3schools.com/css/css_positioning.asp

    您也不应该描述身体内部的定位,因为您放入其中的所有东西都会尽量远离顶部和左侧的 50%。与变换相同。这些样式应该添加到您正在设置样式的实际元素中。

    background-repeat: no-repeat、background-attachment: fixed 和 position: static 将确保渐变适应窗口大小并且不会出错。

    您可以看到我如何将高度、宽度和边距样式添加到您的卡片类中。

    https://jsfiddle.net/AnthrOne/1guch73d/14/

    【讨论】:

    • 谢谢...就是这样
    • 稍微解释一下会很有帮助。问题是什么?您的代码是如何解决的?
    【解决方案2】:

    我会看看这个link 它似乎有你要找的东西。我认为由于默认值是从上到下,代码中的“到底部”部分可能会导致问题?

    我在此link 上对其进行了测试,它似乎可以以任何方式工作(有或没有“到底部”)。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多