Trying to make a configurable polka dot background using just CSS - html

I'm trying to make a grid background out of dots. I can't just use an image, because I need everything to be configurable:
background color
dot color
dot size
space between dots
Unless there's a better solution, I think the only way I can achieve this is with pure CSS. I've done some looking around and so far the closest thing i've found is using a radial-gradient. I'm having trouble though; I haven't been able to find a solution that lets me configure both the dot size and the space between dots while keeping a circle shape. I've gotten close, but than my dots end up looking like diamonds instead of circles. Here's what i've come up with so far:
https://jsfiddle.net/yzpuydtn/
body {
background-image: radial-gradient(black 2px, white 2px);
background-size:40px 40px;
}
Does anyone have any suggestions? Initially i'd like to have my dots be 2px x 2px and 40 px apart. Is there a better way to do this, or am I just configuring my gradient incorrectly? I think i'm close, but depending on how I zoom they look like either circles, diamonds or squares and I need it to always look like circles.

Using %: https://jsfiddle.net/yzpuydtn/11/
Using vw: http://jsfiddle.net/otwhu0uk/2/
Here is an example. I really hope this helps you.
body {
/* Controls size of dot */
background-image: radial-gradient(black 5%, white 0%);
/* Controls Spacing, First value will scale width, second, height between dots */
background-size:5% 10%;
}

Related

Unwanted 1px padding if div has a border. Only in chrome

Similar to this question (solution is useless): Chrome 1px line bug
My code: https://jsfiddle.net/91gryhdu/2/ (parent has blue border, red background. Child has no border and black background)
You shouldn't be able see any red, but in chrome, red edges appear if the blue border exists. Depending on the zoom level, there will either be 0, 2, or 4 red lines. Never 1 or 3.
On my computer, default zoom (100%) looks like this:
If I zoom > 100%:
If I zoom < 100%:
This is what it should look like regardless of zoom. Firefox shows no red, no matter the zoom level.
My code has the class circle and commented out border-radius because I ran into this issue while trying to create the below circles with only CSS. Its easier to see the issue with boxes.
I have tried relative vs absolute positioning, different display settings, defining all margins, em/px/%, paddings, etc. Nothing affects it. This leads me to believe its a CSS bug. The "solutions" I have found online, only fix the visual aspect.
The desired circles are very small and the white dot in the center becomes noticeably off-center in chrome. Its just enough to not look quite right.
How can I work around this issue without images/svg? I just need a little white circle inside a colored circle with a border. The connecting lines and the flattened tops don't matter. What matters is that the white dot is centered at the atomic level.
I have found that I can add a margin to the other two edges to counter the unwanted margins. In theory I could use JS to detect zoom and apply the CSS. Seems like a desperate/unreliable solution.
I'm going in circles trying to figure this out...
You could do this easily with flexbox, like so:
.outer-circle {
display: flex;
align-items: center;
justify-content: center;
width: .6rem;
height: .6rem;
padding: .4rem;
background: blue;
border-radius: 50%;
}
.inner-dot {
background: white;
height: 100%;
width: 100%;
border-radius: 50%;
}
<div class="outer-circle">
<div class="inner-dot"></div>
</div>
The Chrome bug could be due to the size units of your elements, they are small and your px values may not divide well by 4 which correspond to high resolution displays on e.g. retina Macs etc. If your pixels can't be divided by 4 without creating a floating point value, you may get unwanted artifacts like this and centered elements may be a pixel off the dead center as the algorithm has to decide where to put the color (it cannot put color halfway between pixels).
It is due to Chrome bug (https://groups.google.com/a/chromium.org/g/paint-dev/c/ERXM1sHcpyQ)
I had a same problem in Chrome vesrion 108.0.5359.125, now I have updated to the latest (109.0.5414.120) and gap has dissapeared.
(I know this is not a solution as you can't force your users to update, but hopefully this issue will become outdated in a time)

CSS Background positions

In trying to finalize the layout for my blog, I am having one issue I cant seem to get past. I have two different backgrounds that I want to use for my blog. One of the backgrounds is used just for the header of the blog. The other background I want to add is for the rest. I have been trying to find a way to get my body background to repeat after so much spacing with little luck.
If there is a way to have a background repeat-y after a certain position that would be perfect. But that doesn't seem possible from my searches. If there is another way to accomplish this, it would be very helpful.
This is the site I am trying to edit.
As you can see, the header has the proper background, but I cant figure a way to get that background everywhere else. For clarification, the background header has a blue background under the home/search buttons, so that is why I cant just have one repeating header.
I just made a few edits to my site, and I got to to look very close to how I want. I did more of a quick fix that is "good enough". I just made the header background repeat, and it looks pretty good for the most part. The only problem with it now is that the blue bar that is part of the header sometimes shows up at the bottom of the screen, which is okay I guess. If anyone has a better solution I would love to hear it.
When you specify "repeat-y" then there is no posibility as far as I know to make the background begin repeating after some coordinate.
However, since you have 2 different backgrounds, you don't need to.
Just specify the non repeating background first, and with the adequate dimensions; it will hide the other
This CSS
.test1 {
background-image: linear-gradient(90deg, black, red), linear-gradient(0deg, white, yellow);
background-position: 0px 0px, 0px 0px;
background-size: 100% 100px, 100% 50px;
background-repeat: no-repeat, repeat-y;
}
produces a black & red top, followed by a repeating pattern of yellow stripes
demo
It is not clear from your example if this is enough; if not you would need another background, between the first and the second, to hide the amount of the repeating background needed

Overriding background color

I've got question about CSS.
I've got 2 divs, one with background-color: #000; and second with transparent png file partly without background (so transparent), let's call it background-image: url(images/scrolltotop.png);.
What I want, is to override background-color with transparency from png file, so the background doesn't cover my png file. I'll give images to show you what i mean:
and now image with the result I want to get (above) - with background from html style.
Is there any way to cover background-color with transparency?
edit: maybe this img will tell you more, what I want to do (above)
background-color: transparent;
Have you tried that?
EDIT
http://jsfiddle.net/FCXGu/3/
border-width: 20px;
border-image: url("//i.imgur.com/hg2Thfa.png") 20 stretch;
-moz-border-image: url("//i.imgur.com/hg2Thfa.png") 20 stretch;
-webkit-border-image: url("//i.imgur.com/hg2Thfa.png") 20 stretch;
It takes a moment to understand how this can work. The border of the image essentially acts like the "padding" as it is generally thicker than normal. You could thought just have the top of the border thick for your cutout in which case your code might look like:
border-image: url("//i.imgur.com/hg2Thfa.png") 20 0 0 0 stretch;
This would mean the top border of your div, would use the top 20 pixels of the image you are using, and the rest of the image would just be used in the content area of your div. Either way, the effect you want can be achieved with border-image.
Is it the best way/only way? Probably not, but it is one way.
Here is the fiddle: http://jsfiddle.net/FCXGu/3/
I added a content area just to illustrate where everything is in this one: http://jsfiddle.net/FCXGu/4/
Without seeing your exact image, page, and usage I couldn't tell you the best way to create a png, use stretch vs repeat, etc. But border-image is pretty flexible. However it is does not work in ALL browsers. Just the good ones.

div background image zoom issue

I have a div with background image which contains 3 colors of the same icon
I shift the icon (background-position: top/center/bottom) according to what page is user viewing:
All works fine utill I zoom the page (ctrl + mousewheel) - than the background image seems to shift one pixel up or something, so i can see one-pixel line of the other icon at the bottom of my wrapper div:
Screens are from IE but it looks even more broken on iPad...
Any thoughts about what is causing this and how to fix it?
You can prevent any of the other images inside the sprite from showing by using diagonal sprites, or simply leaving some space between each image.
I shift the icon (background-position: top/center/bottom)
Use should use explicit px offsets instead. I suspect that will be slightly more robust when it comes to zooming.
There's nothing you can really do to prevent things sometimes being "1px off" when you zoom.
For example, if you have a 42px high element, and you zoom to 125%, then you have a 52.5px high element. The browser must round that number one way or the other.
Since those images are bitmaps, they always gonna look bad wen you zoom them.
You can do tree things:
Use a library like raphael JS and inlude your icons as vectors: http://raphaeljs.com/
Wrap your icons into spans for example and using a PX size and not EM's.
Leave more speace between your sprites
Try to make better resolution image and try it again.
It's better practice (and ultimately gives you much better control) to use pixel positioning rather than top/center/bottom when implementing CSS sprites, that way the image you want to show can be slightly larger (or with a little spacing) and therefore support that visual overflow you're seeing when you zoom. Your other images/states won't be affected by the neighbouring image/state because you're setting their position with a pixel-specific location rather than top/center/bottom e.g. (from article link below)
#panel-a {
background: transparent url(sprite.jpg) 0 -200px no-repeat;
}
#panel-b {
background: transparent url(sprite.jpg) -96px -200px no-repeat;
}
#panel-c {
background: transparent url(sprite.jpg) -172px -200px no-repeat;
}
#panel-d {
background: transparent url(sprite.jpg) -283px -200px no-repeat;
}
Not to mention that pixel positioning allows you to add additional states to your image without affecting other existing states if you add them onto the bottom of your image, for example. Of course that changes when you start adding images horizontally.
Here's a good reference: http://www.alistapart.com/articles/sprites

css gradiant background long page

I have a a background image on my html with css page, from blue at the top, to white at the bottom.
However, the image 400x800 and the page is much longer, so it repeats.
The page length varies all the time.
Is it possible to solve this so the background stretches to the page length somehow? or maye make it not repeat and make the background white with the image on top?
If yes, how please?
The easiest is to set the image to not repeat and then set the background color the same as one end of the gradient:
body {
background: white url('yourImage') repeat-x top left;
}
Getting a gradient to stretch to fit the window is doable, but is more work and would require some javascript.
I know that there are jQuery / Javascript methods of accomplishing this:
Resizable Backgrounds with jQuery
Stretch background image - jQuery - Demo
but besides resorting to solutions like those - you could:
Select a pattern for your background that repeats (textures etc.)
Create a very long background image that could handle most navigation that your page
would deal with.
Another alternative is to make your gradient the same at both ends with the maximum colour difference at the 50% mark.