英文:
How to fill a rotating background with responsive layout in CSS?
问题
当在 div 内部或全屏旋转元素时,如果您知道尺寸,可以通过使内部元素的边界足够宽或足够高来隐藏它。在响应式网页设计中,当高度/宽度相差太大时,对角线变得太长似乎是一个问题。
这是一个旋转背景图案的示例,在某些方面可以工作,但一旦变得太高或太宽,它就会破裂,绿色背景颜色会渗出:Codepen
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
width: 100vw;
height: 100vh;
overflow: hidden;
background: green;
}
.pattern {
width: 200%;
height: 200%;
position: fixed;
top: -50%;
right: -50%;
background-image: conic-gradient( white 0deg 45deg, black 45deg 90deg, white 90deg 135deg, black 135deg 180deg, white 180deg 225deg, black 225deg 270deg, white 270deg 315deg, black 315deg 360deg);
animation: rotate 4s linear infinite;
}
@keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(1turn);
}
}
<div class="pattern"></div>
英文:
When rotating a element inside a div, or fullscreen, if you know the dimensions you can make it wide or tall enough to hide it by making the inner element bounds. With responsive web design, this seem to be a issue when height/width is too far off each other where the diagonal becomes too long.
Here is a example of an rotating background pattern that works in some aspects, but once it goes too tall or too wide it breaks and green background color bleeds: Codepen
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
width: 100vw;
height: 100vh;
overflow: hidden;
background: green;
}
.pattern {
width: 200%;
height: 200%;
position: fixed;
top: -50%;
right: -50%;
background-image: conic-gradient( white 0deg 45deg, black 45deg 90deg, white 90deg 135deg, black 135deg 180deg, white 180deg 225deg, black 225deg 270deg, white 270deg 315deg, black 315deg 360deg);
animation: rotate 4s linear infinite;
}
@keyframes rotate {
0% {
transform: rotate(0);
}
0% {
transform: rotate(1turn);
}
}
<!-- language: lang-html -->
<div class="pattern"></div>
<!-- end snippet -->
答案1
得分: 3
请看这个解决方案,max获取100vh或100vw的最大值,将其添加到一个变量中,并将其添加到宽度和高度,我做了一些其他更改以使其按预期工作:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
width: 100vw;
height: 100vh;
overflow: hidden;
background: green;
}
.pattern {
--size: calc(100vmax * 1.5);
width: var(--size);
height: var(--size);
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-image: conic-gradient(
white 0deg 45deg,
black 45deg 90deg,
white 90deg 135deg,
black 135deg 180deg,
white 180deg 225deg,
black 225deg 270deg,
white 270deg 315deg,
black 315deg 360deg
);
animation: rotate 4s linear infinite;
}
@keyframes rotate {
0% {
transform: translate(-50%, -50%) rotate(0);
}
0% {
transform: translate(-50%, -50%) rotate(360deg);
}
}
<div class="pattern"></div>
英文:
See this solution, max takes the highest value of 100vh or 100vw, adds it to a variable and adds it to the width and height, I made some other changes to make it work as expected:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
width: 100vw;
height: 100vh;
overflow: hidden;
background: green;
}
.pattern {
--size: calc(100vmax * 1.5);
width: var(--size);
height: var(--size);
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-image: conic-gradient(
white 0deg 45deg,
black 45deg 90deg,
white 90deg 135deg,
black 135deg 180deg,
white 180deg 225deg,
black 225deg 270deg,
white 270deg 315deg,
black 315deg 360deg
);
animation: rotate 4s linear infinite;
}
@keyframes rotate {
0% {
transform: translate(-50%, -50%) rotate(0);
}
0% {
transform: translate(-50%, -50%) rotate(360deg);
}
}
<!-- language: lang-html -->
<div class="pattern"></div>
<!-- end snippet -->
答案2
得分: 1
您的代码可以简化如下。您只需要一个具有inset
属性的大负值。
body {
background: green;
}
.pattern {
position: fixed;
inset: -200vmax;
background: repeating-conic-gradient(white 0 12.5%, black 0 25%);
animation: rotate 4s linear infinite;
}
@keyframes rotate {
0% {
transform: rotate(1turn);
}
}
<div class="pattern"></div>
英文:
Your code can be simplified like below. All you need a big negative value with inset
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
background: green;
}
.pattern {
position: fixed;
inset: -200vmax;
background: repeating-conic-gradient(white 0 12.5%, black 0 25%);
animation: rotate 4s linear infinite;
}
@keyframes rotate {
0% {
transform: rotate(1turn);
}
}
<!-- language: lang-html -->
<div class="pattern"></div>
<!-- end snippet -->
答案3
得分: 0
这在正常情况下保持响应性 - 如果设备不是狭窄且超长的话。我已经为更窄的宽度添加了@media
,否则我无法让它工作得更好。我很好奇是否有更好的解决方案!
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
width: 100vw;
height: 100vh;
overflow: hidden;
background: green;
display: flex;
justify-content: center;
align-items: center;
}
.pattern {
width: 200vw;
min-width: 200vh;
height: 200vw;
min-height: 200vh;
position: fixed;
background-image: conic-gradient(
white 0deg 45deg,
black 45deg 90deg,
white 90deg 135deg,
black 135deg 180deg,
white 180deg 225deg,
black 225deg 270deg,
white 270deg 315deg,
black 315deg 360deg
);
background-size: cover;
animation: rotate 4s linear infinite;
}
@media screen and (max-width: 640px){
.pattern {
width: 400vh;
min-width: 400vh;
height: 400vw;
min-height: 400vw;
}
}
@keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(1turn);
}
}
<div class="pattern"></div>
希望有所帮助!
英文:
This stays responsive under normal conditions - if device isn't narrow and super long. I have added @media
for narrower width, otherwise I couldn't get it to work better. I'm curious if there is a better solution to it!
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
width: 100vw;
height: 100vh;
overflow: hidden;
background: green;
display: flex;
justify-content: center;
align-items: center;
}
.pattern {
width: 200vw;
min-width: 200vh;
height: 200vw;
min-heigth: 200vh;
position: fixed;
background-image: conic-gradient(
white 0deg 45deg,
black 45deg 90deg,
white 90deg 135deg,
black 135deg 180deg,
white 180deg 225deg,
black 225deg 270deg,
white 270deg 315deg,
black 315deg 360deg
);
background-size: cover;
animation: rotate 4s linear infinite;
}
@media screen and (max-width: 640px){
.pattern {
width: 400vh;
min-width: 400vh;
height: 400vw;
min-height: 400vw;
}
}
@keyframes rotate {
0% {
transform: rotate(0);
}
0% {
transform: rotate(1turn);
}
}
<!-- language: lang-html -->
<div class="pattern"></div>
<!-- end snippet -->
Hopefully it helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论