Issues with alignment when it comes to flex - html

I have two issues with my flex I require assistance resolving. I created a fiddle here to access: https://jsfiddle.net/dmj5yscz/
Issue 1: I am unable to move the elements to the center (see red in screenshot). Tried margin-left:auto and margin-right:auto in various places but no luck. (note: should only happen if width is no more than 1144px)
Issue 2: I want the text to be vertically aligned in the center (any screen width)
Here is the code:
<!-- newsletter section -->
<div class="newsletter_section">
<div class="newsletter_inner_section">
<div class="newsletter_left content-box">
<img src="xxx">
</div>
<div class="newsletter_mobile_col">
<div class="newsletter_center content-box">
<p class="newsletter_text_section">Text1 </p>
</div>
<p class="newsletter_input_section">Text2 </p>
</div>
</div>
</div>
/* newsletter section */
.newsletter_inner_section{
display:flex;
}
.newsletter_gif{
width:150px;
height:auto;
}
.newsletter_left,
.newsletter_center,
.newsletter_right{
display:inline-flex;
}
.newsletter_left{
width:auto;
}
.newsletter_center{
width:100%;
}
.newsletter_right{
width:40%;
justify-content:flex-end;
}
.newsletter_text_section{
color:black !important;
font-size:20px !important;
font-weight:bold;
}
/* Media Newsletter section only */
#media (max-width:1144px){
.newsletter_right,
.newsletter_center{
width:auto;
}
.content-box {
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
}
.newsletter_inner_section{
justify-content:center;
display: flex;
flex-direction:row;
}
.newsletter_text_section{
padding-left:15px;
margin-bottom:0;
}
.newsletter_mobile_col {
flex-grow: 1;
display: flex;
flex-direction: column;
}
}
#media (max-width:565px){
.newsletter_inner_section{
flex-direction:column;
}
.newsletter_left{
margin-left:auto;
margin-right:auto;
}
.newsletter_gif{
padding-bottom:20px;
}
}

You have set: flex-grow:1; which is making second flex-div to take available space. Please remove this and it will be center aligned
.newsletter_mobile_col {
/* flex-grow: 1; */
display: flex;
flex-direction: column;
}
And yes, use align-items:center; on flex container to make it center aligned across cross-axis.
#media (max-width: 1144px){
.newsletter_inner_section {
justify-content: center;
display: flex;
flex-direction: row;
align-items: center;/* here */
}
}
And if you want to behaviour on all screens, just write the above CSS outside media query :)

To display flex items into center
Flexbox link
(justify content)
For the second, set another display flex with: flex direction column and center it

Related

How to resize an image to fit inside a flexbox, and contain everything inside a grid of 1fr?

I'm trying to create a grid which has 1fr and contains flexboxes and grid inside it . But the .container grid decreases in size different than I expected for the smaller screen sizes (used mediaquery).
The flexbox (portfolio-main-card) is used to contain the image and the text with image having flex:1 and text with flex:2 respectively . But the image is going way beyond the flex:1 .The html part for the flexbox is:
<div class="portfolio-main-card">
<div class="my-image">
<img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fhyperallergic.com%2Fwp-content%2Fuploads%2F2016%2F01%2FCat_023-FF.jpg&f=1&nofb=1" alt=""/>
</div>
<div class="my-text">
<div class="my-text-p">
<p>Welcome to my portfolio</p>
</div>
</div>
</div>
and the CSS part for it is
.portfolio-main-card {
display: flex;
align-items: center;
justify-content: space-around;
border:2px solid yellow;
background-color: rgb(134, 107, 134);
width: 100%;
height: 50vh;
}
.my-image {
flex:1 ;
border: 2px solid red;
justify-self: center;
}
.my-text {
display: flex;
flex:2;
justify-content: center;
}
The whole code is here
You have to style your <img> tags
Use the style below and see the result :)
.my-image {
height:100%;
width:50%;
display:flex;
}
.my-image img{
height:100%;
object-fit:cover;
width: 100%;
}

flexbox not centering with justify-content, div elements taking up too much space i'm guessing, but why?

I've been trying to get these objects to center and when I used an <a href> tag, I could see that I was able to click way away from the picture and still the link would activate. I am assuming this means that the child containers are taking up 50% of the width each, despite only a tiny portion of the container being full. Why is there blank space that is preventing me from aligning my objects?
RELEVANT HTML:
<div class="container">
<div class="previous">
<img class="containerimg" src="https://i.imgur.com/YgZ2GOl.png">
<p>Previous Project </p>
</div>
<div class="next">
<img class="containerimg" src="https://i.imgur.com/s11MTLc.png">
<p> Next Project</p>
</div>
</div>
RELEVANT CSS:
.container {
display: flex;
justify-content: center;
}
.containerimg {
width: 30%;
height: auto;
}
.next {
display: flex;
flex-direction: column;
}
.previous{
display: flex;
flex-direction: column;
}
CODEPEN: https://codepen.io/daniel-albano/pen/zYGKZEw?editors=1100
Your question is a little vague, but I'm assuming that you want to center the .previous and .next divs.
Since both of these are using display: flex already, you simply need to add align-items: center to the .previous and .next classes to make them center horizontally. If you also want the content (the image and text) to center vertically, you'll need to add justify-content: center. Here's the result:
.next {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.previous {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
If you're trying to make the images in those divs take up more space, you'll need to increase the width rule below. Since you commented that you need 100%, you'll need to change it to this:
.containerimg {
width: 100%;
height: auto;
}
I found the issue, I needed my images to contain 100% of the space and I needed to assign a width element to the child containers.
.container {
display: flex;
justify-content: center;
width:100vw;
}
.previous, .next{
width:30%;
display:flex;
flex-direction:column;
align-items:center;
}
img{
width:100%;
}
<div class="container">
<div class="previous">
<img class="containerimg" src="https://i.imgur.com/YgZ2GOl.png">
<p>caption 1</p>
</div>
<div class="next">
<img class="containerimg" src="https://i.imgur.com/s11MTLc.png">
<p>caption 2</p>
</div>
</div>
You should be able to solve this issue by adding "align-items: center" to your .next and .previous classes. The reason for this is that when you switch the flex-direction to column that also switches how align-items and justify-content work, essentially reversing them.
.next {
display: flex;
flex-direction: column;
align-items: center;
}
.previous{
display: flex;
flex-direction: column;
align-items: center;
}

how to make a percentage height div responsive to content inside 100vh section

morning all,
thought this one would be straight forward but not (unless I'm missing something obvious)
[EDIT] The Layout of the front-page section has to match the front cover of a book that has a yellow stripe 28% of the height of the book, 60% down the page [/EDIT]
I have a section that I want 100vh within that section I have two divs set to percentage height, each with content. It all looks great until I reduce the screen size. How can I have a percentage height div and keep it responsive to the content (apart from using media queries). Note, I'm using flex to position the content within the divs.
[EDIT] In other words (if necessary) increase the height of the 60% .intro div and the 28% .stripe div to accommodate the text wrapping
I've tried playing with using min-height:28%; on the .stripe class but that then breaks the constraints of the image height being 80% of the .stripe height. Can anyone help please.
.fs {
height: 100vh;
}
.container {
margin:0;
background-color:#545454;
overflow: auto;
}
.intro {
height:60%;
}
.bees-intro {
font-size:60px;
color:#f4d00b;
text-align:center;
}
.stripe {
height:28%;
background-color:#f4d00b;
color:#000000;
}
.bee-quest {
font-size:80px;
margin-right:0px;
height:100%;
margin-right:40px;
}
.bee-quest img {
margin-right:20px;
height:80%;
}
.align-center-center {
display: flex;
justify-content: center;
align-items: center;
}
.align-right-center {
display: flex;
align-items: center;
justify-content: flex-end;
flex-wrap: wrap;
}
<section id="front-page">
<div class="fs container">
<div class="intro align-center-center">
<h1 class="bees-intro">The Bees of Greater Manchester</h1>
</div>
<div class="stripe">
<div class="bee-quest align-right-center">
<img src="bee-quest.png">
<span class="">QUEST</span>
</div>
</div>
</div>
</section>
A working pen can be seen HERE
I'm not 100% sure what you mean by it's not responsive but i'm guessing you want the yellow box to wrap around the bee logo and text when it's in mobile? To do this, remove the 28% height from the stripe and and use the content within it to determine the height.
Example here:
https://jsfiddle.net/m3fnp0nL/
Let me know if this isn't what you're after :)
Thanks to Shaun I've figured out a good solution
The .stripe is set to min-height: 28vh; and the image is set to height: calc(28vh - 20px);
I've set the .intro to min-height:60vh so as the wrapping text will increase the container size.
.fs {
height: 100vh;
}
.container {
margin:0;
background-color:#545454;
overflow: auto;
}
.intro {
min-height:60vh;
}
.bees-intro {
font-size:60px;
color:#f4d00b;
text-align:center;
}
.stripe{
background-color:#f4d00b;
color:#000000;
min-height:28vh;
}
.bee-quest {
font-size:80px;
margin:10px;
display:flex;
flex-wrap:wrap;
justify-content: center;
align-items: center;
}
.bee-quest img {
height: calc(28vh - 20px);
}
#media (min-width: 768px) {
.bee-quest img {
margin-right:20px;
}
.bee-quest span {
margin-right:20px;
}
}
.align-center-center {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
.align-right-center {
display: flex;
align-items: center;
justify-content: flex-end;
flex-wrap: wrap;
}
<section id="front-page">
<div class="fs container">
<div class="intro align-center-center">
<h1 class="bees-intro">The Bees of Greater Manchester</h1>
</div>
<div class="stripe align-right-center">
<div class="bee-quest">
<img src="http://www.beequest.co.uk/images/bee-quest.png">
<span class="">QUEST</span>
</div>
</div>
</section>
The original Pen has been updated with the correct answer
The only problem I have now is that the content scrolls within the divs

Vertically center multiple lines of flex items

I am trying to vertically align the child DIV of a wrapper but unfortunately not. Requirement is full-width and height of the screen wrapper with two child DIV vertically and horizontally centered. (height of the div is being handled by jQuery to make window height)
Note: I don't want to make child DIV width:100%
This is what I have tried:
.wrapper {
width: 100%;
height:100%;
min-height:600px; /*just for example*/
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
background:#e1e1e1;
}
.row1 {
background: #cdcdcd;
}
.row2 {
background: #404040; color:#fff;
}
<div class="wrapper">
<div class="row1">This is row one</div>
<div class="row2">This is row two</div>
</div>
An initial setting of a flex container is flex-direction: row. This means that the children of the container ("flex items") will line up in a row.
To override this default you can add flex-wrap: wrap to the container and give the items width: 100%, which forces one item per row. But you're saying this method isn't an option in this case.
So another option is to switch the container to flex-direction: column.
.wrapper {
width: 100%;
height:100%;
min-height:600px; /*just for example*/
display: flex;
flex-direction: column; /* NEW */
justify-content: center;
align-items: center;
flex-wrap: wrap;
background:#e1e1e1;
}
.row1 {
background: #cdcdcd;
}
.row2 {
background: #404040; color:#fff;
}
<div class="wrapper">
<div class="row1">This is row one</div>
<div class="row2">This is row two</div>
</div>

Flexbox creates blank space on the right of a page

Whenever i remove the flex display- it goes away. Can't understand what's happening there. Been going through source up and down, but no element seems to be stretching there.
html of it
<div class="newheadwrap">
<div class="newlogo newhead">with contents</div>
<div class="newtagline newhead" >with contents</div>
<div class="mainhead newhead">with contents</div>
</div>
and the css
.newheadwrap {
-o-display:flex;
-webkit-display:flex;
-ms-display:flex;
-moz-display:flex;
display:flex;
flex-direction:row;
width:966px;
margin:0 auto;
}
.newlogo {
width:17%;
padding-top:43px;
}
.newtagline {
width:33%;
color:white;
padding-top:93px;
font-family: berkeleyFont;
font-size:16px;
float:right !important;
}
.mainhead {
width:50%;
}
I know it's not advised to post live links here, but fiddle will not do in this case, so, here is a link
https://aps-direct.myshopify.com
One way to remove the extra padding to the right side of your current page:
Add overflow: hidden; to the .newheadwrap
.newheadwrap {
-o-display: flex;
-webkit-display: flex;
-ms-display: flex;
-moz-display: flex;
display: flex;
flex-direction: row;
width: 966px;
margin: 0 auto;
overflow: hidden;
}
You have extra space because of this:
.span9 {
width: 717px;
}
reduce the width and it will fit