2016-02-29 66 views
-5

我在C#后面创建了以下div,但无法在屏幕的center中使用position。任何人都有合适的答案是值得赞赏的要做到这一点如何居中绝对定位动态创建div

<div class="rect" style="top:" + (rectangles.ElementAt(i).Y + 50).ToString() + "px;left:" + (rectangles.ElementAt(i).X + 50).ToString() + "px;width:" + (rectangles.ElementAt(i).Width - 1).ToString() + "px;height:" + (rectangles.ElementAt(i).Height - 1).ToString() + "px;border-color:Black;text-shadow:3px 3px Black;border-width:3px;border-style:solid;border-collapse:seperate;position:absolute;background-color:#" + rectangles.ElementAt(i).Slice.Elements.ElementAt(0).color.ToString() + ";vertical-align:middle;text-align:center;font-family:Arial;font-size:48px;color:White;font-weight:bold;display:table-cell;" ><div style="position: relative;top: 50%;transform: translateY(-50%);"><span>" + rectangles.ElementAt(i).Slice.Elements.ElementAt(0).Symbol + "</span><br/><span>" + rectangles.ElementAt(i).Slice.Elements.ElementAt(0).AverageMC + "%</span></div></div> 
+0

只有这个代码将不会帮助..'提供more' – Moumit

+0

可能的复制[如何中心与位置绝对动态创建一个div(HTTP ://stackoverflow.com/questions/35696003/how-to-center-a-div-created-dynamically-with-position-absolute) –

回答

0
  1. 单程被设定的宽度,然后使用margin: 0 auto;

<style> div { width: 360px; margin: 0 auto; } </style>

  • 另一种方式通过CSS居中一个div是
  • <style> div { width: 360px; margin-left:auto; margin-right:auto; float:none; } </style>

    使用ID和类div否则将适用于所有的div