CSS Flexbox not working for navigation (mobile screen) - css

First off I am very new to CSS and following an online course.
I am trying to create a navigation bar that changes from row to column when going into mobile. I am using flexbox and #media queries for that however, it seems like my code does not make any change to the navigation direction when going to mobile. Please help! ]
.container {
display: flex;
color: white;
justify-content: flex-end;
}
.box {
width: 90px;
height: 3rem;
margin: 0.5rem;
background-color: black;
text-align: center;
}
.box-four {
width: 100px;
}
#media screen and (max-width: 600px) {
.container {
flex-direction: row;
justify-content: center;
flex-grow: 2rem;
flex-wrap: wrap;
flex-basis: 10%;
}
}

Can't say exactly what problems you are having without seeing some example HTML to work with but at first glance it seems you should be using flex-direction:column for your mobile view.
Note well: What is in the media query is your mobile styles in this case. You can tell because you have specified max-width which means that it will apply to all screen sizes below the width provided (600px).
See the below as an example which incorporates your provided CSS with some basic HTML I made. (Resize the browser window to test).
.container {
display: flex;
color: white;
}
.box {
width: 90px;
height: 3rem;
margin: 0.5rem;
background-color: black;
text-align: center;
}
.box-four {
width: 100px;
}
#media screen and (max-width: 600px) {
.container {
flex-direction: column;
justify-content: center;
flex-grow: 2rem;
flex-wrap: wrap;
flex-basis: 10%;
}
}
<div class="container">
<div class="box">Entry 1</div>
<div class="box">Entry 2</div>
<div class="box">Entry 3</div>
<div class="box">Entry 4</div>
</div>
You can also read more about flex-direction at the MDN Web Docs

Related

Can't center header image with CSS no matter what

I am using a theme to which I have added an image header (with our logo). I have called this image "https://www.londonim.co.il/wp-content/uploads/2019/04/logo.jpg". the website address is https://londonim.co.il - it is in Hebrew (regardless).
The div in which "logo.jpg" is situated is inheriting some display from the there (i think "block"), and I would very much like it to be centered.
In order to try and center it i have used margins of 25% but it would not keep its central alignment when displayed in bigger screens (unless in full screen mode).
when i temper with the wrapper's display property it generally disappears or loses the alignment altogether. Any thoughts please?
theme header:
<div class="cutewp-container" id="cutewp-header" itemscope="itemscope" itemtype="http://schema.org/WPHeader" role="banner">
<div class="cutewp-head-content clearfix" id="cutewp-head-content">
<div class="cutewp-outer-wrapper">
<div class="cutewp-header-inside clearfix">
<div id="cutewp-logo">
<div class="site-branding">
<a href="https://www.londonim.co.il/" rel="home" class="cutewp-logo-img-link">
<img src="https://www.londonim.co.il/wp-content/uploads/2019/04/logo.jpg" alt="" class="cutewp-logo-img">
</a>
</div>
my tweaks:
/* homepage tweaks */
.cutewp-main-wrapper {
position:relative!important;
margin-left:12.5%;
margin-right:12.5%;
}
#cutewp-logo {
margin-left:25%;
margin-right:25%;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
#cutewp-primary-navigation {
text-align: center;
text-align: -webkit-center;
}
#cutewp-header {
text-align: center;
text-align: -webkit-center;
}
.menu-main-container {
margin-left: 25%;
}
Thanks in advance for the help!
The best solution is to:
#cutewp-logo {
margin-left:25%;//delete this
margin-right:25%;//delete this
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;//delete this
width: 100vw;//add this
}
The element isn't centering because it doesn't occupy the full width of the screen therefore has no reference on to be centered to
You need to clear out a few of those margin-left and margin-right
CHANGE
#cutewp-logo {
margin: 5px 0px 5px 0px;
float: left;
width: 41%;
}
.cutewp-main-wrapper {
position: relative!important;
margin-left: 12.5%;
margin-right: 12.5%;
}
#media only screen and (max-width: 890px) {
#cutewp-header {
margin-left: 25%;
background-color: white;
}
.cutewp-content-wrapper {
margin-left: 25%;
}
}
TO
#cutewp-logo {
margin: auto;
}
.cutewp-main-wrapper {
position: relative!important;
}
#media only screen and (max-width: 890px) {
#cutewp-header {
background-color: white;
}
.cutewp-content-wrapper {
margin: auto;
}
}
This will fix the issue on both your desktop and mobile view.

why is my media query not changing anything?

I am trying to apply media queries to my website, however nothing is changing.
I have tried changing the position of my media query in the CSS flow, but no joy.
here is the specific part of the code:
.desktop-header {
display: flex;
justify-content: space-between;
align-items: center;
height: auto;
width: auto;
padding: 0 1.5rem;
}
.mobile-header {
display: none;
}
#media only screen and (max-width: 480px) {
.desktop-header {
display: none;
}
.mobile-header {
display: flex;
height: 4rem;
width: auto;
justify-content: space-around;
align-items: center;
}
}
I would like to replace the desktop header with a simplified mobile header that just displays images rather than the text. Currently, nothing is changing at all. Everything seems sound in the html, so I can only think I am leaving something out of the CSS?
Thank you for your help in advance!
I found nothing wrong with your code. It's absolutely fine! May be there are other conflicting code that bars your code from displaying as you intended.
Here I added some HTML and it's working fine with your code.
.desktop-header {
display: flex;
justify-content: space-between;
align-items: center;
height: auto;
width: auto;
padding: 0 1.5rem;
}
.mobile-header {
display: none;
}
#media only screen and (max-width: 480px) {
.desktop-header {
display: none;
}
.mobile-header {
display: flex;
height: 4rem;
width: auto;
justify-content: space-around;
align-items: center;
}
}
<div class="desktop-header">
<h1>Desktop Header</h1>
</div>
<div class="mobile-header">
<h1>Mobile Header</h1>
</div>
use these media queries and follow at https://css-tricks.com/snippets/css/media-queries-for-standard-devices/
#media screen
and (device-width: 320px)
and (device-height: 640px)
and (-webkit-device-pixel-ratio: 3) {
}

Flexbox layout not working as intended

i have this HTML
<div class="flex-container">
<div class="flex-full">1</div>
</div>
<div class="flex-container">
<div class="flex">2</div>
<div class="flex">3</div>
<div class="flex">4</div>
</div>
Styled by this CSS
*{
margin:0;
padding:0;
}
.flex-container{
width: 90%;
max-width: 960px;
display: flex;
background-color: lightgray;
margin-left: auto;
margin-right: auto;
flex-wrap: wrap;
}
.flex-full{
background-color: red;
flex: 1 0 100%;
}
.flex{
background-color: blue;
flex: 1 0 90px;
}
#media screen and (max-width: 480px) {
.flex-container {
background-color: lightgreen;
width: 100%;
}
}
#media screen and (max-width: 480px) {
.flex-container {
background-color: lightgreen;
width: 100%;
}
}
But the result is not as intended. i will try to give a visual presentation of the problem(upload image wont work):
On screen width 320px and below, i get the following result
111111111111111
222222233333333
444444444444444
What i expected was:
phone screen bigger screen
111111111111111 111111111111111111
222222222222222 222222333333444444
333333333333333
444444444444444
I hope it made sence, please help me out :)
JSFiddle
Are you maybe looking for flex-direction: column;?
on .flex-container.
In Flex, you may use justify-content and align-content for horizontal and vertical alignment, respectively.
Also flex-basis takes the same values as the width and height properties, and specifies the initial main size of the flex item, before free space is distributed according to the flex factors.
.flex-container {
flex-direction: row;
justify-content: space-between;
align-content: space-between;
}
.flex1, .flex4{
flex-basis: 100%;
}
.flex2, .flex3{
flex-basis: 50%;
}
#media screen and (max-width: 320px){
.flex2, .flex3{
flex-basis: 100%;
}
}
You may also use width: 100% and width: 50% instead if flex-basis.

Switching flex items from row to column alignment

I have a 'card' like html component, that it displays this way:
+------+-----------------+-------+
| 1 | 2 | 3 |
+------+-----------------+-------+
And when I go under the 720px I want that became like this:
+------+-------------------------+
| | 2 |
| 1 +-------------------------+
| | 3 |
+------+-------------------------+
I am having difficulties to figure out how can I achieve this with flexbox. I am wondering if it is the right solution for this kind of problem.
the initial state, is currently like this:
<div class="card">
<div class="status"></div>
<div class="title">Tryout.it</div>
<div class="details">156 emails</div>
<div class="action"><button>go</div>
</div>
and my CSS something like this:
.card {
margin: 10px;
min-width: 320px;
min-height: 42px;
background-color: white;
border: 1px solid #eee;
border-radius: 6px;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
padding: 9px 10px;
}
.card .status {
width: 30px;
height: 24px;
}
.card .title {
flex-grow: 1;
}
You cannot achieve this with flexbox with your current HTML structure.
(In the future you can use the grid layout for this, but at the moment it is not supported.)
However, if you place 2 and 3 in a container you can do this:
.card {
margin: 10px;
border: 1px solid #eee;
border-radius: 6px;
display: flex;
justify-content: space-between;
padding: 9px 10px;
}
.card .avatar {
width: 30px;
height: 24px;
}
.card .container {
flex-grow: 1;
display: flex;
flex-wrap: wrap;
}
.card .container .name {
width: 50%;
}
.card .container .presentation {
flex-grow: 1;
}
#media (max-width: 720px) {
.card .container .name {
width: 100%;
}
}
<div class="card">
<div class="avatar">
x
</div>
<div class="container">
<div class="name">Mr. Potato</div>
<div class="presentation">Hi!</div>
</div>
</div>
You can use CSS media query to apply a different style your div's at a certain screen size.
#media screen and (max-width: 720px) {
.css-class {
}
#css-id {
}
}
#ciaoben, Since you disappeared after asking you question, I'll provide an answer based upon the logic that the .title and .details blocks are meant to be blocks 2 and 3 from your diagram and that the .action is meant to be a fourth block on the opposite side.
Simply put, you need another layer for your HTML structure before you can apply the CSS to adjust it. You would wrap the blocks you want to have the appearance of altered in another DIV.
Once you have the HTML structure updated, it's a simple matter of applying a #media query to adjust the appearance at the desired screen size.
Please see my example below. Note: I updated your CSS to a more streamlined flexbox approach. Further, it applis display: flex to the .action block because, as I noted above, you don't identify it at all.
.card {
margin: 10px;
min-width: 320px;
min-height: 42px;
background-color: white;
border: 1px solid #eee;
border-radius: 6px;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
padding: 9px 10px;
}
.card .status {
width: 30px;
}
.card .stretchit, .card .action {
-webkit-flex: 1;
flex: 1;
}
#media screen and (min-width: 720px) {
.card .stretchit {
display: flex;
}
.card .title, .card .details {
-webkit-flex: 1;
flex: 1;
}
}
<div class="card">
<div class="status"></div>
<div class="stretchit">
<div class="title">Tryout.it</div>
<div class="details">156 emails</div>
</div>
<div class="action"><button>go</button></div>
</div>

firefox v.37 nested squared flexboxes

I made a flexbox layout with some squared flexboxes nested in the main layout flexboxes. It works fine in Chrome and IE but Firefox v37.02 won't make them squared or doesn't show them at all.
I would like the containers themselves to be flexboxes of the main layout, so I can rearrange them with media-queries and have the content flexboxes wrap in the available space.
Here is a Fiddle example https://jsfiddle.net/SanMoll/jj591x8f/
<div class="main">
<div class="article">
<div class="itemXL"></div>
<div class="itemXL"></div>
</div>
<div class="aside">
<div class="itemL"></div>
<div class="itemL"></div>
<div class="itemL"></div>
<div class="itemL"></div>
</div>
</div>
<style>
.main {
display:flex;
width:100%;
height:100%;
background-color:#000;
}
.article {
/* display: flex;*/
flex-wrap: wrap;
flex-direction: column;
width: 50%;
flex: 1;
}
.itemXL {
background-color: #f4f4f4;
width: 97%;
height:0;
margin-right: 3%;
margin-bottom: 3%;
padding-bottom: 97%;
}
.aside {
/*display: flex;*/
flex-wrap: wrap;
flex-direction: row;
width: 50%;
}
.itemL {
background-color: #f4f4f4;
width: 47%;
height:0%;
margin-right: 3%;
margin-bottom: 3%;
padding-bottom:47%;
}
</style>
Any help is much appreciated.
Thank in advance!
You can use the flex-basis it is like the widht of the element but for flexboxes.
For example flex-basis: 50%;
and in the media query: flex-basis: 100%; (if flex-wrap is wrap the container will push the others down)
You should also start variable names not with numbers, you can put a '_'infront then it is fine
If you have problems with browser support just add this to be sure that it works in every browser
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;

Resources