Proper CSS for aligning multiple divs - html

Okay so I am trying to build a main header for multiple web pages that I am building. In this header, I have an image, text center below the image, (all to the left of my main header), a main header which I want centered in the page, and two links (home, and contact us) which will be placed to the right of the main heading. Here is my HTML and CSS so far:
<div class="header">
<div class="logoHeader">
<img src="6.jpg">
<p> My Image Header </p>
</div>
<h3> Page Header </h3>
Home Page
Contact Us
</div>
div.logoHeader {
width: 125px;
}
.logoHeader img {
margin: auto;
display: block;
}
.logoHeader p {
margin: auto;
font-size: 70%;
text-align: center;
font-weight: bold;
}
This correctly centers the text below the image, but now I don't know how to align my header to the center of the page, links to the right of the header, and then finally aligning the header div to center. When all is said and done, I want it to look like this:
IMAGE
Image Main Header (centered within middle of img) 2 links
(text centered under image)
All of this being centered in my window. Can anyone help me out with this? Any help would be greatly appreciated.

There are many ways to do this, but here is one way. I float the left (the logo and name) and right (the links) to opposite sides then use text-align:center and display:inline to center the page header
/* CSS */
.header {
background:grey;
display:inline-block;
width:100%;
text-align: center;
}
.logoHeader {
width: 125px;
float:left;
}
.pageHeader {
display: inline;
margin: 0 auto;
}
.headerLinks {
float:right;
}
.logoHeader img {
margin: auto;
display: block;
}
.logoHeader p {
margin: auto;
font-size: 70%;
text-align: center;
font-weight: bold;
}
/* HTML (slightly changed) */
<div class="header">
<div class="logoHeader">
<img src="6.jpg" />
<p>My Image Header</p>
</div>
<h3 class='pageHeader'>Page Header</h3>
<div class='headerLinks'>
Home Page
Contact Us
</div>
</div>
Demo here
EDIT
If you want it evenly spaced vertically as well and not use hard coded paddings and such to do so, you could change the setup to use a table format instead. You'll have to play with the width of each section and such to get it exactly the way you want it. I'm using the same HTML as above
.header {
background:grey;
vertical-align:middle;
width:100%;
padding:15px;
display:table
}
.header * {
display:table-cell;
width:33.3%; /* 100 / number of sections */
vertical-align:middle;
}
.logoHeader img {
margin: 0 auto;
}
.logoHeader p {
display:block;
margin:0 auto;
font-size: 70%;
text-align: center;
font-weight: bold;
}
.pageHeader {
text-align:center;
}
.headerLinks {
text-align:center;
}
Demo of tabular approach
For something like this nav a table isn't a poor decision, but some people recommend never using tables for layout, so you could also use flexbox's display:flex; justify-content:space-around instead if you'd like

Related

How to move a p tag up within a div tag? [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Vertically align text next to an image?
(26 answers)
Closed 2 years ago.
I am currently working on a navigation bar for my website, and am super new to CSS and HTML.
I have a header split up into two divs, one for the top half displaying the logo, title, and login page, and another for the navigation buttons. I am running into issues, where my p tag leaves the div that it is inside of, and gets in the way of my navigation buttons. Here is an image of the issue:
[1]: https://i.stack.imgur.com/MoSsk.png
What you are looking at is the inspect element view of a p tag next to an image tag, both within a div.
Here is some html:
header{
background-color: #B71C1C;
color: #FFFFFF;
position:fixed;
top:0;
left:0;
width:100%;
height:150px;
margin-left: 0px;
margin-right: 0px;
}
#bar_logo{
max-height:90px;
max-width:90px;
height:auto;
width:auto;
margin:0;
padding:0;
}
.upper_banner{
width:100%;
height:60%;
margin: 0;
padding: 0;
}
#title_banner{
margin:0;
padding:0;
display: inline;
height:60%;
font-size: 45px;
font-family: Helvetica;
text-align:center;
top: -20;
}
#login{
float: right;
margin:10px;
}
.lower_banner{
width: 100%;
height: 40%;
margin:0;
padding:0;
}
<header>
<div class="upper_banner">
<img id="bar_logo" src="https://picsum.photos/100/100" alt="Title">
<p id="title_banner">
Title of Website
</p>
<a id="login" href="login.html">login</a>
</div>
</header> <!-- added by Rickard -->
I am having some troubles working out this issue, as when I do highlighting over the div "upper_banner" it doesn't highlight past my image on the far right. As you can see, I was attempting to use
top: -20;
in an attempt to move it up 20 px, but no difference. I have a feeling that this issue is because my p tag is starting at a certain point, and because my height is set to a fixed pixel count, it is going down that number of pixels, which leads to it overflowing into the div below.
Apologies for the wicked beginner question, super new to all of this.
Try adding the following in .upper_banner class element's CSS.
display:flex;
justify-content: center;
For more clarity, do search around and study flex boxes and flex alignments. It could help you a lot!
All elements have position: static as default. If you want to move elements around with top or left, you need to change the positioning to relative, absolute or fixed.
Elements align normally at the bottom. If you want a different alignment, you can use vertical-align. It's today better to use display: flex and then control where the elements are with align-items: center or justify-content: center.
You should rarely use float or position: absolute because that makes the elements loose their height, and can mess up the rest of the design.
Inline-elements adjust their width and height to the content, so it doesn't make any sense to give them a height (like your #title_banner).
Here is the code I added, where I target all (*) elements that are children (>) to .upper_banner.
.upper_banner > * {
vertical-align: middle;
}
header{
background-color: #B71C1C;
color: #FFFFFF;
position:fixed;
top:0;
left:0;
right:0;
height:150px;
margin-left: 0px;
margin-right: 0px;
}
#bar_logo{
max-height:90px;
max-width:90px;
height:auto;
width:auto;
margin:0;
padding:0;
}
.upper_banner{
width:100%;
margin: 0;
padding: 0;
}
/* ADDED */
.upper_banner > * {
vertical-align: middle;
}
#title_banner{
margin:0;
padding:0;
display: inline;
font-size: 45px;
font-family: Helvetica;
}
#login{
float: right;
margin:10px;
}
.lower_banner{
width: 100%;
height: 40%;
margin:0;
padding:0;
}
<header>
<div class="upper_banner">
<img id="bar_logo" src="https://picsum.photos/100/100" alt="Title">
<p id="title_banner">
Title of Website
</p>
<a id="login" href="login.html">login</a>
</div>
</header>
Here is an example, where I used flex. Comments are within the code:
header{
background-color: #B71C1C;
color: #FFFFFF;
position:fixed;
top:0;
left:0;
height:150px;
margin-left: 0px;
margin-right: 0px;
/* ADDED */
right: 0;
}
#bar_logo{
max-height:90px;
max-width:90px;
height:auto;
width:auto;
margin:0;
padding:0;
}
.upper_banner{
/*width:100%;*/
margin: 0;
padding: 0;
/* ADDED */
display: flex;
align-items: center;
}
#title_banner{
margin:0;
padding:0;
/*display: inline;*/
font-size: 45px;
font-family: Helvetica;
/* ADDED */
flex: 1 1 auto /* fill up all excessive space, pushing #login to the right */
}
#login{
/*float: right;*/
/*margin:10px;*/
/* ADDED */
padding: 10px; /* margin messes up spacing for flex layout */
align-self: flex-start;
}
/*.lower_banner{
width: 100%;
height: 40%;
margin:0;
padding:0;
}*/
<header>
<div class="upper_banner">
<img id="bar_logo" src="https://picsum.photos/100/100" alt="Title">
<p id="title_banner">
Title of Website
</p>
<a id="login" href="login.html">login</a>
</div>
</header>

css page layout - foreground "overlying" background

I recently started to get familiar with web application development and struggle with a basic css layout question.
The desired layout is a central area overlapping several background elements.
See here:
This image shows the desired layout:
I have also hacked it on codepen:
Codepen
Where I use html divs to build the background around the central area.
<div class="page-element">
<div class="element-side"></div>
<div class="element-middle"></div>
<div class="element-side"></div>
</div>
and css to place it like that:
.page-element {
display: flex;
}
.element-middle {
width: 90%;
}
.element-side {
flex: 1;
}
But the way I did it there doesn´t look like proper layout style.
What is the right way in modern css to do such a layout?
Since most of your structure is fine I kept it, and the major changes is how to create the left/right gutter and the overflow on the header/footer.
For the left/right gutter I simply removed the side elements, kept the width: 90% and used margin: 0 auto to horizontally get things centered.
For the header/footer overflow I removed the below-nav/footer1 elements, added pseudo elements on the main (done with CSS only) and an extra bottom/top padding on the header/footer so the pseudo won't overlap their content.
.nav1, .nav2, .nav1 > div {
display: flex;
background: #f05d23;
}
.nav-elem {
flex: 1;
}
.nav1 > div, .nav2 > div {
margin: 0 auto; /* added, center horiz. */
width: 90%;
text-align: center;
padding: 1em;
box-sizing: border-box;
}
.nav2 > div {
padding-bottom: 3em; /* added, avoid overlap */
}
.main {
position: relative;
display: flex;
background: #e4e6c3;
}
.main::before, .main::after { /* added, create overflow */
content: '';
position: absolute;
left: 5%;
width: 90%;
height: 3em;
background: #f7f7f2;
}
.main::before {
top: 100%;
}
.main::after {
bottom: 100%;
}
.main section {
margin: 0 auto; /* added, center horiz. */
width: 90%;
padding: 1em;
box-sizing: border-box;
background: #f7f7f2;
}
.footer {
display: flex;
background: #2d2a32;
}
.footer section {
margin: 0 auto; /* added, center horiz. */
width: 90%;
padding: 1em;
padding-top: 4em; /* added, avoid overlap */
box-sizing: border-box;
text-align: center;
color: #f7f7f2;
}
body {
padding: 0;
margin: 0;
}
h1, h2 {
font: bold 2em Sans-Serif;
margin: 0 0 1em 0;
}
h2 {
font-size: 1.5em;
}
p {
margin: 0 0 1em 0;
}
<div class="nav1">
<div>
<div class="nav-elem">Plan</div>
<div class="nav-elem">AdminPlan</div>
<div class="nav-elem">Help</div>
<div class="nav-elem">Login</div>
</div>
</div>
<div class="nav2">
<div>
<h1>Pageheader.</h1></div>
</div>
<div class="main">
<section>
<h1>Main Content</h1>
<p>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by
arches into stiff sections.</p>
<p>The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. "What's happened to me? " he thought. It wasn't a dream.</p>
<p>His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had
recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then
turned to look out the window at the dull weather. Drops</p>
</section>
</div>
<div class="footer">
<section>
<h2>Footer</h2>
<p>Whatever goes into a page </p>
</section>
</div>
Note, the .footer section's padding value in padding-top: 4em; can of course be added to the existing shorthand property like this: padding: 4em 1em 1em; (/* top | horizontal | bottom */)
Here's an example of how to achieve that - sorry but have been typing.on mobile.
https://jsfiddle.net/su5w1595/
.wrapper {
Position: absolute;
Height:100%;
Width:100%;
}
.top {
Position:relative;
Background-color:blue;
Width:100%;
Height:30%;
}
.middle {
Position:relative;
Background-color:red;
Width:100%;
Height:30%;
}
.footer {
Position:relative;
Background-color:yellow;
Width:100%;
Height:30%;
}
.main {
Position:absolute;
Top:20%;
Left:10%;
Height: 60%;
Width:80%;
Margin:auto;
Background-color: white;
}

Center header inside parent

I use bootstrap and joomla.
I have a new design that requires my div and h2, to be 1920 pixels wide and have centered text. What i have done is:
.page-header {
background-color:#3c4547;
width:1920px;
top:0px !important;
position:relative;
margin-top:0px;
display:block;
padding:20px 0px 25px 0px;
z-index:999;
left:-385px;
}
.page-header h2 {
color: #white;
font-size:36px;
font-weight:400;
margin-left: auto;
margin-right: auto;
text-align: center;
}
The wrapper is only 1600 pixels wide or something, thats why i cheat with negative margin.
but it doesnt center the text right on resize - its need to be responsive aswell. Any suggestions?
This is my HTML code and i cannot edit it:
<div class="page-header"><h2 itemprop="name">Header text</h2></div>
It has a as parents.
This is basicly what i want and have, but it doesnt center on responsive:
.
and this is how it looks just with just 100% width:
For Lion and others, to explain the whole code:
<div class="parent">
<div class="page-header>
<h2>testest</h2>
</div>
</div>
.parent {
width:1000px;
}
.page-header {
width:100%;
background:red;
}
.h2 {
color: #ffffff;
text-align:center;
}
** try to use this code, i hope this code will help you :)**
div h2 {
max-width: 1920px;
width: 100%;
height: auto;
display: flex;
justify-content: center;
}
UPDATED
please replace your code to this
.page-header {
background-color:#3c4547;
width:100%;
top:0px !important;
margin-top:0px;
display:block;
padding:20px 0px 25px 0px;
z-index:999;
left:-385px;
}
.page-header h2 {
color: #white;
font-size:36px;
font-weight:400;
margin-left: auto;
margin-right: auto;
text-align: center;
}
no need of position..
Sorry if i did not get your point but if you want to make your h2 center align then why don't you use margin..
see this
.page-header{width:100%;}
h2{margin-left:auto;margin-right:auto;width:auto;text-align:center;}
.section{ margin-left: auto;margin-right: auto;text-align: center; width: 100%;}
i am assuming that you wanted to make your h2 text center align with responsiveness so i typed code like this..correct me if m wrong..
For Testing
<div class="page-header">
<h2 itemprop="name">Header text</h2>
</div>
<div class="section">
<p>lalalala</p>
<p>lalalala</p>
<p>lalalala</p>
<p>lalalala</p>
<p>lalalala</p>
</div>
please check my example and let me know your structure is different.. ty
screenshot
You guys had some good answers, but didnt really solve my problem - i am sure one of yours can be a solution to another with a problem simular to this, but in my case i solved the problem by using javascript to expand the id tag.
if(jQuery('.page-header').length > 0){
pageHeader = jQuery('.page-header');
jQuery('#main').prepend(pageHeader);
}

HTML Header With Image And Text - Align Text To Bottom?

I am writing an HTML page with CSS. At the top of my page I want to show a header with an image and text (image to the left of the text). The image size is 64 x 64 pixels and I want the text to be large.
I was able to do almost everything except I want to align the text at the bottom but, no matter what I do, I can't seem to get the text to stop placing itself at the top.
Here is the HTML for my header:
<div id="container" class="container">
<div class="header">
<div class="header image"></div>
<div class="header text">Header Text</div>
</div>
</div>
and here is the CSS;
.container .header {
height: 65px;
border:2px solid red;
}
.container .header .image {
background: url("../images/icon64.png") no-repeat;
float: left;
display: inline-block;
width: 65px;
border:2px solid green;
}
.container .header .text {
float: left;
display: inline-block;
vertical-align: bottom;
font-family: sans-serif;
font-size: x-large;
border:2px solid blue;
}
I have been reading several web pages after searching for how to do this. I found one page that seemed pretty straight forward. They said you have to use inline-block for the display property in order for vertical-align to be honored.
I changed my CSS to what you see above but that still did not work. Here is what my header looks like:
(Note the border coloring is just for visualizing what's going on.)
Can anyone tell me what I am doing wrong and how to fix it so that my text is vertically aligned at the bottom?
Thank you.
That is correct, set elements as inline-blocks and use vertical-align. However, that means not to float the elements! Floated elements are floats and you negate the display: inline-block declaration: http://jsfiddle.net/qQtG9/2/ (I've cleaned your code some).
HTML:
<div class="header">
<div class="image"></div><div class="text">Header Text</div>
</div>
CSS:
.header {
border:2px solid red;
}
.header .image {
background: url("http://placehold.it/64x64")
no-repeat;
width: 65px;
height: 65px;
border:2px solid green;
}
.header .text {
font: x-large sans-serif;
border:2px solid blue;
}
.header .image,
.header .text {
display: inline-block;
vertical-align: bottom;
}
You can also try giving the #header a position:relative
and then give the .text position absolute, so if you give bottom:0; it will be stack to the bottom of the #header div

I am trying to align three elements, on the left, the right, and the center. Center element = dynamic width, containing element = width of browser

Here is what I am trying to do: I have a <h1> element, a <time> element, and a <div>, all within a <header> that is the full width of the browser window. The <h1> element needs to be on the left, the <time> element, which changes width with the time, needs to be centered, and the <div> needs to be on the right.
I have been trying to work this out for a while but haven't had any luck. Perhaps it requires some javascript? I also need to be able to (I think using absolute positioning?) vertically center align them all, and they are all different font sizes.
Heres the HTML so far:
<header>
<h1>blahblah.com</h1>
<time>THE TIME</time>
<div id="controls">
DISPLAY CONTROLS
</div>
</header>
and the CSS:
* {
margin: 0px;
padding: 0px;
}
header {
background: black;
color: white;
width: 100%;
font-family: wendy;
}
header h1 {
display: inline-block;
font-size: 40px;
margin-left: 10px;
}
header time {
font-size: 30px;
}
header #controls {
display: inline-block;
}
#controls p {
display: inline-block;
font-size: 20px;
}
Thank you very much!
Time is an inline element, so text-align: center for the header is enough to get the time centered. Further, get rid of those unnecessary inline-block styles.
And then the base aligning style sheet shrinks to this fiddle example:
header {
width: 100%;
overflow: hidden;
text-align: center;
}
header h1 {
float: left;
}
header #controls {
float: right;
}
Overflow is added to assure extending the height of the header to that of the floated elements , whichever is tallest.