Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

html - How to position element in the top right corner using CSS inset?

Instead of the top and right CSS properties, I'd like to use inset to position the element.

For example, the following code places the element to the top right corner:

.top-right {
  top: 0;
  right: 0;
} 

How would you achieve the same using the inset property? I have tried using different ways and can't figure it out.

Although I set both top and right to 0 with inset in the following example, it doesn't work:

.top-right {
  inset: 0 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It would be:

.top-right {
  inset: 0 0 auto auto;
}

The order is top, right, bottom, left. It has the same multi-value syntax as the margin or the padding shorthand.

If you use inset: 0 0;, it's the same as inset: 0 0 0 0; and it means you also set the bottom and the left properties to 0. You could set them to the initial value (auto) instead to solve your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...