relative element in flex-box , losing the width - html

I have a relative element within which there is an element with an image background that is absolute, when the parent of the relative element becomes a flex-box I lose the width, only when I bring the relative element static width (width: num px), I do not lose the width but the image does not responsive.
/* -------------------------------- */
/* header */
header {
margin-bottom: 24px;
display:flex;
}
.header-items h1 {
font-weight: 900;
font-size: 48px;
text-transform: uppercase;
line-height: 48px;
margin-bottom: 32px;
}
.header-items p {
margin-bottom: 35px;
font-weight: 500;
}
.pre--order {
display: flex;
align-items: baseline;
margin-bottom: 64px;
}
.pre--order a {
padding: 16px 26px;
background-color: #f16718;
text-transform: uppercase;
border-radius: 8px;
color: #ffffff;
margin-right: 32px;
}
.pre--order p {
font-weight: 700;
text-transform: uppercase;
}
.keyboard--header {
position: relative;
overflow: hidden;
max-width: 100%;
height: 425px;
}
.kh--image {
background-image: url("https://www.beauchamp.com/_wp/wp-content/uploads/2020/12/xIsrael_TelAviv_City_shai-pal1.jpg");
background-repeat: unset;
position: absolute;
width: 100%;
height: 100%;
background-size: 100% 100%;
border-radius: 15px;
top: 0;
left: 24px;
}
<header>
<div class="container">
<section class="header-items">
<h1>Typemaster Keyboard</h1>
<p>
Improve your productivity and gaming without breaking the bank.
Upgrade to a high quality mechanical typing experience.
</p>
<div class="pre--order">
Pre-order now
<p>Release on 5/27</p>
</div>
</div>
</section>
<section>
<div class="keyboard--header">
<div class="kh--image"></div>
</div>
</section>

Here is an example where I've wrapped the flexbox, given the flex child an explicit width&height, and fixed up a bunch of things like margin/padding you should generally base off typography not px
header {
margin-bottom: 1.5em;
display: flex;
flex-flow: row wrap;
}
.header-items h1 {
font-weight: 900;
font-size: 3rem;
text-transform: uppercase;
line-height: 3rem;
margin-bottom: 2rem;
}
.header-items p {
margin-bottom: 2rem;
font-weight: 500;
}
.pre--order {
display: flex;
align-items: baseline;
}
.pre--order a {
padding: 1em 1.5em;
background-color: #f16718;
text-transform: uppercase;
border-radius: 8px;
color: #fff;
margin-right: 2em;
}
.pre--order p {
font-weight: 700;
text-transform: uppercase;
}
.keyboard--header {
width: 100%;
height: 200px;
}
.kh--image {
background-image: url("https://www.beauchamp.com/_wp/wp-content/uploads/2020/12/xIsrael_TelAviv_City_shai-pal1.jpg");
background-clip: cover;
background-size: 100%;
width: 100%;
height: 100%;
border-radius: 15px;
}
<header>
<div class="container">
<section class="header-items">
<h1>Typemaster Keyboard</h1>
<p>
Improve your productivity and gaming without breaking the bank. Upgrade to a high quality mechanical typing experience.
</p>
<div class="pre--order">
Pre-order now
<p>Release on 5/27</p>
</div>
</section>
</div>
<div class="keyboard--header">
<div class="kh--image"></div>
</div>
</header>

Related

How to have centered title and an image on just on right in responsive header

First, I'm so sorry because I know that it's possible, but I really suck at CSS.
This is what I'd like to do:
I've managed to do it but it's really messy... The main issue is that my header isn't responsive at all and I'd to know what is the best way to do it (I know that usually flexbox is a good practice when it comes to build something responsive but my issue is that if I create 2 columns thanks to Flexbox I won't be able to align them just next to each other).
This is my current code (I know it's uggly):
header {
background-color: #c16200;
color: white;
margin-top: 1em;
overflow: hidden;
position: sticky;
top: 0;
width: 100%; /* Full width */
z-index: 1;
max-height: 8vh;
}
h1 {
color: white;
text-align: center;
font-size: 2em;
font-family: "Raleway", sans-serif;
text-transform: uppercase;
letter-spacing: 0.08em;
position: relative;
}
.logo {
position: absolute;
top: 10px;
right: 35%;
height: 2.5em;
}
.line {
margin: 0 auto;
width: 17em;
height: 2px;
border-bottom: 2px solid white;
}
.header-sentence {
margin-top: 0.2em;
font-size: 0.8em;
font-style: italic;
text-align: center;
font-family: "Raleway", sans-serif;
}
<header id="myHeader" class="sticky">
<div class="header-title">
<h1>
Title
</h1>
<img
src="https://cdn.glitch.com/33ba966f-5c93-4fa3-969c-a216a9d7629c%2F167931478_735371457343939_8305934260393763828_n.png?v=1617205161517"
class="logo"
alt="logo plane"
/>
<div class="line"></div>
<p class="header-sentence">
subtitle
</p>
</div>
</header>
Thank you guys!
In HTML with CSS it is sometimes a good idea to do some nesting of elements.
I used an wrapper element (.header-title-composition) to layout title, line, and subtitle vertically . This is all wrapped alongside the paper plane inside .header-title, which is responsible for the horizontal layout
header {
background-color: #c16200;
color: white;
margin-top: 1em;
overflow: hidden;
position: sticky;
top: 0;
width: 100%;
/* Full width */
z-index: 1;
/* This destroys everything inside this demonstration */
/* Basing a height on the actual viewport's height is somewhat dangerous */
/* max-height: 8vh; */
}
.header-title {
display: flex;
align-items: center;
}
.header-title :first-child {
margin-left: auto;
}
.header-title :last-child {
margin-right: auto;
}
h1 {
color: white;
text-align: center;
font-size: 2em;
font-family: "Raleway", sans-serif;
text-transform: uppercase;
letter-spacing: 0.08em;
position: relative;
}
.logo {
/* position: absolute;
top: 10px;
right: 35%;*/
height: 2.5em;
}
.line {
margin: 0 auto;
width: 17em;
height: 2px;
border-bottom: 2px solid white;
}
.header-sentence {
margin-top: 0.2em;
font-size: 0.8em;
font-style: italic;
text-align: center;
font-family: "Raleway", sans-serif;
}
<header id="myHeader" class="sticky">
<div class="header-title">
<div class="header-title-composition">
<h1>
Title
</h1>
<div class="line"></div>
<p class="header-sentence">
subtitle
</p>
</div>
<img src="https://cdn.glitch.com/33ba966f-5c93-4fa3-969c-a216a9d7629c%2F167931478_735371457343939_8305934260393763828_n.png?v=1617205161517" class="logo" alt="logo plane" />
</div>
</header>
Here somewhat of a starting point for you. First of all, I added .header-brand as wrapper for title, line, sentence and image. Used display: flex for alignment. The additional media query takes care of the alignment, when the screen size is below 480px (But try it out on your own, since there are probably still some issues with that)
header {
background-color: #c16200;
color: white;
margin-top: 1em;
overflow: hidden;
position: sticky;
top: 0;
width: 100%;
/* Full width */
z-index: 1;
max-height: 80vh;
display: flex;
}
.header-brand {
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
}
h1 {
color: white;
text-align: center;
font-size: 2em;
font-family: "Raleway", sans-serif;
text-transform: uppercase;
letter-spacing: 0.08em;
position: relative;
margin: 0;
}
.logo {
height: 2.5em;
margin-left: 20px;
}
.line {
margin: 0 auto;
width: 17em;
height: 2px;
border-bottom: 2px solid white;
}
.header-title {
margin-top: 10px;
}
.header-sentence {
margin-top: 0.2em;
font-size: 0.8em;
font-style: italic;
text-align: center;
font-family: "Raleway", sans-serif;
}
#media screen and (max-width: 480px) {
.line {
width: 100%;
}
.logo {
margin-left: 0;
}
}
<header id="myHeader" class="sticky">
<div class="header-brand">
<div class="header-title">
<h1>
Title
</h1>
<div class="line"></div>
<p class="header-sentence">
subtitle
</p>
</div>
<img src="https://cdn.glitch.com/33ba966f-5c93-4fa3-969c-a216a9d7629c%2F167931478_735371457343939_8305934260393763828_n.png?v=1617205161517" class="logo" alt="logo plane" />
</div>
</header>
Combine flexbox and a simple wrapper using text-align: center, the decorated line can be a pseudo-element.
h1,
div,
p {
margin: 0;
}
header {
display: flex;
justify-content: center;
align-items: center;
background-color: #c16200;
color: white;
overflow: hidden;
position: sticky;
padding: 0.5rem;
top: 0;
width: 100%;
/* Full width */
z-index: 1;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}
.logo-wrapper {
text-align: center;
margin-right: 1rem;
}
header img {
width: 2rem;
height: 2rem;
}
h1 {
font-weight: bold;
font-size: 1.5rem;
}
h1::after {
width: 10rem;
height: 1px;
display: block;
background-color: white;
content: '';
}
<header class="sticky">
<div class="logo-wrapper">
<h1>TITLE</h1>
<p>the tag line</p>
</div>
<img src="https://cdn.glitch.com/33ba966f-5c93-4fa3-969c-a216a9d7629c%2F167931478_735371457343939_8305934260393763828_n.png?v=1617205161517" class="logo" alt="logo plane" />
</header>
I suppose you want the element which contains the title and subtitle centered, and the image aligned right to that, not both together centered. So here's a solution:
The .title-container is centered within the header using display: flex and other flex settings (see below) on the header. Avoiding both the text container and the image to be centered together is done by applying position: absolute to the image, making it a child of .title-container and applying position: relative to .title-container to make it the position reference for the absolutely positioned image. That way the image isn't considered at all when centering the .title-container.
Take a look at the position parameters for the image: Vertically-centered alignement is achieved by top: 50% and transform: translateY(-50%);, the horizontal position is done with a negative right value: -2.5rem, i.e. the width of the image (2rem) plus 0.5rem for the distance to the text container. Adjust all values as needed.
* {
box-sizing: border-box;
}
body {
margin: 0;
}
header {
display: flex;
justify-content: center;
align-items: center;
background-color: #c16200;
color: white;
position: fixed;
padding: 1rem;
top: 0;
width: 100%;
font-family: Calibri, 'Trebuchet MS', sans-serif;
}
.title-container {
text-align: center;
position: relative;
width: 200px;
}
.title-container .line {
border-top: 1px solid white;
width: 100%;
margin: 2px 0 4px;
}
.title-container img {
width: 2rem;
height: 2rem;
position: absolute;
right: -2.5rem;
top: 50%;
transform: translateY(-50%);
}
.title-container h1 {
font-size: 1.5rem;
margin: 0;
}
.title-container p {
margin: 0;
}
<header>
<div class="title-container">
<h1>TITLE</h1>
<div class="line"></div>
<p>SUBTITLE</p>
<img src="https://cdn.glitch.com/33ba966f-5c93-4fa3-969c-a216a9d7629c%2F167931478_735371457343939_8305934260393763828_n.png?v=1617205161517" class="logo" alt="logo plane" />
</div>
</header>

DIVs are moving from their positions when zooming out of the page

So basically my problem is when I zoom in-out of the page, some DIVs move out from their position which causes my layout to break. I used the position relative and absolute properties as well as used % instead px as my units in positioning. I've gone through some articles saying that it's not favorable to use position absolute and that this was an expected behavior but I couldn't seem to find an answer to my question. I also tried find a way to do it using CSS Flexbox but i couldn't find a direct answer to my problem.
My expected layout should be something like https://snipboard.io/Mr8sNv.jpg but as I said before, it breaks when zooming in/out.
Here is my HTML.
<div class="Container">
<div class="background">
<div class="Space"></div>
<p class="Branding_Design">BRANDING & DESIGN</p>
<div class="Project_One">Project One</div>
<div class="SliderBOX"></div>
<div style="position:absolute; right: 8%; top: 59%;">
<div class="ProjectDetailsContainer">
<p class="Project_Details">PROJECT DETAILS</p>
</div>
</div>
<div style="position:absolute; right: 12%; top: 62.5%;">
<div class="ViewSlidesContainer">
<p class="View_Slides">VIEW SLIDES</p>
</div>
</div>
</div>
</div>
Here is my CSS
.Container {
width: 100%;
position: relative;
height: 721px;
}
.background {
background: black;
width: 100%;
height: 100%;
}
h3 {
text-align: center;
}
.Space {
width: 1000px;
height: 100px;
background: red;
position: relative;
z-index: -1;
}
.SliderBOX{
height: 525px;
width: 50%;
background: yellow;
margin: 0 auto;
}
.Branding_Design {
font-size: 10px;
font-weight: 500;
font-family: 'Open Sans';
color: #f7ac53;
letter-spacing: 0.5px;
position: absolute;
top: 39%;
left: 12%;
}
.Project_One {
color: white;
font-family: "Merriweather";
font-size: 39px;
font-weight: 300;
position: absolute;
top: 45%;
left: 14%;
z-index: 1;
}
.ProjectDetailsContainer {
position: relative;
}
.Project_Details {
color: white;
font-family: 'Open Sans';
font-weight: 400;
font-size: 11px;
}
.ViewSlidesContainer {
position: relative;
}
.View_Slides {
color: white;
font-family: 'Open Sans';
font-weight: 400;
font-size: 11px;
}
I also included my code in jsfiddle in order to make it easier to go through my code
https://jsfiddle.net/wa3bLx1h/22/
What am I doing wrong?
I think believe the code below, is what you're looking for correct? Flex-box would the solution to remedy this problem. I would suggest just go thru MDN tutorials and mess around with each properties of flex-box. Your problem helped me understand Parent and Child containers better now.
<div class="container">
<div class="background">
<div class="space"></div>
<div class="textCon1">
<div class="p-text-1">
<p class="brandingDesign"> BRANDING & DESIGN</p>
</div>
<div class="p-text-2">
<p class="projectOne">Project One</p>
</div>
</div>
<div class="sliderBox"></div>
<div class="projectDetailContainer">
<div class="p-text-3">
<p class="projectDetails"> PROJECT DETAILS</p>
</div>
<div class="viewSlideContainer">
<p class="viewSlides">VIEW SLIDES</p>
</div>
</div>
</div>
</div>
* {
margin: 0;
padding: 0;
}
.container {
height: 721px;
width: 100%;
}
.background {
height: 100%;
width: 100%;
background: black;
display: flex;
align-items: center;
justify-content: center;
}
.sliderBox {
height: 525px;
width: 50%;
display: flex;
background: yellow;
}
.textCon1 {
display: flex;
flex-direction: column;
margin-right: 10px;
}
.brandingDesign {
font-size: 10px;
font-weight: 500;
font-family: "Open Sans";
color: #f7ac53;
letter-spacing: 0.5px;
margin-right: 100px;
padding: 5px;
}
.projectOne {
color: white;
font-family: "Merriweather";
font-size: 39px;
font-weight: 300;
margin-left: 50px;
padding: 5px;
}
.projectDetailContainer {
display: flex;
flex-direction: column;
margin-top: 200px;
margin-left: 100px;
}
.projectDetails {
color: white;
font-family: "Open Sans";
font-weight: 400;
font-size: 11px;
margin-left: 50px;
padding: 10px;
}
.viewSlides {
color: white;
font-family: "Open Sans";
font-weight: 400;
font-size: 11px;
padding: 5px;
}

Inline display of div tags

I am trying to get 2 div tags to be inline with each other. When I did use display: inline-block it doesn't format correctly.
What I am trying to achieve see image
Code:
.packaging_details {
display: block;
border: solid 1px #000;
padding: 10px;
width: 95%;
margin-left: auto;
margin-right: auto;
margin-top: 30px;
margin-bottom: 30px;
}
.heading_texts {
text-transform: uppercase;
font-weight: bold;
font-size: 14px;
text-align: center;
display:inline-block;
}
.Subheading2 {
text-align: center;
font-size: 14px;
text-transform: uppercase;
}
.Subheading1 {
font-weight: bold;
text-transform: uppercase;
text-align: right;
display:inline-block;
}
<div class="packaging_details">
<div class="heading_texts">Company Name</div>
<div class="Subheading1">LABEL</div>
<div class="Subheading2">
<span class="logids">Name:</span> ###
</div>
</div>
It is always good to wrap parts that are supposed to look differently into seperat containers so you can position them differently. Your Problem here is most likely, that "heading_texts", "Subheading1" and "Subheading2" are on the same Layer / in the same position so they will behave similar in the parent Container.
I can offer you one solution: CSS Flex.
The code for that:
.packaging_details {
display: block;
border: solid 1px #000;
padding: 10px;
width: 95%;
margin-left: auto;
margin-right: auto;
margin-top: 30px;
margin-bottom: 30px;
}
.heading_texts {
text-transform: uppercase;
font-weight: bold;
font-size: 14px;
text-align: end;
width: 55%;
}
.Subheading2 {
text-align: center;
font-size: 14px;
text-transform: uppercase;
}
.Subheading1 {
font-weight: bold;
text-transform: uppercase;
text-align: end;
width: 45%;
}
.inline-container{
display: flex;
flex-direction: row;
text-align: center;
justify-content: center;
}
<div class="packaging_details">
<div class="inline-container">
<div class="heading_texts">Company Name</div>
<div class="Subheading1">LABEL</div>
</div>
<div class="Subheading2">
<span class="logids">Name:</span> ###
</div>
</div>
The Only thing you have to be aware of, is that you position the content of the Line by defining the widths of the 2 containers here, so if you want the logo to be more centered you need to play around with the with of both containers, they always need to sum up to 100% width!
You can use flex box as below.
The only challenge by doing it this way is to center align the Subheading2 below heading_text, one way is to set flex-basis 100% of the box width - the width of the Subheading1 element.
This solution is for the case when you don't have access or not able to change html structure but if you can change the html structure that can be easily styled with flex-box.
.packaging_details {
display: flex;
flex-wrap: wrap;
border: solid 1px #000;
padding: 10px;
width: 95%;
margin-left: auto;
margin-right: auto;
margin-top: 30px;
margin-bottom: 30px;
}
.heading_texts {
flex: 1;
text-transform: uppercase;
font-weight: bold;
font-size: 14px;
text-align: center;
}
.Subheading2 {
flex-basis: calc(100% - 55px);
text-align: center;
font-size: 14px;
text-transform: uppercase;
}
.Subheading1 {
font-weight: bold;
text-transform: uppercase;
text-align: right;
}
<div class="packaging_details">
<div class="heading_texts">Company Name</div>
<div class="Subheading1">LABEL</div>
<div class="Subheading2">
<span class="logids">Name:</span> ###
</div>
</div>

Button is not centred even with resolution independent alignment

I have a button on my website, and I used resolution independent alignment. I checked two different computer screens with different resolutions and it looks alright, however, once opened on a larger aspect ratio screen or a mobile/tablet device it re-positions. On a larger aspect ratio screen the buttons moves to the left, on the smaller devices, it floats towards the right. I'm not 100% certain what could of gone wrong. Below is my CSS and HTML code.
HTML:
<div class="Welcome">
<h1>Welcome to Beauty Factory Bookings</h1>
<button>Book Appointment</button>
</div>
CSS:
body {
background: url('http://i.imgur.com/4mKmYMb.jpg') no-repeat fixed center center;
background-size: cover;
font-family: Montserrat;
margin: 0;
padding: 0;
.Welcome {
margin-top: 13%;
}
.Welcome h1 {
text-align: center;
font-family: Montserrat;
font-size: 50px;
text-transform: uppercase;
}
.Welcome button {
height: 60px;
width: 340px;
background: #ff656c;
border: 1px solid #e15960;
color: #fff;
font-weight: bold;
text-transform: uppercase;
font-size: 17px;
font-family: Montserrat;
outline: none;
cursor: pointer;
text-align: center;
margin-left: 33%;
margin-top: 3%;
border-radius: 5px;
}
Any idea how to fix this?
You center a html element, which has the standard position and is a block element with margin-left: auto and margin-right: auto.
It is a convention to write the first letter of classes and ids in lowercase.
Example
Just another way to do it:
I wrap your button in div and center the button using text-align:center
Don't forget to take out margin-left: 33%; in your css
HTML
<div class="Welcome">
<h1>Welcome to Beauty Factory Bookings</h1>
<div class="center">
<button>Book Appointment</button>
</div>
</div>
CSS
.Welcome {
margin-top: 13%;
}
.Welcome h1 {
text-align: center;
font-family: Montserrat;
font-size: 50px;
text-transform: uppercase;
}
.center {
text-align: center;
}
.Welcome button {
height: 60px;
width: 340px;
background: #ff656c;
border: 1px solid #e15960;
color: #fff;
font-weight: bold;
text-transform: uppercase;
font-size: 17px;
font-family: Montserrat;
outline: none;
cursor: pointer;
text-align: center;
margin-top: 3%;
border-radius: 5px;
}
Example: http://codepen.io/anon/pen/EPyjXm
create a new CSS property for a tag and set display to flex(takes up 100% of width; and justify content to center.
EDIT
After checking out your site,
there is a margin-left of 15px coming from somewhere. Get rid of it if that is a possibility or else just add margin-left: 0 to the .Welcome a {...}
.Welcome a {
display: flex;
/*set display mode of anchor to flex*/
justify-content: center; /* Justify content center*/
margin-left: 0; /* Add !important in case it is overwritten*/
}
.Welcome {
margin-top: 13%;
}
.Welcome h1 {
text-align: center;
font-family: Montserrat;
font-size: 50px;
text-transform: uppercase;
}
.Welcome a {
display: flex;
/*set display mode of anchor to flex*/
justify-content: center; /* Justify content center*/
}
.Welcome button {
height: 60px;
width: 340px;
background: #ff656c;
border: 1px solid #e15960;
color: #fff;
font-weight: bold;
text-transform: uppercase;
font-size: 17px;
font-family: Montserrat;
outline: none;
cursor: pointer;
text-align: center;
border-radius: 5px;
}
<div class="Welcome">
<h1>Welcome to Beauty Factory Bookings</h1>
<a href="website_link">
<button>Book Appointment</button>
</a>
</div>

use auto height with float

When use auto height with float its not work but when remove float its work fine. how can fix it ?
there is DIV with auto height (aboutus) ,inside it is another div (aboutus-title p) with float left but the content is overflow how can all content inside div with auto height ?
http://jsfiddle.net/haeb0q8d/1/
.aboutus {
position: relative;
z-index: 2;
width: 100%;
height: auto;
background: #333333;
}
.aboutus-title div h1{
text-align: center;
text-transform: uppercase;
font-size: 24px;
padding-top: 80px;
color: #fcd803;
}
.aboutus-title hr {
margin: 0 auto;
border: 0;
height: 1px;
background: #333;
margin-top: 30px;
width: 60px;
}
.aboutus-detail {
width: 100%;
}
.aboutus-detail p{
text-align: center;
color: #fcd803;
line-height: 25px;
font-size: 17px;
margin-bottom: 30px;
padding-right: 30px;
padding-left: 30px;
float: left;
}
<div class="aboutus" id="aboutus">
<div class="aboutus-title">
<div><h1>about</h1></div>
<hr>
<div class="aboutus-detail">
<p>
We are a tight knit team of digital thinkers, designers and<br>
developers, working together to create fresh, effective projects<br> delivered personally.
</p>
</div>
</div>
</div>
You have to clear float with clear: both.
.aboutus {
position: relative;
z-index: 2;
width: 100%;
height: auto;
background: #333333;
}
.aboutus-title div h1 {
text-align: center;
text-transform: uppercase;
font-size: 24px;
padding-top: 80px;
color: #fcd803;
}
.aboutus-title hr {
margin: 0 auto;
border: 0;
height: 1px;
background: #333;
margin-top: 30px;
width: 60px;
}
.aboutus-detail {
width: 100%;
}
.aboutus-detail p {
text-align: center;
color: #fcd803;
line-height: 25px;
font-size: 17px;
margin-bottom: 30px;
padding-right: 30px;
padding-left: 30px;
float: left;
}
.clear {
clear: both;
}
<div class="aboutus" id="aboutus">
<div class="aboutus-title">
<div>
<h1>about</h1>
</div>
<hr>
<div class="aboutus-detail">
<p>We are a tight knit team of digital thinkers, designers and
<br>developers, working together to create fresh, effective projects
<br>delivered personally.</p>
</div>
</div>
<div class="clear"></div>
</div>