您的位置:

CSS布局与HTML定位

CSS布局和HTML定位是前端开发中必不可少的技术之一,可以帮助我们更好地控制和管理网页的布局和样式,提高用户体验和页面性能。本文将从以下几个方面进行详细阐述。

一、块级元素与行内元素

在CSS布局中,我们要用到块级元素和行内元素。块级元素指的是独占一行的元素,例如:div、p、h1~h6等。行内元素指的是不独占一行,可以与其他元素共存的元素,例如:a、span、img、input等。 块级元素可以设置宽度、高度和外边距、内边距等属性,可以作为容器来包含其他元素。而行内元素只能设置字体大小、颜色、字体样式等属性,不能设置宽度和高度等盒模型属性,也不能包含块级元素。

二、浮动布局

浮动布局常用于网页中的导航栏、图片和文字等排列布局,可以使元素摆脱文档流,避免文本和图片重叠,从而实现多个元素排列在一行或一列的效果。常见的浮动属性值有left和right。
.container{
    width: 960px;
    margin: 0 auto;
}
.box{
    float: left;
    width: 200px;
    height: 200px;
    margin-right: 20px;
}
上述代码将.box元素左浮动,并且设置了宽度、高度和右边距,其中.container是.box的父元素,用于居中样式。

三、绝对定位布局

绝对定位布局是基于元素的父元素进行定位的,通过设置元素的position属性为absolute实现,通常用于实现弹出框、菜单、轮播图等效果。绝对定位的元素会脱离文档流,并且不占据空间。
.parent{
    position: relative;
    width: 960px;
    height: 400px;
}
.child{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
上述代码将.parent元素设为相对定位,.child元素设为绝对定位,并且设置了偏移量和使用transform属性进行居中。

四、弹性盒模型布局

弹性盒模型布局(flex布局)是CSS3推出的一种新的布局方式,可以在不同的屏幕尺寸和设备上实现灵活的布局效果。通过设置父元素的display属性为flex,可以将子元素分配到不同的行或列中,并且自适应调整子元素的宽度和高度。
.container{
    display: flex;
    justify-content: center;
    align-items: center;
}
.box{
    flex: 1;
    height: 100px;
    margin: 10px;
}
上述代码将.container元素设置为flex布局,并且使用justify-content和align-items属性进行水平和垂直居中,.box元素设置了flex属性为1,即自适应分配宽度,并且设置了高度和外边距。

五、响应式布局

响应式布局是指在不同的设备使用不同的布局和样式,在移动设备上自动适应调整页面布局和内容。常见的响应式布局方法有媒体查询、flex布局、bootstrap等开源框架。 例如,以下是响应式布局的代码示例:
/* 移动设备 */
@media (max-width: 768px){
    .container{
        width: 100%;
    }
    .box{
        width: 50%;
        margin: 10px auto;
    }
}
/* PC设备 */
.container{
    width: 960px;
    margin: 0 auto;
}
.box{
    float: left;
    width: 200px;
    height: 200px;
    margin-right: 20px;
}
上述代码中使用@media媒体查询实现在不同设备宽度下的不同布局效果,屏幕宽度小于768px时,采用全屏布局和半屏显示的布局方式。 以上是CSS布局与HTML定位的相关内容,通过掌握不同的布局方式和技巧,可以更好地实现网页的样式和布局,提高用户的体验和页面的性能。
CSS布局与HTML定位

2023-05-12
CSS与HTML布局中的图像定位

2023-05-12
CSS定位和布局

2023-05-12
如何使用相对定位和CSS来布局HTML页面

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)