I'm having problems trying to take a piece of text, center it on the page, and have an image on the left and on the right of it.
Keep in mind, I'm only allowed to change CSS code for positioning. The HTML is completely right.
Here html code:
<div id="container">
<div>
<img src="../logo.png" id="header">
</div>
<div>
<img src="../barbecue01.jpg" id="pic_1">
<div id="aboutus">
<h1>About Us</h1>
<p>
Our restaurant has the best barbecue that you can find at Philadelphia.
We have an amazing team just to serve you, your family, and your friends.
</p>
<h1>Try It Now!</h1>
</div>
<img src="../barbecue02.jpg" id="pic_2">
</div>
</div>
And here is my CSS
#container {
width: 75%;
margin: 15px auto 15px auto;
}
* {
background-color: tan;
}
#pic_1 {
position: absolute;
display: inline-block;
float: left;
}
#pic_2 {
position: absolute;
display: inline-block;
float: right;
}
#aboutus {
position: relative;
text-align: center;
height: 275px;
width: 200px;
color: white;
display: inline-block;
left: 275px;
}
div {
border: solid 2px black;
}
The problem I am running into is that the first image is in the right spot, I'm just trying to get the 2nd image to go on the right side. For some reason, it's just not having it. The text is supposed to be centered.
Any help would be greatly appreciate it
I recommend you use flex instead of float, since float is really not meant for layout.
Stack snippet
#container {
width: 75%;
margin: 15px auto;
}
* {
background-color: tan;
}
#container > div:nth-child(2) {
display: flex;
}
#pic_1 {
flex: 1;
}
#pic_2 {
flex: 1;
}
#aboutus {
flex: 1 1 200px;
text-align: center;
height: 275px;
color: white;
}
div {
border: solid 2px black;
}
<div id="container">
<!-- ADD NEW CODE HERE... -->
<div>
<img src="../logo.png" id="header">
</div>
<div>
<img src="../barbecue01.jpg" id="pic_1">
<div id="aboutus">
<h1>About Us</h1>
<p>Our restaurant has the best barbecue that you can find at Philadelphia. We have an amazing team just to serve you, your family, and your friends. </p>
<h1>Try It Now!</h1>
</div>
<img src="../barbecue02.jpg" id="pic_2">
</div>
</div>
Make them display block and float left
#pic_1 {
display: block;
float: left;
width: 33%;
}
#pic_2 {
display: block;
float: left;
width: 33%;
}
#aboutus {
text-align: center;
display: block;
float: left;
width: 33%;
}
Related
Here is my code. Notice that the height of image is bigger than the div which is what I want. What I don't want is that the other div containing text is at the bottom of the image. Is there any way to fix this? Also, I tried margin-bottom to feature_details.
.feature_details {
display: inline-block;
width: 40%;
text-align: left;
margin-bottom: 4rem;
}
.feature_display {
display: inline-block;
width: 50%;
}
.feature_display__img {
width: 100%;
height: auto;
}
<div class="feature_section__feature_left">
<div class="feature_details">
<h3 class="feature_name">Reviews that really matter</h3>
<div class="feature_info">
<p>
Insolvers makes it easy to create rich, high-quality content using
the inbuilt editor.
</p>
<p>
Add images, gifs, and media - draft, experiment, and share with your
peers before scheduling.
</p>
</div>
</div>
<div class="feature_display">
<div class="feature_display__img">
<img src="https://i.pinimg.com/564x/e9/29/1c/e9291cc39e820cd4afc6e58618dfc9e0.jpg" alt="Feature display" />
</div>
</div>
</div>
Add vertical-align: middle; to your image container.
.feature_details {
display: inline-block;
width: 40%;
text-align: left;
margin-bottom: 4rem;
}
.feature_display {
display: inline-block;
width: 50%;
vertical-align: middle;
}
.feature_display__img {
width: 100%;
height: auto;
}
<div class="feature_section__feature_left">
<div class="feature_details">
<h3 class="feature_name">Reviews that really matter</h3>
<div class="feature_info">
<p>
Insolvers makes it easy to create rich, high-quality content using the inbuilt editor.
</p>
<p>
Add images, gifs, and media - draft, experiment, and share with your peers before scheduling.
</p>
</div>
</div>
<div class="feature_display">
<div class="feature_display__img">
<img src="https://i.pinimg.com/564x/e9/29/1c/e9291cc39e820cd4afc6e58618dfc9e0.jpg" alt="Feature display" />
</div>
</div>
</div>
Update You CSS With These Changes
.feature_section__feature_left{
display: grid;
grid-template-columns: repeat(2,minmax(50%,50%));
align-items: center;
}
.feature_details {
display: inline-block;
/* width: 40%; */
text-align: left;
margin-bottom: 4rem;
}
.feature_display {
display: inline-block;
/* width: 50%; */
}
.feature_display__img {
width: 100%;
height: auto;
}
We can also use relative positioning on the details div and push it from the bottom.
.feature_details {
display: inline-block;
width: 40%;
text-align: left;
margin-bottom: 4rem;
position: relative;
bottom: 5rem;
}
.feature_display {
display: inline-block;
width: 50%;
}
.feature_display__img {
width: 100%;
height: auto;
}
I am attempting to create a full-width banner with three internal inline elements. A back link, a logo and a forward link.
I would also like to use the same code to create a full-width banner with TWO internal inline elements. A left back link and a central logo.
What I have so far, is:
HTML
<section id="header-blue">
<div id="header-wrap">
<div class="header-left"><p>1</p></div>
<div class="header-center"><p>2</p><p>2</p><p>2</p><p>2</p></div>
<div class="header-right"><p>3</p><p>3</p></div>
<div class="clearfix"></div>
</div>
</section>
SCSS:
#header-blue {
width: 100%;
margin-bottom: 50px;
height: auto;
background-color: $primary-blue;
color: #fff;
#header-wrap {
text-align: center;
border: 1px solid green;
margin: 0 auto;
padding: 1rem 2.5rem;
div {
display: inline-block;
vertical-align: middle;
}
}
.header-left {
float: left;
border: 1px solid red;
width: 100px;
}
.header-right {
float: right;
border: 1px solid red;
width: 100px;
}
.header-center {
border: 1px solid red;
margin: 0 auto !important;
display: inline-block;
width: 100px;
}
} // header-blue
I am looking for a solution that is widely supported, so I'm not sure if that rules flex out?
The result is this: FIDDLE
EDIT:
THE FINAL CORRECT DESIGN WHEN COMPLETE
Disclaimer: Please understand that although this may be viewed as a 'duplicate' post, after a fair few hours of online research and trial and error, I am still no further progressed. I would, therefore, like to seek help unique to this problem and learn in the process.
You can build the layout with CSS flexbox.
For clarity and conciseness, I removed several non-essential decorative styles from the original code. I also used compiled CSS for the benefit of those who don't use preprocessors.
layout 1: [left] [center] [right]
#header-wrap {
display: flex; /* 1 */
align-items: flex-start; /* 2 */
justify-content: space-between; /* 3 */
text-align: center;
padding: 1rem 0;
}
#header-blue { margin-bottom: 50px; background-color: #3498DB; color: #fff; }
.header-left { border: 1px solid red; width: 100px; }
.header-right { border: 1px solid red; width: 100px; }
.header-center { border: 1px solid red; width: 100px; }
<section id="header-blue">
<div id="header-wrap">
<div class="header-left">
<p>1</p>
</div>
<div class="header-center">
<p>2</p>
<p>2</p>
<p>2</p>
<p>2</p>
</div>
<div class="header-right">
<p>3</p>
<p>3</p>
</div>
</div>
</section>
Notes:
Establish flex container.
Prevent flex items from expanding full height (a default setting). The flex-start value will align each item at the start of the cross axis of the container. In this case, that's the top of the vertical (Y) axis. If you want the items vertically centered, use the center value instead. The default value is stretch.
Align flex items horizontally in the container. You can also try justify-content: space-around. Note that this method will only center the middle item in the container if the left and right elements (the back/forward links) are equal width. If the links vary in length, you'll need to use another method (see boxes #71-78 here).
layout 2: [left] [center]
#header-wrap::after { /* 4 */
content: "";
width: 100px;
}
#header-wrap {
display: flex; /* 1 */
align-items: flex-start; /* 2 */
justify-content: space-between; /* 3 */
text-align: center;
padding: 1rem 0;
}
#header-blue { margin-bottom: 50px; background-color: #3498DB; color: #fff; }
.header-left { border: 1px solid red; width: 100px; }
.header-right { border: 1px solid red; width: 100px; }
.header-center { border: 1px solid red; width: 100px; }
<section id="header-blue">
<div id="header-wrap">
<div class="header-left">
<p>1</p>
</div>
<div class="header-center">
<p>2</p>
<p>2</p>
<p>2</p>
<p>2</p>
</div>
</div>
</section>
Notes:
Use an invisible pseudo-element to create equal balance on the opposite end of the container. This is essentially a replacement for the DOM element that was removed from the first example. It keeps the middle item centered.
jsFiddle
Browser Support
Flexbox is supported by all major browsers, except IE 8 & 9.
Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes.
For a quick way to add all the prefixes you need, use Autoprefixer.
More details in this answer.
From your structure you could use flex(IE11) and justify-content, then hide .clearfix and remove it when on fourth position:
with 3 (4 including clearfix)
#header-wrap {
display: flex;
justify-content: space-between;
}
#header-wrap > div {
border: solid;
width: 100px;
margin:0 0 auto;/* remove if you want each boxes same height */
}
.clearfix:nth-child(4) {
display: none;
}
.clearfix {
opacity: 0;
}
<section id="header-blue">
<div id="header-wrap">
<div class="header-left"><p>1</p></div>
<div class="header-center"><p>2</p><p>2</p><p>2</p><p>2</p></div>
<div class="header-right"><p>3</p><p>3</p></div>
<div class="clearfix"></div>
</div>
</section>
when only 2 (3) same CSS involved
#header-wrap {
display: flex;
justify-content: space-between;
}
#header-wrap > div {
border: solid;
width: 100px;
margin:0 0 auto;/* remove if you want each boxes same height */
}
.clearfix:nth-child(4) {
display: none;
}
.clearfix {
opacity: 0;
}
<section id="header-blue">
<div id="header-wrap">
<div class="header-left"><p>1</p></div>
<div class="header-center"><p>2</p><p>2</p><p>2</p><p>2</p></div>
<div class="clearfix"></div>
</div>
</section>
for older browsers.
with your structure you could use text-align, :after and the selector +:
with 3 (4)
#header-wrap {
text-align: justify;
}
#header-wrap:after {
content: '';
display: inline-block;
width: 99%;
}
#header-wrap > div {
display: inline-block;
vertical-align: top;
border: solid;
width: 100px;
}
#header-wrap > div + div + div +.clearfix {
display: none;
}
.clearfix {
opacity: 0;
}
<section id="header-blue">
<div id="header-wrap">
<div class="header-left"><p>1</p></div>
<div class="header-center"><p>2</p><p>2</p><p>2</p><p>2</p></div>
<div class="header-right"><p>3</p><p>3</p></div>
<div class="clearfix"></div>
</div>
</section>
and 2(3) same CSS involved:
#header-wrap {
text-align: justify;
}
#header-wrap:after {
content: '';
display: inline-block;
width: 99%;
}
#header-wrap > div {
display: inline-block;
vertical-align: top;
border: solid;
width: 100px;
}
#header-wrap > div + div + div +.clearfix {
display: none;
}
.clearfix {
opacity: 0;
}
<section id="header-blue">
<div id="header-wrap">
<div class="header-left"><p>1</p></div>
<div class="header-center"><p>2</p><p>2</p><p>2</p><p>2</p></div>
<div class="clearfix"></div>
</div>
</section>
Consider positioning the left and right elements differently.
https://jsfiddle.net/5gxLvp8a/4/
#header-wrap {
text-align: center;
border: 1px solid green;
margin: 0 auto;
padding: 1rem 2.5rem;
position: relative;
div {
display: inline-block;
vertical-align: middle;
}
}
.header-left {
float: left;
border: 1px solid red;
width: 100px;
position: absolute;
left: 25px;
}
.header-right {
float: right;
border: 1px solid red;
width: 100px;
position: absolute;
right: 25px;
}
See code snippet below:
html, html a {
font-size: 10px; }
#header-blue {
width: 100%;
margin-bottom: 50px;
height: auto;
background-color: #3498DB;
color: #fff; }
#header-blue #header-wrap {
text-align: center;
border: 1px solid green;
margin: 0 auto;
padding: 1rem 2.5rem;
position: relative; }
#header-blue #header-wrap div {
display: inline-block;
vertical-align: middle; }
#header-blue .header-left {
float: left;
border: 1px solid red;
width: 100px;
position: absolute;
left: 25px; }
#header-blue .header-right {
float: right;
border: 1px solid red;
width: 100px;
position: absolute;
right: 25px; }
#header-blue .header-center {
border: 1px solid red;
margin: 0 auto !important;
display: inline-block;
width: 100px; }
.clearfix:after {
content: " ";
visibility: hidden;
display: block;
height: 0;
clear: both; }
<section id="header-blue">
<div id="header-wrap">
<div class="header-left"><p>1</p></div>
<div class="header-center"><p>2</p><p>2</p><p>2</p><p>2</p></div>
<div class="header-right"><p>3</p><p>3</p></div>
<div class="clearfix"></div>
</div>
</section>
<section id="header-blue">
<div id="header-wrap">
<div class="header-left"><p>1</p></div>
<div class="header-center"><p>2</p><p>2</p><p>2</p><p>2</p></div>
<div class="clearfix"></div>
</div>
</section>
Widely supported - my immediate answer is to use display: table;
Let me 'fiddle' around with this for a moment and get back to you - I was just working on something similar yesterday.
EDIT 1:
At first glance, I would advise utilizing classes versus ID's. This deals with a much broader topic (CSS Specificity) but is extremely useful to think about early in your career. That being said, I am working on a solution for you, as I THINK I know what you want.
As the commenter mentioned - it would help ALOT to see what you want to see as an end result. From my interpretation of your screenshots (poor quality & non-descriptive FYI), I feel like you want this header to maintain the left/back button and the logo on mobile devices. However, on a desktop/laptop viewport size, you want a forward button to show itself.
If this is incorrect, please verify!
EDIT 2:
Going off the above poster's JSFiddle, I've come up with a "better" solution that stacks the elements within the header as opposed to going outside of the 'container' that it exists in: https://jsfiddle.net/f815aa6y/1/
Still working on the right solution to get this to vertically align in the middle :)
I'm trying to layout my first site and I'm stuck on positioning two divs in the same line. I have posted an image below showing the layout I am trying to achieve.
This is the code that i have for the 2 divs at the moment.
<div class="full-width">
<div class="logo">
<img src="img/logo.png"/>
</div>
<div class="social">
<ul class="social-icons">
<li><img src="img/facebookSS.png"/></li>
<li><img src="img/twitter.png"/></li>
<li><img src="img/instagramSS.png"/></li>
</ul>
</div>
<div class="address">
<p>Address to go here</p>
</div>
</div>
I have been playing around with the CSS for a little while but just can't seem to get it right.
What I am looking to do is have all the above on one row, with the nav on the row underneath. Hope that makes sense. I am not using any framework like bootstrap so just using my own classes etc.
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body {
font-size: 20px;
font-family: 'Open Sans', sans-serif;
color: #fff;
position: relative;
}
.logo {
width: 300px;
height: auto;
position: relative;
display: inline-block;
}
.logo img {
width: 100%;
height: auto;
}
.social {
display: inline-block;
float: right;
margin-right: 20%;
}
.social li {
display: inline-block;
}
.social li img {
width: 50px;
height: auto;
}
.full-width {
width: 100%;
margin: 0 auto;
position: relative;
text-align: center;
}
You need to create more containers for your div's. Here is a very basic example to explain:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="test.css">
<title></title>
</head>
<body>
<div class="container">
<div id="one"></div>
<div id="two">
<div id="three"></div>
<div id="four"></div>
</div>
</div>
</body>
</html>
The container class would take up the full width of the page and contain everything above your navbar. Div one would be your logo, than div two would be another container in which you could put more divs (three and four) that take up a percentage of the height of div two. Than inside of one of these divs, you would need put your social logos, and the address in the next one so it shows underneath. Here is the CSS:
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.container {
width: 100%;
}
#one {
height: 300px;
width: 300px;
background-color: green;
float: left;
margin-left: 25%;
}
#two {
height: 300px;
width: 500px;
float: left;
margin-left: 10%;
}
#three {
height: 30%;
width: 100%;
background-color: yellow;
}
#four {
height: 70%;
width: 100%;
background-color: blue;
}
This is just a very basic example, only to be used as a concept for your idea. Obviously remove the cheesy background colors and modify
Updated:
I created a div with the class .top that has a defined width, which allows you to center anything within it with margin:auto;. I created a section around your social icons and floated it right. This is a better example than my previous one because here the logo is centered.
I hope this helps: https://jsfiddle.net/0sptpx0j/3/
Hi guys thanks for all the advice, i decided after reading about absolute positioning to go down that route. this is what i have come up with.
<div class="full-width">
<div class="logo">
<img src="img/logo.png"/>
</div>
<div class="social">
<div class="social-list">
<ul class="icons">
<li><img src="img/facebookSS.png"/></li>
<li><img src="img/twitterSS.png"/></li>
<li><img src="img/instagramSS.png"/></li>
</ul>
</div>
<div class="address">
<p>Address goes in here</p>
</div>
</div>
</div>
.logo {
width: 300px;
height: auto;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.logo img{
width: 100%;
height: auto;
}
.social {
float: right;
width: 300px;
}
.social-list {
width: 100%;
}
.icons {
list-style: none;
padding: 0;
}
.icons li {
display: inline-block;
margin-right: 10px;
}
.icons img {
width: 50px;
height: auto;
}
.full-width {
width: 100%;
margin: 0 auto;
position: relative;
text-align: center;
}
I'm new to CSS, and I've looked for help in the previous forums on this issue. I think I'm doing everything right but my floated elements are being yanked to the left.
Here is my code:
div {
display: block;
}
.grid {
width: 660px;
position: relative;
float: left;
padding-bottom: 10px;
clear: left;
}
.home {
text-align: center;
float: left;
width: 33.3333333%;
position: relative;
padding: 25px;
}
.third {
display: inline-block;
position: relative;
float: left;
height: 150px;
width: 150px;
border-radius: 25px;
}
.third img {
float: left;
position: relative;
}
And my html:
<div class="grid">
<article class="home">
<article class="third">
<img src="" /></article>
</article>
<article class="home">
<article class="third">
<img src="" /></article>
</article>
<article class="home">
<article class="third">
<img src="" /></article>
</article>
</div>
Help please!
I can't comment yet…
Your original code on fiddle
The problem come from padding in .home class.
I have disabled padding:25px; here in .home class, because padding is added to width in CSS:
The modified version (without padding) on fiddle
Now it's not "pulled too far on the left".
What you can do instead, is to add margin:25px; to .third class, like this:
The modified version (with margin) on fiddle
EDIT: A CLEAN REVISITED VERSION:
The HTML code:
<div class="grid">
<article class="home">
<div class="third">
<img src="http://lorempicsum.com/nemo/350/200/1" />
</div>
</article>
<article class="home">
<div class="third">
<img src="http://lorempicsum.com/futurama/350/200/6" />
</div>
</article>
<article class="home">
<div class="third">
<img src="http://lorempicsum.com/up/350/200/6" />
</div>
</article>
</div>
The CSS code:
.grid {
width: 660px;
position: relative;
float: left;
padding-bottom: 10px;
clear: left;
}
.home {
text-align: center;
float: left;
width: 33.3333333%;
}
.third {
display:table-cell;
height: 150px;
width: 150px;
padding: 25px;
border-radius:25px;
vertical-align:middle;
background-color:#eee; //added just for test display
}
.third img {
border:none;
width: 100%;
height: auto;
}
The result here on fiddle.
Images are adaptative, centered vertically and horizontally.
The .third class have a light grey background color just for testing and displaying the curved borders and the centered images inside it.
I have also replaced in html code, the second <article> tag by a <div> tag, because it is redundant.
Please use the updated code I think it will work.
div {
display: block;
}
.grid {
width: 660px;
position: relative;
float: left;
padding-bottom: 10px;
clear: left;
}
.home {
text-align: center;
float: left;
width: 33.3333333%;
position: relative;
padding: 25px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.third {
display:block;
border-radius: 25px;
}
.third img {
width:100%;
height:auto;
}
Here's a possible correction of your code to you :
See this fiddle
I've changed a little the HTML structure like this :
<section class="home">
<article class="third">
<img src="http://lorempicsum.com/futurama/350/200/1" />
</article>
<article class="third">
<img src="http://lorempicsum.com/futurama/350/200/1" />
</article>
<article class="third">
<img src="http://lorempicsum.com/futurama/350/200/1" />
</article>
</section>
It's better for semantic to have section around article and not article around article.
I've simplify the CSS code like this :
section.home {
width: 660px;
position: relative;
float: left;
padding-bottom: 10px;
clear: left;
}
article.third {
text-align: center;
float: left;
width: 150px;
position: relative;
padding: 25px;
overflow: hidden;
}
.third img {
border-radius: 25px;
width: 100%;
position: relative;
}
If you use fluid width for container, then use fluid width for padding/margin of article.
In that case, i use fixed width of the container and for padding values.
How can I center my h1 tag into the middle of my banner without setting a padding?
HTML
<div class="banner">
<div class="bannerContainer">
<h1>Group Title</h1>
</div>
</div>
CSS
.banner {
height: 500px;
width: 100%;
background-color: #000;
}
.bannerContainer {
}
Can you do something with vertical-align: middle; and display: table-cell; etc?
There's several options, I recommend looking through this - https://css-tricks.com/centering-css-complete-guide/
One option would be: http://jsfiddle.net/dtq7fed3/ which uses a line-height on the container that is the same of the height of the banner.
.banner {
height: 500px;
width: 100%;
background-color: #000;
}
.bannerContainer {
text-align: center;
line-height: 500px;
color: #fff;
}
This only works if the banner height is going to remain stagnant
Using transform, you can position in centrally like so: http://jsfiddle.net/otghf6zo/1/
adding this code will position the title in the exact middle of the containing div regardless of size.
h1 {
color: #fff;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
You can use CSS table like this
.banner {
height: 500px;
width: 100%;
background-color: #000;
display: table;
text-align: center;
color: white;
}
.bannerContainer {
display: table-cell;
vertical-align: middle;
}
<div class="banner">
<div class="bannerContainer">
<h1>Group Title</h1>
</div>
</div>
Or Flexbox like this
.banner {
height: 500px;
width: 100%;
background-color: #000;
display: flex;
color: white;
align-items: center;
justify-content: center;
}
<div class="banner">
<div class="bannerContainer">
<h1>Group Title</h1>
</div>
</div>