【问题标题】:Margin-left css not working左边距css不起作用
【发布时间】:2017-07-02 00:54:39
【问题描述】:

我正在使用 jquery 日期选择器,我在我的 jsp 页面中添加了日期选择器,我想将它向左移动,但它不起作用..
在我的 jsp 页面中,我有

<div id="datepicker" class="datepicker">
        <script>
        $("#datepicker").datepicker();
        </script>
        </div>

在我的 css 文件中,我有

.datepicker{

        margin-top:20px;
        margin-left:600px;
        position: fixed;

}

但是元素总是在左上角..有人对此有解决方案吗?当我想定位元素时,我总是使用 margin-top 和 margin-left,它总是有效..提前致谢!

【问题讨论】:

  • 那是因为您设置了position: fixed;,所以它会将其放在左上角,因为这些是topleft 的默认值(0)。如果您要这样定位它,请将topleft 设置为您想要的值。
  • 我放了top:20pxleft:600px,但它是一样的..据我所知,margin-left 和 margin-top 将该元素向左和底部移动该数量的像素,这就是我需要的..我不知道顶部和左侧与边缘顶部和边缘左侧有什么区别..

标签: css jquery-ui-datepicker


【解决方案1】:

我无法让 datepicker 函数在 codepen 上运行,但 CSS 格式化部分工作正常:https://codepen.io/anon/pen/OWdXKL

<style>
  .datepicker{
        margin-top:20px;
        margin-left:600px;
        position: fixed;
};
</style>

<div id="datepicker" class="datepicker">this div is 600 px from the left; 20 px from the top</div>

<script>
  $( "#datepicker" ).datepicker();
</script>

我的猜测是您的样式表中的其他地方会覆盖您提供的片段中定义的边距。

【讨论】:

  • 它现在可以工作了,在我在 Eclipse 中清理了项目并删除了历史之后......无论如何,谢谢!