简体   繁体   English

CSS Div of Width自动填充整个父Div

[英]CSS Div Of Width Auto Filling Entire Parent Div

This is my code: 这是我的代码:

.divUserRepCont
{
    position: absolute;

    top: 0;
    left: 105px;

    width: 195px;
    height: 25px;   
}

.divUserContCon
{
    width: auto;
    height: 100%;

    background-color: red;
}

.divUserCon
{
    width: 50px;
    height: 20px;
}

HTML: HTML:

<div class="divUserRepCont">
    <div class="divUserContCon">
        <div class="divUserCon">

        </div>
    </div>
</div>

I am expecting a red bar of width 50px, but instead the auto width "divUserContCon" is filling up the entire 195px of it's parent div. 我期望宽度为50px的红色条,但是自动宽度“ divUserContCon”填充了其父div的整个195px。 Why is this? 为什么是这样?

EDIT: 编辑:

The purpose of divUserContCon having an auto width is because of this: divUserContCon具有自动宽度的目的是因为:

divUserCon's size will be changing dynamically, and it will have a background colour itself. divUserCon的大小将动态变化,并且其自身将具有背景色。

divUserContCon, will be the container for divUserCon, which itself will have a background colour AND padding. divUserContCon将是divUserCon的容器,该容器本身将具有背景色和填充。

So if divUserCon is 50px wide, with a green background, divUserContCon will be 50px wide (auto) + some padding and it's background colour. 因此,如果divUserCon的宽度为50px,背景为绿色,则divUserContCon的宽度为50px(自动)+一些填充和背景色。

width:50px; 

Needs to be put in divUserContCon. 需要放在divUserContCon中。 You can't rely on auto to only take up the width of any child divs. 您不能仅依靠auto来占用任何子div的宽度。

Try this 尝试这个

<style type="text/css">
.divUserRepCont {
  position: absolute;
  top: 0;
  left: 105px;
  width: 195px;
  height: 25px;   
}
.divUserContCon {
    width: auto;
    height: 25px;
}
.divUserCon {
    width: 50px;
    height: 25px;
    background-color: red;
}
</style>

If you want your .divUserCon in the center then just add margin: 0 auto; 如果您想将.divUserCon放在center则只需添加margin: 0 auto; in the css. 在CSS中。

This is how I solved it: 这是我解决的方法:

.divUserRepCont
{
    position: absolute;

    top: 0;
    left: 105px;

    width: 195px;
    height: 25px;   

    background-color: orange;
}

.divUserContCon
{
    display: inline-block;

    height: 100%;

    padding-right: 20px;

    background-color: yellow;
}

.divUserCon
{
    display: inline-block;
    height: 20px;

    border-top: 5px solid rgb(62, 62, 62);

    background-color: red;
}

I replaced the width: auto with display: inline-block; 我将width:auto替换为display:inline-block;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM