Why my Element is going outside the header element - html

There is a div with a class of favourite which is not aligning in the header section.
Yes this is a react project but it is edited like a normal project for this question.
<div className='header'>
<img class='profilePhoto' src='./userIcon'></img>
<div class='brand-logo'>
</div>
<div class='favourite'>Favourite</div> <!-- This Favourite is going outisde div.header-->
</div>
CSS File:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.header {
width: 100vw;
height: 5rem;
background: #2475b0;
position: fixed;
}
.profilePhoto {
position: absolute;
margin-left: 32px;
vertical-align: middle;
top: 50%;
transform: translateY(-50%);
}
.brand-logo {
display: flex;
height: 100%;
justify-content: center;
align-items: center;
}
.favourite {
display: inline-block;
position: relative;
right: 3rem;
}
Brand Logo CSS
.icon-box {
background: #eaf0f1;
width: 19rem;
height: 75%;
border-radius: 1rem;
text-align: center;
vertical-align: bottom;
}
.brand-name {
display: flex;
height: 100%;
justify-content: center;
align-items: center;
margin-top: 0.3rem;
font-family: 'Luckiest Guy', cursive;
font-size: 2rem;
}
Image Preview:
Any tip will be helpful.. Thank you in advance

Try this piece of code in favourite class.
.favourite {
text-align: right;
position: relative;
right: 3rem;
margin-top: -45px;
}
Adjust margin-top as your need.

Increase the height of header div
.header {
height: 10rem;
}

You don't need to give position for make your logo in center. Just remove fix height from header and try using padding. Hope will help you.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.header {
width: 100vw;
padding: 20px;
background: #2475b0;
position: fixed;
}
<div class='header'>
<div class='brand-logo'>
<img class='profilePhoto' src='/userIcon'>
</div>
<div class='favourite'>Favourite</div> <!-- This Favourite is going outisde div.header-->
</div>

add display: inline-block to 3 classes:
.profilePhoto, .brand-logo, .favourite {
display: inline-block;
}
and remove position attribute in class favourite. One more, i think you should use 'class' attribute, not 'className' like :
<div className='header'>

Try this just add height and padding.
.header {
width: 100vw;
height: 10rem;
background: #2475b0;
padding-bottom:10px;
position: fixed;
}

Thanks: https://stackoverflow.com/users/2875348/ravibagul91
Guys this is happening because .brand-logo is taking 100% height!
When I removed:
height: 100%
then code is working fine...
I just simply removed flexbox and used position property to align my .brand-logo
.brand-logo{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.header {
width: 100vw;
height: 5rem;
background: #2475b0;
position: fixed;
}
.profilePhoto {
position: absolute;
margin-left: 32px;
vertical-align: middle;
top: 50%;
transform: translateY(-50%);
}
.brand-logo {
display: flex;
height: 100%;
justify-content: center;
align-items: center;
}
.favourite {
display: flex;
position: fixed;
top: 50px;
left: 10px;
}
<div class='header'>
<img class='profilePhoto' src='./userIcon'>
<div class='favourite'>Favourite</div>
<div class='brand-logo'></div>
</div>

Related

HTML/CSS: Positioning a background element responsively

Here's a rough mockup of what I am trying to create, I have the page laid out but the only piece left that I am struggling with is positioning the background circle in a way that is responsive.
I tried using position absolute with top & left positioning but even small screen changes will completely throw off the layout.
What would you guys recommend as the best way to approach this? Thanks!
code demo: https://jsfiddle.net/e91g8tdj/11/
Try this in your Css :
.yourClassHere{
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Since you haven't provided any demo of your working code so had to create a responsive one from scratch using css only. It might look a bit janky in the snippet below so its better to copy the code in a file and view in a browser.
Let me know in the comments if it breaks.
*,
*::before,
*::after {
box-sizing: border-box;
padding: 0;
margin: 0;
}
.container {
width: 100%;
height: 100vh;
}
.divider1,
.divider2 {
width: 100%;
height: 50vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.divider1 {
background-color: silver;
gap: 2rem;
}
.divider2 {
background-color: gray;
}
.row1,
.row2,
.row3 {
width: 90%;
height: 25%;
background-color: #2a2a2a;
}
.row3 {
width: 40%;
height: 70%;
}
.row1-row2-wrapper, .row3-wrapper {
width: 40%;
height: inherit !important;
display: flex;
justify-content: end;
align-items: center;
position: relative;
}
.row1-row2-wrapper {
flex-direction: column;
justify-content: center;
gap: 2rem;
z-index: 1;
}
.row1-row2-wrapper::before, .row1-row2-wrapper::after, .row3::before, .row3::after {
content: '';
position: absolute;
border: 4px solid #fff;
border-radius: 50%;
}
.row1-row2-wrapper::before {
inset: 0;
z-index: -1;
height: 105%;
width: 100%;
}
.row1-row2-wrapper::after {
width: 2rem;
height: 2rem;
bottom: -18%;
right: 24%;
}
.row3::before {
width: 1.4rem;
height: 1.4rem;
top: 18%;
right: 20%;
}
.row3::after {
width: 1rem;
height: 1rem;
top: 25%;
right: 16%;
}
<div class="container">
<div class="divider1">
<div class="row1-row2-wrapper">
<div class="row1"></div>
<div class="row2"></div>
</div>
</div>
<div class="divider2">
<div class="row3-wrapper">
<div class="row3"></div>
</div>
</div>
</div>

Why div containing img does not stretch his parent's div height

I wonder why mainCountainerHeadLogo does not stretch parent div mainCountainerHead height?
If I scale the page, both mainCountainerHeadTitle and mainCountainerHeadMenu stretch mainCountainerHead just fine.
Sorry for my english and thanks in advance!
http://jsfiddle.net/gvcs0r6b/
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
.mainCountainer {
min-height: 100%;
width: 70%;
margin: 0 auto;
}
.mainCountainerHead {
background-color: aqua;
height: auto;
}
.mainCountainerHeadLogo {
height: 100px;
width: 20%;
background-color: blue;
float: left;
position: relative;
overflow: hidden;
}
.mainCountainerHeadLogo img {
position: absolute;
width: 100%;
height: auto;
top: -50%;
right: -50%;
bottom: -50%;
left: -50%;
margin: auto
}
.mainCountainerHeadTitle{
margin-left: 20%;
width: 80%;
height: auto;
text-align: center;
padding-top: 3%;
}
.mainCountainerHeadMenu{
margin-left: 20%;
text-align: center;
background-color: orange;
width: 80%;
height: auto;
padding-top: 2%;
text-align: center;
}
.mainLink {
display: inline-block;
padding: 5px;
}
.mainLinkButton {
width: 90px;
height: 30px;
background-color: green;
font-size: 16px;
border: none;
color: white;
padding: 5px;
}
.mainLinkButton:hover {
background-color: darkgreen;
}
.mainLinkDropdown {
position: relative;
display: inline-block;
padding: 5px;
}
.dropdownContent {
display: none;
position: absolute;
min-height: 30px;
min-width: 130px;
text-align: left;
background-color: #f1f1f1;
z-index: 10;
}
.dropdownContent a {
display: block;
color: black;
padding: 12px 16px;
text-decoration: none;
}
.mainLinkDropdown:hover .dropdownContent{
display: block;
}
.dropdownContent a:hover{
background-color: #ddd;
}
<div class="mainCountainer">
<div class="mainCountainerHead">
<div class="mainCountainerHeadLogo">
<img src="https://i.ibb.co/cYzWJFM/logo-Copy.jpg" title="logo" />
</div>
<div class="mainCountainerHeadTitle">
<h4>Welcome aboard!</h4>
</div>
<div class="mainCountainerHeadMenu">
<div class="mainLink">
<button class="mainLinkButton">Main</button>
</div>
<div class="mainLinkDropdown">
<button class="mainLinkButton">Dropdown</button>
<div class="dropdownContent">
Link 1
Link 2
Link 3
</div>
</div>
<div class="mainLink">
<button class="mainLinkButton">Contacts</button>
</div>
</div>
</div>
</div>
In answer to your question:
That's because the float property puts the HTML elements out of the normal page flow, and this causes what you're experiencing. Its effect is similar to position: absolute which is to move the element to "a different layer".
How to solve it?
Well... there are a lot of ways to achieve what you want, and almost all of them requires to refactorize your code. Actually, you have a lot of code that makes it difficult to achieve your goal. You should get rid of float and start using other technics like Flexbox.
I could show you a solution if you provide a sketch of the layout you want.
change the CSS for img to this
.mainCountainerHeadLogo img {
width: 100%;
height: 100%;
margin: auto
}

Vertically Centering an img within an a tag within a div

I'm trying to vertically center an image in a div in an <a> tag but I can't get the CSS to vertically center the image for the life of me.
div {
height: 7rem;
width: 90%;
text-align: center;
margin: 1.25rem auto;
padding: 0 5.5rem 0 5.5rem;
font-size: .8rem;
border-style: solid;
border-width: .08rem;
}
div * {
position: relative;
top: 50%;
transform: translateY(-50%);
}
div img {
width: 4rem;
}
div div {
margin: 0 auto;
width: 4rem;
height: 100%;
}
<div class="footer">
<div id="footerhome">
<a href="https://www.google.com">
<img src="http://images.clipartpanda.com/smiley-face-transparent-background-tumblr_mdv6nltwdB1qdic05o1_500.gif" />
</a>
</div>
</div>
This is what I'm currently working with:
http://codepen.io/SamanthaBae/pen/LRomdr
I've tried the solution of making the image the background image of the div it's contained in and inserting the <a> tag in between, but it doesn't seem to work as a link, even though I can get it to vertically align:
I have checked here and here as resources, but have not been able to apply their methods in this case.
You should to replace this line:
.footer * {
position: relative;
top: 50%;
transform: translateY(-50%);
}
With this code:
.footer #footerhome, .footer a {
position: relative;
top: 50%;
transform: translateY(-50%);
}
.footer a {
display: block;
}
Your problem start when you align all child to their parents.
There are multiple possibilities for centering images vertically.
Other possible solutions can be found here
I used display:flexbox here.
#footerhome {
display: flex;
align-items: center;
}
Here's a codepen
http://codepen.io/Maharkus/pen/ozRyNK
There is one old hack for this if you don't want to deal with flexbox. In short, you should implement table-like semantics for your mark-up with display: table and display: table-cell rules:
.footer {
display: table;
}
#footerhome {
display: table-cell;
vertical-align: middle;
}
UPD
Following your updated code:
#footerhome {
display: table;
}
a {
display: table-cell;
vertical-align: middle;
height: // some fixed height
}
What you were going for was something like this. You'll notice I've added the positioning styles to the <img> again.
div {
height: 7rem;
width: 90%;
text-align: center;
margin: 1.25rem auto;
padding: 0 5.5rem 0 5.5rem;
font-size: .8rem;
border-style: solid;
border-width: .08rem;
}
div div {
position: relative;
top: 50%;
transform: translateY(-50%);
margin: 0 auto;
width: 4rem;
height: 100%;
}
div img {
width: 4rem;
position: relative;
top: 50%;
transform: translateY(-50%);
}
<div class="footer">
<div id="footerhome">
<a href="https://www.google.com">
<img src="http://images.clipartpanda.com/smiley-face-transparent-background-tumblr_mdv6nltwdB1qdic05o1_500.gif" />
</a>
</div
But if I were you I'd do something like this, which uses flexbox and calls the elements by their classes/ids
#footerhome,
.footer {
box-sizing: border-box;
padding: 0 5.5rem;
border-style: solid
}
.footer {
height: 7rem;
width: 90%;
margin: 1.25rem auto;
font-size: .8rem;
border-width: .08rem
}
#footerhome {
width: 4rem;
height: 100%;
display: flex;
justify-content: center;
margin: 0 auto;
align-items: center;
border-width: 0 .08rem
}
#footerhome img {
width: 4rem
}
<div class="footer">
<div id="footerhome">
<a href="https://www.google.com">
<img src="http://images.clipartpanda.com/smiley-face-transparent-background-tumblr_mdv6nltwdB1qdic05o1_500.gif" />
</a>
</div>
</div>

Center a div below the header

I added a widget area below my header and I try to center it just under the logo.
I tried to add display: block and margin:0 auto or text-align: center on #header-sidebar, but it doesn't work. Also, I tried to change the structure of the HTML code and not only make CSS tweaks, but still, the #header-sidebar stack on the left and not centered.
Here is my code:
HTML code:
<header id="header">
<div class="header-container">
<img style="vertical-align: middle;" src="#">
<nav> //here is the navigation, hidden in mobile view </nav>
<div id="header-sidebar" class="header-widget-area">
<div class="textwidget">
//some widget here
</div>
<div class="mlp_language_box">
// some other widget here
</div>
</div>
</div>
</header>
CSS code:
header#header {
position: relative;
z-index: 300;
height: 70px;
line-height: 70px;
display: block;
}
header#header #phone-toggle-menu {
left: 20px;
margin-left: 0;
position: absolute;
}
header#header .header-container {
max-width: 1200px;
margin: 0 auto;
}
header#header .logo {
width: 100%;
float: none;
box-sizing: border-box;
text-align: center;
position: absolute;
}
display:table and margin:auto for both logo and widget area will do the trick... So toggle button alone can stay absolute.
header#header {
position: relative;
z-index: 300;
height: 70px;
line-height: 70px;
display: block;
}
header#header #phone-toggle-menu {
left: 20px;
margin-left: 0;
position: absolute;
}
header#header .header-container {
max-width: 1200px;
margin: 0 auto;
}
header#header .logo {
box-sizing: border-box;
display: table;
margin: auto;
}
header #header-sidebar{
display: table;
margin: auto;
}
Replace your css code with following :
header#header {
position: relative;
z-index: 300;
height: 70px;
line-height: 70px;
display: block;
}
header#header #phone-toggle-menu {
left: 20px;
margin-left: 0;
position: absolute;
}
header#header .header-container {
max-width: 1200px;
margin: 0 auto;
text-align: center;
}
header#header .logo {
box-sizing: border-box;
text-align: center;
position: absolute;
}

My divs are overlapping, i'm lost

Hey I can't figure out why my divs are overlapping and what i should do...
You can watch the site here: http://hersing.dk/job/
I would like for the div carrying the hr to appear underneed the header-info div
Heres is the code from the site:
#font-face {
font-family: hersing;
src: url(lmroman10-regular.otf);
}
html,
body {
font-family: hersing;
height: 100%;
margin: 0px;
}
.container {
width: 90%;
height: 90%;
left: 5%;
top: 5%;
background: green;
position: absolute;
display: block;
clear: both;
}
.info-name {
left: 5%;
top: 10%;
position: absolute;
display: block;
}
.info-picture {
min-width: 250px;
min-height: 250px;
padding: 4px;
position: absolute;
top: 10%;
right: 5%;
background: black;
display: block;
}
.info-picture img {
height: 100%;
width: 100%;
}
#info-header {
font-size: 400%;
}
#info-title {
font-size: 150%;
font-weight: bold;
}
.header-info {
display: block;
padding: 20px;
position: relative;
width: 100%;
}
.stang-1 {
display: block;
width: 100%;
color: blue;
position: relative;
}
#hr-1 {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
background-color: #f1a857;
}
<div class="container">
<div class="header-info">
<div class="info-name" id="info-name">
...
</div>
<div class="info-picture" id="info-picture">
<img src="images/picture.png" />
</div>
</div>
<div class="stang-1" id="stang-1">
<hr id="hr-1">
</div>
</div>
I hope someome can figure this out, cause i'm pretty lost
Both .info-name and .info-picture are absolute positioned and .header-info has no height defined.
You'd rather use relative positioning + float + clear and/or display: inline-block for both .info-* rules and everything will be fine.
<div class="container">
<div class="header-info">
<div class="info-name" id="info-name">
.....
</div>
<div class="info-picture" id="info-picture">
<img src="images/picture.png" />
</div>
</div>
<div class="stang-1" id="stang-1">
<hr id="hr-1">
</div>
</div>
<style>
#font-face {
font-family: hersing;
src: url(lmroman10-regular.otf);
}
html,
body {
font-family: hersing;
height: 100%;
margin: 0px;
}
.container {
width: 90%;
height: 90%;
left: 5%;
top: 5%;
background: green;
position: absolute;
display: block;
clear: both;
}
.info-name {
left: 5%;
top: 10%;
position: absolute;
display: block;
}
.info-picture {
width: 250px;
height: 250px;
padding: 4px;
position: relative;
top: 10%;
left:70%;
background: black;
display: block;
}
.info-picture img {
height: 100%;
width: 100%;
}
#info-header {
font-size: 400%;
}
#info-title {
font-size: 150%;
font-weight: bold;
}
.header-info {
display: block;
padding: 20px;
position: relative;
width: 100%;
}
.stang-1 {
display: block;
width: 100%;
color: blue;
position: absolute;
}
#hr-1 {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
background-color: #f1a857;
}
</style>
I think this will solve your problem...
In this case, although very impractical, the solution would be to add a line break <br> after the .header-info div.
I repeat, this solution is not the best one by far, and you should, as pointed out in the comments by Paulie_D, change your positioning layout method.
Everything inside the absolutely positioned .container would be better positioned relative. Use css float:left; or float:right; to position elements and clear:both; when you want the next element to start below all floated elements. Use padding on the container and margins on the floated elements for positioning.
Also give .container css class of overflow:auto; to wrap around all elements inside without having to set the height every time.