I have an image inside a div with a class of col-md-6 and I'm trying to position text next to the image right in the middle. What code works for that? The only way I've found possible, but not accurate is by messing with the padding.
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
#wrapper2 h1 {
text-align: center
}
#wrapper2 p {
text-align: center;
}
.col-md-6 {
padding: 0;
}
<div class="container-fluid">
<div id="wrapper2">
<div class="row">
<div class="col-md-6">
<img class="img-responsive" src=https://s-media-cache-ak0.pinimg.com/736x/47/3d/e8/473de83da9ad0e72e340022bb68e9429.jpg>
</div>
<div class="col-md-6">
<h1>Men's Line</h1>
<p>Shop our newest 2017 arrivals</p>
</div>
</div>
</div>
</div>
https://jsfiddle.net/u5qngm5q/
You need to define your own custom column css class and set its display to table-cell. Finally, make vertical-align and text-align properties to middle.
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
#wrapper2 h1 {
text-align: center
}
#wrapper2 p {
text-align: center;
}
.col-md-6 {
padding: 0;
}
.custom-column{
display:table-cell;
height:100%;
width:50%;
vertical-align:middle;
text-align:center
}
<div class="container-fluid">
<div id="wrapper2">
<div class="row">
<div class="custom-column">
<img class="img-responsive" src=https://s-media-cache-ak0.pinimg.com/736x/47/3d/e8/473de83da9ad0e72e340022bb68e9429.jpg>
</div>
<div class="custom-column">
<h1>Men's Line</h1>
<p>Shop our newest 2017 arrivals</p>
</div>
</div>
</div>
</div>
Adding display: flex; and align-items: center; to the .row class will have this effect. Also adding flex-wrap: wrap; will make the code responsive. Then add flex-grow:1; to your text container for centering the text in mobile view Fiddle
HTML
<div class="row flex-center">
<div class="col-md-6 text-container">
<h1>Men's Line</h1>
<p>Shop our newest 2017 arrivals</p>
</div>
</div>
CSS
.flex-center {
align-items: center;
display: flex;
flex-wrap: wrap;
}
.text-container {
flex-grow : 1;
}
Related
Working on flexboxes and I'm unable to get my Awesome Logo header to fall underneath the very top menu which includes the date, login, etc
How it should look:
Actual Image
How my layout is:
My Funky Garbage
I'm assuming it's something with the formatting of my divs from my .html because after placing the header-container and head classes around differently, the header moves :(
html code:
<body>
<div class='menu-container'>
<div class='header-container'>
<div class='header'>
<div class='subscribe'>Subscribe ▾</div>
<div class='logo'><img src='images/awesome-logo.svg'/></div>
<div class='social'><img src='images/social-icons.svg'/></div>
</div>
</div>
<div class='menu'>
<div class='date'>Feb 7, 2023</div>
<div class="links">
<div class='signup'>Sign Up</div>
<div class='login'>Login</div>
</div>
</div>
</body>
CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.menu-container {
color: #fff;
background-color: #5995da;
padding: 20px;
display: flex;
justify-content: center;
}
.menu {
width: 900px;
display: flex;
justify-content: space-between;
}
.links {
display: flex;
justify-content: flex-end;
}
.login {
margin-left: 20px;
}
.header-container {
color: #5995DA;
background-color: #D6E9FE;
display: flex;
justify-content: center;
}
.header {
width: 900px;
height: 300px;
display: flex;
align-items: center;
justify-content: space-between;
}
I'm on the cross axis section of this website: https://www.internetingishard.com/html-and-css/flexbox/
Thanks!
Try adding flex-direction: column-reverse; to .menu-container.
For cleaner code you can also get rid of .header-container and add its css properties to header, change the order of the menu and header divs, and use flex-direction: column;. Like the code below.
<body>
<div class='menu-container'>
<div class='menu'>
<div class='date'>Feb 7, 2023</div>
<div class="links">
<div class='signup'>Sign Up</div>
<div class='login'>Login</div>
</div>
<div class='header'>
<div class='subscribe'>Subscribe ▾</div>
<div class='logo'><img src='images/awesome-logo.svg'/></div>
<div class='social'><img src='images/social-icons.svg'/>
</div>
</div>
</body>
You should move your .header-container out of .menu-container, so they're sibling elements. If needed, create a wrapper for them.
Your .links div is also unclosed, which might be causing you issues.
So your HTML should look something like this:
<body>
<div class='menu-container'>
<div class='menu'>
<div class='date'>Feb 7, 2023</div>
<div class="links">
<div class='signup'>Sign Up</div>
<div class='login'>Login</div>
</div>
</div>
</div>
<div class='header-container'>
<div class='header'>
<div class='subscribe'>Subscribe ▾</div>
<div class='logo'><img src='images/awesome-logo.svg'/></div>
<div class='social'><img src='images/social-icons.svg'/></div>
</div>
</div>
</body>
Using Flexbox I have this simple example..
.container {
display:flex;
flex-direction:column;
align-items:center;
justify-content:center;
height:100vh;
}
.box1{height:100%;background:green;}
.box2{height:100%;background:red;}
.box3{height:100%;background:yellow;}
<div class="container">
<div class="box1">
<img src="https://dummyimage.com/100x100/000/fff">
</div>
<div class="box2">
This is some dummy text
</div>
<div class="box3">
<img src="https://dummyimage.com/100x100/0020/66t">
</div>
</div>
https://jsfiddle.net/y07xr5q3/1/
I would like the content of each div to be vertically centered, is this something that should be done using standard CSS techniques or is there a flex specific way to do it?
Steps:
Make your child elements flex containers with display: flex;.
Set their vertical alignment with align-items: center.
Necessary CSS:
div.box {
align-items: center;
display: flex;
}
.container {
display:flex;
flex-direction:column;
align-items:center;
justify-content:center;
height:100vh;
}
.box {
align-items: center;
display: flex;
}
.box1{height:100%;background:green;}
.box2{height:100%;background:red;}
.box3{height:100%;background:yellow;}
<div class="container">
<div class="box box1">
<img src="https://dummyimage.com/100x100/000/fff">
</div>
<div class="box box2">
This is some dummy text
</div>
<div class="box box3">
<img src="https://dummyimage.com/100x100/0020/66t">
</div>
</div>
Try this
.container {
display:flex;
flex-direction:column;
align-items:center;
justify-content:center;
height:100vh;
}
.box1, .box2, .box3 {
display: flex;
align-items: center;
}
.box1{height:100%;background:green;}
.box2{height:100%;background:red;}
.box3{height:100%;background:yellow;}
<div class="container">
<div class="box1">
<img src="https://dummyimage.com/100x100/000/fff">
</div>
<div class="box2">
This is some dummy text
</div>
<div class="box3">
<img src="https://dummyimage.com/100x100/0020/66t">
</div>
</div>
Live demo https://jsfiddle.net/grinmax_/y07xr5q3/4/
I used the styling from this thread to make a progress bar fill in the empty space in a div (only other item is a button).
The problem is now that align-items: center doesn't vertically center the progress bar. I tried using align-content: center on the child too, with no effect.
Here's the code in case you didn't open the link
.wrapper {
display: flex;
flex-direction: row;
align-items: center;
width: 100%;
height: 5em;
background: #ccc;
}
.wrapper > .left
{
background: #fcc;
}
.wrapper > .right
{
background: #ccf;
flex: 1;
}
Markup:
<div class="wrapper">
<div class="left">Left</div>
<div class="right">Right</div>
</div>
Can someone tell me what I'm doing wrong? Thanks in advance
This is how it looks:
I guess you can do the following to get it right:
There is a margin coming for the .progress element- first you can nullify it:
.wrapper > .left > .progress {
margin: 0;
}
Give 100% height for wrapper
I also removed height: 10vh for the container of wrapper to finish things up.
See revised fiddle here and snippet below:
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
#import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
}
.wrapper {
display: flex;
align-items: center;
width: 100%;
background: #ccc;
height: 100%;
}
.wrapper > .left {
background: #fcc;
flex: 1;
}
.wrapper > .right {
background: #ccf;
}
.wrapper > .left > .progress {
margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-s-6 col-s-offset-3" style="background:purple; position:relative; border-radius:10px;">
<div class="wrapper">
<div class="left">
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="35" aria-valuemin="0" aria-valuemax="100" style="width:35%">
</div>
</div>
</div>
<div class="right">
<button type="button" aside-menu-toggle="menu-1" class="btn btn-sm btn-default">Меню</button>
</div>
</div>
</div>
</div>
</div>
Let me know your feedback on this. Thanks!
A flex container will enable a flex context only to its direct children. Hence, your "progress-bar" div is out of reach. Likewise, your purple background bar is before flex container (wrapper), so don't expect it's content to be centered either. You can check this guide.
Check this code:
<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-s-6 col-s-offset-3" style="background:purple; height:10vh; position:relative; border-radius:10px;">
<div class="wrapper">
<div class="left">
left
</div>
<div class="right">
right
</div>
</div>
</div>
</div>
</div>
Pen based on your code here
Try adding
align-items: center;
justify-content: center;
to your .wrapper class.
Here is a good article on FlexBox positioning:
https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/
I have a row where one column can vary in height so I don't know how high it will be. In order to properly space the adjacent column I have used nested flex boxes.
This works fine on main break point but as soon as I add the flex box then this breaks the responsiveness as the columns don't stack on mobile anymore.
What should I do here? Should I drop flexbox? How else can I achieve this spacing?
.container{
margin-top: 60px;
}
.container{
border: 1px solid #000;
}
.row{
display: flex;
}
.row-center{
display: flex;
flex: 1;
}
.outer{
display: flex;
flex-direction: column;
justify-content: space-around;
border: 1px solid #000;
width: 100%;
}
.one, .two, .three{
flex: 0 0 40px;
border: 1px solid #000;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6"><img src="http://placehold.it/350x500"></div>
<div class="col-xs-12 col-sm-6 row-center">
<div class="outer">
<div class="one">some text</div>
<div class="two">some text</div>
<div class="three">some text</div>
</div>
</div>
</div>
</div>
jsfiddle mirror: https://jsfiddle.net/y68dnzwy/
May be this help you:
.container{
margin-top: 60px;
}
.container{
border: 1px solid #000;
}
.row{
display: flex;
}
.row-center{
display: flex;
flex: 1;
}
.outer{
display: flex;
flex-direction: column;
justify-content: space-around;
border: 1px solid #000;
width: 100%;
}
.one, .two, .three{
flex: 0 0 40px;
border: 1px solid #000;
}
/* Added: */
#media screen and (min-width:100px) and (max-width: 980px) {
.row-center {
flex: auto;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6"><img src="http://placehold.it/350x500"></div>
<div class="col-xs-12 col-sm-6 row-center">
<div class="outer">
<div class="one">some text</div>
<div class="two">some text</div>
<div class="three">some text</div>
</div>
</div>
</div>
</div>
Demo
Used this with bootstrap 3, 4-text boxes in row with same height depending on the dynamic text, where on tablet and mobile are only 2 in row. And with tetx centered in middle
Somehow this flex magic makes on mobile, that only text boxes that are in same row have same height, so if last item is super tall, only last 2 items are super tall, not affecting first 2.
<div class="row row-flex-box">
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="thumbnail flex-col-vertical-center">
<div class="caption"><span>text text text text</span></div>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="thumbnail flex-col-vertical-center">
<div class="caption"><span>text text text text text text text text</span></div>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="thumbnail flex-col-vertical-center">
<div class="caption"><span>text text text text</span></div>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="thumbnail homepage-slider-bottom-block-single flex-col-vertical-center">
<div class="caption"><span>text text text texttext text text texttext text text text</span></div>
</div>
</div>
</div>
Css:
.row-flex-box {
display: flex; /* make cols same heigh */
flex-wrap: wrap; /* alows responsive behavior of cols (otherwise cols will never break on mobile)*/
}
.flex-col-vertical-center {
display: flex; /* specifing display/flex-direction/justifi-content only because i want to have text aligned in the middle of boxes much cleaner than display:table-cell way*/
flex-direction: column;
justify-content: center;
text-align:center;
height: 100%; /* since cols have bigger height this has effect, also can be ussefull - height: calc(100% - 15px); with 15px bottom margin */
}
http://www.bootply.com/somVIZEXxC# Here is the bootply for my website, basically I want the cols col-lg-6 to be the same height so that the images I have inside them are even, at the moment, one image stretches below the other one.
Edit: Current version http://www.bootply.com/UIWf6q09h4
Content should be placed within columns, and only columns may be
immediate children of rows
So get out the h1 from the .row and set the class .row-eq-height to .row like this:
.row-eq-height{
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
<div class="row row-eq-height"></div>
Here the full information http://getbootstrap.com.vn/examples/equal-height-columns/
Here a minimal snippet to illustrate:
.row-eq-height{
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<h1 class="text-center"> Where do we cater to? </h1>
<div class="row row-eq-height">
<div class="col-xs-6">
<img src="http://i.stack.imgur.com/gijdH.jpg?s=328&g=1" class="img-responsive">
</div>
<div class="col-xs-6" style="background: blueviolet">
<img src="http://orig00.deviantart.net/0cbb/f/2013/107/3/a/lnd_small_bmp_64x64_by_shadowblitz-d620m47.jpg" class="img-responsive">
</div>
</div>
Like I said I don't know the limitation of your img (height, if can be background, etc.) Here some tips to achieve what you want:
Remove the margin from row and the padding from col-, add width: 100% to your image like this:
.row-eq-height{
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
margin: 0;
}
.row-eq-height [class*="col-"]{
padding: 0;
}
.row-eq-height img{
width: 100%;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<h1 class="text-center"> Where do we cater to? </h1>
<div class="row row-eq-height">
<div class="col-xs-6">
<img src="http://i.stack.imgur.com/gijdH.jpg?s=328&g=1" class="img-responsive">
</div>
<div class="col-xs-6" style="background: blueviolet">
<img src="http://orig00.deviantart.net/0cbb/f/2013/107/3/a/lnd_small_bmp_64x64_by_shadowblitz-d620m47.jpg" class="img-responsive">
</div>
</div>
If the image can be background you can define a specific proportional height you can use padding-top, let say 4:3 that would be 3 * 100 / 4 = 75 = padding-top
So you can do this:
.row-eq-height{
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
margin: 0;
}
.row-eq-height [class*="col-"]{
padding: 0;
}
.img-container{
background-size: cover;
padding-top: 75%; /*you can change it to obtain a desired height*/
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<h1 class="text-center"> Where do we cater to? </h1>
<div class="row row-eq-height">
<div class="col-xs-6">
<div class="img-container" style="background-image: url(http://i.stack.imgur.com/gijdH.jpg?s=328&g=1)"></div>
</div>
<div class="col-xs-6">
<div class="img-container" style="background-image: url(http://orig00.deviantart.net/0cbb/f/2013/107/3/a/lnd_small_bmp_64x64_by_shadowblitz-d620m47.jpg)"></div>
</div>
</div>
just add the "equal-heights" class to the columns parent
$('.equal-heights').each(function(){
var highestBox = 0;
$(this).children('[class*="col-"]').each(function(index, el){
if( $(el).height() > highestBox ) highestBox = $(el).height();
});
$(this).children('[class*="col-"]').css('height',highestBox);
});
the html would be like this...
<div class="row equal-heights">
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
</div>
see this pen...
Try taking out your
<h1 class="text-center"> Where do we cater to? </h1>
because one "row" is meant to add up to 12. Right now, you have this h1 in the row along with your two col-lg-6, which should be in a row of their own (6+6=12).
The h1 should be in its own row with its own col-lg-12, or whatever you might want.
Did you try using max-height:?
heres a fork of your code http://www.bootply.com/wVSqmPKERL
added another row to the columns and add the class of height-fix which just adds
a max-height attribute that you can set to whatever you need.
edit
You can also combine this with min-height if the images are to small
Here is a responsive grid with images. The image containers have equal height and the images themselves are vertically centered;
.grid {
margin-top: 25px;
display: flex;
flex-wrap: wrap;
}
.grid>[class*='col-'] {
display: flex;
flex-direction: column;
align-items: stretch;
margin-bottom: 15px;
}
.grid img {
margin: auto 0;
}
#media (max-width: 767px) {
.container {
max-width: 100%;
}
}
#media (max-width: 575px) {
.container {
max-width: 100%;
padding-left: 0;
padding-right: 0;
}
.posts-grid>[class*='col-'] {
padding-left: 5px;
padding-right: 5px;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="grid">
<div class="col-sx-6 col-md-4 col-lg-2">
<img src="http://placehold.it/350x300" class="img-responsive">
</div>
<div class="col-sx-6 col-md-4 col-lg-2">
<img src="http://placehold.it/350x450" class="img-responsive">
</div>
</div>
</div>