草庐IT

CSS基础-09-布局(定位 position、浮动float,元素对其、图像对其、文本对齐、元素内内容对齐,元素堆叠)

开发运维玄德公 2023-03-28 原文

1 定位 (position)

1.1 static 定位

  • 效果

静态定位的元素不会受到 top, bottom, left, right影响。

  • 示例
div.static {
    position: static;
}

1.2 fixed 定位

  • 作用
    相对于浏览器窗口是固定的,即不会因滚动条移动
  • 示例
p.pos_fixed
{
    position:fixed;

}

1.3 relative 定位

  • 效果
    相对其正常位置移动
  • 示例
{
    position:relative;
    left:-20px;
}

h2.pos_right
{
    position:relative;
    left:20px;
}

可使用: top, bottom, left, right

1.4 absolute 定位

  • 效果
    定位在页面的一个绝对位置
  • 示例
{
    position:absolute;
    left:150px;
    top:150px;
}

1.5 sticky 定位

  • 效果
    在一定范围( 定义top, bottom, left, right)内正常移动,超出则不移动
  • 示例
div.sticky {
    position: -webkit-sticky; /* Safari */
    position: sticky;
    top: 0;
}

2. 浮动(float:)

2.1 简单使用

float:right;

说明:值可以是top, bottom, left, right

2.2 相邻元素的浮动

  • 设置 margin 后,元素会以该间距排列
{
    float:left;
    margin:5px;
}
  • 元素大小不同儿排不齐,可以定义一下元素大小
{
    float:left;
    width:110px;
    height:90px;
    margin:5px;
}

2.3 清除浮动 clear

  • 效果

如果没有足够空间,浮动的元素会变成一行相邻在一起。
使用 clear 之后,该元素左右不会出现浮动元素

  • 示例
clear:both;

2.4 示例-首字浮动

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>玄德公记事</title>
<style>
span
{
    float:left;
    width:1.2em;
    font-size:300%;
    font-family:algerian,courier;
    line-height:80%;
}
</style>
</head>

<body>
    <h2 style="text-align:center">隆中对</h2>
<p>
<span>自</span>董卓已来,豪杰并起,跨州连郡者不可胜数。曹操比于袁绍,则名微而众寡,然操遂能克绍,以弱为强者,非惟天时,抑亦人谋也。今操已拥百万之众,挟天子而令诸侯,此诚不可与争锋。孙权据有江东,已历三世,国险而民附,贤能为之用,此可以为援而不可图也。荆州北据汉、沔,利尽南海,东连吴会,西通巴、蜀,此用武之国,而其主不能守,此殆天所以资将军,将军岂有意乎?益州险塞,沃野千里,天府之土,高祖因之以成帝业。刘璋暗弱,张鲁在北,民殷国富而不知存恤,智能之士思得明君。将军既帝室之胄,信义著于四海,总揽英雄,思贤如渴,若跨有荆、益,保其岩阻,西和诸戎,南抚夷越,外结好孙权,内修政理;天下有变,则命一上将将荆州之军以向宛、洛,将军身率益州之众出于秦川,百姓孰敢不箪食壶浆以迎将军者乎?诚如是,则霸业可成,汉室可兴矣!
</p>

</body>
</html>
  • 效果


    image.png

2.5 示例 一个网页

说明:这个代码是教程上抄来的

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>蜀</title> 
<style>
* {
    box-sizing: border-box;
}
body {
    margin: 0;
}
.header {
    background-color: #2196F3;
    color: white;
    text-align: center;
    padding: 15px;
}
.footer {
    background-color: #444;
    color: white;
    padding: 15px;
}
.topmenu {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #777;
}
.topmenu li {
    float: left;
}
.topmenu li a {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 16px;
    text-decoration: none;
}
.topmenu li a:hover {
    background-color: #222;
}
.topmenu li a.active {
    color: white;
    background-color: #4CAF50;
}
.column {
    float: left;
    padding: 15px;
}
.clearfix::after {
    content: "";
    clear: both;
    display: table;
}
.sidemenu {
    width: 25%;
}
.content {
    width: 75%;
}
.sidemenu ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}
.sidemenu li a {
    margin-bottom: 4px;
    display: block;
    padding: 8px;
    background-color: #eee;
    text-decoration: none;
    color: #666;
}
.sidemenu li a:hover {
    background-color: #555;
    color: white;
}
.sidemenu li a.active {
    background-color: #008CBA;
    color: white;
}
</style>
</head>
<body>

<ul class="topmenu">
  <li><a href="#home" class="active">主页</a></li>
  <li><a href="#news">新闻</a></li>
  <li><a href="#contact">联系我们</a></li>
  <li><a href="#about">关于我们</a></li>
</ul>

<div class="clearfix">
  <div class="column sidemenu">
    <ul>
      <li><a href="#flight">The Flight</a></li>
      <li><a href="#city" class="active">The City</a></li>
      <li><a href="#island">The Island</a></li>
      <li><a href="#food">The Food</a></li>
      <li><a href="#people">The People</a></li>
      <li><a href="#history">The History</a></li>
      <li><a href="#oceans">The Oceans</a></li>
    </ul>
  </div>

  <div class="column content">
    <div class="header">
      <h1>The City</h1>
    </div>
    <h1>Chania</h1>
    <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
    <p>You will learn more about responsive web pages in a later chapter.</p>
  </div>
</div>

<div class="footer">
  <p>底部文本</p>
</div>

</body>
</html>

3. 对齐

3.1 各种元素居中对齐

3.1.1 元素居中对齐

  • 使用
    margin: auto; 可使元素居中对齐
  • 示例
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>hello world</title>
    <style>
        .center {
            margin: auto;
            width: 60%;
            border: 3px solid #73AD21;
            padding: 10px;
        }
    </style>
</head>

<body>

    <div class="center">
        <p>margin: auto 使得元素居中对齐</p>
    </div>

</body>

</html>
  • 结果显示

如图可见,绿色框(不是文字)在页面的中间。

image.png

3.1.2 图像居中对齐

  • 使用
    同样使用margin: auto;
    但是image是内联元素,需要用display: block
  • 示例
img {
    display: block;
    margin: 0 auto;
}

3.1.3 文本居中对齐

之前《文本和字体》中有详细说明

text-align: center;

3.2 左或右对齐

3.2.1 position 实现

.right {
    position: absolute;
    right: 0px;
}

3.2.2 使用 float 方式

.right {
    float: right;
}

3.3 边框中内容居中对齐

3.3.1 使用 padding实现

说明:padding是内边距,在之前的文档 “盒子和轮廓”中可以看到详细说明。我们可以用内边距将内容“推到”居中的位置。

  • 代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>hello world</title>
    <style>
        .center {
            padding: 70px;
            border: 3px solid green;
            width: 150px

        }
    </style>
</head>

<body>

    <div class="center">
        <p>元素中的文字居中对齐</p>
    </div>

</body>

</html>
  • 显示


    image.png

3.3.2 使用 line-height

  • 方法

line-height 设置文本行高是200
height设置元素高200,则文本正好垂直居中。
text-align: center;使得文本水平居中

  • 示例
.center {
    line-height: 200px;
    height: 200px;
    border: 3px solid green;
    text-align: center;
}

4. 元素的堆叠顺序

  • 示例

元素定义了一个绝对位置,会与其他元素堆叠。设置z-index:-1 后,将其放到下层

img
{
    position:absolute;
    left:0px;
    top:0px;
    z-index:-1;
}

image.png

有关CSS基础-09-布局(定位 position、浮动float,元素对其、图像对其、文本对齐、元素内内容对齐,元素堆叠)的更多相关文章

  1. ruby - capybara field.has_css?匹配器 - 2

    我在MiniTest::Spec和Capybara中使用以下规范:find_field('Email').must_have_css('[autofocus]')检查名为“电子邮件”的字段是否具有autofocus属性。doc说如下:has_css?(path,options={})ChecksifagivenCSSselectorisonthepageorcurrentnode.据我了解,字段“Email”是一个节点,因此调用must_have_css绝对有效!我做错了什么? 最佳答案 通过JonasNicklas得到了答案:No

  2. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  3. ruby - 在哈希的键数组中追加元素 - 2

    查看我的Ruby代码:h=Hash.new([])h[0]=:word1h[1]=h[1]输出是:Hash={0=>:word1,1=>[:word2,:word3],2=>[:word2,:word3]}我希望有Hash={0=>:word1,1=>[:word2],2=>[:word3]}为什么要附加第二个哈希元素(数组)?如何将新数组元素附加到第三个哈希元素? 最佳答案 如果您提供单个值作为Hash.new的参数(例如Hash.new([]),完全相同的对象将用作每个缺失键的默认值。这就是您所拥有的,那是你不想要的。您可以改用

  4. css - 用 watir 检查标签类? - 2

    我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes

  5. 「Python|Selenium|场景案例」如何定位iframe中的元素? - 2

    本文主要介绍在使用Selenium进行自动化测试或者任务时,对于使用了iframe的页面,如何定位iframe中的元素文章目录场景描述解决方案具体代码场景描述当我们在使用Selenium进行自动化测试的时候,可能会遇到一些界面或者窗体是使用HTML的iframe标签进行承载的。对于iframe中的标签,如果直接查找是无法找到的,会抛出没有找到元素的异常。比如近在咫尺的例子就是,CSDN的登录窗体就是使用的iframe,大家可以尝试通过F12开发者模式查看到的tag_name,class_name,id或者xpath来定位中的页面元素,会抛出NoSuchElementException异常。解决

  6. postman接口测试工具-基础使用教程 - 2

    1.postman介绍Postman一款非常流行的API调试工具。其实,开发人员用的更多。因为测试人员做接口测试会有更多选择,例如Jmeter、soapUI等。不过,对于开发过程中去调试接口,Postman确实足够的简单方便,而且功能强大。2.下载安装官网地址:https://www.postman.com/下载完成后双击安装吧,安装过程极其简单,无需任何操作3.使用教程这里以百度为例,工具使用简单,填写URL地址即可发送请求,在下方查看响应结果和响应状态码常用方法都有支持请求方法:getpostputdeleteGet、Post、Put与Delete的作用get:请求方法一般是用于数据查询,

  7. 软件测试基础 - 2

    Ⅰ软件测试基础一、软件测试基础理论1、软件测试的必要性所有的产品或者服务上线都需要测试2、测试的发展过程3、什么是软件测试找bug,发现缺陷4、测试的定义使用人工或自动的手段来运行或者测试某个系统的过程。目的在于检测它是否满足规定的需求。弄清预期结果和实际结果的差别。5、测试的目的以最小的人力、物力和时间找出软件中潜在的错误和缺陷6、测试的原则28原则:20%的主要功能要重点测(eg:支付宝的支付功能,其他功能都是次要的)80%的错误存在于20%的代码中7、测试标准8、测试的基本要求功能测试性能测试安全性测试兼容性测试易用性测试外观界面测试可靠性测试二、质量模型衡量一个优秀软件的维度①功能性功

  8. ruby-on-rails - 在 Ruby (on Rails) 中使用 imgur API 获取图像 - 2

    我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path

  9. python ffmpeg 使用 pyav 转换 一组图像 到 视频 - 2

    2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p

  10. ES基础入门 - 2

    ES一、简介1、ElasticStackES技术栈:ElasticSearch:存数据+搜索;QL;Kibana:Web可视化平台,分析。LogStash:日志收集,Log4j:产生日志;log.info(xxx)。。。。使用场景:metrics:指标监控…2、基本概念Index(索引)动词:保存(插入)名词:类似MySQL数据库,给数据Type(类型)已废弃,以前类似MySQL的表现在用索引对数据分类Document(文档)真正要保存的一个JSON数据{name:"tcx"}二、入门实战{"name":"DESKTOP-1TSVGKG","cluster_name":"elasticsear

随机推荐