I'm facing a little problem.. I'm trying to build some kind of "Matrix" to build a snake game in angular, and for some reason, there is a margin / padding I can not remove. Here is my code:
<!-- snake.component.html -->
<div id="ng-snake-main">
<div *ngFor="let row of matrix" class="row">
<ng-container *ngFor="let box of row.boxes>
<snake-box [box]="box"></snake-box>
</ng-container>
</div>
</div>
<!-- box.component.html -->
<div class="box"></div>
Both using the same style file:
// styles.scss
.row {
margin: 0px;
padding: 0px;
}
.box {
display: inline-block;
background-color: blue;
width: 30px;
height: 30px;
border: 1px solid black;
}
so, why is there a space between rows??? I think that it doesn't make sense, but I'm sure I'm missing something. I'll left some screenshots:
In this screenshot you can check that the "snake-box" component is adding some kind of margin.
In this other screenshot you can see that the div actually doesn't have margin/padding.
Is angular adding margin to my component? If yes, how can I remove it?
The answer to you problem is CSS FLEX.
It would work with these styles:
// styles.scss
.row {
display: flex;
flex-direction: row;
}
.box {
background-color: blue;
width: 30px;
height: 30px;
border: 1px solid black;
}
If you want more information about flexbox, here is an interesting link https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Related
I've been working on a website, and all was going well until I had someone try to test it on their iPhone. All the content seems to be squished onto the screen on iPhone. This happened in Safari, Chrome, and Duck Duck Go. I am unable to recreate the problem in any of the iPhone simulators I've tried on the internet, but have seen the problem on both iPhones I've tested it on.
Here is what it looks like on the iPhone:iPhone image
Here is what it looks like on my android: Android image
The layout uses flexbox extensively, which my research leads me to believe is the problem. I have tried adding this css to the bottom of my css file to prevent flexboxes from shrinking, but it has not solved the problem:
* {
flex-shrink: 0;
}
Let me know if you need more source code, but since the problem is throughout the entire page I would have to post all of it.
Edit: Here is the pastebin of the css https://pastebin.com/c0VjkGDc
Edit 2: Here is an example of the HTML from one of the cards that seems to be squished on iPhone (ignore the php):
<div class='container'>
<div class='results-container'>
<div class='result-card'>
<div class='card-date'>
<div class='month'>{$date_month}</div>
<div class='day'>{$date_day}</div>
<div class='year'>{$date_year}</div>
</div>
<div class='card-info'>
<div class='card-title'>{$aud_position}</div>
<div class='card-subtitle'>{$aud_name}</div>
<div class='card-location'><i class="fas fa-map-marker-alt"></i> {$aud_location}</div>
<div class='card-pay'><span class='pay-span'>{$aud_pay}</span></div>
<div class='card-icons'>
<i class='icon-red fas fa-clock'></i> App deadline passed
</div>
</div>
<div class='card-actions'>
<div class='card-fave'><i class="fas fa-heart"></i></div>
</div>
</div>
</div>
</div>
And here is relevant CSS for that:
.container {
display: flex;
}
.results-container {
width: 100vw;
display: flex;
flex-direction: column;
}
.result-card {
flex: 1;
display: flex;
margin-top: 5px;
background-color: white;
padding: 15px;
}
.card-date {
flex: 0 0 5em;
display: flex;
flex-direction: column;
border-right: 1px solid #ddd;
align-items: center;
justify-content: center;
border-radius: 5px;
}
.day {
font-size: 2em;
}
.card-info {
padding-left: 15px;
flex: 1;
display: flex;
flex-direction: column;
line-height: 1.5em;
}
.card-title {
font-weight: bold;
font-size: 1.1em;
}
.card-pay {
margin: 10px 0;
}
.pay-span {
background-color: #f0f0f0;
border-radius: 5px;
padding: 10px;
border: 1px solid #ddd;
}
* {
flex-shrink: 0;
}
As mentioned in the comment of the question above, the original HTML and CSS contain quite a lot of errors. To validate and fix the errors, the following online resources can be used:
W3 HTML Validator: https://validator.w3.org/nu/
W3 CSS Validator: https://jigsaw.w3.org/css-validator/
On the other hand, if you apply the Bootstrap responsive framework in your website, it can save you quite a lot of efforts to handle different screen sizes.
I am trying to build a basic messenger view on a mobile screen. So there is a header at the top, a scrollable list of messages in the middle, and a bar at the bottom with a textarea and button to send a new message.
I am using an autosize plugin to makes the textarea expand as a user types their message. The problem is that as it changes the height property, it will start to overflow the height of the container that it is in, instead of that container expanding to take up more room.
A working sample is here: https://codepen.io/jwynveen/pen/RJdWLB?editors=1100#0
#container {
width: 412px;
height: 660px;
border: solid 2px black;
display: flex;
flex-direction: column;
}
#container h1 {
border-bottom: solid 1px gray;
margin-bottom: 0;
padding: 1rem;
}
#container #message-list {
flex-grow: 1;
flex-shrink: 1;
overflow-y: scroll;
}
#container #message-list .message {
margin: 1rem;
padding: 1rem;
border: solid 1px gray;
}
#container #message-input-bar {
display: block;
}
#container #message-input {
padding: 1rem;
display: flex;
border-top: solid 2px red;
}
#container #message-input textarea {
flex-grow: 1;
}
<html>
<div id="container">
<h1>My Header</h1>
<div id="message-list">
<div class="message">This is a dummy message.</div>
<div class="message">This is a dummy message.</div>
</div>
<div id="message-input">
<textarea style="height: 100px"></textarea>
<button id="send">Send</button>
</div>
</div>
</html>
If there are no messages in the center area, the textarea makes its container expand as expected. But once the center area has enough to scroll, the textarea starts to overflow.
Found a fix based on the suggestion by #vaishali-kapadia. I wrapped the #message-input with another div so that the added div is display:block and the existing one maintains the flexbox layout.
Changed from:
<div id="message-input">
<textarea style="height: 100px"></textarea>
<button id="send">Send</button>
</div>
To:
<div id="message-input-bar">
<div id="message-input">
<textarea style="height: 100px"></textarea>
<button id="send">Send</button>
</div>
</div>
With the added CSS (though not necessary since the div is display:block by default):
#message-input-bar {
display:block;
}
Solution 1
You have applied manual height to container. so it stops there after reaching that particular height.
Instead apply height: auto; so that container can expand as per the content
See this codepen -
https://codepen.io/vaishalik3/pen/QxoyMd?editors=1100#0
Solution 2 -
In case you want scrollbar as it is.
Apply display: block; instead of flex to #message-input
width: 100%; or as per your need to textarea
See this codepen - https://codepen.io/vaishalik3/pen/ERMPLd?editors=1100#0
Solution 3
Apply display: grid; to .container
display: flex; to #message-input
See this codepen - https://codepen.io/vaishalik3/pen/RJdqzq?editors=1100#0
Hope this helps :)
Remove
style="height: 100px"
from your textarea and provide height:100% to CSS.
https://codepen.io/anon/pen/ZRPQBX?editors=1100#0
I'm trying to make an "image mosaic" that consists mostly of images of the same size, and some of them the double height.
They all should align neatly like this:
enter image description here
But still like this:
enter image description here
I know is more "easy" do this with divs and "float:left" but i have to do with list.
Someone had i idea how can i fix this?!
Use flexbox. You don't HAVE to use list? Unless this is a homework assignment in which case he's making you do something that's not best practice
.mosaic-container {
display: flex;
justify-content: space-between;
width: 330px;
padding: 10px;
background-color: blue;
border: 3px solid black;
}
.left-col, .mid-col, .right-col {
display: flex;
flex-direction: column;
flex: 0 0 100px;
}
.left-col .mosaic-tile:first-of-type, .right-col .mosaic-tile:first-of-type {
margin-bottom: 10px;
}
.mid-col .mosaic-tile {
height: 100%;
}
.mosaic-tile {
width: 100%;
height: 100px;
background-color: #fff;
}
<div class="mosaic-container">
<div class="left-col">
<div class="mosaic-tile">1.1</div>
<div class="mosaic-tile">2.1</div>
</div>
<div class="mid-col">
<div class="mosaic-tile">1.2</div>
</div>
<div class="right-col">
<div class="mosaic-tile">1.3</div>
<div class="mosaic-tile">2.2</div>
</div>
</div>
I believe you will be able to implement this in a simpler and more future proof way by using CSS Grids https://learncssgrid.com/ Float based layouts are generally discouraged, similar to table based layouts.
If you are set on float based, I would create a left column div, middle column div, and right column div all float: left. Then I would place the two images in the left, tall in the middle, and last two in the right column div.
I am developing a web app with meteor and material design lite.
Here is what I want to do :
Here is the result with a too small window :
And here is the result with a bigger window :
The HTML code :
<template name="myRefrigerator_header">
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<!-- Title -->
<span class="mdl-layout-title">My Refrigerator</span>
<!-- Add spacer, to align navigation to the right -->
<div class="mdl-layout-spacer"></div>
</div>
<!-- Simple Textfield -->
<div id="msg-layout">
<form action="#">
<div class="mdl-textfield mdl-js-textfield" id="msg-layout-content">
<input class="mdl-textfield__input" type="text" name="content">
<label class="mdl-textfield__label" for="sample1">Text...</label>
</div>
<button class="mdl-button mdl-js-button mdl-button--primary"
id="msg-layout-add-button">
Enregistrer
</button>
</form>
</div>
</header>
</template>
And the CSS applied to it :
#msg-layout {
background-color: #F5F5F5;
margin: 0px 25px 15px 25px;
padding-left: 10px;
padding-right: 10px;
display: flex;
flex-direction: row-reverse;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: stretch;
}
#msg-layout-content {
color: #3F51B5;
flex: 1 1 0;
}
#msg-layout-add-button {
}
I don't understand why I don't have the right behavior, I have specified that I only want one row and that my input should resize.
What am I getting wrong ?
First, you need to give the id="msg-layout" to the <form>. Only direct children of a display:flex layout will get the special attributes of flexible containers. You are negating flexbox by nesting a <form> where the flexible children would be. Your flexible layout only has one child, the <form>.
This is the best resource I have found for learning flex-box https://css-tricks.com/snippets/css/a-guide-to-flexbox/
I would suggest trimming things down. Flex-box isn't the problem, and more that libraries of style rules make a lot of assumptions - that you may not be able to see from the top.
Here is a stripped down version / http://codepen.io/sheriffderek/pen/MKzMgp
HTML
<form class='this-form' action='#'>
<label class='input-w' for='sample1'>
<input type='text' id='sample1'>
<span>Text...</span>
</label>
<button>Enregistrer</button>
</form>
SCSS
* { // reset box model
box-sizing: border-box;
}
.this-form {
display: flex; // a
flex-direction: row; // b
align-items: center; // d
max-width: 24rem;
padding: .5rem;
border: 1px solid blue;
.input-w {
position: relative;
flex-grow: 1; // c
input {
width: 100%;
border: 0;
border-bottom: 1px solid gray;
&:focus {
outline: none;
+ span {
transform: translate(0, -50px);
opacity: 0;
}
}
}
span {
position: absolute;
bottom: 3px;
left: 0;
color: gray;
font-size: 12px;
transition: 1s;
}
}
button {
height: 30px;
background: transparent;
border: 0;
color: blue;
margin-left: .5rem;
}
}
The mdl-textfield__input class has a set width of 300px due to animation limitations. You need to override this (say width: 100%) to get an expandable textfield. However, this may have adverse affects on the animations since CSS animations don't like unknown lengths.
Let's say I have a div, 100px wide, and a variable number (from 1 to 6) of elements, 10px wide, inside that div.
How can I equally space them so that:
if there is 1 element inside, there will be no additional spacing
if there are from 2 to 6 elements, spacing between each would be 80px (for 2), 35px (for 3), 20px (for 4), etc...
The first item will always be placed at the most left position, without padding, and the last item will always be placed at the most right position, also without padding.
I'm not concerned about IE, so this could be CSS3. Anyways, I am concerned about javascript. I know this would be a 1 liner in JS, but I certainly want to avoid it if possible, so please refrain answering if you're going to post a JS solution.
Regards
Edit:
Example: http://codepen.io/anon/pen/wbiFA
HTML
<div class="container">
<div class="item">
</div>
<div class="item">
</div>
<div class="item">
</div>
<div class="item">
</div>
</div>
CSS:
.container {
width: 900px;
border: 1px solid red;
display: flex;
justify-content: space-between;
height: 50px;
}
.item {
border: 1px solid blue;
flex-basis: auto;
width: 171px
}
Ok, did it :)
You don't need CSS3 features like flexible boxes. The following CSS2.1 features are enough:
text-align:justify
display: inline-block
::after pseudo-element
.container {
width: 900px;
border: 1px solid red;
height: 50px;
text-align: justify;
}
.container:after {
content: '';
width: 100%;
display: inline-block;
}
.item {
border: 1px solid blue;
width: 171px;
height: 100%;
display: inline-block;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
Using Flexbox i managed a close enough result:
http://codepen.io/coljung/pen/bufmh
.container {
border: 1px solid red;
width:1000px;
height:100px;
display: flex;
justify-content: space-around;
}
.item {
border: 1px solid blue;
background:red;
width:100px;
height:100%;
}
Now, it doesnt achieve the exact padding you are looking for. In that case you have to do it manually for every single case.