【问题标题】:Using css3 to horizontally align the div tags使用css3水平对齐div标签
【发布时间】:2014-01-05 12:34:52
【问题描述】:

请问如何使用 css3 水平对齐 div 标签的两个内容。我的封闭主体的总宽度为 950 像素,我将第一个 div 标签的宽度指定为 470 像素。我的假设是第二个 div 标签将占据 480 像素的剩余空间,但它在定位时没有占据

这是html代码

<div class="left colborder">
    <p>To know what's cooking you can check our <a href="#"> events </a> for more
        <br />For more information Contact Us</p>
    <div class="left-ContactInformation">
        <img src="Picture%20Related/Email%20Logo.png" alt="Email Address Icon" title="Email Address Icon" id="EmailAddressIcon" />j.banti09@gmail.com
        <br />
        <img src="Picture%20Related/Telephone%20Logo.png" alt="Phone Number Icon" title="Phone Number Icon" id="PhoneNumberIcon" />08138549501</div>
</div>
<!--There Should be a separator (vertical)-->
<form action="/" method="post">
    <div class="right">Tell us what you think or feel about this site
        <div id="right-UserComment">
            <input type="text" name="Name" placeholder="Name" required />
            <input type="email" name="Email" placeholder="Email Address" required />
            <br />
            <textarea cols="40" rows="3" wrap="hard" required placeholder="What's up?" name="comments"></textarea>
        </div>
        <button type="submit" value="submit">Send</button>
    </div>
</form>

这是CSS,它包括容器、左右div标签css。

container{
    width:950px;
    margin:0;
    margin-left:auto;
    margin-right:auto;
    border-style:solid;
    background-color:orange;
}



.left {
    width:450px;
    margin-left:10px;
}

.right{
    width:390px;
   margin-left:10px;
}

http://jsfiddle.net/isherwood/rXT95/

【问题讨论】:

  • 我们可以在这里看到你的 CSS 吗?我猜您从未将 .left 和 .right 设置为浮动或内联,因此它们被设置为块。
  • 您可能会发现这很有帮助 -- stackoverflow.com/questions/2935404/…

标签: html css


【解决方案1】:
container{ 
    width:950px; margin:0;
    margin-left:auto; margin-right:auto; border-style:solid;
    background-color:orange; 
} 
.left {
    width:450px; 
    margin-left:10px; 
    float:left;
 } 
 .right{ 
    margin-left:0 auto; 
 }

已更新。 http://jsfiddle.net/3VF9Q/

【讨论】:

  • 这是CSS,它包括容器、左右div标签css。容器{宽度:950px;边距:0;左边距:自动;边距右:自动;边框样式:实心;背景颜色:橙色; } .left { 宽度:450px;左边距:10px; } .right{ 宽度:390px;左边距:10px; }
  • 更新了 CSS。这是你想要的吗? :)
【解决方案2】:

这是一个带有答案的 JS Fiddle。我使用基于百分比的宽度而不是像素宽度:http://jsfiddle.net/4nHr8/

CSS如下:

.left {
    float:left;
    width: 40%;
    padding: 0 2.5%;
    display:inline-block;
    border-left: 2px solid #333;
}
.right {
    float:left;
    width: 40%;
    padding: 0 2.5%;
    display: inline-block;
}

【讨论】:

    【解决方案3】:

    你只需要浮动两个有问题的元素。

    您可以在这里查看: http://jsfiddle.net/rXT95/2/

    HTML:

    <div class="container">
        <div class="left colborder">
            <p>To know what's cooking you can check our <a href="#"> events </a> for more
                <br />For more information Contact Us</p>
            <div class="left-ContactInformation">
                <img src="Picture%20Related/Email%20Logo.png" alt="Email Address Icon" title="Email Address Icon" id="EmailAddressIcon" />j.banti09@gmail.com
                <br />
                <img src="Picture%20Related/Telephone%20Logo.png" alt="Phone Number Icon" title="Phone Number Icon" id="PhoneNumberIcon" />08138549501</div>
        </div>
        <!--There Should be a separator (vertical)-->
        <form action="/" method="post">
            <div class="right">Tell us what you think or feel about this site
                <div id="right-UserComment">
                    <input type="text" name="Name" placeholder="Name" required />
                    <input type="email" name="Email" placeholder="Email Address" required />
                    <br />
                    <textarea cols="40" rows="3" wrap="hard" required placeholder="What's up?" name="comments"></textarea>
                </div>
                <button type="submit" value="submit">Send</button>
            </div>
        </form>
    </div>
    

    CSS:

    .container{
        width:950px;
        margin:0;
        margin-left:auto;
        margin-right:auto;
        border-style:solid;
        background-color:orange;
        overflow: hidden; /* added */
    }
    
    .left {
        width:450px;
        margin-left:10px;
        float: left; /* added */
    }
    
    .right{
        width:390px;
       margin-left:10px;
        float: left; /* added */
    }
    

    【讨论】:

      【解决方案4】:

      使用 CSS3 box-sizing: border-box 模型(IE8+):http://jsfiddle.net/ashjt/3br3a/2/

      使用普通 CSS:http://jsfiddle.net/ashjt/3br3a/4/

      【讨论】:

        【解决方案5】:

        检查我在外部 div 上添加的以下 html 并为 div 添加样式

        <br>
        <div style='width:100%'>
        <div class="left colborder" style='width:50%;float:left'>
        <p>To know what's cooking you can check our 
            <a href="#"> events </a>
            for more
            <br />
        For more information Contact Us</p>
        
        <div class="left-ContactInformation">
            <img src="Picture%20Related/Email%20Logo.png" alt="Email Address Icon" title="Email Address Icon" id="EmailAddressIcon" />
            j.banti09@gmail.com
            <br /><img src="Picture%20Related/Telephone%20Logo.png" alt="Phone Number Icon" title="Phone Number Icon" id="PhoneNumberIcon" />
        08138549501</div>
        </div>
        <!--There Should be a separator (vertical)-->
        <div style='width:50%;float:right'><form action="/" method="post">
        <div class="right">Tell us what you think or feel about this site
        
        <div id="right-UserComment"><input type="text" name="Name" placeholder="Name" required />
            <input type="email" name="Email" placeholder="Email Address" required />
            <br />
            <textarea cols="40" rows="3" wrap="hard" required placeholder="What's up?" name="comments">
            </textarea>
        </div>
             <button type="submit" value="submit">Send</button></div>
             </form>
             </div>
        </div>`
        

        【讨论】:

        • @Kamlesh:可以格式化代码blocks,不需要使用backtic inline code sn-ps
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-23
        • 2014-04-11
        • 1970-01-01
        • 2013-04-16
        • 2017-11-05
        • 2010-09-07
        相关资源
        最近更新 更多