jquery - 允许 HTML 内容超出父级的宽度

标签 jquery html css

我正在尝试修改我网站上的内容以在不更改字体大小的情况下降低每行的字符数。为此,我想降低内容容器的宽度。目前宽度是 984px,我打算把它降低到 757px。

我遇到的问题是带有内容的图像。所有图像都在 content 元素内的 div 或 figure 元素内。我仍然需要图像的最大宽度为 984px。有什么办法可以只用 CSS 来解决这个问题,还是我还需要 jQuery?

这里是一些代码的例子。内容来自于一个类(class),而且类(class)内容很多,所以编辑所有类(class)内容是不可行的。我需要采用现有样式来进行这些更改。

<div class="content">
    <h2 class="course-heading">Driver Bearings</h2>
    <p class="course-content" style="margin-left: 80px;">The unit uses both oil and grease lubricated bearings: the drive (top) bearings use oil while the bearings at the opposite end (bottom) use grease. A closeup view of the top oil fill and drain can be viewed here:</p>
    <div>
        <img alt="Oil Bearing Lubrication" class="course-image" src="/digi-oilView-01.png">
    </div>
    <p class="course-caption">Oil Bearing Lubrication</p>
</div>

此外,所有内容都应位于页面中央。

最佳答案

首先你需要设置一个height所以它不会调整以包含所有内容。

HTML

<div class="content">
     <h2 class="course-heading">Driver Bearings</h2>

    <p class="course-content" style="margin-left: 80px;">The unit uses both oil and grease lubricated bearings: the drive (top) bearings use oil while the bearings at the opposite end (bottom) use grease. A closeup view of the top oil fill and drain can be viewed here:</p>
    <div class="img-container">
        <div>
            <img src="http://placehold.it/950x150"></img>
        </div>
    </div>
    <p class="course-caption">Oil Bearing Lubrication</p>
    <div class="img-container">
        <div>
            <img src="http://placehold.it/950x150"></img>
        </div>
    </div>
    <p class="course-caption">Oil Bearing Lubrication</p>
</div>

CSS

.content {
    background-color:black;
    width:768px;
    margin:0 auto;
}
.img-container > div {
    position:relative;
}
.img-container img {
    position:absolute;
    width:950px;
}

body {
    background-color:yellow;
    color:grey;
    width:1050px;
    display:block;
    margin:0 auto;
}

JS

$(".img-container").each(function () {
    $(this).css('height', $(this).children()[0].children[0].height + 'px');

});

$(".img-container > div > img").each(function () {
    var paddingValue = (Number($(".content").css('width').substring(0, $(".content").css('width').length - 2)) - this.width) / 2;
    $(this).parent().css('left', paddingValue);
});

* 正文 color显然设置只是为了让您可以看到父级之外的内容以及background-color.content *

工作 JSFiddle:http://jsfiddle.net/dmep9w6c/20/ * 已更新 *

关于jquery - 允许 HTML 内容超出父级的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27045068/

相关文章:

javascript - 将一个字符放入具有指定类别的空 div-s 中

javascript - jQuery css 左、上、z-index

javascript - 我如何突出显示 n 个序列字符串单词

css - 仅滚动一个 flexbox 元素,而另一个使用 flex-column 保持固定

html - CSS 在我的页面上不起作用

jquery - 直接链接到隐藏的div

Javascript dom 解析故障排除

jquery - 通过单击将 div 滑入/滑出

php - HTML/CSS 面板不通过 PHP 中的 JavaScript 显示

html - Bootstrap : block image resizing