/    Sign up×
Community /Pin to ProfileBookmark

Centering a div horizontally

Hello.

How can I center one div in the middle of the screen? Mainly I have problems with horizontal centering. If I use absolute positioning, then only the first line is in the middle horizontally. My code:

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″/>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>hey I am a title</title>
<style>
html, body {width: 100%; height: 100%;}
.t7{
text-align: center;
position: relative; top: 50%;}
</style>
</head>
<body>

<!– this code centers an element in the middle of the screen –>

<div class=”t7″>
I am a text in the middle of the screen.
<br>
second line
<br>
third line
<br>
fourth line
<br>
fifth line
</div>

</body>
</html>
[/code]

Thank you.

to post a comment
CSS

3 Comments(s)

Copy linkTweet thisAlerts:
@cootheadJan 05.2022 — Hi there codewitch,

perhaps, this example will solve your problems...

[b]HTML[/b]
<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;Untitled document&lt;/title&gt;

&lt;!--
The internal CSS should be transferred to an external file
&lt;link rel="stylesheet" href="screen.css" media="screen"&gt;
--&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div id="container"&gt;
I am a text in the middle of the screen.
&lt;br&gt;
second line
&lt;br&gt;
third line
&lt;br&gt;
fourth line
&lt;br&gt;
fifth line
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;


[b]CSS[/b]
<i>
</i>html,body {
height: 100%;
margin: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
font: normal 1em / 1.5em sans-serif;
}
#container {
width: 96%;
max-width: 60em;
padding: 1em;
margin: auto;
border: 1px solid #999;
box-sizing: border-box;
background-color: #fff;
text-align: center;
animation: rotate 10s linear infinite;
}
@keyframes rotate {
100%{ transform: rotateX(359deg);}
}


[i]coothead[/i]
Copy linkTweet thisAlerts:
@codewitchauthorJan 05.2022 — Thank you coothead. You are the best!!
Copy linkTweet thisAlerts:
@cootheadJan 05.2022 — 
You are the best!!
[/quote]

Not too bad would suffice. :)

[i]coothead[/i]
×

Success!

Help @codewitch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...