html - 如何正确对齐 Div

标签 html css reactjs

我试图对齐我的 DIV 以形成子弹图。我创建了一个示例数据,我将使用它使子弹图变得动态 这是代码:

      {bulletchartdata.map(
        (item,index)=>{
            return(
    <div>
        <div className="satisfactory"style={{width:item.satisfactoryVal,height:20,backgroundColor:'#f2f2f2'}}/> 
            <div className="badcolor"style={{width:item.badVal,height:20,backgroundColor:'#d8d6d6'}}/>
                <div className="measure"stlye={{width:item.performanceVal,height:10,backgroundColor:'#8c8c8c'}}> 
                    <div className="target"style={{width:item.symbolMarker,width:1,height:15,backgroundColor:'#000000'}}>     
            </div>
        </div>
    </div>
            );

        }
    )}

这是我想在 div 中传递而不是在 CSS 中声明它的示例数据:

    export const bulletchartdata = [
    { performanceVal: 100, symbolMarker: 100, badVal: 300, satisfactoryVal: 150}

这是 CSS:(现在我不想使用这个 CSS(这就是为什么有些行在“注释”中)因为我试图在 DIV 中输入所有样式(检查 div 代码)

    .satisfactory {     
      /* width:300px; */
     /*  height:20px; */
      background-color:#f2f2f2;  /* satisfactory */
      position:relative;
    }


    .badcolor {    
      /* width:160px; */
      /* height:20px; */
      background-color:#d8d6d6; /* badcolor */
      position:relative;
      bottom:0px;
    }


    .measure{  
      width:275px;
      height:7px;
      background-color:#8c8c8c; /* Performance Measure */
      position:inherit;
      right:0px;
      top:9px; 
      bottom:0px;
    }

    .target{   
      width:1px;
     /*  height:20px; */
      background-color:#000000;  /* target */
      position: inherit;
      left:240px;
      right:0px;
      top:-6.5px; 
      bottom:0px;
    }

Here is the Result

I want it to look like this

最佳答案

您将需要对内部 div 使用 position:absolute...并更改您的标记 (无需将每个 div 都包裹在里面)...

.satisfactory {
  position: relative;
}

.satisfactory div {
  position: absolute;
}
<div class="satisfactory" style="background: #f2f2f2;width: 300px;height: 40px;">
  <div class="badcolor" style="background-color:#d8d6d6;height:40px;width:150px;"></div>
  <div class="measure" style="background-color:#8c8c8c;height:10px;width:250px;top:15px"></div>
  <div class="target" style="background-color:#000000;height:40px;width:1px;left:200px;top:0;"></div>
</div>

关于html - 如何正确对齐 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49283220/

相关文章:

jquery - ASP-MVC : Different style-CSS for different languages

javascript - 将 Gatsby JS 站点部署到 Netlify 时出现 "Error running command: Build script returned non-zero exit code"

javascript - 如何在 while 循环中为每个 div 创建一个 ID 并使用 JQuery 发出警报

html - 围绕多行 float div 的 CSS 收缩以适合容器 div

javascript - 加载外部页面时显示图像

html - 如何使用 CSS 倾斜 div 布局,并允许图像填充倾斜区域

javascript - 我可以在没有 webpack 的情况下使用 react 和 babel 吗?或者我需要所有 3 个组合吗?

reactjs - 在 useEffect 中访问 Formik 的 setValues

html - html5 <audio>标签在移动浏览器中的功能受阻

html - 我想更改 Bootstrap 导航栏的背景颜色。