html - 停止 Second Div 环绕 float Div

标签 html css

我在元素中定位 div 时遇到了一个小问题。

+------------------------+
|+-------+ ~~~~     TITLE| <--how can I stop the text wrapping up the 
||       | ~~~~~~~~~~~~~ |\                      left hand side here?
||       | ~~~~~~~~~~~~~ | \
||       | ~~~~~~~~~~~~~ |  \
||       | ~~~~~~~~~~~~~ |   \
|+-------+            ^  |   Title Div (class="widgetHeader" in example)
+---------------------|--+
   ^                  |
   |                   \
 Thermometer div        \
 (all good)              \
                       "widgetContent" in example fiddle

但是,我希望强制 div 的标题阻止内容环绕它。

类似于:

+------------------------+
|+-------+          TITLE|  
||       | ~~~~~~~~~~~~~ |
||       | ~~~~~~~~~~~~~ | 
||       | ~~~~~~~~~~~~~ |  
||       | ~~~~~~~~~~~~~ |   
|+-------+ ~~~~~         |   
+------------------------+

我希望对每个小部件强制执行。

This is the fiddle.

两个主要类是:

.widgetHeader {
    padding-top:5%;
    float:right;
    margin-right:4px;
    font-weight:bold;
    text-decoration:underline;
}
.widgetContent {
    float:left;
    text-align:left;
    padding-top:20%;
}

它位于示例中 CSS 的末尾。

我知道我可以使用 <br /> 轻松解决这个问题HTML 中的标签,但我确信应该可以很容易地在这里实现这一点。

我认为这里的主要问题是 Title右对齐,内容左对齐。

任何 CSS 解决方案(因为它将针对不同尺寸的屏幕设计,我更喜欢使用 % 而不是 px)?

最佳答案

修复代码

不要 float 它,而是可以使用text-align将其右对齐,如下所示:

.widgetHeader {
    padding-top:5%;
    margin-right:4px;
    font-weight:bold;
    text-align:right;
    text-decoration:underline;
}

Here is the updated fiddle


另一种方法

如果我自己从头开始做这件事,我可能会做这样的事情:

.main {
    display:block;
    width:300px;
    border:1px solid #999;
    background-color:#EEE;
    padding:5px;
}
.image {
    float:left;
    display:block;
    width:120px;
    height:120px;
    border:1px solid #999;
    background-color:#FFF;
}
.content {
    display:block;
    margin-left:130px;
    color:#444;
}
.title {
    text-align:right;
    font-weight:bold;
}
.clear{
    clear:both;
}
<div class="main">
    <div class="image"></div>
    <div class="content">
        <div class="title">Title Here</div>
        <p>The rest of the content goes here and will look beautiful and majestic.</p>
        <div class="clear"></div>
    </div>
</div>

Here is a working example

关于html - 停止 Second Div 环绕 float Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26671875/

相关文章:

javascript - 使用 Javascript 隐藏和显示导航栏

jquery - HTML5 效果 GaussianBlur(也称为 "aerowindow")

html - 如何解决带有背景 div 的 2 列布局中的高度自动问题

javascript - 使用 jQuery 添加元素后刷新 css

html - 如何有一个粗体标题文本?

html - 替换 "hover"上的动态背景 - Ruby on Rails

drop-down-menu - 下拉 CSS 菜单(子链接隐藏)

html - 带有粘性页脚和灵活高度的棘手盒子布局

javascript - 在 JavaScript 中单击 <div> 时调用函数

html - 如何使 margin-left 在同一个 div 中正常工作?