I am having trouble fixing/anchoring some slogan text to a website header. I get it to the position i want but when the page is resized the text is no longer in the position i want it. Can this be done in html and css or does the text have to be put onto the actual image?
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
background-color: #fff;
color: #555;
font-family: 'Lato', 'Arial'. 'sans-serif';
font-weight: 300;
font-size: 20px;
text-rendering: optimizeLegibility;
}
.row {
max-width: 1140px;
margin: 0 auto;
}
header {
background-image: url(img/view3.jpg);
height: 100vh;
background-size: cover;
background-position: center;
}
.header-text-box {
position: absolute;
width: 1140px;
top: 50%;
left: 30%;
color: white;
}
<link href="https://fonts.googleapis.com/css?family=Lato:100,300,300i,400" rel="stylesheet">
<nav>
<div class="row">
<a href="index.html">
<img src="https://static1.squarespace.com/static/524263b4e4b0adb01903b572/t/575edefe86db433ce0efcf9b/1465835270342/" alt="developer logo" class="avatar">
</a>
<ul class="main-nav">
<li>About</li>
<li>Projects</li>
<li>Blog</li>
</ul>
</div>
</nav>
<div class="header-text-box">
<h1>The header text i</h1>
<p>want goes here.</p>
</div>
Would this solution be considered bad practice?
header {
background-image: url(img/test.jpg);
height: 100vh;
background-size: cover;
background-position: center;
width: 100vw;
}
.header-text-box {
position: absolute;
width: 1140px;
top: 50vh;
left: 30vw;
color: white;
}
Try using vh and vw for your positioning. % is a bit funky especially with height.
.header-text-box {
top: 50vh;
left: 30vw;
}
Also be sure to set
header {
position: relative;
}
so the .header-text-box will follow the header tag instead of the body tag
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
background-color: #fff;
color: #555;
font-family: 'Lato', 'Arial'. 'sans-serif';
font-weight: 300;
font-size: 20px;
text-rendering: optimizeLegibility;
}
.row {
max-width: 1140px;
margin: 0 auto;
}
header {
background-image: url(https://static1.squarespace.com/static/524263b4e4b0adb01903b572/t/575edefe86db433ce0efcf9b/1465835270342/);
height: 100vh;
background-size: cover;
background-position: center;
display: flex; /* this is the magic css */
justify-content:center; /* this is the magic css */
align-items:center; /* this is the magic css */
}
.header-text-box {
color: white;
}
<link href="https://fonts.googleapis.com/css?family=Lato:100,300,300i,400" rel="stylesheet">
<nav>
<div class="row">
<header>
<div class="header-text-box">
<h1>The header text i</h1>
<p>want goes here.</p>
</div>
</header>
<ul class="main-nav">
<li>About</li>
<li>Projects</li>
<li>Blog</li>
</ul>
</div>
</nav>
Not sure if this is what you are going for. But I am assuming you want the header text centered in the header image. I try to avoid absolute position as it tends to be messy.
for this type of stuff I like to use flex
here is a fun way to master flex: http://flexboxfroggy.com/
flexbox w background image and vmin
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
background-color: #fff;
color: white;
}
header {
background-image: url(https://static1.squarespace.com/static/524263b4e4b0adb01903b572/t/575edefe86db433ce0efcf9b/1465835270342);
height: 100vh;
background-size: cover;
background-position: center;
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-content: center;
align-items: center;
}
header > h1 {
font-size: 12vmin;
flex: 0 1 auto;
align-self: auto;
}
header > p {
font-size: 6vmin;
flex: 0 1 auto;
align-self: auto;
}
<header>
<h1>The header text</h1>
<p>subtext this is subtext</p>
</header>
Related
I'm working on a project and my CSS doesn't seem to be cascading correctly.
I have set some styles (mainly the color property) at the HTML tag level. However it is not cascading to the rest of my elements, specifically the .logo class, but also some other classes. Can anyone help me understand why this is happening?
Here is my code:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-family: Helvetica, Arial, sans-serif;
color: seashell;
background-color: black;
text-align: center;
font-size: 22px;
opacity: 0.9;
}
/* Header and Nav Bar */
.header {
display: flex;
justify-content: space-between;
align-items: center;
height: 69px;
width: 100%;
position: fixed;
top: 0;
z-index: 10;
border-bottom: 1px solid seashell;
}
.logo {
margin-left: 10px;
max-height: 50px;
}
.navigation {
display: flex;
list-style: none;
}
.navigation a {
padding: 15px;
}
.navigation a:hover {
text-decoration: none;
}
/******* Hero *******/
#hero {
background-image: url(/Images/img-mission-background.webp);
background-size: cover;
background-repeat: no-repeat;
background-position: center;
display: flex;
flex-direction: column;
justify-content: center;
position: relative;
top: 70px;
height: 700px;
width: 1200px;
margin: auto;
opacity: 0.8;
}
#hero h2,
h4 {
padding: 12px;
}
/***** Content ******/
.content {
position: relative;
top: 100px;
}
<!--******* Header ******* -->
<header class="header">
<img src="/Images/img-tea-cozy-logo.webp" alt="Tea Cozy Logo" class="logo" />
<ul class="navigation">
<li>Mission</li>
<li>Featured Tea</li>
<li>Locations</li>
</ul>
</header>
<section id="hero">
<h2>Our Mission</h2>
<h4>
Handpicked, Artisanally Curated, Free Range, Sustainable, Small Batch, Fair Trade, Organic Tea
</h4>
</section>
<section class="content">
<h1>Tea of the Month</h1>
</section>
My header should be fixed on the page so i couldn't use float:right;. I'm %150 newbie around here. Logo should be on right side of the navbar and also responsive. I tried margin, float and other flex properties. I'm just going to be mad. Where is the mistake.
#import url('https://fonts.googleapis.com/css2?family=Oswald&display=swap');
body {
margin: 0;
font-family: 'Trebuchet MS', sans-serif;
background-color: #efefef;
}
.wrapper {
position: relative;
}
.header-logo {
width: 20vw;
height: 20vw;
}
header {
width: 100%;
height: 4rem;
background: #609F92;
position: fixed;
display: flex;
font-family: Oswald, sans-serif;
font-size: 1.5rem;
}
#header-img {
width: 100%;
height: 100%;
border-radius: 50%;
box-shadow: 1px 1px 2px 1px;
}
#nav-bar {
display: flex;
flex-direction: row;
}
#nav-bar ul {
display: flex;
flex-direction: row;
align-items: center;
list-style: none;
}
#nav-bar li {
margin: 10px;
}
<div class="wrapper">
<header id="header">
<nav id="nav-bar">
<ul>
<li>Features</li>
<li>About Us</li>
<li>Contact</li>
</ul>
<div class="header-logo">
<img id="header-img" src="https://thumbnails-photos.amazon.com/v1/thumbnail/lFJOXJpuTKGgtJYa9-wScA?viewBox=943%2C943&ownerId=A4PYAHHROL8LR&groupShareToken=OSTx_M1GRRS1y_rPWtVfGA.8mpQdgJWAet53NrSPN2TyS">
</div>
</nav>
</header>
</div>
The issue is mainly caused because you nesting so many flexboxes within each other. As such the elements will not span the entire available width automatically.
Give the nav tag a width of 100% to fill out the entire containers width: #nav-bar { width: 100% }
to align the logo to the right within a flexbox use margin-left: auto: .header-logo { margin-left: auto; }
Also you could improve your code by removing the ID from the nav element and target the nav element directly. As semantically you should only have one nav element it would be unecessary to asign an id to it. Same rule also counts for the header element.
Then you could remove display: flex; from the header which has only one child element in the first place and as such is useless. IMHO it would be smarter though to close the nav with the ul as the logog is semantically not part of the navbar.
Last but not least you could remove flex-direction: row as it is the default value anyways.
#nav-bar {
width: 100%;
}
.header-logo {
margin-left: auto;
}
/* original CSS */
#import url('https://fonts.googleapis.com/css2?family=Oswald&display=swap');
body {
margin: 0;
font-family: 'Trebuchet MS', sans-serif;
background-color: #efefef;
}
.wrapper {
position: relative;
}
.header-logo {
width: 20vw;
height: 20vw;
}
header {
width: 100%;
height: 4rem;
background: #609F92;
position: fixed;
display: flex;
font-family: Oswald, sans-serif;
font-size: 1.5rem;
}
#header-img {
width: 100%;
height: 100%;
border-radius: 50%;
box-shadow: 1px 1px 2px 1px;
}
#nav-bar {
display: flex;
flex-direction: row;
}
#nav-bar ul {
display: flex;
flex-direction: row;
align-items: center;
list-style: none;
}
#nav-bar li {
margin: 10px;
}
<div class="wrapper">
<header id="header">
<nav id="nav-bar">
<ul>
<li>Features</li>
<li>About Us</li>
<li>Contact</li>
</ul>
<div class="header-logo">
<img id="header-img" src="https://thumbnails-photos.amazon.com/v1/thumbnail/lFJOXJpuTKGgtJYa9-wScA?viewBox=943%2C943&ownerId=A4PYAHHROL8LR&groupShareToken=OSTx_M1GRRS1y_rPWtVfGA.8mpQdgJWAet53NrSPN2TyS">
</div>
</nav>
</header>
</div>
I also created a Codepen for you where I corrected the code to be semantically correct and to shroten it to the necessary lines: Codepen
I'm trying to place 3 images on the same row using flexbox. I also want to change the size of the first image as it is too big. However, my CSS rules are not being applied properly. I have the images each inside their own div with a class of flex-item-3 to indicate that they are flex items inside my flex container. However, I can't seem to override the default CSS rules.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
font-family: 'Lato', 'sans-serif', 'arial';
color: #fff;
font-size: 15px;
font-weight: 300;
text-transform: uppercase;
text-rendering: optimizeLegibility;
}
.wrapper {
max-width: 100%;
margin: 0 auto;
}
section {
padding: 80px 0;
}
h2 {
margin: 0;
padding: 0;
}
.flex {
display: flex;
justify-content: space-between;
background-image: linear-gradient(rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6)), url("img/truck.jpg");
background-size: cover;
background-position: center;
}
.flex-item {
padding-top: 15px;
height: 100vh;
}
.item1 {
padding-top: 0;
padding-left: 30px;
}
.logo {
height: 90px;
}
.main-nav ul {
display: flex;
margin: 0;
padding: 0;
list-style: none;
}
.main-nav a {
color: white;
padding: 1rem;
text-decoration: none;
}
.main-nav a:hover {
color: orange;
}
.hero {
position: absolute;
top: 50%;
left: 25%;
transform: translate(-50%, -50%);
}
.hero a {
text-decoration: none;
color: white;
display: block;
margin: 10px;
padding: 10px;
border-radius: 10px;
}
.btn {
background-color: orange;
}
.flex2 {
display: flex;
justify-content: center;
align-items: center;
}
.flex-item-2 {
color: #312c2c;
width: 100%;
text-align: center;
}
.second-flex-item p {
margin-top: 20px;
margin-bottom: 40px;
font-size: 19px;
color: #312c2c;
}
.second-flex-item h2 {
font-weight: 900;
font-size: 25px;
}
.flex-3 {
display: flex;
justify-content: center;
max-width: 100%;
}
.flex-3-items {
display: flex;
width: 100%;
height: auto;
}
<body class="wrapper">
<header class="flex">
<div class="flex-item item1">
<img src="Resources/img/moBurgerzLogo.png" class="logo">
</div>
<nav class="flex-item main-nav">
<ul>
<li>Our Story</li>
<li>Locations</li>
<li>Menu</li>
<li>Order now</li>
</ul>
</nav>
<div class="hero">
<h2>Best Burgers <br> In DA City(D.C)</h2>
<a href="#" class="btn" btn-full>Order now!</a>
<a href="#" class="btn" btn-ghost>View Menu!</a>
</div>
</header>
<section class="flex-2">
<div class="flex-item-2 second-flex-item">
<h2>Established in 2017 in D.M.V</h2>
<p class="story"> moBurgerz was founded in 2017 by owner Mahamed Ibrahim.<br> Since then we have been serving the best hand crafted burgers in the D.M.V.<br>All our meat is halal and all of our ingridients are organic.</p>
<img src="Resources/img/white_truck.jpeg" class="truck-img">
</div>
</section>
<section class="flex-3">
<div class="flex-3-item meal-photos photo-1">
<img src="Resources/img/cheeseSteak.jpg">
</div>
<div class="flex-3-item meal-photos">
<img src="Resources/img/goodMoanin.jpeg">
</div>
<div class="flex-3-item meal-photos">
<img src="Resources/img/moGul.jpeg">
</div>
</section>
</body>
.flex-3-item img { max-width:100%; } should do the trick.
Also, look at this: Auto Resize Image in CSS FlexBox Layout and keeping Aspect Ratio?
Try this
.logo {
width: 100%;
height: auto;
}
I am trying to get a banner with "slogan" and an image to appear inside the header area. When I write it out it appears below the header section, and I cannot figure out how to get it inside the header area.
I would also like to make it responsive. So I would like the "slogan" to move up and the image to move below the "slogan" when it reaches less than 768px
Thanks
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Cabin', sans-serif;
}
/* Header */
.header-image {
background-image: url('https://images.unsplash.com/photo-1472376758045-62f519fc676d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8300925c51f6d651fa7cb9c41eaafb45&auto=format&fit=crop&w=2850&q=80');
background-position: center;
background-size: cover;
background-attachment: scroll;
color: white;
width: 100%;
height: 49em;
}
.header-wrapper {
display: grid;
grid-template-columns: 1fr 1fr;
width: 100%;
height: 49em;
}
.logo {
grid-column: 1;
display: flex;
justify-content: flex-start;
margin-left: .5em;
}
.nav-header {
grid-column: 2;
list-style: none;
display: flex;
justify-content: space-around;
}
/* Banner */
.slogan,
.banner {
margin: 0;
}
.banner-image {
width: 15em;
height: 15em;
border-radius: 50%;
}
<!-- HEADER -->
<header class="header-image">
<div class="header-wrapper">
<h1 class="logo">STARTUP NAME</h1>
<nav>
<ul class="nav-header">
<li class="navmenu">Services</li>
<li class="navmenu">Pricing</li>
<li class="navmenu">Contact</li>
</ul>
</nav>
</div>
</header>
<!-- BANNER -->
<section class="main-banner">
<div class="banner-container">
<h1 class="slogan banner">Some Cool Slogan</h1>
<span class="slogan banner">A cool subtitle explaining said cool Slogan</span>
<img class="banner-image banner" src="https://images.unsplash.com/photo-1532007271951-c487760934ae?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=f7ad947fb913065733cd1494ed95ef59&auto=format&fit=crop&w=1100&q=80" alt="">
</div>
</section>
I adjusted the code to have a wrapper around the title and to have it at the center of the page. And added media query CSS to come one below the other in mobile
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Cabin', sans-serif;
}
/* Header */
.header-image {
background-image: url('https://images.unsplash.com/photo-1472376758045-62f519fc676d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8300925c51f6d651fa7cb9c41eaafb45&auto=format&fit=crop&w=2850&q=80');
background-position: center;
background-size: cover;
background-attachment: scroll;
color: white;
width: 100%;
height: 49em;
}
.header-wrapper {
display: grid;
grid-template-columns: 1fr 1fr;
width: 100%;
height: 49em;
}
.logo {
grid-column: 1;
display: flex;
justify-content: flex-start;
margin-left: .5em;
}
.nav-header {
grid-column: 2;
list-style: none;
display: flex;
justify-content: space-around;
}
/* Banner */
.slogan,
.banner {
margin: 0;
}
.banner-image {
width: 15em;
height: 15em;
border-radius: 50%;
}
section.main-banner {
position: absolute;
width:100%;
text-align:center;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.sloganWrapper{
display:inline-block;
vertical-align: middle;
}
img.banner-image.banner {
vertical-align: middle;
}
#media only screen and (max-width:767px){
.sloganWrapper{
display:block;
}
}
<!-- HEADER -->
<header class="header-image">
<div class="header-wrapper">
<h1 class="logo">STARTUP NAME</h1>
<nav>
<ul class="nav-header">
<li class="navmenu">Services</li>
<li class="navmenu">Pricing</li>
<li class="navmenu">Contact</li>
</ul>
</nav>
</div>
</header>
<!-- BANNER -->
<section class="main-banner">
<div class="banner-container">
<div class="sloganWrapper">
<h1 class="slogan banner">Some Cool Slogan</h1>
<span class="slogan banner">A cool subtitle explaining said cool Slogan</span></div>
<img class="banner-image banner" src="https://images.unsplash.com/photo-1532007271951-c487760934ae?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=f7ad947fb913065733cd1494ed95ef59&auto=format&fit=crop&w=1100&q=80" alt="">
</div>
</section>
I think it's because your .main-banner is not being contained in the header.
Try making header position: absolute; and then move the banner image to .main-banner
See if it's what you need...
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Cabin', sans-serif;
}
/* Header */
.header-wrapper {
position: absolute;
top: 0;
left: 0;
display: grid;
grid-template-columns: 1fr 1fr;
width: 100%;
color: #fff;
}
.logo {
grid-column: 1;
display: flex;
justify-content: flex-start;
margin-left: .5em;
}
.nav-header {
grid-column: 2;
list-style: none;
display: flex;
justify-content: space-around;
}
/* Banner */
.main-banner {
padding-top: 100px;
background-image: url('https://images.unsplash.com/photo-1472376758045-62f519fc676d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8300925c51f6d651fa7cb9c41eaafb45&auto=format&fit=crop&w=2850&q=80');
background-position: center;
background-size: cover;
background-attachment: scroll;
color: white;
width: 100%;
height: 49em;
margin-bottom: 20px;
}
.slogan,
.banner {
margin: 0;
}
.banner-image {
width: 15em;
height: 15em;
border-radius: 50%;
}
<!-- HEADER -->
<header>
<div class="header-wrapper">
<h1 class="logo">STARTUP NAME</h1>
<nav>
<ul class="nav-header">
<li class="navmenu">Services</li>
<li class="navmenu">Pricing</li>
<li class="navmenu">Contact</li>
</ul>
</nav>
</div>
</header>
<!-- BANNER -->
<section class="main-banner">
<div class="banner-container">
<h1 class="slogan banner">Some Cool Slogan</h1>
<span class="slogan banner">A cool subtitle explaining said cool Slogan</span>
<img class="banner-image banner" src="https://images.unsplash.com/photo-1532007271951-c487760934ae?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=f7ad947fb913065733cd1494ed95ef59&auto=format&fit=crop&w=1100&q=80" alt="">
</div>
</section>
Testing
I'm sure I have made some kind of really obvious stupid mistake here but it's been bugging me for a while now and I just cannot figure it out no matter where I look.. I am just practicing with a very basic, boring site and I need the image to be responsive in the section underneath the header, but for some reason no matter what I do, I just can't get it to work.
Sorry if this has been answered, I had a look and couldn't find anything exactly the same. If anyone could shed some light on this for me I would be very appreciative. Thanks in advance.
HTML:
<html>
<body>
<header>
<div class="container">
<div class="brand">
<img class="brandImg" src="https://photos-1.dropbox.com/t/2/AACo_XuN80WW6m3RltLuUDD-Koivyw205OCV55h43hevVQ/12/184045382/png/32x32/1/_/1/2/network.png/EKPi4YsBGMICIAIoAg/u6N5dEYvNDRNysVhT6Arx-eKOa64tOkilzRp8K3e93Y?preserve_transparency=1&size=1600x1200&size_mode=3" alt="Brand Image">
<h3 class="mainTitle">Network Solutions</h3>
</div>
<nav>
<ul class="navBar">
<li>About |</li>
<li>Portfolio |</li>
<li>Contact</li>
</ul>
</nav>
</div>
</header>
<section id="showcase">
<div class="heroImg">
<div class="container">
</div>
</div>
</section>
</body>
</html>
CSS:
/* MAIN STYLES */
.container {
max-width: 60%;
margin: 0 auto;
}
/* HEADER */
header {
height: 100px;
border-bottom: 2px solid black;
border-top: 2px solid black;
}
.brand {
float: left;
display: inline;
margin-top: 20px;
}
.brandImg {
height: 4em;
width: 4em;
float: left;
margin-bottom: 5px;
}
.mainTitle {
font-family: 'IBM Plex Mono', monospace;
line-height: 1.2em;
position: relative;
left:8px;
top: -5px;
}
/* MAIN NAVIGATION */
ul {
float: right;
}
li {
margin-top: 15px;
float: left;
display: inline;
font-size: 40px;
padding-left: 15px;
}
a {
color: #000;
text-decoration: none;
}
a:hover {
color: gray;
}
/* HERO SECTION */
#showcase {
width: 100%;
height: 100%;
}
.heroImg {
background-image: url("https://photos-1.dropbox.com/t/2/AADgiVKPX---q_yzz3R6QXjMuvUF9x1suRGMjLMV8QkZVQ/12/184045382/jpeg/32x32/1/_/1/2/hero.jpeg/EKPi4YsBGMICIAIoAg/-UTK8zUqda_wA3F-VrZAdIZvo84OHHGWCbLEcdCi1K8?size=1600x1200&size_mode=3");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
Codepen link
The logo image in your pen is throwing a 403 error. Generally speaking, you can make an image responsive with the following CSS:
img {
display: inline-block;
max-width: 50px; /* Width of your image */
max-height: 50px; /* Height of your image */
width: 100%;
height: auto;
}
Here is a JSFiddle example. Drag the window to see how the image reacts.
img {
display: inline-block;
max-width: 650px;
max-height: 250px;
width: 100%;
height: auto;
}
<img src="http://via.placeholder.com/650x250" alt="Stack Overflow Test Image" />
Here's another example using Flexbox making the image responsive relative to another element:
#test {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
img {
display: inline-block;
max-width: 250px;
max-height: 150px;
width: 100%;
height: auto;
}
h1 {
margin-left: 15px;
-webkit-flex: none;
-ms-flex: none;
flex: none;
}
<div id="test">
<div id="logo">
<img src="http://via.placeholder.com/250x150" alt="Stack Overflow Test Image" />
</div>
<h1>This is a Test Title</h1>
</div>