HTML: Sub-pixel border

2019-01-19 16:52发布

Is it possible to have a border that is thinner than 1px and works in IE6+ and is not an image and renders properly visually?

Thank you.

10条回答
Fickle 薄情
2楼-- · 2019-01-19 17:18

No. You can't show a size smaller than one pixel because pixels are the basic unit of the monitor. And anyway, no browser I know of allows you to specify sub-pixel widths. They just get rounded up to 1px or down to 0px.

查看更多
Melony?
3楼-- · 2019-01-19 17:20

To render native 1px borders on high DPI/@2x/retina displays, there are a couple of tricks.

On Firefox and Safari (macOS and iOS), use a 0.5px border:

/* Fallback: Most browsers now render 0.5px as 1px though */
.el {
  border: 1px solid red;
}
.retina .el {
  border: 0.5px solid red;
}

On Chrome, use a box-shadow with a 0.5px spread:

.retina-chrome .el {
  box-shadow: 0 0 0 0.5px red;
}

Use JS to add a class to the HTML element to only target @2x+ displays.

if (window.devicePixelRatio >= 2) {
  document.documentElement.classList.add(
    window.chrome ? 'retina-chrome' : 'retina'
  );
}

For @1x displays, use a slightly lighter color 1px border.

查看更多
冷血范
4楼-- · 2019-01-19 17:26

I don't know about IE8-10 (IE6-7 definitily no go) , but in Chrome and FF I get the thinnest border with box-shadow. Works best to get a 1px <hr> instead of the autorendered 2px, but can be used on a border as well.

The thin border on the HR is more prominent in FF than Chrome, but also Chrome renders 2px.

http://jsfiddle.net/GijsjanB/3G28N/

.thin {
    border: 1px solid white;
    box-shadow: 0 0 1px black;
}
查看更多
唯我独甜
5楼-- · 2019-01-19 17:27

you can transform the line like that:

.thin{ -ms-transform:scale(1, 0.5); -webkit-transform:scale(1, 0.5); transform:scale(1, 0.5);}

or, if the line is vertical

.thin{ -ms-transform:scale(0.5, 1); -webkit-transform:scale(0.5, 1); transform:scale(0.5, 1);}
查看更多
来,给爷笑一个
6楼-- · 2019-01-19 17:28

Although this isn't (currently) possible in any version of IE or Edge, on the latest versions of Firefox and Chrome you can now use border width values less than 1px.

.borderTest {
	box-sizing: border-box;
	display: block;
	margin: 0.5em;
	padding: 0.5em;
	width: calc( 100% - 1em );
}
.borderTest:nth-child(1){
	border: 1px solid #000
}
.borderTest:nth-child(2){
	border: 0.75px solid #000
}
.borderTest:nth-child(3){
	border: 0.5px solid #000
}
.borderTest:nth-child(4){
	border: 0.25px solid #000
}
<div class="borderTest">1px</div>
<div class="borderTest">0.75px</div>
<div class="borderTest">0.5px</div>
<div class="borderTest">0.25px</div>

This outputs the following on a UHD screen:

enter image description here

查看更多
相关推荐>>
7楼-- · 2019-01-19 17:36

I think you could define the width of a border using units like em which would come out to less than 1px, and it would be valid. However, like Will Martin said, for display purposes it would just round it up or down to a whole pixel.

查看更多
登录 后发表回答