简体   繁体   English

如何在div的左侧添加加号

[英]How to add a plus character at the left side of the div

I'm having some div and trying to add a plus character at the left side of the div after a h3 element. 我有一些div,并尝试在h3元素后的div左侧添加加号。 I've managed to add the plus with span but it won't go to the left side, it stays centered.. 我设法用span添加了加号,但不会移到左侧,而是保持居中。

 .wrapper { position: absolute; top: 0; left: 0; width: 100%; } .chatter { margin-top:5px; width:70%; border-spacing:5px; table-layout: fixed; empty-cells:show; border-collapse:separate; font:78%/130% "Arial", "Helvetica", sans-serif; border:1px solid gray; box-shadow:0px -1px 2px 1px #d2d6d8; border-top:1px solid #eee; border-right:1px solid #d2d5d7; border-left:1px solid #d2d5d7; border-bottom:1px solid #d2d5d7; border-radius:3px; font-size:large; } .c_head { box-shadow:0px -1px 2px 1px #d2d6d8; border-top:1px solid #eee; border-right:1px solid #d2d5d7; border-left:1px solid #d2d5d7; border-bottom:1px solid #d2d5d7; padding-right:9px; height:31px; overflow:hidden; margin-bottom:1px; border-radius:3px; } .c_header { margin:0; padding:0; padding-left:9px; color:#777777; overflow:hidden; height:31px; line-height:31px; font-size:1.2em; font-weight:bold; padding-bottom:3px; } 
 <div class="wrapper" style="height:100%; background-color:Silver;"> <center> <div class="chatter" runat="server"> <div class="c_head"> <h3 class="c_header">Chat - BETA<span style="text-align:left; padding-left:0; position:relative; left:0;">+</span></h3> </div> </div> </center> </div> 

How can I possibly set it so it aligns to the left in the div ? 如何设置它使其与div的左侧对齐? I've tried anything by now. 到目前为止,我已经尝试过任何方法。

You could achieve that by positioning the <span> element absolute ly and give a position of relative to the <div> to establish a containing block for the absolutely positioned element. 你可以实现,通过定位<span>元件absolute Ly和给出positionrelative<div>建立包含块的绝对定位的元素。

Then you could play with left or other offsets to move the element to the right position. 然后,您可以使用left偏移量或其他偏移量将元素移动到右位置。

 .wrapper { position: absolute; top: 0; left: 0; width: 100%; } .chatter { margin-top:5px; width:70%; border-spacing:5px; table-layout: fixed; empty-cells:show; border-collapse:separate; font:78%/130% "Arial", "Helvetica", sans-serif; border:1px solid gray; box-shadow:0px -1px 2px 1px #d2d6d8; border-top:1px solid #eee; border-right:1px solid #d2d5d7; border-left:1px solid #d2d5d7; border-bottom:1px solid #d2d5d7; border-radius:3px; font-size:large; margin-left: auto; /* <-- Added declarations */ margin-right: auto; /* instead of <center> */ text-align: center; /* dropped element. */ } .c_head { box-shadow:0px -1px 2px 1px #d2d6d8; border-top:1px solid #eee; border-right:1px solid #d2d5d7; border-left:1px solid #d2d5d7; border-bottom:1px solid #d2d5d7; padding-right:9px; height:31px; overflow:hidden; margin-bottom:1px; border-radius:3px; position: relative; /* <-- Added declaration */ } .c_header { margin:0; padding:0; padding-left:9px; color:#777777; overflow:hidden; height:31px; line-height:31px; font-size:1.2em; font-weight:bold; padding-bottom:3px; } .c_header > span { position:absolute; /* <-- Edited declaration */ left:0; } 
 <div class="wrapper" style="height:100%; background-color:Silver;"> <div class="chatter" runat="server"> <div class="c_head"> <h3 class="c_header">Chat - BETA<span>+</span></h3> </div> </div> </div> 


As a side-note: <center> element has been deprecated since HTML 4 and removed since HTML 5 — But some web browsers still support it. 附带说明: <center>元素自HTML 4起已被弃用,而自HTML 5起已被删除-但是某些Web浏览器仍支持该元素。 Therefore it shouldn't be used for new projects. 因此,不应将其用于新项目。

Instead, in order to align block-level elements at the center you could set the left and right margin of the element to auto . 相反,为了使块级元素在中心对齐,您可以将元素的左右margin设置为auto And for inline-level elements, you could set text-align: center on the parent element. 对于内联级别的元素,您可以将text-align: center设置在父元素的text-align: center

I think you can just float it left? 我想你可以把它浮下来吗? You might also then want to clear the float on the enclosing container. 然后,您可能还想清除封闭容器上的浮标。

.group:after {
  content: "";
  display: table;
  clear: both;
}
...
<div class="c_head group">
  <h3 class="c_header">Chat - BETA<span style="float:left; padding-left:0; position:relative; left:0;">+</span></h3>
</div>

Depends on which browsers you are supporting. 取决于您支持的浏览器。

something like this? 这样的东西?

HTML HTML

<div class="wrapper" style="height:100%; background-color:Silver;">
    <center>
        <div class="chatter" runat="server">
            <div class="c_head">
                <div class="plus"> <h3> +     </h3> </div>
                <div class="c_header"> <h3>Chat - BETA</h3> </div>
            </div>
        </div>
    </center>
</div>

CSS CSS

.wrapper {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
}
.chatter
{
   margin-top:5px;
   width:70%;
   border-spacing:5px;
   table-layout: fixed;
   empty-cells:show;
   border-collapse:separate;
   font:78%/130% "Arial", "Helvetica", sans-serif;
   border:1px solid gray;
   box-shadow:0px -1px 2px 1px #d2d6d8;
    border-top:1px solid #eee;
    border-right:1px solid #d2d5d7;
    border-left:1px solid #d2d5d7;
    border-bottom:1px solid #d2d5d7;
   border-radius:3px;
   font-size:large;
}
.c_head
{
    box-shadow:0px -1px 2px 1px #d2d6d8;
    border-top:1px solid #eee;
    border-right:1px solid #d2d5d7;
    border-left:1px solid #d2d5d7;
    border-bottom:1px solid #d2d5d7;
    border-radius:3px;
}
.c_header
{
    width:70%;
    color:#777777;
    height:100%;
    font-size:1.2em;
    font-weight:bold;
}

.plus {width:30%; height:100%; float:left; font-size:31px;}

http://jsfiddle.net/ockzukjs/ http://jsfiddle.net/ockzukjs/

Remove the inline styles to the h3 in the HTML 在HTML中将内联样式删除为h3

 .wrapper { position: absolute; top: 0; left: 0; width: 100%; } .chatter { margin-top: 5px; width: 70%; border-spacing: 5px; table-layout: fixed; empty-cells: show; border-collapse: separate; font: 78%/130%"Arial", "Helvetica", sans-serif; border: 1px solid gray; box-shadow: 0px -1px 2px 1px #d2d6d8; border-top: 1px solid #eee; border-right: 1px solid #d2d5d7; border-left: 1px solid #d2d5d7; border-bottom: 1px solid #d2d5d7; border-radius: 3px; font-size: large; } .c_head { box-shadow: 0px -1px 2px 1px #d2d6d8; border-top: 1px solid #eee; border-right: 1px solid #d2d5d7; border-left: 1px solid #d2d5d7; border-bottom: 1px solid #d2d5d7; padding-right: 9px; height: 31px; overflow: hidden; margin-bottom: 1px; border-radius: 3px; } .c_header { margin: 0; padding: 0; padding-left: 9px; color: #777777; overflow: hidden; height: 31px; line-height: 31px; font-size: 1.2em; font-weight: bold; padding-bottom: 3px; position: relative; } .c_header:before { content: "+"; position: absolute; left: 0; } .c_header:after { content: "+"; position: absolute; right: 0; } 
 <div class="wrapper" style="height:100%; background-color:Silver;"> <center> <div class="chatter" runat="server"> <div class="c_head"> <h3 class="c_header">Chat - BETA</h3> </div> </div> </center> </div> 

What I did: 我做了什么:

Changed this line of HTML 更改了这一行HTML

<h3 class="c_header">Chat - BETA</h3>

add this to CSS 将此添加到CSS

    .c_header
    {
        margin:0;
        padding:0;
        padding-left:9px;
        color:#777777;
        overflow:hidden;
        height:31px;
        line-height:31px;
        font-size:1.2em;
        font-weight:bold;
        padding-bottom:3px;
        position: relative;
    }
    .c_header:before {
      content:"+";
      position: absolute;
      left:0;
    }
    .c_header:after {
      content:"+";
      position: absolute;
      right:0;
    }

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

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