简体   繁体   English

动态添加div:固定,动​​态宽度,居中

[英]Dynamically add div: fixed, dynamic width, centred

I know this is a common issue, but hear me out, because I haven't seen any solutions that have worked. 我知道这是一个常见问题,但是请听我说,因为我还没有看到任何可行的解决方案。

This centred div works perfectly on jsfiddle, but not with my chrome extension. 这个居中的div可以完美地在jsfiddle上运行,但是不能与我的chrome扩展一起使用。 In my chrome extension I dynamically append a div with this css to the body of a given webpage (using document.body.appendChild), but it doesn't show up. 在我的chrome扩展程序中,我将带有此CSS的div动态附加到给定网页的正文中(使用document.body.appendChild),但未显示。 When I inspect further, I can see that the website that I am visiting is applying it's own styles to my div; 当我进一步检查时,可以看到我访问的网站正在将自己的样式应用于div; I'm not sure if this affects things. 我不确定这是否会影响事情。

http://jsfiddle.net/StEek/ http://jsfiddle.net/StEek/

html: HTML:

.myDiv{
    position: fixed;
    /* center the element */
    right: 0;
    left: 0;
    margin-right: auto;
    margin-left: auto;
    /* give it dimensions */
    min-height: 10em;
    width: 90%;
    background-color: red;
}

css: CSS:

<div class="myDiv">
    <p>coolio</p>
</div>

Is there any way to dynamically add a div, with ONLY my style applied to it, to ANY webpage? 有什么方法可以将仅应用我的样式的div动态添加到任何网页吗? I want a fixed, dynamic width centred div that will be appended to the body element. 我想要一个固定的,动态宽度居中的div,该div将附加到body元素上。

You need to give your div an id and set your styles for that id instead of the class element. 您需要给div一个id并为该id设置样式,而不是class元素。

If a website set styles for all divs on the webpage, you will need to override these styles with your own. 如果网站为网页上的所有div设置了样式,则您需要使用自己的样式来覆盖这些样式。

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

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