html - 取id时如何套用css样式?

标签 html css

我是 HTML 编码的新手,我有以下 HTML 元素:

<div id="map_canvas" style="width: 500px; height: 370px; border: 1px solid #ccc; float: left"></div>

我想为它申请一个“左”CSS 样式。我一直在为其他元素使用:

<div id="left" >
    <input type=button ....
</div>

但是,如您所见,map_canvas 已经是我的元素的 id,所以我如何应用 id left 的 CSS 元素> 为了它?

最佳答案

#map_canvas {
    width: 500px;
    height: 370px;
    border: 1px solid #ccc;
}

.left {
    float: left;
    border: 1px solid blue;
}

<div class="left">Not #map_canvas</div>
<div id="map_canvas" class="left">This is #map_canvas .left</div>

http://jsfiddle.net/94dwn/3/

请注意,如果“下一个”元素未被“清除”,则元素的宽度决定了它会 float 到下一个“行”,因此如果一个元素在显示上比之前的 floated 元素,它可以转到该元素的右侧/左侧。在这种情况下,您应该使用 clear

<div class="left">Not #map_canvas</div>
<div id="map_canvas" class="left">This is #map_canvas .left</div>
<div class="cleared">Clear</div>

#map_canvas {
    width: 500px;
    height: 370px;
    border: 1px solid #ccc;
}
.left {
    float: left;
    border: 1px solid blue;
}
.cleared {
    clear: both;
    background: green;
    border: 1px solid blue;
    color: white;
}

http://jsfiddle.net/94dwn/4/

关于html - 取id时如何套用css样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8691305/

相关文章:

css - Bootstrap 4 网格元素不在同一水平线上

html - 获取 CSS :hover on overlapping siblings

javascript - 即使仅呈现文本,Firefox 上的 HTML5 低 FPS

html - CSS/HTML 链接问题

html - 如何允许在 RichHtmlField 控件中使用 javascript?

css - 限制图像的最大高度,这取决于其 sibling 的高度

html - 背景附件问题

javascript - Image Fades Into Another,然后函数加载新网页

html - 如何在 Bulma 模式页脚中的同一水平位置具有左对齐和右对齐的内容?

JavaScript 函数未加载/工作