您的位置:

如何优雅地设置CSS边框和间距?

一、添加边框

在CSS中,设置边框的属性为border。边框的设置可以包括宽度、样式和颜色。下面是一个添加红色边框的例子:

.box {
  border: 1px solid red;
}

上面的代码表示在class为box的元素周围添加宽度为1像素、样式为实线、颜色为红色的边框。

另外,还可以分别设置边框的上下左右,通过分别设置border-top、border-bottom、border-left和border-right来实现。例如:

.box {
  border-top: 1px solid red;
  border-left: 2px dotted blue;
  border-bottom: 3px double green;
  border-right: 4px dashed yellow;
}

上面的代码表示在class为box的元素的上面添加宽度为1像素、样式为实线、颜色为红色的边框,左边添加宽度为2像素、样式为点线、颜色为蓝色的边框,下面添加宽度为3像素、样式为双线、颜色为绿色的边框,右边添加宽度为4像素、样式为虚线、颜色为黄色的边框。

二、设置边框弧度

CSS3提供了border-radius属性来设置元素的边框弧度,即让元素拥有圆角。例如:

.box {
  border: 1px solid black;
  border-radius: 10px;
}

上面的代码表示在class为box的元素周围添加宽度为1像素、样式为实线、颜色为黑色的边框,并且让元素的四个角都呈现10像素的圆角。

border-radius还可以设置不同的弧度值来实现不同的效果。例如:

.box {
  border: 1px solid black;
  border-top-left-radius: 20px;
  border-top-right-radius: 10px;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 15px;
}

上面的代码表示在class为box的元素周围添加宽度为1像素、样式为实线、颜色为黑色的边框,并且让元素的左上角呈现20像素的圆角,右上角呈现10像素的圆角,左下角呈现5像素的圆角,右下角呈现15像素的圆角。

三、增加间距

在CSS中,可以使用padding和margin来增加元素内部和外部的间距。

padding属性用于设置元素内部的间距,即元素内容和边框之间的距离。例如:

.box {
  border: 1px solid black;
  padding: 10px;
}

上面的代码表示在class为box的元素周围添加宽度为1像素、样式为实线、颜色为黑色的边框,并且在元素内部添加10像素的间距。

margin属性用于设置元素外部的间距,即元素和其它元素之间的距离。例如:

.box {
  border: 1px solid black;
  margin: 10px;
}

上面的代码表示在class为box的元素周围添加宽度为1像素、样式为实线、颜色为黑色的边框,并且让元素和其它元素之间保留10像素的间距。

和边框一样,padding和margin属性也可以分别设置上下左右的间距。例如:

.box {
  border: 1px solid black;
  padding: 10px 20px 15px 5px;
  margin: 5px 10px;
}

上面的代码表示在class为box的元素周围添加宽度为1像素、样式为实线、颜色为黑色的边框,并且让元素的上面保留10像素的间距,右边保留20像素的间距,下面保留15像素的间距,左边保留5像素的间距,和其它元素之间保留上下5像素、左右10像素的间距。

如何优雅地设置CSS边框和间距?

2023-05-12
如何优化CSS边框和外边距

2023-05-12
如何设置CSS内边距百分比

2023-05-12
如何优雅地设置HTML表格边框?

2023-05-16
如何优雅地设置CSS边框宽度?

2023-05-12
CSS设置页面边距

2023-05-12
如何优雅地设置字母间距?

2023-05-12
如何设置CSS中的右边距来优化页面排名

2023-05-12
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)