My #main element ignores it's wrapper padding. I set position:absolute on children, but when I try to change it from position:static,to position:relative it just ignores parent's height. Any workarounds?
body, html {
height: 100%;
margin: 0;
padding: 0;
}
#wrapper-body {
height: 100%;
display: flex;
flex-direction: column;
}
#wrapper-header {
width: 100%;
flex: 0 1 50px;
background: url("header.png");
display: flex;
align-items: center;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.8);
z-index: 5;
}
#wrapper-main {
flex: 1 1 auto;
display: flex;
align-items: center;
justify-content: center;
position: relative;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
padding: 25px;
}
#wrapper-footer {
width: 100%;
flex: 0 1 auto;
background-color: #212121;
}
#menu {
display: flex;
flex-direction: row;
list-style-type: none;
right: 0;
position: absolute;
}
.menu-button {
background-color: #3B3B3B;
width: 100px;
height: 22px;
margin-right: 15px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
color: #F7F7F7;
border-radius: 2px;
font-family: "codropsicons", verdana;
font-weight: 700;
text-transform: uppercase;
font-size: 14px;
transition: 0.5s;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
text-shadow: 2px 1px 2px rgba(0, 0, 0, 0.3);
}
.active-button, .menu-button:hover {
background-color: #E0962D;
}
#main {
background-color: green;
height: 100%;
width: 100%;
position: absolute;
}
#copyright {
height: 20px;
width: auto;
display: flex;
align-items: center;
font-family: "codropsicons", verdana;
font-weight: 700;
text-transform: uppercase;
font-size: 10px;
color: #F7F7F7;
margin-left: 15px;
opacity: 0.1;
}
<div id="wrapper-body">
<div id="wrapper-header">
<nav id="menu">
<a class="menu-button active-button">O nas</a>
<a class="menu-button">Oferta</a>
<a class="menu-button">Galeria</a>
<a class="menu-button">Kontakt</a>
</nav>
</div>
<div id="wrapper-main">
<main id="main">
Test
<br> Test
<br>
</main>
</div>
<div id="wrapper-footer">
<div id="copyright">Koyot © 2017 All rights reserved</div>
</div>
</div>
https://jsfiddle.net/Ldmmxw9m/3/
It doesn't ignore the parents height when using position: relative, it simply keeps the padding of the parent, but apart from that it fills the parent - see my snippet. Of course the parent's height has to be set when you use a percentage value for the child's height...
#wrapper-main{
flex:1 1 auto;
display:flex;
align-items:center;
justify-content:center;
position:relative;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
padding:25px;
background-color:yellow;
height: 200px;
}
#main{
background-color:green;
height:100%;
width:100%;
position:relative;
}
<div id="wrapper-main">
<main id="main">
some content
</main>
</div>
As Santi said, you can remove position: absolute on #main, since that will place the element relative to it's nearest positioned ancestor, ignoring the ancestor's padding.
Or if that's not an option, you could use top/left/right/bottom values that match the padding amount, and remove the padding on the parent if that's no longer needed.
/* TAGS */
body,
html {
height: 100%;
margin: 0;
padding: 0;
}
/* END OF TAGS */
/* WRAPPERS */
#wrapper-body {
height: 100%;
display: flex;
flex-direction: column;
}
#wrapper-header {
width: 100%;
flex: 0 1 50px;
background: url("header.png");
display: flex;
align-items: center;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.8);
z-index: 5;
}
#wrapper-main {
flex: 1 1 auto;
display: flex;
align-items: center;
justify-content: center;
position: relative;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
}
#wrapper-footer {
width: 100%;
flex: 0 1 auto;
background-color: #212121;
}
/* END OF WRAPPERS */
/* CONTENT */
#menu {
display: flex;
flex-direction: row;
list-style-type: none;
right: 0;
position: absolute;
}
.menu-button {
background-color: #3B3B3B;
width: 100px;
height: 22px;
margin-right: 15px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
color: #F7F7F7;
border-radius: 2px;
font-family: "codropsicons", verdana;
font-weight: 700;
text-transform: uppercase;
font-size: 14px;
transition: 0.5s;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
text-shadow: 2px 1px 2px rgba(0, 0, 0, 0.3);
}
.active-button,
.menu-button:hover {
background-color: #E0962D;
}
#main {
background-color: green;
top: 25px;
left: 25px;
right: 25px;
bottom: 25px;
position: absolute;
}
#copyright {
height: 20px;
width: auto;
display: flex;
align-items: center;
font-family: "codropsicons", verdana;
font-weight: 700;
text-transform: uppercase;
font-size: 10px;
color: #F7F7F7;
margin-left: 15px;
opacity: 0.1;
}
<body>
<div id="wrapper-body">
<div id="wrapper-header">
<nav id="menu">
<a class="menu-button active-button">O nas</a>
<a class="menu-button">Oferta</a>
<a class="menu-button">Galeria</a>
<a class="menu-button">Kontakt</a>
</nav>
</div>
<div id="wrapper-main">
<main id="main">
Test
<br> Test
<br>
</main>
</div>
<div id="wrapper-footer">
<div id="copyright">Koyot © 2017 All rights reserved</div>
</div>
</div>
</body>
1. Remove position: absolute; from #main.
Absolutely positioned items are "out of the flow". Setting the parent to relative will modify the absolute child element's bounding box to it's own height and width, but padding is not taken into account.
2. Change the wrapper's align-items: center; to align-items: stretch;
It seems to me that you don't want the child to be vertically-aligned in the middle, but rather to take up the entire height of the wrapper. align-items: stretch will apply this behavior.
Updated Fiddle
/* TAGS */
body,
html {
height: 100%;
margin: 0;
padding: 0;
}
/* END OF TAGS */
/* WRAPPERS */
#wrapper-body {
height: 100%;
display: flex;
flex-direction: column;
}
#wrapper-header {
width: 100%;
flex: 0 1 50px;
background: url("header.png");
display: flex;
align-items: center;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.8);
z-index: 5;
}
#wrapper-main {
flex: 1 1 auto;
display: flex;
align-items: stretch;
justify-content: center;
position: relative;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
padding: 25px;
}
#wrapper-footer {
width: 100%;
flex: 0 1 auto;
background-color: #212121;
}
/* END OF WRAPPERS */
/* CONTENT */
#menu {
display: flex;
flex-direction: row;
list-style-type: none;
right: 0;
position: absolute;
}
.menu-button {
background-color: #3B3B3B;
width: 100px;
height: 22px;
margin-right: 15px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
color: #F7F7F7;
border-radius: 2px;
font-family: "codropsicons", verdana;
font-weight: 700;
text-transform: uppercase;
font-size: 14px;
transition: 0.5s;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
text-shadow: 2px 1px 2px rgba(0, 0, 0, 0.3);
}
.active-button,
.menu-button:hover {
background-color: #E0962D;
}
#main {
background-color: green;
width: 100%;
}
#copyright {
height: 20px;
width: auto;
display: flex;
align-items: center;
font-family: "codropsicons", verdana;
font-weight: 700;
text-transform: uppercase;
font-size: 10px;
color: #F7F7F7;
margin-left: 15px;
opacity: 0.1;
}
<body>
<div id="wrapper-body">
<div id="wrapper-header">
<nav id="menu">
<a class="menu-button active-button">O nas</a>
<a class="menu-button">Oferta</a>
<a class="menu-button">Galeria</a>
<a class="menu-button">Kontakt</a>
</nav>
</div>
<div id="wrapper-main">
<main id="main">
Test<br> Test
<br>
</main>
</div>
<div id="wrapper-footer">
<div id="copyright">Koyot © 2017 All rights reserved</div>
</div>
</div>
</body>
Try adding this to the parent element:
box-sizing:border-box;
This changes how the width and height is calculated by the browser so it includes padding and borders. With flex, using border-box on the parent has corrected spacing issues.
Related
This question already has answers here:
How to make an element width: 100% minus padding?
(15 answers)
Closed 12 months ago.
Can anyone tell me how to solve this problem, As you can see my .tab
class is set to 100% width and it's overlapping on the
.main-container class? I also use overflow on my main container
however it began having a scroll can any tell me what is the correct
way to solve this problem? Thank you
.main-container {
border: 1px solid #D8D8D8;
border-radius: 2px;
width: 99%;
margin: 0 auto;
min-height: 10px;
/* overflow: auto; */
margin-top: 3px;
background: blue;
padding: 10px;
}
.tabs {
position: relative;
padding: 10px;
display: inline-block;
background-color: #f2f7fd;
border-radius: 6px;
width: 100%;
border: 1px solid transparent;
color: #333333;
}
.tabs-title {
margin-bottom: 6px;
font-size: 18px;
font-weight: 600;
color: #000000;
line-height: 22px;
padding: 0 2px;
}
.tabs-subtitle {
margin-bottom: 15px;
position: relative;
padding: 0 4px;
line-height: 18px;
}
.tabs-subtitle+hr {
height: 1px;
border: none;
background-color: #7a8a9a;
display: block;
margin-bottom: 15px;
}
.tabs-content {
position: relative;
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
.tabs-rdb {
display: none;
order: 1;
}
.tabs-lbl {
position: relative;
top: 0;
padding: 8px 14px;
padding-bottom: 10px;
cursor: pointer;
background-color: transparent;
display: inline-block;
border-radius: 6px 6px 0 0;
font-size: 13px;
order: 2;
font-weight: normal;
user-select: none;
color: #000000;
}
.tabs.tabs-lbl-bold .tabs-lbl {
font-weight: bold;
}
.tabs-rdb+.tabs-lbl:before {
content: '';
position: absolute;
width: 0;
height: 1px;
border: 6px;
background-color: #2575c4;
bottom: 2px;
left: 50%;
transform: translateX(-50%);
transition: .32s ease width;
opacity: 0;
}
.tabs.tabs-lbl-bold .tabs-rdb+.tabs-lbl:before {
height: 2px;
}
.tabs-lbl:hover {
color: #2575c4;
}
.tabs-lbl:first-of-type {
margin-left: 16px;
}
.tabs-lbl:last-of-type {
margin-right: 16px;
}
.tabs-rdb:checked+.tabs-lbl {
background-color: #ffffff;
color: #2575c4;
box-shadow: 0 1px 3px rgba(0, 0, 0, .14);
}
.tabs-rdb:checked+.tabs-lbl+.tabs-text {
display: block;
}
.tabs-rdb:checked+.tabs-lbl:before {
width: 32%;
opacity: 1;
}
.tabs-text {
display: none;
top: 0;
order: 999;
padding: 10px;
background-color: #ffffff;
border-radius: 6px;
line-height: 18px;
box-shadow: 0 0px 8px -4px rgba(0, 0, 0, .24);
width: 100%;
z-index: 1;
min-height: 38px;
}
<div class="main-container">
<div class="tabs tabs-lbl-bold">
<div class="tabs-title">Main Title (Optional)</div>
<div class="tabs-subtitle">Subtitle Lorem ipsum dolor at met (Optional).</div>
<hr>
<div class="tabs-content">
<input id="nkTabs_a_1" type="radio" class="tabs-rdb" checked name="tabs-a">
<label for="nkTabs_a_1" class="tabs-lbl">Home</label>
<div class="tabs-text">This area is for Home tab.</div>
<!-- -->
<input id="nkTabs_a_2" type="radio" class="tabs-rdb" name="tabs-a">
<label for="nkTabs_a_2" class="tabs-lbl">About</label>
<div class="tabs-text">
This area is for About tab. This area is for About tab. This area is for About tab. This area is for About tab. This area is for About tab.
</div>
<!-- -->
<input id="nkTabs_a_3" type="radio" class="tabs-rdb" name="tabs-a">
<label for="nkTabs_a_3" class="tabs-lbl">Help</label>
<div class="tabs-text">This area is for Help tab. This area is for Help tab.</div>
</div>
</div>
</div>
This overflow is caused by the box-sizing. Just add .tabs { box-sizing: border-box; } and the issue should be fixed.
The default value of box-sizing: content-box does not include the paddings and border. As such 100% width of the parent + paddings and border will always overflow! As such you have an overflow by 22px without border-box
PS: To stop the horizontal overflow of the viewport you should use width: auto instead of width: 99% on the wrapping parent element.
.tabs {
box-sizing: border-box;
}
/* original CSS */
.main-container {
border: 1px solid #D8D8D8;
border-radius: 2px;
width: 99%;
margin: 0 auto;
min-height: 10px;
/* overflow: auto; */
margin-top: 3px;
background: blue;
padding: 10px;
}
.tabs {
position: relative;
padding: 10px;
display: inline-block;
background-color: #f2f7fd;
border-radius: 6px;
width: 100%;
border: 1px solid transparent;
color: #333333;
}
.tabs-title {
margin-bottom: 6px;
font-size: 18px;
font-weight: 600;
color: #000000;
line-height: 22px;
padding: 0 2px;
}
.tabs-subtitle {
margin-bottom: 15px;
position: relative;
padding: 0 4px;
line-height: 18px;
}
.tabs-subtitle+hr {
height: 1px;
border: none;
background-color: #7a8a9a;
display: block;
margin-bottom: 15px;
}
.tabs-content {
position: relative;
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
.tabs-rdb {
display: none;
order: 1;
}
.tabs-lbl {
position: relative;
top: 0;
padding: 8px 14px;
padding-bottom: 10px;
cursor: pointer;
background-color: transparent;
display: inline-block;
border-radius: 6px 6px 0 0;
font-size: 13px;
order: 2;
font-weight: normal;
user-select: none;
color: #000000;
}
.tabs.tabs-lbl-bold .tabs-lbl {
font-weight: bold;
}
.tabs-rdb+.tabs-lbl:before {
content: '';
position: absolute;
width: 0;
height: 1px;
border: 6px;
background-color: #2575c4;
bottom: 2px;
left: 50%;
transform: translateX(-50%);
transition: .32s ease width;
opacity: 0;
}
.tabs.tabs-lbl-bold .tabs-rdb+.tabs-lbl:before {
height: 2px;
}
.tabs-lbl:hover {
color: #2575c4;
}
.tabs-lbl:first-of-type {
margin-left: 16px;
}
.tabs-lbl:last-of-type {
margin-right: 16px;
}
.tabs-rdb:checked+.tabs-lbl {
background-color: #ffffff;
color: #2575c4;
box-shadow: 0 1px 3px rgba(0, 0, 0, .14);
}
.tabs-rdb:checked+.tabs-lbl+.tabs-text {
display: block;
}
.tabs-rdb:checked+.tabs-lbl:before {
width: 32%;
opacity: 1;
}
.tabs-text {
display: none;
top: 0;
order: 999;
padding: 10px;
background-color: #ffffff;
border-radius: 6px;
line-height: 18px;
box-shadow: 0 0px 8px -4px rgba(0, 0, 0, .24);
width: 100%;
z-index: 1;
min-height: 38px;
}
<div class="main-container">
<div class="tabs tabs-lbl-bold">
<div class="tabs-title">Main Title (Optional)</div>
<div class="tabs-subtitle">Subtitle Lorem ipsum dolor at met (Optional).</div>
<hr>
<div class="tabs-content">
<input id="nkTabs_a_1" type="radio" class="tabs-rdb" checked name="tabs-a">
<label for="nkTabs_a_1" class="tabs-lbl">Home</label>
<div class="tabs-text">This area is for Home tab.</div>
<!-- -->
<input id="nkTabs_a_2" type="radio" class="tabs-rdb" name="tabs-a">
<label for="nkTabs_a_2" class="tabs-lbl">About</label>
<div class="tabs-text">
This area is for About tab. This area is for About tab. This area is for About tab. This area is for About tab. This area is for About tab.
</div>
<!-- -->
<input id="nkTabs_a_3" type="radio" class="tabs-rdb" name="tabs-a">
<label for="nkTabs_a_3" class="tabs-lbl">Help</label>
<div class="tabs-text">This area is for Help tab. This area is for Help tab.</div>
</div>
</div>
</div>
Trying to make a legend like block and struggling with alignment, need some advice-correction:
.footertext {
display: flex; /* establish flex container */
flex-direction: row; /* default value; can be omitted */
flex-wrap: nowrap; /* default value; can be omitted */
justify-content: space-between;
width: 260px;
margin-top: 30px;
background: yellow;
}
.circlemarker {
height: 15px;
width: 15px;
border-radius: 50%;
margin-right: 78px;
box-shadow: 6px 6px 10px -1px rgba(0, 0, 0, 0.10),
-6px -6px 10px -1px rgba(255, 255, 255, 0.7);
}
.circlemarker p {
padding-left: 20px;
font-style: normal;
font-weight: normal;
font-size: 12px;
line-height: 14px;
}
#accactive {
background: #1698D1;
}
#accdisabled {
background: #979797;
}
#accprivileged {
background: #FF9563;
}
<div class="footertext">
<div>
<div class="circlemarker" id="accactive" ><p>Active</p></div>
</div>
<div>
<div class="circlemarker" id="accdisabled"><p>Disabled</p></div>
</div>
<div>
<div class="circlemarker" id="accprivileged"><p>Privileged</p></div>
</div>
</div>
so its looks almost ok, because I add
margin-right: 78px;
but such kind of "hardcoded" value and if size will change it will not fit well, so how I can change css so its looks same but be independent from margin? Also it seems not centred vertically.
p.S.
Yellow background has been added just to show boundaries of parent
.footertext {
width: 260px;
margin-top: 30px;
background: yellow;
padding: 1rem;
}
.wrapper{
display: flex; /* establish flex container */
flex-direction: row; /* default value; can be omitted */
justify-content: space-between;
}
.circlemarker-wrapper{
display: flex;
align-items: center;
justify-content: space-evenly;
}
.circlemarker {
height: 15px;
width: 15px;
border-radius: 50%;
margin-right: 0.5rem;
box-shadow: 6px 6px 10px -1px rgba(0, 0, 0, 0.10),
-6px -6px 10px -1px rgba(255, 255, 255, 0.7);
}
.circlemarker p {
padding-left: 20px;
font-style: normal;
font-weight: normal;
font-size: 12px;
line-height: 14px;
}
#accactive {
background: #1698D1;
}
#accdisabled {
background: #979797;
}
#accprivileged {
background: #FF9563;
}
<div class="footertext">
<div class="wrapper">
<div class="circlemarker-wrapper">
<div class="circlemarker" id="accactive" ></div>
Active
</div>
<div class="circlemarker-wrapper">
<div class="circlemarker" id="accdisabled"></div>
Disabled
</div>
<div class="circlemarker-wrapper">
<div class="circlemarker" id="accprivileged"></div>
Privileged
</div>
</div>
</div>
Try this.
The problem is "p" is outer the box model of "div class="circlemarker".
Hope this my idea have solved your problem by delete tag "p" and the circle move to div:before
.footertext {
display: flex; /* establish flex container */
flex-direction: row; /* default value; can be omitted */
flex-wrap: nowrap; /* default value; can be omitted */
justify-content: space-between;
width: 260px;
margin-top: 30px;
background: yellow;
padding: 1rem;
}
.circlemarker:before {
content: "";
position: absolute;
left: 0;
height: 15px;
width: 15px;
border-radius: 50%;
box-shadow: 6px 6px 10px -1px rgba(0, 0, 0, 0.10),
-6px -6px 10px -1px rgba(255, 255, 255, 0.7);
}
.circlemarker {
position:relative;
padding-left: 20px;
font-style: normal;
font-weight: normal;
font-size: 12px;
line-height: 14px;
}
#accactive:before {
background: #1698D1;
}
#accdisabled:before {
background: #979797;
}
#accprivileged:before {
background: #FF9563;
}
<div class="footertext">
<div>
<div class="circlemarker" id="accactive" >Active</div>
</div>
<div>
<div class="circlemarker" id="accdisabled">Disabled</div>
</div>
<div>
<div class="circlemarker" id="accprivileged">Privileged</div>
</div>
</div>
Move the text outside the circlemarker to get the width of the flex items correct.
Make the footertext flex items be flexboxes themselves to get vertical centering.
.footertext {
display: flex; /* establish flex container */
flex-direction: row; /* default value; can be omitted */
flex-wrap: nowrap; /* default value; can be omitted */
justify-content: space-between;
width: 260px;
margin-top: 30px;
background: yellow;
}
.footertext > div {
display: flex;
align-items: center;
}
.circlemarker {
height: 15px;
width: 15px;
display: inline-block;
border-radius: 50%;
box-shadow: 6px 6px 10px -1px rgba(0, 0, 0, 0.10),
-6px -6px 10px -1px rgba(255, 255, 255, 0.7);
}
span {
font-size: 12px;
line-height: 14px;
}
#accactive {
background: #1698D1;
}
#accdisabled {
background: #979797;
}
#accprivileged {
background: #FF9563;
}
<div class="footertext">
<div>
<div class="circlemarker" id="accactive"></div><span>Active</span>
</div>
<div>
<div class="circlemarker" id="accdisabled"></div><span>Disabled</span>
</div>
<div>
<div class="circlemarker" id="accprivileged"></div><span>Privileged</span>
</div>
</div>
I have tried using flexbox to center a couple buttons and a p but I'm having some troubles.
I have this code
.container {
background: rgb(255, 126, 50, 0.15);
padding: 0 2rem;
border-radius: 0.375rem;
margin: 3.625rem 1.25rem 2rem 1.25rem;
background: black;
}
.layout {
height: calc(100vh - 4.5rem);
background: balck;
}
.loginSection {
width: 50vh;
margin: 0 auto;
border: 1px solid #ffffff;
}
.loginSection p {
display: flex;
align-items: center;
margin: 16px 0;
text-align: center;
width: 12rem;
border: 1px solid #ffffff;
}
.btn-login {
height: 1.25rem;
display: flex;
color: rgba(255, 255, 255, 0.54);
text-decoration: none;
align-items: center;
width: 12rem;
margin: 0 auto;
border: 1px solid #ffffff;
padding: 1rem 1.25rem;
line-height: 1rem;
border-radius: 0.375rem;
font-size: 1rem;
}
.btn-login img {
margin-right: 1rem;
}
.btn-continue {
height: 1.25rem;
display: flex;
color: rgba(255, 255, 255, 0.54);
text-decoration: none;
align-items: center;
width: 12rem;
margin: 0 auto;
border: 1px solid #ffffff;
padding: 1rem 1.25rem;
line-height: 1rem;
border-radius: 0.375rem;
font-size: 1rem;
}
<div class="layout">
<div class="container">
<div class="loginSection">
<button class="btn-login" href="/login">
Log-In
</button>
<p>Enter your email</p>
<button class="btn-continue" href="/home">Continue</button>
</div>
</div>
</div>
But I can center the p
| | Log-In | |
|Enter your email |
| | Continue | |
.container {
background: rgb(255, 126, 50, 0.15);
padding: 0 2rem;
border-radius: 0.375rem;
margin: 3.625rem 1.25rem 2rem 1.25rem;
background: black;
}
.layout {
height: calc(100vh - 4.5rem);
background: balck;
}
.loginSection {
width: 50vh;
margin: 0 auto;
border: 1px solid #ffffff;
}
.layout p {
text-align: center;
width: 12rem;
border: 1px solid #ffffff;
color: white;
margin: auto;
}
.btn-login {
height: 1.25rem;
display: flex;
color: rgba(255, 255, 255, 0.54);
text-decoration: none;
align-items: center;
width: 12rem;
margin: 0 auto;
border: 1px solid #ffffff;
padding: 1rem 1.25rem;
line-height: 1rem;
border-radius: 0.375rem;
font-size: 1rem;
}
.btn-login img {
margin-right: 1rem;
}
.btn-continue {
height: 1.25rem;
display: flex;
color: rgba(255, 255, 255, 0.54);
text-decoration: none;
align-items: center;
width: 12rem;
margin: 0 auto;
border: 1px solid #ffffff;
padding: 1rem 1.25rem;
line-height: 1rem;
border-radius: 0.375rem;
font-size: 1rem;
}
<div class="layout">
<div class="container">
<div class="loginSection">
<button class="btn-login" href="/login">
Log-In
</button>
<p>Enter your email</p>
<button class="btn-continue" href="/home">Continue</button>
</div>
</div>
</div>
Flexbox is typically used by setting flex properties on the parent container to affect the layout of the children inside of that container.
It looks like you're adding display: flex and align-items: center on the .loginSection p element, when flex properties should be set on the parent .loginSection.
If your intention is to center all the elements inside of the .loginSection horizontally, then you can get rid of the flex properties on the children, and add the following rules:
.loginSection {
display: flex;
flex-direction: column;
justify-content: center;
// ... other styles here
}
You'll want to add display:flex with flex-direction: column and align-items:center to the .loginSection container.
Typically when using flex, you flex the container and then the children become "flex-items" rather than flexing the items themselves.
.loginSection {
margin: 0 auto;
display: flex;
flex-direction: column;
align-items:center;
}
.loginSection p { text-align:center; }
<div class="layout">
<div class="container">
<div class="loginSection">
<button class="btn-login" href="/login">
Log-In
</button>
<p>Enter your email</p>
<button class="btn-continue" href="/home">Continue</button>
</div>
</div>
</div>
Or if you want them to all be stretched out use align-items:stretch
.loginSection {
margin: 0 auto;
padding: 1rem; /* optional if you don't want it to stretch the whole width */
display: flex;
flex-direction: column;
align-items:stretch;
}
.loginSection p { text-align:center; }
<div class="layout">
<div class="container">
<div class="loginSection">
<button class="btn-login" href="/login">
Log-In
</button>
<p>Enter your email</p>
<button class="btn-continue" href="/home">Continue</button>
</div>
</div>
</div>
Having trouble centering text vertically in a fieldset. This particularly when a sibling is hidden.
This is what the code should looks like when the sibling is showing:
#title {
margin: 20px;
}
#definition {
margin: 0 auto;
margin-top: 5%;
text-align: center;
max-width: 60%;
font-size: 1.5vmax;
}
hr {
color: white;
background-color: white;
width: 80%;
height: 1px;
}
#formulaLine {
color: white;
background-color: white;
height: 1px;
}
section#formula {
width: auto;
max-width: 70%;
background: #393e46;
box-shadow: inset 2px 5px 10px rgb(24, 23, 23);
border-radius: 5px;
margin: 5% auto;
padding: 10px;
font-size: 2vmax 1vmin;
}
.center {
text-align: center;
}
p .center {
margin-top: 5%;
}
.tBox {
position: relative;
width: auto;
max-width: 100%;
min-height: 400px;
max-height: 500px;
background-color: #222831;
border-radius: 5px;
margin: 40px auto;
align-content: center;
color: #eeeeee;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12) !important;
font-family: 'Lato', sans-serif;
}
.legend {
padding: 0.2em 0.8em;
background: #d65a31;
border-radius: 25px;
float: left;
margin-top: -20px;
margin-left: 20px;
width: auto;
min-width: 200px;
font-size: 3vmax 2vmin;
font-family: 'Lato', sans-serif;
}
<div>
<fieldset class="tBox">
<legend class="legend">Definition</legend>
<div id="definition">Answers if we did what we said we would do. BECAUSE IT'S LONG I'LL ADD EXTRA TEXT TO SHOW MULTI-LINE EFFECT</div>
<div>
<hr>
<section id="formula">
<div class="row">
<p class="column center" style="margin-top: 5%; margin-left: 3%;">Formula:</p>
<div class="column center">
<p>∑ # completed tasks in month 'A' (from month 'B' schedule)</p>
<hr id="formulaLine">
<p>∑ # tasks forecased to finish in month 'A'</p>
</div>
</div>
</section>
</div>
</fieldset>
</div>
Problem is that when hiding the formula sibling (I am using React), the definition doesn't center. It looks like this:
#title {
margin: 20px;
}
#definition {
margin: 0 auto;
margin-top: 5%;
text-align: center;
max-width: 60%;
font-size: 1.5vmax;
}
hr {
color: white;
background-color: white;
width: 80%;
height: 1px;
}
#formulaLine {
color: white;
background-color: white;
height: 1px;
}
section#formula {
width: auto;
max-width: 70%;
background: #393e46;
box-shadow: inset 2px 5px 10px rgb(24, 23, 23);
border-radius: 5px;
margin: 5% auto;
padding: 10px;
font-size: 2vmax 1vmin;
}
.center {
text-align: center;
}
p .center {
margin-top: 5%;
}
.tBox {
position: relative;
width: auto;
max-width: 100%;
min-height: 400px;
max-height: 500px;
background-color: #222831;
border-radius: 5px;
margin: 40px auto;
align-content: center;
color: #eeeeee;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12) !important;
font-family: 'Lato', sans-serif;
}
.legend {
padding: 0.2em 0.8em;
background: #d65a31;
border-radius: 25px;
float: left;
margin-top: -20px;
margin-left: 20px;
width: auto;
min-width: 200px;
font-size: 3vmax 2vmin;
font-family: 'Lato', sans-serif;
}
<div>
<fieldset class="tBox">
<legend class="legend">Definition</legend>
<div id="definition">MY TEXT HERE. IT CAN GET LONG. MULTI-LINE. HERE'S MORE TO FILL THIS OUT. LONG LONG LONG.</div>
</fieldset>
</div>
Note that the CSS for this second example is the same as above. What am I doing wrong? I've tried Top, Float, and a variety of other options. None seem to work.
I would recommend adjusting your markup to support using :only-child in CSS. This is a pseudo-class that represents an element without any siblings. Definitely give the documentation a review for some other examples.
/* Selects each <p>, but only if it is the only child of its parent. */
p:only-child {
background-color: lime;
}
It's pretty useful for situations just like this and the implementation wouldn't take very many changes.
var formula = document.createElement("P");
formula.innerText = "This element represents your formula being added to the container which removes the styles applied with :only-child.";
var active = false;
function toggleFormula() {
active = !active;
document.getElementById("legend").innerText = active ? "Click here to hide formula." :
"Click here to show formula.";
let tbox = document.getElementById("t-box");
if (active)
tbox.appendChild(formula);
else
tbox.removeChild(formula);
}
.container { position: relative; }
.legend {
position: absolute;
top: -10px;
left: 20px;
z-index: 1;
padding: 0.2em 0.8em;
background: #d65a31;
border-radius: 25px;
width: auto;
min-width: 200px;
font-size: 3vmax 2vmin;
font-family: 'Lato', sans-serif;
cursor: pointer;
}
#definition {
margin: 0 auto;
margin-top: 5%;
text-align: center;
max-width: 60%;
font-size: 1.5vmax;
}
#definition:only-child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.tBox {
position: relative;
width: auto;
max-width: 100%;
min-height: 400px;
max-height: 500px;
background-color: #222831;
border-radius: 5px;
margin: 40px auto;
align-content: center;
color: #eeeeee;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12) !important;
font-family: 'Lato', sans-serif;
}
#definition {
margin: 0 auto;
margin-top: 5%;
text-align: center;
max-width: 60%;
font-size: 1.5vmax;
}
#definition:only-child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="container">
<legend id="legend" class="legend" onclick="toggleFormula();">Click here to show formula.</legend>
<fieldset id="t-box" class="tBox">
<div id="definition">Answers if we did what we said we would do. BECAUSE IT'S LONG I'LL ADD EXTRA TEXT TO SHOW MULTI-LINE EFFECT</div>
</fieldset>
</div>
Alternative options are using position: absolute or display: flex:
/* Absolute Version */
#definition.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Flex Version */
.tBox {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
With your exiting code, just adding few properties to your css will be an easy fix. Flex properties are really helpful in these scenarios, align-items: center will align all elements inside the div to align vertically and justify-content: center will align items horizontally.
section#formula {
width: auto;
max-width: 70%;
background: #393e46;
box-shadow: inset 2px 5px 10px rgb(24, 23, 23);
border-radius: 5px;
margin: 5% auto;
padding: 10px;
font-size: 2vmax 1vmin;
display: flex;
justify-content: center;
align-items: center;
}
.center {
justify-content: center;
display: flex;
width: 100%;
flex-flow: row wrap;
}
You can see it here.
I have three flexbox containers (header, .content__description and footer) and two of them (header and .content__description) don't fit the children's height in Chrome, but only in Firefox. Therefore, the first container is covered by the second one. Why does it occur?
/* util.css */
.break {
width: 100%;
}
/* An issue
html,
body {
height: 100%;
}
*/
/* The solution I found */
html {
height: 100%;
}
body {
min-height: 100%;
}
/* Other styles */
body {
display: flex;
flex-direction: column;
justify-content: space-between;
color: rgb(255, 255, 255);
background-color: rgb(20, 142, 210);
}
header {
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
margin-top: 3em;
margin-bottom: 3em;
}
.header__logo {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
padding-bottom: 1em;
}
.header__logo img {
width: 3em;
height: 3.3em;
margin-right: 0.6em;
}
.header__logo span {
font-size: 5em;
font-weight: 400;
}
.header__slogan {
font-size: 1.5em;
font-weight: 400;
text-align: center;
}
.content {
margin-left: auto;
margin-right: auto;
}
.content__description {
display: flex;
flex-direction: column;
padding-top: 3em;
padding-bottom: 3em;
background-color: rgb(0, 114, 174);
position: relative;
}
.content__description__signin {
position: absolute;
text-decoration: none;
text-align: center;
color: black;
background-color: rgb(254, 190, 0);
box-sizing: border-box;
font-size: 2em;
border: 0.2em rgb(255, 255, 255) solid;
line-height: 1em;
width: 6em;
padding: 0.2em;
top: calc(100% - 0.9em);
left: calc(50% - 3em);
}
.content__description__signin:hover {
background-color: rgb(241, 177, 0);
}
.content__description__strep {
width: 13em;
text-align: center;
font-size: 2em;
box-sizing: border-box;
}
.content__description__strep img {
width: 5em;
height: 5em;
border: 0.2em rgb(255, 255, 255) solid;
border-radius: 50%;
box-shadow: 0.2em 0.5em 1em rgba(0, 0, 0, 0.3);
box-sizing: border-box;
margin-bottom: 1em;
}
.content__description__strep figcaption {
background-color: rgb(2, 98, 148);
padding: 1em;
margin-left: -1em;
margin-right: -1em;
}
footer {
flex-grow: 1;
background-color: rgb(255, 255, 255);
color: rgb(0, 0, 0);
min-height: 1.8em;
}
<body>
<header>
<div class="header__logo">
<img src="images/logo.svg" alt="logo">
<span>Sailing</span>
</div>
<span class="break"></span>
<h1 class="header__slogan">You can!</h1>
</header>
<div class="content">
<div class="content__description">
<figure class="content__description__strep">
<img src="images/meeting.jpg" alt="meeting">
<figcaption>Talking</figcaption>
</figure>
<figure class="content__description__strep">
<img src="images/sailing.jpg" alt="sailing">
<figcaption>Sailing</figcaption>
</figure>
<figure class="content__description__strep">
<img src="images/cinema.jpg" alt="cinema">
<figcaption>Watching</figcaption>
</figure>
Sign in
</div>
</div>
<footer> </footer>
</body>
EDIT #1
The display style of .content__description is changed to "flex" (misprint).
EDIT #2
I've found the solution - switched body {height: 100%} to body {min-height: 100%}. It works perfectly for me.