why the align content property isn't working here? - css

shouldn't the flex items of the .center-section-container be closer together when this property is use? they doesn't seem to be affected all.
i'm using flex wrap so the items are now in different lines but two far away to each other, I would like them to be closer on the cross axis.
here is what i have done:
body {
border: solid orange;
width: 100vw;
height: 100vh;
color: lightgrey;
}
.main-container {
border: solid black;
width: 100%;
max-width: 840px;
margin: 0;
display: flex;
}
.main-container>* {
border: solid pink;
}
.square {
min-height: 154.53px;
min-width: 171.53px;
border-radius: 10px;
background-color: #555;
}
/**here I have the problem**/
.center-section-container {
display: flex;
flex-wrap: wrap;
align-content: flex-start;
}
.center-section-container>* {
border: solid yellow;
}
.text1 {
color: black;
}
.subtext {
flex-basis: 60%;
}
.button-grey,
.button-white {
border-radius: 5px;
flex-basis: 40%;
height: 50px;
}
.button-grey {
background-color: grey;
color: white;
}
.button-white {
border: solid grey;
background-color: white;
color: inherit;
}
.aside {
width: 200px;
}
<article class=main-container>
<div class="square"></div>
<section class="center-section-container">
<h1 class="text1">Centro de Fisioterapia Guzmán Fernandez </h1>
<h4 class="subtext">Fisioterapia general </h2>
<button class="button-grey" type="button" name="button">Reservar
</button>
<button class="button-white" type="button" name="button">Pedir
</button>
</section>
<aside class="aside">
<h3 class="valoraciones"> 24 valoraciones </h3>
<div class="number-container">
<div class="number">8,9</div>
</div>
<a class="comentarios" href="#"> ver comentarios</a>
<a class="estrella" href="#"> <img src="images/star.svg" alt="votación estrella" width="20px" height="20px" title="simbolo estrella">
</a>
</aside>
</article>

I am not 100% sure if you mean this, but in order to have less vertical distance between the flex items, you need to remove the default margin of the h1 and h4 tags (BTW, you have a typo in the closing h2/h4 tag).
.center-section-container {
display: flex;
flex-wrap: wrap;
align-content: center;
}
.center-section-container>* {
border: solid yellow;
margin: 0;
}
I added margin: 0 to the child elements which moves them closer together. This alone would align the flex items at the top of their container, and I understood you want them in the vertical middle, so I also added / changed align-content: center; for the container. HTH
body {
border: solid orange;
width: 100vw;
height: 100vh;
color: lightgrey;
}
.main-container {
border: solid black;
width: 100%;
max-width: 840px;
margin: 0;
display: flex;
}
.main-container>* {
border: solid pink;
}
.square {
min-height: 154.53px;
min-width: 171.53px;
border-radius: 10px;
background-color: #555;
}
/**here I have the problem**/
.center-section-container {
display: flex;
flex-wrap: wrap;
align-content: center;
}
.center-section-container>* {
border: solid yellow;
margin: 0;
}
.text1 {
color: black;
}
.subtext {
flex-basis: 60%;
}
.button-grey,
.button-white {
border-radius: 5px;
flex-basis: 40%;
height: 50px;
}
.button-grey {
background-color: grey;
color: white;
}
.button-white {
border: solid grey;
background-color: white;
color: inherit;
}
.aside {
width: 200px;
}
<article class=main-container>
<div class="square"></div>
<section class="center-section-container">
<h1 class="text1">Centro de Fisioterapia Guzmán Fernandez </h1>
<h4 class="subtext">Fisioterapia general </h4>
<button class="button-grey" type="button" name="button">Reservar
</button>
<button class="button-white" type="button" name="button">Pedir
</button>
</section>
<aside class="aside">
<h3 class="valoraciones"> 24 valoraciones </h3>
<div class="number-container">
<div class="number">8,9</div>
</div>
<a class="comentarios" href="#"> ver comentarios</a>
<a class="estrella" href="#"> <img src="images/star.svg" alt="votación estrella" width="20px" height="20px" title="simbolo estrella">
</a>
</aside>
</article>

Related

How to align title and button on the same line?

I have a title and a button. The title should be left-aligned and the button should be right-aligned. But I have a problem that the button goes up.
How to align title and button on the same line?
.page-header {
margin: -16px -16px 16px;
padding: 16px;
color: $white;
background-color: $red;
}
<header class="page-header">
<h1>{{title }}</h1>
<span fxFlex></span>
<button mat-icon-button>
<mat-icon>navigate_before</mat-icon>
</button>
</header>
Add this CSS on page-header class:
.page-header {
...;
display: flex;
justify-content: space-between;
align-items: center;
}
Display Method
.page-header {
border: 1px solid black;
}
.display-child-element {
display: inline-block;
vertical-align: middle;
border: 1px solid red;
}
<header class="page-header">
<h1 class="display-child-element">TITLE</h1>
<button class="display-child-element">TEST</button>
</header>
Flexbox Method
.flex-parent-element {
display: flex;
border: 1px solid black;
width: 50%;
}
.flex-child-element {
flex: 1;
border: 1px solid red;
}
<header class="page-header flex-parent-element">
<h1 class="flex-child-element">TITLE</h1>
<button class="flex-child-element">TEST</button>
</header>
Grid Method
.grid-container-element {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 20px;
border: 1px solid black;
width: 50%;
}
.grid-child-element {
margin: 10px;
border: 1px solid red;
}
<header class="page-header grid-container-element">
<h1 class="grid-child-element">TITLE</h1>
<button class="grid-child-element">TEST</button>
</header>

Centering flexbox content after a linebreak

I am trying to center text inside a parent element of limited width. However the text is set in a large font, which might cause a line-break. However the element line-break does not decrease the width of the element. Is there a way to center a text inside a parent wrapper if the text does not fit?
You can find a failing example in the stack-overflow code sample. The top box has a line-break and should still be centered.
.wrapper {
width: 900px;
margin: 0 auto;
background: lightgrey;
}
.box {
display: flex;
align-items: center;
justify-content: center;
width: 300px;
height: 200px;
border: 1px solid green;
}
.box:nth-child(1) {
background: green;
font-size: 45px;
}
.box:nth-child(2) {
background: orange;
}
<div class="wrapper">
<div class="box"><h3>Lorem Ipsum</h3></div>
<div class="box"><h3>Lorem Ipsum</h3></div>
</div>
Just add the text-align: center;
.wrapper {
width: 900px;
margin: 0 auto;
background: lightgrey;
}
.box {
display: flex;
align-items: center;
justify-content: center;
/* text-align: center; */
width: 300px;
height: 200px;
border: 1px solid green;
}
.box > * {
flex: 0 0 50%;
}
.box:nth-child(1) {
background: green;
font-size: 45px;
}
.box:nth-child(2) {
background: orange;
}
<div class="wrapper">
<div class="box"><h3>Lorem Ipsum</h3></div>
<div class="box"><h3>Lorem Ipsum</h3></div>
</div>
You can use width:min-content; with the first child (https://caniuse.com/#feat=intrinsic-width)
.wrapper {
width: 900px;
margin: 0 auto;
background: lightgrey;
}
.box {
display: flex;
align-items: center;
justify-content: center;
width: 300px;
height: 200px;
border: 1px solid green;
}
.box:nth-child(1) h3 {
width:-webkit-min-content;
width:-moz-min-content;
width:min-content;
border:1px solid;
}
.box:nth-child(1) {
background: green;
font-size: 45px;
}
.box:nth-child(2) {
background: orange;
}
<div class="wrapper">
<div class="box">
<h3>Loreme Ipsum</h3>
</div>
<div class="box">
<h3>Lorem Ipsum</h3>
</div>
</div>

Trouble Aligning Buttons Within a div

I'm making a simple calculator page but I'm having a lot of trouble aligning the buttons the way I would like. I have all of my buttons inside of a div which is already the width I want each row of buttons to be. My problem is that no matter how I try to do the margins or the buttons either have extra room on the right side and they don't line up with the right side of the viewport or they are too big and they overflow onto the next line. I'm sure there is a simple solution to this but I'm too much of a beginner to see it. :(
Currently my CSS for the button container and buttons is as follows:
#buttons {
width:525px;
margin-left: 25px;
margin-top: 10px;
}
button {
width: 110px;
height: 110px;
border: 2px solid black;
border-radius: 20px;
margin: 10px 0px 0px 0px;
}
Codepen here.
Try width: 33% on the buttons. It tells the browser to use (roughly) 1/3rd if the available space for each button.
Flex box is worth learning.
Changes are noted with /**/ in the CSS.
Full example, https://codepen.io/anon/pen/LOZEPp
CSS
body {
background-color: #141414;
font-family: 'Rubik', sans-serif;
display: flex; /**/
justify-content: center; /**/
padding: 0; /**/
margin: 0; /**/
}
#calc-main {
background-color: #607466;
width: 550px; /**/
height: 700px; /**/
border: 3px solid black;
border-radius: 20px;
display: flex; /**/
flex-direction: column; /**/
justify-content: space-around; /**/
}
#calc-brand {
flex: 1; /**/
background: yellow;
text-align: center;
padding: 0; /**/
margin: 10px; /**/
}
#calc-viewport {
flex: 1; /**/
margin: 10px; /**/
font-size: 2em;
background-color: #D4D2A5;
padding: 0.25em; /**/
text-align: right;
border: 2px solid black;
border-radius: 20px;
}
#buttons {
margin: 10px; /**/
flex: 16; /**/
background: orange; /**/
display: flex; /**/
flex-direction: column; /**/
align-items: center; /**/
padding: 5px; /**/
}
.button-row { /**/
flex: 1; /* important */
background: yellow;
width: 100%; /**/
margin: 5px; /**/
display: flex; /**/
flex-direction: row; /**/
justify-content: space-between; /**/
}
button {
flex: 1; /* important */
max-width: 110px; /**/
max-height: 110px; /**/
border: 2px solid black;
border-radius: 20px;
margin: 10px; /**/
}
HTML
<body>
<div id='calc-main'>
<h1 id='calc-brand'>JAVASCRIPT CALCULATOR</h1>
<div id='calc-viewport'>
<p id='main-output'>0</p>
<p id='secondary-output'>0</p>
</div>
<div id=buttons>
<div class="button-row">
<button type='button' class='button-danger'>AC</button>
<button type='button' class='button-danger'>CE</button>
<button type='button' class='button-danger'>CE</button>
<button type='button'>÷</button>
</div>
<div class="button-row">
<button type='button' class='button-danger'>AC</button>
<button type='button' class='button-danger'>CE</button>
<button type='button' class='button-danger'>CE</button>
<button type='button'>÷</button>
</div>
<div class="button-row">
<button type='button' class='button-danger'>AC</button>
<button type='button' class='button-danger'>CE</button>
<button type='button' class='button-danger'>CE</button>
<button type='button'>÷</button>
</div>
<div class="button-row">
<button type='button' class='button-danger'>AC</button>
<button type='button' class='button-danger'>CE</button>
<button type='button' class='button-danger'>CE</button>
<button type='button'>÷</button>
</div>
</div>
</div>
</body>
Try This:
.wrapper {
border: 1px solid #000;
text-align: center;
display: inline-block;
background-color: #607466;
}
.result {
margin:10px 20px;
}
input {
height: 20px;
border: 1px solid;
outline: none;
width: 100%;
background: yellow;
}
span {
background: #AAA;
width: 60px;
height: 60px;
display: inline-block;
margin: 0 20px 10px 10px;
line-height: 60px;
float: left;
cursor: pointer;
background: #FFF;
border: 1px solid;
}
span:hover {
background-color: #AAA;
}
.result ~ p {
padding-left: 10px;
clear: both;
}
<div class="wrapper">
<p class="result"><input type="text" name="result"></p>
<p><span>AC</span><span>CE</span><span>/</span><span>*</span></p>
<p><span>7</span><span>8</span><span>9</span><span>-</span></p>
<p><span>4</span><span>5</span><span>6</span><span>+</span></p>
<p><span>1</span><span>2</span><span>3</span><span>=</span></p>
<p><span>0</span><span>.</span><span>Invisible</span></p>
<br style="clear: both">
</div>

How shrink-wrap a div with flexbox? [duplicate]

This question already has answers here:
How to disable equal height columns in Flexbox?
(4 answers)
Closed 5 years ago.
What's the best way to shrink-wrap a div using flex-box?
In the snippet below, I have a wrapper (the green border) shrink-wrapping the content (red & blue boxes) on all sides but the bottom.
How can I get this accomplished?
Here's a plunker demo: https://plnkr.co/edit/u89JPIbZObTYIfRejlO1?p=preview
.red {
width: 100px;
height: 50px;
background-color: red;
}
.blue {
width: 100px;
height: 50px;
background-color: blue;
}
.container2 {
width: 400px;
height: 300px;
border: solid;
display: flex;
justify-content: center;
}
.wrapper2 {
border: solid green;
padding: 5px;
}
<div class="container2">
<div class="wrapper2">
<div class="red">x</div>
<div class="blue">x</div>
</div>
</div>
you can use :
align-items
.container2 {
width: 400px;
height: 300px;
border: solid;
display: flex;
justify-content: center;
align-items:flex-start;/* update here */
}
.red {
width: 100px;
height: 50px;
background-color: red;
}
.blue {
width: 100px;
height: 50px;
background-color: blue;
}
.container2 {
width: 400px;
height: 300px;
border: solid;
display: flex;
justify-content: center;
align-items:flex-start;
}
.wrapper2 {
border: solid green;
padding: 5px;
/*margin:0 auto auto*/
}
<div class="container2">
<div class="wrapper2">
<div class="red">x</div>
<div class="blue">x</div>
</div>
</div>
or margin
.wrapper2 {
border: solid green;
padding: 5px;
margin:0 auto auto/* update here */
}
.red {
width: 100px;
height: 50px;
background-color: red;
}
.blue {
width: 100px;
height: 50px;
background-color: blue;
}
.container2 {
width: 400px;
height: 300px;
border: solid;
display: flex;
justify-content: center;
/* align-items:flex-start;*/
}
.wrapper2 {
border: solid green;
padding: 5px;
margin:0 auto auto
}
<div class="container2">
<div class="wrapper2">
<div class="red">x</div>
<div class="blue">x</div>
</div>
</div>
a reminder/titorial: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Use the align-items: flex-start; property on .container2
.red {
width: 100px;
height: 50px;
background-color: red;
}
.blue {
width: 100px;
height: 50px;
background-color: blue;
}
.container2 {
width: 400px;
height: 300px;
border: solid;
display: flex;
justify-content: center;
align-items: flex-start;
}
.wrapper2 {
border: solid green;
padding: 5px;
}
<div class="container2">
<div class="wrapper2">
<div class="red">x</div>
<div class="blue">x</div>
</div>
</div>

Can this be simplified using CSS flexbox?

I have this setup of photos and thumbnails made with regular CSS, and I was wondering: can it be simplified using flexbox?
http://jsfiddle.net/frank_o/n9pM2/10/
HTML:
<div class="one">
<h1>Can this:</h1>
<div class="wrapper">
<div class="photos">
<img src="http://placekitten.com/200/400" />
</div>
<div class="thumbnails">
<img src="http://placekitten.com/70/70" />
<img src="http://placekitten.com/70/70" />
</div>
</div>
</div>
CSS:
.one .photos {
border: 2px solid lightgrey;
float: left;
width: calc(100% - 80px);
}
.one .photos img {
width: 100%;
}
.one .thumbnails {
width: 70px;
float: right;
border: 2px solid red;
}
.one .thumbnails img {
display: block;
margin-bottom: 5px;
}
https://codepen.io/polinq/pen/gObLoZx
Please have a look here. Is that what you were looking for?
Happy to provide clarification if something is not clear?
.one .wrapper {
display: flex;
}
.one .thumbnails {
display: flex;
flex-direction: column;
border: 2px solid red;
align-self: flex-start;
margin-left: 5px;
}
.two .wrapper {
display: flex;
flex-direction: column;
}
.two .thumbnails {
display: flex;
flex-direction: row;
border: 2px solid red;
margin-left: 5px;
}
You can simplify it a lot with grid:
.grid {
display: grid;
grid-template-columns: 9fr 1fr;
grid-gap: 14px
}
img {
width: 100%;
border: solid 5px lightgray;
}
.thumbs img{
border: solid 2px #f00
}
<h1>Can this:</h1>
<div class="grid">
<img src="http://placekitten.com/200/400" />
<div class="thumbs">
<img src="http://placekitten.com/70/70" />
<img src="http://placekitten.com/70/70" />
</div>
</div>

Resources