【问题标题】:The text in float right is displayed in the left sidefloat right 中的文本显示在左侧
【发布时间】:2013-02-13 02:39:20
【问题描述】:

我在显示以下代码时遇到了问题。我关心将<div class="contectCode"> 划分为两个<div class="left"><div class="right">。所以,switch的一些case应该显示在左边,一些显示在右边。

<?php

        $xml = simplexml_load_file("Profiles.xml");

        foreach($xml->children() as $Developer){ 
        echo '<div class="contectCode" style="height: 260px;">';

            echo '<div class="left" style="width:365px;">';
            echo '<b>' . $Developer['name'] . '</b>';
            echo '</div><br/>';

            echo '<div class="left"  style="width:365px; float:left; text-align:left;">';
            echo '<p><b><i>Communications with you</i></b></p>';
            echo '</div>';

            echo '<div class="right" style="width:365px; float:right; text-align:left;">';
            echo '<p><b><i>Communications with other developers </i></b></p>';
            echo '</div>';
            foreach($Developer->children() as $factor){

                switch ($factor->getName()){

                    case "trust":
                        echo '<div class="left" style="width:365px; float:left; text-align:left;">';
                        if($Developer['gender'] == "Male")
                            echo '<p>You have selected (trusted) him to help you ' . $factor . ' times</p><br/>';
                        else
                            echo '<p>You have selected (trusted) her to help you ' . $factor . ' times</p><br/>';
                        echo '</div>';
                        break;
                    case "response":
                        echo '<div class="left" style="width:365px; float:left; text-align:left;">';
                        if($Developer['gender'] == "Male")
                            echo '<p>He hes responded to your help request ' . $factor . ' times</p><br/>';
                        else
                            echo '<p>She has responded to your help request ' . $factor . ' times</p><br/>';
                        echo '</div>';
                        break;
                    case "indegreeOutdegree":
                        echo '<div class="left" style="width:365px; float:left; text-align:left;">';
                        if($Developer['gender'] == "Male")
                            echo '<p>He has helped you to complete your code ' . $factor . ' times</p><br/>';
                        else
                            echo '<p>She has helped you to complete your code ' . $factor . ' times</p><br/>';
                        echo '</div>';
                        break;
                    case "recommended":
                        echo '<div class="left" style="width:365px; float:left; text-align:left;">';
                        if($Developer['gender'] == "Male")
                            echo '<p>He has been recommended to you by others ' . $factor . ' times</p><br/>';
                        else
                            echo '<p>She has been recommended to you by others ' . $factor . ' times</p><br/>';
                        echo '</div>';
                        break;
                    case "trustG":
                        echo '<div class="right" style="width:365px; float:right; text-align:left;">';
                        if($Developer['gender'] == "Male")
                            echo '<p>He has been selected (trusted) by others ' . $factor . ' times</p><br/>';
                        else 
                            echo '<p>She has been selected (trusted) by others ' . $factor . ' times</p><br/>';
                        echo '</div>';
                        break;
                    case "responseG":
                        echo '<div class="right" style="width:365px; float:right; text-align:left;">';
                        if($Developer['gender'] == "Male")
                            echo '<p>He has responded to others requests ' . $factor . ' times</p><br/>';
                        else 
                            echo '<p>She has responded to others requests ' . $factor . ' times </p><br/>';
                        echo '</div>';
                        break;
                    case "indegreeOutdegreeG":
                        echo '<div class="right" style="width:365px; float:right; text-align:left;">';
                        if($Developer['gender'] == "Male")
                            echo '<p>He has helped other developers ' . $factor . ' times</p><br/>';
                        else
                            echo '<p>She has helped other developers ' . $factor . ' times</p><br/>';
                        echo '</div>';
                        break;
                    case "recommendedG":
                        echo '<div class="right" style="width:365px; float:right; text-align:left;">';
                        if($Developer['gender'] == "Male")
                            echo '<p>He has been recommended to help by others ' . $factor . ' times</p><br/>';
                        else
                            echo '<p>She has been recommended to help by others ' . $factor . ' times</p><br/>';
                            echo '</div>';
                            break;
                }
            }

            echo '</div>';
        }



       ?>  

如果输出显示左右 div 的案例,则它运行良好。但是,如果输出在右侧 div 中显示案例,则会将它们向左移动。我想将它们显示在右侧而不是右侧。

那么,我该怎么做呢?

CSS是

.contectCode
{
font-size:15px;
line-height:24px;
margin-left:13px;
margin-right:13px;
border-style:solid;
border-width:2px;
border-color:#000066;
padding:10px;
margin-top:5px;
margin-bottom:10px;
}

.left
{
float:left;
width:60%;
}

.right
{
float:left;
}

【问题讨论】:

  • 你能告诉我们你的实际输出 html 吗?

标签: html css-float text-align


【解决方案1】:

怎么样:

.right
{
    float:left;
    text-align:right;
}

【讨论】:

    【解决方案2】:

    Ghadeer - 我可能在您的帖子中遗漏了一些内容,但我将您的 HTML 示例和 CSS 都提取了,并将其放入带有附加渲染的快速页面中(在 Chrome v24 中,使用颜色显示布局)。在您的两个 HTML 示例中,右侧 div 中的文本与 div 的左侧对齐,因为您的内联样式中有 text-align:left

    如果您想要右对齐文本,请考虑在您的 .right CSS 选择器中添加 text-align:right,并从 &lt;div class="right"&gt;...&lt;/div&gt; 元素中删除您的内联样式。

    无论如何,这样维护起来要容易得多!

    顺便说一句,考虑到您正在逐字生成 HTML,为什么首先要有任何内联样式?看起来这些样式更适合放在您的样式表中。

    【讨论】:

    • 不,我需要保留对齐文本。而且,我使用 float:right 是因为我希望这个 div 在右侧,但又要在左侧对齐
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2011-08-31
    相关资源
    最近更新 更多