gpt4 book ai didi

css - 在 XHTML 页面上定位对象

转载 作者:太空宇宙 更新时间:2023-11-04 05:22:15 24 4
gpt4 key购买 nike

我正在为 Web 应用程序创建 Web 用户界面。该界面有页眉、页脚和水平线中的两个矩形之间。矩形由空格分隔。所有这些功能都是使用标签创建的。 下面给出了 CSS 来格式化左边的矩形:

#sidebar1 {    
position: absolute;
left: 150px;
width: 450px;
height:250px;
background: #EBEBEB;
padding: 20px 0;
padding-left:20px;
padding-right:20px;
margin: 50px auto;
border: 1px solid #000000;

}

问题是,当您挤压或扩展浏览器窗口时,这些矩形会改变位置或相互重叠。这是 Not Acceptable 。我在 CSS 中尝试了 position 关键字的其他选项,例如 position: fixed 或 position:relative 或 static 但表示仍然是错误的。同样,我尝试了 float:left 和 float: right 但这仍然使矩形移动和重叠。尽管您正在扩展或挤压窗口,但我希望这些矩形静止不动。请问你的解决方法是什么?最好的问候enter image description here

当前代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to Spring Web MVC project</title>
<style type="text/css">


body {
font: 100% Verdana, Arial, Helvetica, sans-serif;
margin: 0; /* it's good practice to zero the margin and padding of the body element to account for differing browser defaults */
/*padding: 0;
text-align: center; /* this centers the container in IE 5* browsers. The text is then set to the left aligned default in the #container selector */
color: black;
}

#header {
background: green;
/* top: 100px; */
text-align: center;
margin: 55px;
padding: 0 10px; /* this padding matches the left alignment of the elements in the divs that appear beneath it. If an image is used in the #header instead of text, you may want to remove the padding. */
}


#sidebar1 {
position: absolute;
left: 150px;
width: 450px; /* since this element is floated, a width must be given */
height:250px;
background: #EBEBEB; /* the background color will be displayed for the length of the content in the column, but no further */
padding: 20px 0; /* top and bottom padding create visual space within this div */
padding-left:20px;
padding-right:20px;
margin: 50px auto;
border: 1px solid #000000;

}
#sidebar2 {
position: absolute;
right: 150px;
width: 450px; /* since this element is floated, a width must be given */
height: 250px;
background: #EBEBEB; /* the background color will be displayed for the length of the content in the column, but no further */
padding: 20px 0; /* top and bottom padding create visual space within this div */
padding-left:20px;
padding-right:20px;
margin: 50px auto;
border: 1px solid #000000;

}




#footer {
padding: 0 10px; /* this padding matches the left alignment of the elements in the divs that appear above it. */
background:green;
width:95%;
margin:55px;
position: relative;
bottom: -300px;;

}

.clearfloat { /* this class should be placed on a div or break element and should be the final element before the close of a container that should fully contain a float */
clear:both;
height:0px;
font-size: 1px;
line-height: 0px;
}


</style><!--[if IE]>
<style type="text/css">
/* place css fixes for all versions of IE in this conditional comment */
.thrColHybHdr #sidebar1, .thrColHybHdr #sidebar2 { padding-top: 30px; }
.thrColHybHdr #mainContent { zoom: 1; padding-top: 15px; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]--></head>

<body>
<div id="header">
<h1>Header</h1>
<!-- end #header --></div>
<!--<div id="container"> -->

<div id="sidebar1">
<h3>blah blah</h3>
<p><li>blah blah</li> </p>
<p><li>blah blah</li> </p>
<p><li>blah blah</li></p>
<!-- end #sidebar1 --></div>

<div id="sidebar2">
<h3>blah blah</h3>
<p><li>blah blah</li> </p>
<p><li>blah blah </li></p>
<p><li>blah blah</li></p>
<!-- end #sidebar2 --></div>
<p>
<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
</p>


<p><br class="clearfloat" />
</p>
<div id="footer">
<p>Footer</p>
<p>Terms & Conditions</p>

<!-- end #footer --></div>
<!-- end #container --> <!--</div> -->
</body>
</html>

最佳答案

没有理由在这里对任何东西进行绝对定位……这只会让人头疼。一般来说,只有在绝对必要时才做绝对定位,这对我来说几乎完全是当我必须让某物漂浮在其他东西之上时。否则,一切都与其他事物相关,这是默认的。

这里有一个简单的策略来实现这一点:

<div class="pagecontainer">
<div class="header">
Header Goes Here
</div>

<div class="bodycontainer">
<div class="floatleft">Leftbox</div>
<div class="floatleft">Rightbox</div>
<div class="clear"></div>
</div>

<div class="footer">
Footer Goes Here
</div>
</div>

样式如下:

.floatleft {
float: left;
otherstyle: here;
margin: 5px;
}

.clear {
clear:both;
}

是的,这是抽象的,因此您必须根据您的特定目的对其进行自定义。

避免重叠的关键是确保 body 容器内的 div 永远不会超过 bodycontainer 父元素的大小(不要忘记边距,在某些情况下填充(愚蠢的 IE)计入该值)并且两者之后向左漂浮并带有清晰的漂浮物。

如果你想让它变得简单得吓人,可以使用像 960.gs 这样的网格。

关于css - 在 XHTML 页面上定位对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6295643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com