GVKun编程网logo

css如何设置div的宽度(css设置div大小)

4

本文的目的是介绍css如何设置div的宽度的详细情况,特别关注css设置div大小的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解css如何设置div的宽度的机会,同

本文的目的是介绍css如何设置div的宽度的详细情况,特别关注css设置div大小的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解css如何设置div的宽度的机会,同时也不会遗漏关于css div的宽度随内部元素的宽度的变化而变化_html/css_WEB-ITnose、css – Div为页面宽度的100%,减去右边固定div的宽度(但没有设置边距右)、css – 如何使div的宽度在两个div之间拉伸、css – 如何使一个父div自动大小到其子div的宽度的知识。

本文目录一览:

css如何设置div的宽度(css设置div大小)

css如何设置div的宽度(css设置div大小)

css设置div的宽度的方法是,给div添加width属性,并将属性值设置为合适的值,例如【width:100px;】。我们还可以给div设置百分比宽度,如【width:50%;】。

本文操作环境:windows10系统、css 3、thinkpad t480电脑。

其实我们要设置div的宽度是非常简单的,因为css中有现成的属性width,该属性就是用来设置元素的宽度的。

属性值:

  • auto 默认值。浏览器可计算出实际的宽度。

  • length 使用 px、cm 等单位定义宽度。

  • % 定义基于包含块(父元素)宽度的百分比宽度。

  • inherit 规定应该从父元素继承 width 属性的值。

示例代码:

<!DOCTYPE html>
<html>
<head>
<Meta charset=utf-8> 
<title></title> 
<style>
img.normal
{
	height:auto;
}
img.big
{
	height:120px;
}
p.ex
{
	height:100px;
	width:100px;
}
</style>
</head>

<body>
<img class=normal src=logocss.gif width=95 height=84 /><br>
<img class=big src=logocss.gif width=95 height=84 />
<p class=ex>这个段落的高和宽是 100px.</p>
<p>这是段落中的一些文本。这是段落中的一些文本。
这是段落中的一些文本。这是段落中的一些文本。
这是段落中的一些文本。这是段落中的一些文本.</p>
</body>
</html>

相关视频分享:css视频教程

css div的宽度随内部元素的宽度的变化而变化_html/css_WEB-ITnose

css div的宽度随内部元素的宽度的变化而变化_html/css_WEB-ITnose

div是块级元素,如果不设置他的明确的宽度,那他就等于父元素的宽度,如果要让其它随着子元素的变化而变化,需要改变他的属性,给他加上这个css属性即可display:inline-block

或干脆把div直接替换为span解决一切问题

css – Div为页面宽度的100%,减去右边固定div的宽度(但没有设置边距右)

css – Div为页面宽度的100%,减去右边固定div的宽度(但没有设置边距右)

我把头发拉了出来因为我无法解决这个问题.我正在开发一个wiki.典型页面右侧有一个侧栏,宽度固定.此侧边栏的左侧是文本,div,表格等(页面内容).基本上,我希望一切都在全宽 – 这意味着如果它在侧边栏旁边,它应该是全宽减去侧边栏的长度.如果它不在侧边栏旁边,则应该是页面的整个宽度.像这样:

_______________________________
|                          ___  |
| text text text text te- |   | |
| xt text text text text. | S | | 
| ______________________  | B | |
| |                     | |   | |
| |        table        | |___| |
| |          or         |       |
| |         div         |       |
| |_____________________|       |
|                               |
| more text text text text tex- |
| t text text text text text t- |
| ext text text text text text  |
| ____________________________  |
| |        another table      | |
| |             or            | |
| |            div            | |
| |___________________________| |
|_______________________________|

在文本的情况下,它完全包围侧边栏.如果我有一个超长句子,它一直到侧边栏的左边,然后打破一个新的行.如果我有一个超长段落,所有文本都会在侧边栏旁边突然出现,并且也会在它下面.

但是,我遇到的问题是当我想在侧边栏的左侧放置div时.设置为100%时,div不会考虑侧边栏的宽度并扩展到页面的整个宽度,从而与侧边栏重叠.通常我只是将div设置为侧边栏宽度的右边距.但是,出于我的目的,这是不可行的,因为在不同的屏幕分辨率下,div可能在侧边栏下面的页面上较低,因此div的右边会有一个空白的间隙(加上它只是更容易编码,以解决这个问题,所以页面的div知道它们应该有多宽.)例如,在我上面的文字图片中,如果页面较薄,因为它的屏幕分辨率较低,则第一个框可能位于侧边栏下方.使用侧边栏的边距右侧,这意味着框的右侧会有很大的间隙,并且它不会是页面的整个宽度.

这是我正在使用的代码.

<html>
<head>
<style type="text/css">#sidebar {
float: right;
width: 250px;
height: 200px;
border: 1px solid red;
}

#la {
border: 1px solid green;
width: 100%;
}
</style>
</head>
<body>

<div id="content">

    <div id="sidebar">Sidebar with fixed width</div>

    <p>The sun is a star.</p>
    <p>It's yellow.</p>
    <p>It's very hot.</p>

    <div id="la">This div is next to the sidebar and should bump up right next to its left side,as in,100% of the page minus the width of the sidebar. It should not overlap the sidebar. This is not something that can just be done automatically with margin-right using the width of the sidebar because it might not actually be next to the sidebar on certain screen resolutions.</div>

    <p>The sun is a star.</p>
    <p>It's yellow.</p>
    <p>It's very hot.</p>

    <div id="la">This div is NOT next to the sidebar on most screen resolutions - it is under it where there is nothing to the right of it,so it should be the full width of the page.</div>


</div>

</body>
</html>

我希望有一种方法可以做到这一点,如果东西旁边的东西,如div中的表,它将在页面的最大宽度减去侧边栏的宽度.如果它不在侧边栏旁边,那么它就是页面的整个宽度.我想我只是希望其他所有内容都像文本一样 – 它会直接转到它右侧的任何内容(如果侧边栏不在那里,可以是侧边栏,如果侧边栏不在那里,可以是页面右侧).

先感谢您!

总结

以上是小编为你收集整理的css – Div为页面宽度的100%,减去右边固定div的宽度(但没有设置边距右)全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

css – 如何使div的宽度在两个div之间拉伸

css – 如何使div的宽度在两个div之间拉伸

我目前的问题是我有三个div元素;一个漂浮在左边,一个漂浮在右边,一个漂浮在这两个之间.我希望中心div自动拉伸到两个div之间可用宽度的最大宽度.

HTML

<div id="contain">
    <div id="left">1</div>
    <div id="filler"></div>
    <div id="right">2</div>
</div>

CSS

#left {
    text-decoration: none;
    display: inline-block;
    float: left;
    width: auto;
    padding: 0px 20px 0px 20px;
    height: 45px;
    text-align: center;
    line-height: 300%;
    background: #FF9000;
    color: #FFFFFF;
}
#navFiller {
    display: inline-block;
    position: fixed;
    float: left;
    width: auto;
    height: 45px;
    background: #FF9000;
}
#right {
    text-decoration: none;
    display: inline-block;
    float: right;
    width: auto;
    padding: 0px 20px 0px 20px;
    height: 45px;
    text-align: center;
    line-height: 300%;
    background: #FF9000;
    color: #FFFFFF;
}
#contain {
    width: 100%;
    height: 45px;
    padding: 0;
    margin: 0;
    display: inline;
}

Jsfiddle项目:

http://jsfiddle.net/msEBU/

解决方法

如果在浮动元素之后添加填充元素,然后稍微更改其样式(包括为样式块提供正确的ID),您可以得到您想要的内容:
#left {
  display: inline-block;
  float: left;
  padding: 0px 20px 0px 20px;
  height: 45px;
  text-align: center;
  line-height: 300%;
  background: #FF9000;
  color: #FFFFFF;
}
#filler {
  display: block;
  float: none;
  height: 45px;
  background: #F00;
}
#right {
  display: inline-block;
  float: right;
  padding: 0px 20px 0px 20px;
  height: 45px;
  text-align: center;
  line-height: 300%;
  background: #FF9000;
  color: #FFFFFF;
}
#contain {
  width: 100%;
  height: 45px;
  padding: 0;
  margin: 0;
  display: inline;
}
<div id="contain">
  <div id="left">1</div>
  <div id="right">2</div>
  <div id="filler">m</div>
</div>

或者,模拟一个表:

#contain {
   width: 100%;
   padding: 0;
   margin: 0;
   display: table;
 }
 #left,#right {
   text-decoration: none;
   display: table-cell;
   width: 5%;
   text-align: center;
   background: #FF9000;
   color: #FFFFFF;
   padding: 2% 0;
 }
 #filler {
   display: table-cell;
   width: auto;
   background: #F00;
 }
<div id="contain">
  <div id="left">1</div>
  <div id="filler">m</div>
  <div id="right">2</div>
</div>

这两种方法都有其优点.这取决于你哪个适合你.

css – 如何使一个父div自动大小到其子div的宽度

css – 如何使一个父div自动大小到其子div的宽度

我试图使父母的div只有孩子div的宽度。自动宽度使父div适合整个屏幕。孩子们的div将根据其内容而有所不同,所以我需要父div来进行相应的调整。谢谢你的帮助!
<div>
      <div>
           <h4>Column1</h4>
           <dl>
                <ddhttps://www.jb51.cc/tag/Now/" target="_blank">Nowrap;">1</dd>
                <ddhttps://www.jb51.cc/tag/Now/" target="_blank">Nowrap;">2</dd>
           </dl>

           <h4>Column1</h4>
           <dl>    
               <ddhttps://www.jb51.cc/tag/Now/" target="_blank">Nowrap;">1</dd>
               <ddhttps://www.jb51.cc/tag/Now/" target="_blank">Nowrap;">2</dd>
           </dl>
      </div>

      <div>
           <h4>Column2</h4>
           <dl>
                <ddhttps://www.jb51.cc/tag/Now/" target="_blank">Nowrap;">1</dd>
                <ddhttps://www.jb51.cc/tag/Now/" target="_blank">Nowrap;">2</dd>
                <ddhttps://www.jb51.cc/tag/Now/" target="_blank">Nowrap;">3</dd>  
           </dl>
      </div>
</div>

解决方法

你的内部< div>元素可能都是float:left。将尺寸自动分割为容器宽度的100%。尝试在容器div上使用display:inline-block而不是width:auto。或者可能浮动:离开容器,并应用overflow:auto。取决于你究竟在做什么。

今天关于css如何设置div的宽度css设置div大小的介绍到此结束,谢谢您的阅读,有关css div的宽度随内部元素的宽度的变化而变化_html/css_WEB-ITnose、css – Div为页面宽度的100%,减去右边固定div的宽度(但没有设置边距右)、css – 如何使div的宽度在两个div之间拉伸、css – 如何使一个父div自动大小到其子div的宽度等更多相关知识的信息可以在本站进行查询。

本文标签: