简体   繁体   English

CSS 和 Boostrap:在 div 中拟合图像或轮播

[英]CSS and Boostrap: Fitting image or carousel inside div

I have got a series of divs inside Bootstrap grid , as these are generated dynamically it could be of any number.我在Bootstrap grid有一系列divs ,因为这些是动态生成的,它可以是任意数量的。

Each of these div contains:这些div中的每一个都包含:

  • Image or Carousel图像或轮播
  • Couple of words (fit in single line, always)几个词(适合单行,总是)
  • Radio buttons, these also fit in the single line.单选按钮,这些也适合单行。

The dimension all the divs are as follows:所有divs的尺寸如下:

width: 230px;
height: 270px;

I am facing a problem with Image/Carousel placement inside the div .我在div遇到 Image/Carousel 放置问题。 I'm specifying the size of Image/Carousel to be 180px by 180px .我将 Image/Carousel 的大小指定为180px by 180px

But, somehow image is taking all the width of the div ie image rendering with the width of 230px, while the image's height is 180px only.但是,不知何故,图像占用了div所有宽度,即图像渲染的宽度为 230 像素,而图像的高度仅为 180 像素。 See below how it is appearing.下面看看它是如何出现的。

在此处输入图片说明

The desired output : The Image/Carousel should be rendered 180*180 only.所需的输出:图像/轮播应仅呈现 180*180。 So that I can have some empty space each side.这样我就可以在每一边都有一些空的空间。

EXISTING CODE:现有代码:

CSS CSS

        .nop_img {
            width:180px;
            height:180px;
        }
        .row{
            overflow: hidden; 
        }
        .nop_div {
            overflow: hidden;
            background-size: cover;
            background-repeat: no-repeat;
            background-position: 50% 50%;
            width: 230px;
            height: 270px;
        }

HTML CODE: HTML代码:

<div class="container-fluid">
            <div class="row">
                {% for track_id, image_path in image_urls.items %}
                {% if image_path|length == 1 %}
                <div id="nopCarousel_{{ track_id }}"  class="column nop_div">
                    {% for ip in image_path %}
                    <img class="d-block w-100 nop_img" src="{{ ip }}" >
                    {% endfor %}
                    <p>{{ image_path|length }} Face</p>
                    <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1_{{track_id}}" value="option1" checked>
                            <label class="form-check-label" for="exampleRadios1">
                                act1
                            </label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2_{{track_id}}" value="option2">
                            <label class="form-check-label" for="exampleRadios2">
                                act2
                            </label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3_{{track_id}}" value="option2">
                            <label class="form-check-label" for="exampleRadios2">
                                act3
                            </label>
                        </div>
                </div>
                {% else %}
                <div id="nopCarousel_{{ track_id }}" class="carousel slide column nop_div" data-ride="carousel">
                    <!-- <div id="nopCarousel_{{ track_id }}" class="carousel slide column nop_div" data-ride=False data-interval=500> -->
                        <div class="carousel-inner">
                            {% for ip in image_path %}
                            <!-- <h1> Index: {{ forloop.counter }}</h1> -->
                            {% if forloop.counter == 1 %}
                            <div class="carousel-item active">
                                <img class="d-block w-100 nop_img" src="{{ ip }}" >
                            </div>
                            {% endif %}
                            <div class="carousel-item">
                                <img class="d-block w-100 nop_img" src="{{ ip }}" >
                            </div>
                            {% endfor %}
                        </div>
                        <a class="carousel-control-prev" href="#nopCarousel_{{ track_id }}" role="button" data-slide="prev">
                            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                            <span class="sr-only">Previous</span>
                        </a>
                        <a class="carousel-control-next" href="#nopCarousel_{{ track_id }}" role="button" data-slide="next">
                            <span class="carousel-control-next-icon" aria-hidden="true"></span>
                            <span class="sr-only">Next</span>
                        </a>
                        <p>{{ image_path|length }} Faces</p>
                        <!-- Radio button -- Can give dropdown too -->
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1_{{track_id}}" value="option1" checked>
                            <label class="form-check-label" for="exampleRadios1">
                                act1
                            </label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2_{{track_id}}" value="option2">
                            <label class="form-check-label" for="exampleRadios2">
                                act2
                            </label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3_{{track_id}}" value="option2">
                            <label class="form-check-label" for="exampleRadios2">
                                act3
                            </label>
                        </div>
                    </div>

                    {% endif %}
                    {% endfor %}
                </div>
            </div>

Set your image rules to include max-width:180px;设置您的图像规则以包含 max-width:180px; and width:100%;和宽度:100%;

More information if interested: https://blog.prototypr.io/what-even-is-the-difference-between-width-and-max-width-8f37b282c7f1如果有兴趣了解更多信息: https : //blog.prototypr.io/what-even-is-the-difference-between-width-and-max-width-8f37b282c7f1

将图像放入 a<div> , 在列内<div></div><div id="text_translate"><p>我创建了两列并排浮动。 左列显示文本,右列显示图像。 我一直无法找到正确的代码来将我的图像放在右列中。</p><p> 我一直在列 div 本身内的图像周围放置一个 div,看起来我在绕圈子。 我也希望图像保持其比例。</p><p> 谁能解释一下我在 CSS 上哪里出了问题?</p><pre> &lt;:DOCTYPE html&gt; &lt;;--[if lt IE 7 ]&gt;&lt;html lang="en" class="ie6"&gt; &lt;:[endif]--&gt; &lt;;--[if IE 7 ]&gt;&lt;html lang="en" class="ie7"&gt;&lt;:[endif]--&gt; &lt;;--[if IE 8 ]&gt;&lt;html lang="en" class="ie8"&gt;&lt;:[endif]--&gt; &lt;;--[if IE 9 ]&gt;&lt;html lang="en" class="ie9"&gt;&lt;:[endif]--&gt; &lt;;--[if (gt IE 9)|:(IE)]&gt;&lt;;--&gt;&lt;html lang="en"&gt;&lt;:--&lt;;[endif]--&gt; &lt;head&gt; &lt;style&gt; * { box-sizing: border-box; } body { margin: 0; padding: 0; height: 100%, background-color;#ffffff: overflow-x; hidden: /* Hide horizontal scrollbar */ } html { width; 100vw: scroll-behavior; smooth. -webkit-font-smoothing: antialiased, -moz-osx-font-smoothing; grayscale: } h1 { font-family; prestige-elite-std: monospace; font-weight: bold; font-size: 35px; padding. 15px 0 50px 15px: },p1 { font-family; prestige-elite-std: monospace; font-weight: bold; font-size: 20px; font-style: italic; padding: 0 0 18px 0px; }:p2 { font-family; prestige-elite-std: monospace; font-size: 18px; padding: 0 0 10px 0px; } #wrapper{ background-color:#ccc2c2; width: 100%; max-width. 1200px: position; relative: margin; 0 auto: padding; 5px 0: } #content { padding; 5px 0 5px 0: position; relative: overflow; hidden: };container { position.relative: clear; both: width; 100%: max-width; 1200px: padding; 5px 0px 5px 0px: overflow; hidden: background-color; #ffffff: };column1 { float. left. width: 30%; padding: 10px; height. 300px: background-color;#fff: position; relative: border; 1px solid #000: };column1 div:col1content { position; absolute: bottom; 60px. }:column2 { float; left: width; 70%: padding. 10px. height. 550px; background-color:#fff; border: 1px solid #000; } .img-container { max-width: 100%; max-height: 100%; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="wrapper"&gt; &lt;div id="content"&gt; &lt;div class="container"&gt; &lt;div class="row"&gt; &lt;div class="column1"&gt; &lt;div class="col1content"&gt; &lt;p class="p1"&gt;Text&lt;/p&gt; &lt;p class="p2"&gt;Text&lt;/p&gt; &lt;p class="p2"&gt;Text&lt;/p&gt; &lt;p class="p2"&gt;Text&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="column2"&gt; &lt;div class="img-container"&gt; &lt;img src="https://s3-eu-west-1.amazonaws.com/graphicalhouse-collective/SP19_poster_crop_2.png"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre></div></div> - Fitting an image inside a <div>, inside a column <div>

暂无
暂无

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

相关问题 图像拟合Div CSS - Image Fitting Div CSS 将图像装入<div> - Fitting an image inside a <div> 图像不适合DIV内部 - Image not fitting inside of a DIV 使用CSS在div中拟合图像 - Fitting image within div with CSS 用引导程序在div内拟合图像 - Fitting image inside div with bootstrap 在div中对图像进行拟合和居中 - Fitting and centering image inside div 将图像放入 a<div> , 在列内<div></div><div id="text_translate"><p>我创建了两列并排浮动。 左列显示文本,右列显示图像。 我一直无法找到正确的代码来将我的图像放在右列中。</p><p> 我一直在列 div 本身内的图像周围放置一个 div,看起来我在绕圈子。 我也希望图像保持其比例。</p><p> 谁能解释一下我在 CSS 上哪里出了问题?</p><pre> &lt;:DOCTYPE html&gt; &lt;;--[if lt IE 7 ]&gt;&lt;html lang="en" class="ie6"&gt; &lt;:[endif]--&gt; &lt;;--[if IE 7 ]&gt;&lt;html lang="en" class="ie7"&gt;&lt;:[endif]--&gt; &lt;;--[if IE 8 ]&gt;&lt;html lang="en" class="ie8"&gt;&lt;:[endif]--&gt; &lt;;--[if IE 9 ]&gt;&lt;html lang="en" class="ie9"&gt;&lt;:[endif]--&gt; &lt;;--[if (gt IE 9)|:(IE)]&gt;&lt;;--&gt;&lt;html lang="en"&gt;&lt;:--&lt;;[endif]--&gt; &lt;head&gt; &lt;style&gt; * { box-sizing: border-box; } body { margin: 0; padding: 0; height: 100%, background-color;#ffffff: overflow-x; hidden: /* Hide horizontal scrollbar */ } html { width; 100vw: scroll-behavior; smooth. -webkit-font-smoothing: antialiased, -moz-osx-font-smoothing; grayscale: } h1 { font-family; prestige-elite-std: monospace; font-weight: bold; font-size: 35px; padding. 15px 0 50px 15px: },p1 { font-family; prestige-elite-std: monospace; font-weight: bold; font-size: 20px; font-style: italic; padding: 0 0 18px 0px; }:p2 { font-family; prestige-elite-std: monospace; font-size: 18px; padding: 0 0 10px 0px; } #wrapper{ background-color:#ccc2c2; width: 100%; max-width. 1200px: position; relative: margin; 0 auto: padding; 5px 0: } #content { padding; 5px 0 5px 0: position; relative: overflow; hidden: };container { position.relative: clear; both: width; 100%: max-width; 1200px: padding; 5px 0px 5px 0px: overflow; hidden: background-color; #ffffff: };column1 { float. left. width: 30%; padding: 10px; height. 300px: background-color;#fff: position; relative: border; 1px solid #000: };column1 div:col1content { position; absolute: bottom; 60px. }:column2 { float; left: width; 70%: padding. 10px. height. 550px; background-color:#fff; border: 1px solid #000; } .img-container { max-width: 100%; max-height: 100%; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="wrapper"&gt; &lt;div id="content"&gt; &lt;div class="container"&gt; &lt;div class="row"&gt; &lt;div class="column1"&gt; &lt;div class="col1content"&gt; &lt;p class="p1"&gt;Text&lt;/p&gt; &lt;p class="p2"&gt;Text&lt;/p&gt; &lt;p class="p2"&gt;Text&lt;/p&gt; &lt;p class="p2"&gt;Text&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="column2"&gt; &lt;div class="img-container"&gt; &lt;img src="https://s3-eu-west-1.amazonaws.com/graphicalhouse-collective/SP19_poster_crop_2.png"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre></div></div> - Fitting an image inside a <div>, inside a column <div> Boostrap:通过CSS在DIV中添加图像 - Boostrap: Add image in DIV via CSS Boostrap 4 轮播图像无法正确显示 - Boostrap 4 Carousel Image not displaying properly 容器内的boostrap div - boostrap div inside a container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM