So I've been struggling with this for some while and the motive is just to learn but I simply want to center two <h2> titles next to each other. Preferably wrapping them in some sort of a container so that I simply can apply margin: 0 auto; Like:
<div>
<h2>Hello!</h2>
<h2>Hello!</h2>
</div>
Here, check the snippet. You'll get both h2 centered.
.container{
text-align:center;
}
h2{
text-align:left;
display:inline-block;
}
<div class="container">
<h2>Hello!</h2>
<h2>Hello!</h2>
</div>
A little different approach than Xahed, but working just as good
DEMO HERE:
http://jsfiddle.net/s0y9pg0L/
.container {
margin: 0 auto;
width:250px;
border: 1px solid red;
}
.centered-text {
text-align: center;
}
h2 {
display: inline-block;
}
with HTML
<div class="container">
<div class="centered-text">
<h2>Hello!</h2>
<h2>Hello!</h2>
</div>
</div>
This is a little more proof if you're actually gonna use it in a website.
Simplest version I can think of is, very similar to the other ones posted (sorry).
If you do not want to affect all other h2s in the page, give the specific ones a class
h2{
display: inline-block;
}
.wrapper{
text-align: center;
}
<div class="wrapper">
<h2>Hello!</h2>
<h2>Hello!</h2>
</div>
Here, I hope this helps. I think this is what you meant by center two h2 titles next to each other.
#wrapper {
width: 500px;
overflow: hidden;
border: 1px solid black;
margin: 0 auto
}
#title1 {
float:left;
width: 249px;
margin: 0 auto;
text-align: center;
}
#title2 {
overflow: hidden;
width: 249px;
text-align: center;
}
<div id="wrapper">
<div id="title1"><h2>Hello!</h2></div>
<div id="title2"><h2>Hello!</h2></div>
</div>
Related
I have a flow chart that was originally an image which ought to be simple enough to translate into CSS, but as I have little skill in CSS div manipulation I was hoping the wizards of Stack Overflow could help.
I'm trying to replicate the above image in CSS. It doesn't have to look exactly the same, but I'd like to keep the basic layout the same.
I've taken a stab at the second and third columns just to see if I could get that part figured out, but I can't seem to get the second item in the second column to line up with the first item in the second column.
If someone could help me with just that portion, I would be eternally grateful.
.RoleContainerTop {
border: 1px black solid;
margin: auto;
text-align: center;
width: 100px;
margin: 20px;
float: left;
}
.RoleContainerMiddle {
border: 1px black solid;
margin: auto;
text-align: center;
width: 100px;
margin-top: 75px;
float: left;
}
.RoleContainerBottom {
border: 1px black solid;
margin: auto;
text-align: center;
width: 100px;
margin-top: 150px;
float: left;
}
.RoleContainer p {
text-align: center
}
<div>
<div>
<div class="RoleContainerTop">
<p>
Abracadabra
</p>
</div>
<div class="RoleContainerMiddle">
<p>
Shazam
</p>
</div>
<div class="RoleContainerBottom">
<p>
Alakazam
</p>
</div>
</div>
</div>
I would do something like this. The key is to create your columns of variable width, and from there it's pretty simple. I chose percentage width but you could do it however you like.
I would also advise you to consolidate your css a bit :). You're repeating alot of code that is shared between like elements.
CSS:
* {
box-sizing: border-box;
}
.column-25 {
width: 25%;
padding: 10px;
float: left;
}
.column-25:last-child {
float: right;
}
.block {
width: 100%;
height: 60px;
margin-bottom: 20px;
border: 1px solid #000;
}
.block.center {
margin-top: 40px;
}
HTML:
<div class="container">
<div class="column-25">
<div class="block"></div>
</div>
<div class="column-25">
<div class="block"></div>
<div class="block"></div>
</div>
<div class="column-25">
<div class="block center"></div>
</div>
<div class="column-25">
<div class="block"></div>
<div class="block"></div>
</div>
</div>
From here, you could look into absolute positioned elements with some :before/:after wizardry to create the arrows if you'd like.
jsfiddle demo
Is it possible, with CSS, while using a row with two column, one with an image and another with text, to have their content vertically aligned in the middle?
I've been banging my head for days over this and tried everything I could possibly think of.
This is the basic structure that I'm using which is entirely based on percentages. This is for a responsive one-page layout based on a series of sections, each with min-height set to 100%.
CSS
html, body {
width: 100%;
height: 100%;
}
section {
width: 100%;
min-height: 100%;
display:table;
height:inherit;
}
.row {
width: 100%;
height: 100%;
display:table-cell;
}
.col-left, .col-right {
float: left;
width: 50%;
height: 100%;
}
/*--this should be vertically centred--*/
.content {
}
HTML
<section>
<div class="row">
<div class="col-left">
<div class="content">
<h1>SOME TEXT</h1>
</div>
</div>
<div class="col-right">
<div class="content">
<img src="SOME IMAGE URL">
</div>
</div>
</div>
</section>
JSFiddle
.row {
...
display:table-row;
}
.col-left, .col-right {
...
display: table-cell;
vertical-align: middle;
}
Demo
You were 95% of the way there as you laid out the structure using display:table, and display:table-cell, but you floated what should have been display:table-cell, and set table-cell on what should have been table row. Use this instead:
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
section {
width: 100%;
min-height: 100%;
display:table;
height:inherit;
}
.row {
width: 100%;
height: 100%;
display:table-row;
}
.col-left, .col-right {
display:table-cell;
width: 50%;
height: 100%;
vertical-align:middle;
}
.col-left {
background: MidnightBlue
}
.col-right {
background: ForestGreen;
text-align: center;
}
.content {
border: 1px dashed red;
}
h1 {
font-family: sans-serif;
color: white;
}
<section>
<div class="row">
<div class="col-left">
<div class="content">
<h1>I should be vertically aligned in the middle...<br><br>And so should the image on the right...
</h1>
</div>
</div>
<div class="col-right">
<div class="content">
<img src="https://cdn4.iconfinder.com/data/icons/miu-flat-social/60/stackoverflow-128.png">
</div>
</div>
</div>
</section>
you can try this:
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
section {
width: 100%;
min-height: 100%;
display:table;
height:inherit;
}
.row {
width: 100%;
height: 100%;
display:table;
}
.col-left, .col-right {
float: left;
display:table;
vertical-align:middle;
width: 50%;
height: 100%;
}
.col-left {
background: MidnightBlue
}
.col-right {
background: ForestGreen;
text-align: center;
}
.content {
display:table-cell;
vertical-align:middle;
height:100%;
border: 1px dashed red;
}
h1 {
font-family: sans-serif;
color: white;
}
<section>
<div class="row">
<div class="col-left">
<div class="content">
<h1>I should be vertically aligned in the middle...<br><br>And so should the image on the right...
</h1>
</div>
</div>
<div class="col-right">
<div class="content">
<img src="https://cdn4.iconfinder.com/data/icons/miu-flat-social/60/stackoverflow-128.png">
</div>
</div>
</div>
</section>
For anyone interested in this topic, this issue and the answers provided raised in me another question regarding which method to apply in this situation, and in fact, for building columns in general: Floating or Display property. For any newbie or self-taught wannabe developer like my self may be interesting to know what I've concluded after research and testing.
I find my self often using different methods for building columns that revolve around Floating the elements and in one way or another always require some hacking in the end to do exactly what I want, leaving me with a feeling of inconsistency and unreliability.
One would think that something so vital for layout structure would have at this point in time some obvious, elegant and simple solution. Apparently it has, and it's called Display Table. It might have limitations in some very specific situations but in general it's all you need. It's rock solid and you can pretty much do anything you want with it. Unfortunately, I think the word "table" is still a kind of taboo. This method however is simple, comprehensible, reliable and doesn't require any hacks to behave as expected. Vertical alignment which is always a struggle is also made easy this way.
A simple structure like this is all you need:
.container {
display: table;
}
.row {
display: table-row;
}
.col {
display: table-cell;
}
For some reason (probably because of the nasty word "table"), you wont be able to find this method suggested anywhere with a simple search on basic css columns.
I thought this articles on the subject were interesting:
Give Floats the Flick in CSS Layouts
Farewell Floats: The Future of CSS Layout
I wanted to make my text to make my text lower to make it to the center of the <div> tag. But I tried to use margin-top to make it lower a little bit but it just can't work. This is my code.
.loremipsum {
background-color: white;
height: 1060px;
}
.content {
font-family: 'Lato';
text-align: center;
}
h3 {
font-size: 75px;
}
.h3 {
border: 2px black solid;
width: 750px;
margin: auto;
margin-top: 100px;
}
<div class="loremipsum" id="loremipsum" >
<div class="h3">
<h3 class="content" >Why Choose Us?</h3>
</div>
<p class="content">We create the best front-end design.Each template cost only $5.</p>
</div>
I recommend you to look up a lot of web based tutorials before you go any further, as web based development is vast (esp for a complete beginner)
I've created a quick demo of margin and padding examples below, (I would post as a comment, although for demonstration purposes, this might be better)
html{background:gray;}
div {
height: 20px;
border: 1px solid black;
}
.right {
margin-right: 100px;
}
.left {
margin-left: 100px;
}
.top {
margin-top: 100px;
}
.bottom {
margin-bottom: 100px;
}
.left-padded{
padding-left:100px;
}
.right-padded{
padding-right:100px;
}
.top-padded{
padding-top:100px;
}
.bottom-padded{
padding-bottom:100px;
}
<div class="normal">I'm normal</div>
<div class="right">I'm margin-right</div>
<div class="left">I'm margin-left</div>
<div class="top">I'm margin-top</div>
<div class="bottom">I'm margin-bottom</div>
<div class="left-padded">I'm padded-left</div>
<div class="right-padded">I'm padded-right</div>
<div class="top-padded">I'm padded-top</div>
<div class="bottom-padded">I'm padded-bottom</div>
You can set property to important. so it will give priority
` .h3 {
border: 2px black solid;
width: 750px;
margin:auto;
margin-top:100px;
}
h3.content{padding:25px} `
Check it on fiddle : [http://jsfiddle.net/515zttfr/][1]
I would like to know what the best way is to format the following layout:
(with eveything aligned and spaced neatly):
Here is the HTML:
<div class"wrapper">
<img alt="Image 1" src="images/image1.png" />
<div class="description">
<h1>Heading 1</h1>
<p>Paragraph 1</h1>
</div>
</div>
I tried the following but the vertical-align property does not seem to be working as I cannot align the top of the h1 with the top of the image:
img, div.description {
float: left;
}
div.description { margin-left: 10px; vertical-align: top; }
h1 { background: blue; }
p { background: red; }
What if instead of how the right hand side part is displayed below,
we wanted the right hand side to also be vertically centered instead
of being top aligned?
Here is the JSFiddle link:
http://jsfiddle.net/johngoche99/ZPKZj/1/
OK, to keep the text from dropping down below when the browser is resized it is necessary to specify the width of the wrapper element to something like 700px. Then it works.
Thanks.
in css you need to do this
img{
float: left;
height: 300px
}
div{
float: left;
}
h1{
padding: 10px;
background-color: #584480;
color: #fff;
font-weight: normal;
font-size: 25px;
margin: 0 0 10px 10px;
}
p{
padding: 10px;
background-color: #E24480;
color: #fff;
font-weight: normal;
font-size: 25px;
margin: 0 0 10px 10px;
}
nothing more ...
Hope this will help you ...
This can be accomplished with simple CSS.
img, div{
float: left;
margin: 10px;
}
http://jsfiddle.net/ZPKZj/2/
IRL, do NOT use this CSS. It is far too generic to be useful in any production environment. You might give your elements IDs or classes to allow the rules to be much more specific.
It looks you markup need a little change to be more, khm right;
HTML:
<div id="all">
<div id="sidebar">
<img class="side_image" alt="Image 1" src="http://images.nationalgeographic.com/wpf/media-live/photos/000/005/cache/green-iguana_563_600x450.jpg" />
</div>
<div id="main">
<h1>Heading 1</h1>
<p>Paragraph 1</h1>
</div>
</div>
CSS:
#sidebar { float: left; }
#sidebar { margin-right: 40px; }
h1 {
margin-bottom: 30px;
margin-top: 0;
}
link to look how it will be:
http://jsfiddle.net/56Z7C/1/
I think you want something like this:
JSFIDDLE
You want to use css here. You will add an ID to the first div like <div id="wrapper"> this is your main div. Then in the second div you add <div id="headings"> for the headings. then in your css add the beneath code. (note: this isn't the best css code ever. but it works :))
html:
<div id="wrapper">
<img alt="Image 1" src="http://images.nationalgeographic.com/wpf/media-live/photos/000/005/cache/green-iguana_563_600x450.jpg" />
<div id="headings">
<h1>Heading 1</h1>
<p>Paragraph 1</h1>
</div>
</div>
css:
#wrapper{
width: 960px;
margin: 0 auto;
}
#wrapper img{
float: left;\
margin-right: 40px;
padding-right: 40px;
}
#headings{
position: relative;
float: left;
}
h1{
margin-top: -5px;
}
Hope it helps!
Vertical-align only works on tables. If you want to do that with divs, you could try using display: table:
<div class="table">
<div class="row">
<div class="cell">
<img alt="Image 1" width="100" src="http://images.nationalgeographic.com/wpf/media-live/photos/000/005/cache/green-iguana_563_600x450.jpg" />
</div>
<div class="cell" id="stuff">
<h1>Heading 1</h1>
<p>Paragraph 1</p>
</div>
</div>
</div>
And the CSS:
.table { display: table; }
.row { display: table-row; }
.cell { display: table-cell; }
#stuff { vertical-align: middle; }
This has the advantage of not being dependent on sizes/margins of elements, but is unsupported in IE7 and below. As all things in life, display: table is a tradeoff.
Sorry if the title isn't very clear, but I'm trying to figure out how to solve the following problem using CSS.
I have a text that should be shown in two different lines: imagine first line is "hello world" and second is "goodbye"
I'd need to show the text in this way, in the center of the screen:
hello world
goodbye
I'm using this as first attempt but all I get is both lines centered. I'd need to have the second line aligned to the right.
div#logo {
clear: both;
padding: 1em;
border: 0;
margin: 1em auto;
text-align: center;
width: 75%;
}
Thank you.
The simplest solution: Have your paragraph aligned to the right with the max-width set to the width of the first-line, the second line will automatically break and be aligned to the right.
html
<div id="logo">
<p>Hello World goodbye!</p>
</div>
css
#logo
{
display:block;
width:100px;
position:relative;
margin-left:auto;
margin-right:auto;
}
#logo p
{
max-width:80px;
text-align:right;
}
Here's a functional fiddle
HTML:
<div id="logo"><p>First line<br />Remaining Text</p></div>
CSS:
div#logo {text-align:center;}
div#logo p {display:inline-block; text-align:right;}
If you know the width of the text inside the logo div it's a relatively simple thing to do, if you don't I do believe you're going to need jQuery to accomplish it.
<div id="logo">
<p class="right">
hello world<br />
goodbye
</p>
</div>
div#logo {
clear: both;
padding: 1em;
border: 0;
margin: 1em auto;
text-align: center;
width: 75%;
position:relative;
height:100px;
}
.right{
position:absolute;
width:90px;
margin-left:-45px;
}
Demo: http://jsfiddle.net/calder12/QnGDs/1/
If you put that text in a p element like:
<div id="logo">
<p>Hello world <br />goodbye</p>
</div>
And apply the following css:
div#logo
{
width: 75%;
margin: 0px auto;
}
#logo p
{
text-align: right;
}
I believe that is what you want.
How about putting your second line in some other container and then push it towards the right
<div id="logo">
hello world<br/>
<span class="right">goodbye</span>
</div>
div#logo {
clear: both;
padding: 1em;
border: 0;
margin: 1em auto;
text-align: center;
width: 75%;
}
.right {
text-align: right;
float: right;
}
only in case the width is known or pre-set
<div class="wrapper">
<div class="first-line">hello word</div>
<div class="second-line">goodbye</div>
</div>`
<style>
.wrapper{ width: 70px; margin: 0 auto;}
.first-line { float: left;}
.second-line{ float: right;}
</style>`
Explanation
.wrapper:
width - must be known and pre-set not necessarily in the css you can use in-line style
margin - top and bottom 0px where left and right equally balanced
.first-line:
float - to the left
.second-line
float - to the right
Please check the link to see working example
http://jsfiddle.net/pQTBz/