如何将文字放在与图像

时间:2018-04-28 22:53:51

标签: html css

我试图将此页面上的h3和段落移动到h3与图像顶部齐平的位置,而不是略低于图像的顶行,如果这是有意义的话。我只是设法能够按照我想要的方式获取图片旁边的文字,但现在我只需要修复它。

代码:

h1 {
  position: absolute;
  font-family: aquilinetworegular;
  color: #000066;
  font-size: 50px;
  top: -50px;
  left: 20px;
}

h2 {
  text-align: center;
  font-family: Arial;
  font-size: 200%
}

h3 {
  font-size: 200%
}

p.tagline {
  position: absolute;
  color: #800000;
  top: 80px;
  left: 12px;
  font-size: 19px;
  padding-top: 15px;
}

p {
  font-size: 19px;
}

.washington {
  float: right;
}

.washington img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.greene {
  float: right;
}

.greene img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.knox {
  float: right;
}

.knox img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.wayne {
  float: right;
}

.wayne img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.arnold {
  float: right;
}

.arnold img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.steuben {
  float: right;
}

.steuben img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.marion {
  float: right;
}

.marion img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.kosciuszko {
  float: right;
}

.kosciuszko img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.morgan {
  float: right;
}

.morgan img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.tallmadge {
  float: right;
}

.tallmadge img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.hamilton {
  float: right;
}

.hamilton img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.moultrie {
  float: right;
}

.moultrie img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.laurens {
  float: right;
}

.laurens img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.lafayette {
  float: right;
}

.lafayette img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.rochambeau {
  float: right;
}

.rochambeau img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.chastellux {
  float: right;
}

.chastellux img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.knyphausen {
  float: right;
}

.knyphausen img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.rall {
  float: right;
}

.rall img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.clinton {
  float: right;
}

.clinton img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.cornwallis {
  float: right;
}

.cornwallis img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.howe {
  float: right;
}

.howe img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.gage {
  float: right;
}

.gage img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.burgoyne {
  float: right;
}

.burgoyne img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.andre {
  float: right;
}

.andre img {
  float: left;
  padding-right: 15px;
  padding-bottom: 40px;
}

.northerncampaign {
  margin: 0 0 15px 20px;
  padding: 15px;
  overflow: hidden;
}

.northerncampaign img {
  margin-right: 15px;
  float: left;
}

.southerncampaign {
  margin: 0 0 15px 20px;
  padding: 15px;
  overflow: hidden;
  height: 300px;
}

.southerncampaign img {
  margin-right: 15px;
  float: left;
}

.image-center {
  display: flex;
  justify-content: space-evenly;
  width: 100%;
  margin-top: 40px;
}

.image-center2 {
  display: flex;
  justify-content: space-evenly;
  width: 100%;
  margin-top: 40px;
}

.imagecenter3 {
  display: flex;
  justify-content: space-evenly;
  width: 100%;
  margin-top: 40px;
}

.imagecenter4 {
  display: flex;
  justify-content: space-evenly;
  width: 100%;
  margin-top: 40px;
}

.imagecenter6 {
  display: flex;
  justify-content: space-evenly;
  width: 100%;
  margin-top: 40px;
}

form {
  float: right;
}


/* Navbar container */

.navbar {
  overflow: hidden;
  background-color: #333;
  font-family: Arial;
  display: flex;
  justify-content: center;
  width: 100%;
}


/* Links inside the navbar */

.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}


/* The dropdown container */

.dropdown {
  float: left;
  overflow: hidden;
}


/* Dropdown button */

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  /* Important for vertical align on mobile phones */
  margin: 0;
  /* Important for vertical align on mobile phones */
}


/* Add a red background color to navbar links on hover */

.navbar a:hover,
.dropdown:hover .dropbtn {
  background-color: blue;
}


/* Dropdown content (hidden by default) */

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}


/* Links inside the dropdown */

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}


/* Add a grey background color to dropdown links on hover */

.dropdown-content a:hover {
  background-color: #ddd;
}


/* Show the dropdown menu on hover */

.dropdown:hover .dropdown-content {
  display: block;
}
<h1>Soldiers of Columbia</h1>
<br>
<br>
<br>
<p class="tagline">A research site about the American Revolutionary War</p>
<br>
<br>
<br>
<form>
  <input type="text" placeholder="Search..." required>
  <input type="button" value="Search">
</form>
<br>
<br>
<img src="https://upload.wikimedia.org/wikipedia/commons/9/90/The_March_to_Valley_Forge_William_Trego.png" width=1350 height=350>
<div class="navbar">
  <div class="container">
    <a href="index.html">Home</a>
    <a href="causes.html">Causes</a>
    <a href="amrevbattles.html">Battles</a>

    <div class="dropdown">
      <button class="dropbtn">Armies<i class="fa fa-caret-down"></i>
     </button>
      <div class="dropdown-content">
        <a href="continentalforces.html">Continental</a>
        <a href="britishforces.html">British</a>
        <a href="frenchforces.html">French</a>
        <a href="hessianforces.html">Hessian</a>
      </div>
    </div>

    <div class="dropdown">
      <button class="dropbtn">Commanders<i class="fa fa-caret-down"></i>
	   </button>
      <div class="dropdown-content">
        <a href="americancommanders.html">American</a>
        <a href="britishcommanders.html">British</a>
        <a href="frenchcommanders.html">French</a>
        <a href="hessiancommanders.html">Hessian</a>
      </div>
    </div>
  </div>
</div>
<h2>Battles of the American Revolutionary War</h2>
<div class="northerncampaign">
  <img src="https://upload.wikimedia.org/wikipedia/commons/b/b9/U.S._Army_-_Artillery_Retreat_from_Long_Island_1776.jpg" width=350 height 300>
  <a href="northerncampaign.html">
    <h3>Northern theater</h3>
  </a>
  <p>Read about the battles of the war's northern theater</p>
</div>
<div class="southerncampaign">
  <img src="https://upload.wikimedia.org/wikipedia/commons/8/85/KingsMountain_DeathOfFerguson_Chappel.jpg" width=350 height=300>
  <a href="southerncampaign.html">
    <h3>Southern theater</h3>
  </a>
  <p>Read about the battles of the war's southern theater</p>
</div>

1 个答案:

答案 0 :(得分:1)

这是因为h3 line-heightmargin被推下来,所以如果添加h3 {margin: 0; line-height: 1;},文字将与图片的顶部对齐。并且你改变了值。

检查此代码:

&#13;
&#13;
.northerncampaign {
  margin: 0 0 15px 20px;
  padding: 15px;
  overflow: hidden;
  vertical-align: text-bottom;
}

.northerncampaign img {
  margin-right: 15px;
  float: left;
}

.southerncampaign {
  margin: 0 0 15px 20px;
  padding: 15px;
  overflow: hidden;
  height: 300px;
}

.southerncampaign img {
  margin-right: 15px;
  float: left;
}

h3 {
  line-height: 1;
  margin: 0;
}
&#13;
<div class="northerncampaign">
  <img src="https://upload.wikimedia.org/wikipedia/commons/b/b9/U.S._Army_-_Artillery_Retreat_from_Long_Island_1776.jpg" width=350 height 300>
  <a href="northerncampaign.html">
    <h3>Northern theater</h3>
  </a>
  <p>Read about the battles of the war's northern theater</p>
</div>
<div class="southerncampaign">
  <img src="https://upload.wikimedia.org/wikipedia/commons/8/85/KingsMountain_DeathOfFerguson_Chappel.jpg" width=350 height=300>
  <a href="southerncampaign.html">
    <h3>Southern theater</h3>
  </a>
  <p>Read about the battles of the war's southern theater</p>
</div>
&#13;
&#13;
&#13;