html - 如何定位 flexbox 元素

标签 html twitter-bootstrap css flexbox

<分区>

我正在制作一个用 flex 类构建的网格系统。这是我第一次使用 flex,所以我不太确定它是如何工作的。

到目前为止我制作的网格 is looking like this .

我希望 flexbox 元素 6 看起来像下图:

Flexbox item 6

目前元素 5 和元素 6 各占 50% 的宽度

item5 和 item6 占了 50% 的宽度。我似乎无法改变这一点。我希望第 5 项占 66% 左右,第 6 项占 33%,但元素周围仍有余量。

有人能看出我做错了什么吗?

<style>

.d-flex {
  display: -ms-flexbox!important;
  display: flex!important
}

.d-inline-flex {
    display: -ms-inline-flexbox!important;
    display: inline-flex!important
}
.flex-wrap {
    -ms-flex-wrap: wrap!important;
    flex-wrap: wrap!important
}
.bg-img {
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat; }

/* CTA Buttons */

.btn-top-left {
  position: absolute;
  left: 10%;
  top: 10%
}

.btn-top-right {
  
}

.btn-center {
  position: absolute;
  left: 50%;
  top: 50%
}

.btn-bottom-left {
  
}

.btn-bottom-right {
  
}


/* :: Set overlay over image and placement of text */
.single-welcome-post {
  position: relative;
  z-index: 1; }
  .single-welcome-post::after {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.4);
    content: '';
    z-index: 5; }
  .single-welcome-post .welcome-post-content {
    position: absolute;
    z-index: 100;
    left: 50px;
    bottom: 50px;
    right: 50px; }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .single-welcome-post .welcome-post-content {
        left: 20px;
        bottom: 20px;
        right: 20px; } }
    @media only screen and (max-width: 767px) {
      .single-welcome-post .welcome-post-content {
        left: 15px;
        bottom: 15px;
        right: 15px; } }
  .single-welcome-post.style-2 {
    height: 645px; }
 

 

.hero-area {
  position: relative;
  z-index: 0;
  background-color: #ffffff;
  padding: 5px; }
  .hero-area .hero-single-section {
    position: relative;
    z-index: 0;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 50%;
    flex: 0 0 50%;
    max-width: 50%;
    width: 50%; }
    @media only screen and (max-width: 767px) {
      .hero-area .hero-single-section {
        -webkit-box-flex: 0;
        -ms-flex: 0 0 100%;
        flex: 0 0 100%;
        max-width: 100%;
        width: 100%; } }
  .hero-area .item1 {
    height: 700px;
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item1 {
        height: 600px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item1 {
        height: 500px; } }
    @media only screen and (max-width: 767px) {
      .hero-area .item1 {
        height: 350px; } }
  .hero-area .item2 {
    height: 340px;
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item2 {
        height: 290px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item2 {
        height: 240px; } }
    @media only screen and (max-width: 767px) {
      .hero-area .item2 {
        height: 200px; } }
  .hero-area .item3 {
    height: 350px;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 calc(50% - 10px);
    flex: 0 0 calc(50% - 10px);
    max-width: calc(50% - 10px);
    width: calc(50% - 10px);
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item3 {
        height: 300px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item3 {
        height: 250px; } }
    @media only screen and (max-width: 767px) {
      .hero-area .item3 {
        height: 200px; } }
  .hero-area .item4 {
    height: 350px;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 calc(50% - 10px);
    flex: 0 0 calc(50% - 10px);
    max-width: calc(50% - 10px);
    width: calc(50% - 10px);
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item4 {
        height: 300px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item4 {
        height: 250px; } }
    @media only screen and (max-width: 767px) {
      .hero-area .item4 {
        height: 200px; } }

  .hero-area .item5 {
    height: 350px;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 calc(50% - 10px);
    flex: 0 0 calc(50% - 10px);
    max-width: calc(130% - 10px);
    width: calc(130% - 10px);
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item5 {
        height: 300px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item5 {
        height: 250px;
        max-width: calc(100% - 10px); } }
    @media only screen and (max-width: 767px) {
      .hero-area .item5 {
        height: 200px; 
        max-width: calc(100% - 10px); } }

.hero-area .item6 {
    height: 350px;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 calc(50% - 10px);
    flex: 0 0 calc(50% - 10px);
    max-width: calc(100% - 10px);
    width: calc(100% - 10px);
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item6 {
        height: 300px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item6 {
        height: 250px;
        max-width: calc(100% - 10px); } }
    @media only screen and (max-width: 767px) {
      .hero-area .item6 {
        height: 200px; 
        max-width: calc(100% - 10px); } }

.hero-area .item7 {
    height: 350px;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 calc(50% - 10px);
    flex: 0 0 calc(50% - 10px);
    max-width: calc(100% - 10px);
    width: calc(100% - 10px);
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item7 {
        height: 300px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item7 {
        height: 250px;
        max-width: calc(100% - 10px); } }
    @media only screen and (max-width: 767px) {
      .hero-area .item7 {
        height: 200px; 
        max-width: calc(100% - 10px); } }



.single-blog-post {
  position: relative;
  z-index: 1; }
  .single-blog-post .blog-thumb {
    position: relative;
    z-index: 1; }
    .single-blog-post .blog-thumb img {
      width: 100%; }
  .single-blog-post .blog-content .post-tag {
    color: #a6a6a6;
    text-transform: uppercase;
    font-size: 12px;
    display: block;
    font-weight: 700;
    margin-bottom: 5px; }
    .single-blog-post .blog-content .post-tag:hover, .single-blog-post .blog-content .post-tag:focus {
      color: #ff1662; }

    /* Set font-size */
    @media only screen and (max-width: 767px) {
      .single-blog-post .blog-content .post-tag {
        font-size: 10px; } }
  .single-blog-post .blog-content .post-title {
    font-weight: 600;
    font-size: 30px;
    display: block;
    color: #282828;
    margin-bottom: 10px; }
    
  

    /* Align text */
    @media only screen and (max-width: 767px) {
      .single-blog-post .blog-content .post-title {
        font-size: 16px; } }
    .single-blog-post .blog-content .post-title:hover, .single-blog-post .blog-content .post-title:focus {
      color: #ff1662; }
  .single-blog-post .blog-content .post-meta {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex; }

    /* Set space between text elements*/
    .single-blog-post .blog-content .post-meta a {
      color: #ffffff;
      margin-right: 15px;
      font-size: 11px;
      line-height: 1;
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-align: end;
      -ms-flex-align: end;
      -ms-grid-row-align: flex-end;
      align-items: flex-end; }
    

      /* Set color and placement on text*/
      @media only screen and (max-width: 767px) {
        .single-blog-post .blog-content .post-meta a {
          font-size: 8px;
          margin-right: 5px; } }
      .single-blog-post .blog-content .post-meta a img {
        margin-right: 10px; }
      .single-blog-post .blog-content .post-meta a:last-child {
        margin-right: 0; }
      .single-blog-post .blog-content .post-meta a:hover, .single-blog-post .blog-content .post-meta a:focus {
        color: #ff1662; }
  .single-blog-post.white .blog-content .post-title,
  .single-blog-post.white .blog-content .post-tag,
  .single-blog-post.white .blog-content .post-meta a {
    color: #ffffff; }
    .single-blog-post.white .blog-content .post-title:hover, .single-blog-post.white .blog-content .post-title:focus,
    .single-blog-post.white .blog-content .post-tag:hover,
    .single-blog-post.white .blog-content .post-tag:focus,
    .single-blog-post.white .blog-content .post-meta a:hover,
    .single-blog-post.white .blog-content .post-meta a:focus {
      color: #008ebe; }
  .single-blog-post.style2 .blog-content .post-title {
    font-size: 24px; }



/* Sidebar Nyhedsbrev */
.sidebar-area {
  position: relative;
  z-index: 1; }
  @media only screen and (min-width: 768px) and (max-width: 991px) {
    .sidebar-area {
      margin-top: 100px; } }
  @media only screen and (max-width: 767px) {
    .sidebar-area {
      margin-top: 100px; } }

.single-widget-area {
  position: relative;
  z-index: 1; }
  .single-widget-area.newsletter-widget {
    background-color: #f0f4f8;
    padding: 50px 20px;
    text-align: center; }
    .single-widget-area.newsletter-widget h4 {
      margin-bottom: 25px; }
    .single-widget-area.newsletter-widget form input {
      text-align: center;
      width: 100%;
      background-color: #d4dfe3;
      height: 54px;
      font-size: 12px;
      font-style: italic;
      color: #4c4c4c;
      border: none;
      margin-bottom: 15px; }
    .single-widget-area.newsletter-widget p {
      font-size: 12px;
      font-style: italic; }
  .single-widget-area.news-widget h4 {
    margin-bottom: 60px; }
</style>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<!-- ##### Hero Area Start ##### -->
    <div class="hero-area d-flex flex-wrap">

        <div class="hero-single-section">
            <!-- Single Welcome Post -->
            <div class="single-welcome-post bg-img item1" style="background-image: url(http://vouzalis.dk/stack/1.jpg);">
                <!-- Content -->
                <a href="https://www.youtube.com/watch?v=0aNNYEUARAk" class="video-play-btn"><i class="fa fa-play"></i></a>
                <!-- Content -->
                <div class="welcome-post-content">
                    <!-- Single Blog Post -->
                    <div class="single-blog-post white">
                        <div class="blog-content">
                            <a href="#" class="post-tag">Featured</a>
                            <a href="#" class="post-title">Watch a tiny cat taking a bath</a>
                            <div class="post-meta">
                                <a href="#">Here is a text about the picture</a>
                            </div>
                            <button href="#" class="btn btn-success btn-center">Read More</button>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>

        <div class="hero-single-section">
            <!-- Single Welcome Post -->
            <div class="single-welcome-post bg-img item2" style="background-image: url(http://vouzalis.dk/stack/2.jpg);">
                <!-- Content -->
                <div class="welcome-post-content">
                    <!-- Single Blog Post -->
                    <div class="single-blog-post white">
                        <div class="blog-content">
                            <a href="#" class="post-tag">Featured</a>
                            <a href="#" class="post-title">Spain: Take a virtual tour</a>
                            <div class="post-meta">
                                <a href="#"> Her er en tekst</a>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>

            <div class="hero-second-section d-flex flex-wrap">
                <!-- Single Welcome Post -->
                <div class="single-welcome-post bg-img item3" style="background-image: url(http://vouzalis.dk/stack/3.jpg);">
                    <!-- Content -->
                    <div class="welcome-post-content">
                        <!-- Single Blog Post -->
                        <div class="single-blog-post style2 white">
                            <div class="blog-content">
                                <a href="#" class="post-tag">Featured</a>
                                <a href="#" class="post-title">5 Tips to create your garden</a>
                            </div>
                        </div>
                    </div>
                    
                </div>

                <!-- Single Welcome Post -->
                <div class="single-welcome-post bg-img item4" style="background-image: url(http://vouzalis.dk/stack/4.jpg);">
                    <!-- Content -->
                    <div class="welcome-post-content">
                        <!-- Single Blog Post -->
                        <div class="welcome-post-content">
                        <!-- Single Blog Post -->
                            <div class="single-blog-post style2 white">
                                <div class="blog-content">
                                    <a href="#" class="post-tag">Featured</a>
                                    <a href="#" class="post-title">10 Movies you need to see</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="hero-single-section">
            <!-- Single Welcome Post -->
            <div class="single-welcome-post bg-img item5" style="background-image: url(http://vouzalis.dk/stack/1.jpg);">
                <!-- Content -->
                <a href="https://www.youtube.com/watch?v=0aNNYEUARAk" class="video-play-btn"><i class="fa fa-play"></i></a>
                <!-- Content -->
                <div class="welcome-post-content">
                    <!-- Single Blog Post -->
                    <div class="single-blog-post white">
                        <div class="blog-content">
                            <a href="#" class="post-tag">Featured</a>
                            <a href="#" class="post-title">Watch a tiny cat taking a bath</a>
                            <div class="post-meta">
                                <a href="#">Here is a text about the picture</a>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
        <div class="hero-single-section">
            <!-- Single Welcome Post -->
            <div class="single-welcome-post bg-img item6" style="background-image: url(http://vouzalis.dk/stack/2.jpg);">
                <!-- Content -->
                <a href="https://www.youtube.com/watch?v=0aNNYEUARAk" class="video-play-btn"><i class="fa fa-play"></i></a>
                <!-- Content -->
                <div class="welcome-post-content">
                    <!-- Single Blog Post -->
                    <div class="single-blog-post white">
                        <div class="blog-content">
                            <a href="#" class="post-tag">Featured</a>
                            <a href="#" class="post-title">Watch a tiny cat taking a bath</a>
                            <div class="post-meta">
                                <a href="#">Here is a text about the picture</a>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
    </div>
<!-- ##### Hero Area end ##### -->

最佳答案

我不会使用宽度百分比。我将最后 2 个元素包装到另一个 div 中,使其 flex ,然后将 flex:1; 添加到第一个元素并将 flex:2; 添加到第二个元素特定的屏幕宽度。

这也会使宽度响应。底线是,避免百分比,对 flexbox 元素使用 flex: 属性。

.wrapper {
  width: 100%;
  min-height: 150px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.one {
  min-height: 150px;
  flex: 2;
  background: red;
}

.two {
  min-height: 150px;
  flex: 1;
  background: green;
}
<div class="wrapper">
  
  <div class="one"></div>
  <div class="two"></div>
  
</div>

关于html - 如何定位 flexbox 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54809139/

上一篇:css - 更少的 css 避免全局化变量

下一篇:CSS 类似乎没有正确继承/覆盖属性

相关文章:

javascript - 单击提交按钮后jquery显示警报

html - 当我将鼠标悬停在整个 block 上而不仅仅是文本时,如何激活链接?

javascript - 来自另一个页面的正文加载中的ajax弹出中心

html - CSS 下拉菜单,将嵌套的 UL 对齐到父 LI 的右边缘

twitter-bootstrap - Bootstrap 更改图例标记中文本的大小和字体

twitter-bootstrap - React-Bootstrap 下拉列表未展开

css - 将 CSS3 变换/动画与 font-face 一起使用会产生类似 "wobbly"微调器 gif 的效果

javascript - 如何在移动浏览器中即时更改网站的默认视口(viewport)设置?

css链接能触发transition吗?

html - css 最大宽度不起作用