How to position flexbox elements
本问题已经有最佳答案,请猛点这里访问。
我正在制作一个用flex类构建的网格系统。这是我第一次使用flex,所以我还不确定它如何运行。
我到目前为止制作的网格
看起来像这样。
我希望flexbox项目6看起来像下面的图片:
目前,第5项和第6项的宽度分别为50%
item5和item6的宽度为50%。我似乎无法改变这一点。我希望第5项占66%,第6项占33%,但元素周围仍有边距。
有人可以看到我在做什么吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | <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> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/> <!-- ##### Hero Area Start ##### --> <!-- Single Welcome Post --> <!-- Content --> <i class="fa fa-play"> <!-- Content --> <!-- Single Blog Post --> Featured Watch a tiny cat taking a bath Here is a text about the picture <button href="#" class="btn btn-success btn-center">Read More</button> <!-- Single Welcome Post --> <!-- Content --> <!-- Single Blog Post --> Featured Spain: Take a virtual tour Her er en tekst <!-- Single Welcome Post --> <!-- Content --> <!-- Single Blog Post --> Featured 5 Tips to create your garden <!-- Single Welcome Post --> <!-- Content --> <!-- Single Blog Post --> <!-- Single Blog Post --> Featured 10 Movies you need to see <!-- Single Welcome Post --> <!-- Content --> <i class="fa fa-play"> <!-- Content --> <!-- Single Blog Post --> Featured Watch a tiny cat taking a bath Here is a text about the picture <!-- Single Welcome Post --> <!-- Content --> <i class="fa fa-play"> <!-- Content --> <!-- Single Blog Post --> Featured Watch a tiny cat taking a bath Here is a text about the picture <!-- ##### Hero Area end ##### --> |
宽度百分比不是我要使用的东西。我将最后2个元素package到另一个div中,使其弯曲,然后针对此特定屏幕宽度将
这也将使宽度响应。最重要的是,避免百分比,将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | .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; } |
1 |
您可以像这样在引导程序中解决此问题
1 2 3 4 5 6 7 | .vh-100 { height:100vh } .vh-50 { height:50vh } |
1 | <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" /> |